From cd2267e9d3e617229b2e9271603c4e3748b5944e Mon Sep 17 00:00:00 2001 From: Ruslan Bakiev <572431+veikab@users.noreply.github.com> Date: Mon, 23 Feb 2026 08:24:04 +0700 Subject: [PATCH] Remove Telegram API avatar logic from frontend service --- .../api/omni/telegram/avatar/[userId].get.ts | 63 ------------------- frontend/server/graphql/schema.ts | 32 ++++------ omni_chat/src/worker.ts | 8 +-- 3 files changed, 12 insertions(+), 91 deletions(-) delete mode 100644 frontend/server/api/omni/telegram/avatar/[userId].get.ts diff --git a/frontend/server/api/omni/telegram/avatar/[userId].get.ts b/frontend/server/api/omni/telegram/avatar/[userId].get.ts deleted file mode 100644 index ef55606..0000000 --- a/frontend/server/api/omni/telegram/avatar/[userId].get.ts +++ /dev/null @@ -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("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("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; -}); diff --git a/frontend/server/graphql/schema.ts b/frontend/server/graphql/schema.ts index 33fefd8..6ea0650 100644 --- a/frontend/server/graphql/schema.ts +++ b/frontend/server/graphql/schema.ts @@ -312,7 +312,6 @@ async function getDashboard(auth: AuthContext | null) { include: { note: { select: { content: true } }, messages: { select: { occurredAt: true }, orderBy: { occurredAt: "desc" }, take: 1 }, - omniIdentities: { select: { channel: true, externalId: true } }, }, orderBy: { updatedAt: "desc" }, take: 500, @@ -365,26 +364,17 @@ async function getDashboard(auth: AuthContext | null) { channelsByContactId.get(item.contactId)?.add(mapChannel(item.channel)); } - 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, - name: c.name, - avatar, - company: c.company ?? "", - country: c.country ?? "", - location: c.location ?? "", - channels: Array.from(channelsByContactId.get(c.id) ?? []), - lastContactAt: c.messages[0]?.occurredAt?.toISOString?.() ?? c.updatedAt.toISOString(), - description: c.note?.content ?? "", - }; - }); + const contacts = contactsRaw.map((c) => ({ + id: c.id, + name: c.name, + avatar: c.avatarUrl ?? "", + company: c.company ?? "", + country: c.country ?? "", + location: c.location ?? "", + channels: Array.from(channelsByContactId.get(c.id) ?? []), + lastContactAt: c.messages[0]?.occurredAt?.toISOString?.() ?? c.updatedAt.toISOString(), + description: c.note?.content ?? "", + })); const communications = communicationsRaw.map((m) => ({ id: m.id, diff --git a/omni_chat/src/worker.ts b/omni_chat/src/worker.ts index 67e96dc..a12eb39 100644 --- a/omni_chat/src/worker.ts +++ b/omni_chat/src/worker.ts @@ -72,12 +72,6 @@ type ContactProfile = { 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( normalized: OmniInboundEnvelopeV1["payloadNormalized"], externalContactId: string, @@ -105,7 +99,7 @@ function buildContactProfile( return { displayName, - avatarUrl: asString(normalized.contactAvatarUrl) ?? toTelegramAvatarProxyUrl(externalContactId), + avatarUrl: asString(normalized.contactAvatarUrl), }; }