Remove Telegram API avatar logic from frontend service
This commit is contained in:
@@ -1,63 +0,0 @@
|
|||||||
import { getRouterParam, setHeader } from "h3";
|
|
||||||
import { getAuthContext } from "../../../../utils/auth";
|
|
||||||
import { requireTelegramBotToken, telegramApiBase, telegramBotApi } from "../../../../utils/telegram";
|
|
||||||
|
|
||||||
type TelegramProfilePhotoSize = {
|
|
||||||
file_id?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type TelegramUserProfilePhotosResult = {
|
|
||||||
photos?: TelegramProfilePhotoSize[][];
|
|
||||||
};
|
|
||||||
|
|
||||||
type TelegramGetFileResult = {
|
|
||||||
file_path?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function parseUserId(input: string) {
|
|
||||||
if (!/^\d+$/.test(input)) return null;
|
|
||||||
const value = Number(input);
|
|
||||||
if (!Number.isSafeInteger(value) || value <= 0) return null;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
await getAuthContext(event);
|
|
||||||
|
|
||||||
const rawUserId = String(getRouterParam(event, "userId") ?? "").trim();
|
|
||||||
const userId = parseUserId(rawUserId);
|
|
||||||
if (!userId) {
|
|
||||||
throw createError({ statusCode: 400, statusMessage: "invalid telegram user id" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const profile = await telegramBotApi<TelegramUserProfilePhotosResult>("getUserProfilePhotos", {
|
|
||||||
user_id: userId,
|
|
||||||
limit: 1,
|
|
||||||
});
|
|
||||||
|
|
||||||
const sizes = profile.photos?.[0] ?? [];
|
|
||||||
const best = sizes[sizes.length - 1];
|
|
||||||
const fileId = String(best?.file_id ?? "").trim();
|
|
||||||
if (!fileId) {
|
|
||||||
throw createError({ statusCode: 404, statusMessage: "avatar not found" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileMeta = await telegramBotApi<TelegramGetFileResult>("getFile", { file_id: fileId });
|
|
||||||
const filePath = String(fileMeta.file_path ?? "").trim();
|
|
||||||
if (!filePath) {
|
|
||||||
throw createError({ statusCode: 404, statusMessage: "avatar path not found" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = requireTelegramBotToken();
|
|
||||||
const upstream = await fetch(`${telegramApiBase()}/file/bot${token}/${filePath}`);
|
|
||||||
if (!upstream.ok) {
|
|
||||||
throw createError({ statusCode: 502, statusMessage: "failed to load telegram avatar" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const contentType = upstream.headers.get("content-type") || "image/jpeg";
|
|
||||||
const buffer = Buffer.from(await upstream.arrayBuffer());
|
|
||||||
|
|
||||||
setHeader(event, "content-type", contentType);
|
|
||||||
setHeader(event, "cache-control", "private, max-age=300");
|
|
||||||
return buffer;
|
|
||||||
});
|
|
||||||
@@ -312,7 +312,6 @@ async function getDashboard(auth: AuthContext | null) {
|
|||||||
include: {
|
include: {
|
||||||
note: { select: { content: true } },
|
note: { select: { content: true } },
|
||||||
messages: { select: { occurredAt: true }, orderBy: { occurredAt: "desc" }, take: 1 },
|
messages: { select: { occurredAt: true }, orderBy: { occurredAt: "desc" }, take: 1 },
|
||||||
omniIdentities: { select: { channel: true, externalId: true } },
|
|
||||||
},
|
},
|
||||||
orderBy: { updatedAt: "desc" },
|
orderBy: { updatedAt: "desc" },
|
||||||
take: 500,
|
take: 500,
|
||||||
@@ -365,26 +364,17 @@ async function getDashboard(auth: AuthContext | null) {
|
|||||||
channelsByContactId.get(item.contactId)?.add(mapChannel(item.channel));
|
channelsByContactId.get(item.contactId)?.add(mapChannel(item.channel));
|
||||||
}
|
}
|
||||||
|
|
||||||
const contacts = contactsRaw.map((c) => {
|
const contacts = contactsRaw.map((c) => ({
|
||||||
const telegramIdentity = c.omniIdentities.find(
|
|
||||||
(identity) => identity.channel === "TELEGRAM" && /^\d+$/.test(identity.externalId),
|
|
||||||
);
|
|
||||||
const avatar =
|
|
||||||
(c.avatarUrl ?? "").trim() ||
|
|
||||||
(telegramIdentity ? `/api/omni/telegram/avatar/${telegramIdentity.externalId}` : "");
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: c.id,
|
id: c.id,
|
||||||
name: c.name,
|
name: c.name,
|
||||||
avatar,
|
avatar: c.avatarUrl ?? "",
|
||||||
company: c.company ?? "",
|
company: c.company ?? "",
|
||||||
country: c.country ?? "",
|
country: c.country ?? "",
|
||||||
location: c.location ?? "",
|
location: c.location ?? "",
|
||||||
channels: Array.from(channelsByContactId.get(c.id) ?? []),
|
channels: Array.from(channelsByContactId.get(c.id) ?? []),
|
||||||
lastContactAt: c.messages[0]?.occurredAt?.toISOString?.() ?? c.updatedAt.toISOString(),
|
lastContactAt: c.messages[0]?.occurredAt?.toISOString?.() ?? c.updatedAt.toISOString(),
|
||||||
description: c.note?.content ?? "",
|
description: c.note?.content ?? "",
|
||||||
};
|
}));
|
||||||
});
|
|
||||||
|
|
||||||
const communications = communicationsRaw.map((m) => ({
|
const communications = communicationsRaw.map((m) => ({
|
||||||
id: m.id,
|
id: m.id,
|
||||||
|
|||||||
@@ -72,12 +72,6 @@ type ContactProfile = {
|
|||||||
avatarUrl: string | null;
|
avatarUrl: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
function toTelegramAvatarProxyUrl(externalContactId: string) {
|
|
||||||
const id = String(externalContactId ?? "").trim();
|
|
||||||
if (!/^\d+$/.test(id)) return null;
|
|
||||||
return `/api/omni/telegram/avatar/${id}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildContactProfile(
|
function buildContactProfile(
|
||||||
normalized: OmniInboundEnvelopeV1["payloadNormalized"],
|
normalized: OmniInboundEnvelopeV1["payloadNormalized"],
|
||||||
externalContactId: string,
|
externalContactId: string,
|
||||||
@@ -105,7 +99,7 @@ function buildContactProfile(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
displayName,
|
displayName,
|
||||||
avatarUrl: asString(normalized.contactAvatarUrl) ?? toTelegramAvatarProxyUrl(externalContactId),
|
avatarUrl: asString(normalized.contactAvatarUrl),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user