Remove Telegram API avatar logic from frontend service

This commit is contained in:
Ruslan Bakiev
2026-02-23 08:24:04 +07:00
parent cbc2a3b31b
commit cd2267e9d3
3 changed files with 12 additions and 91 deletions

View File

@@ -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;
});

View File

@@ -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,

View File

@@ -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),
};
}