feat(route/telegram): add caching for input entity retrieval (#20128)

This commit is contained in:
Tony
2025-09-22 22:56:01 +08:00
committed by GitHub
parent 6edbe3896e
commit 8ae3cf6f38
+12 -1
View File
@@ -5,6 +5,8 @@ import { Api } from 'telegram';
import { HTMLParser } from 'telegram/extensions/html.js';
import { getClient, getDocument, getFilename, unwrapMedia } from './client';
import { getDisplayName } from 'telegram/Utils.js';
import cache from '@/utils/cache';
import { returnBigInt } from 'telegram/Helpers.js';
export function getGeoLink(geo: Api.GeoPoint) {
return `<a href="https://www.google.com/maps/search/?api=1&query=${geo.lat}%2C${geo.long}" target="_blank">Geo LatLon: ${geo.lat}, ${geo.long}</a>`;
@@ -114,7 +116,16 @@ export function humanDuration(seconds: number) {
export default async function handler(ctx: Context) {
const client = await getClient();
const username = ctx.req.param('username');
const peer = await client.getInputEntity(username);
let peerCache = await cache.get(`telegram:inputEntity:${username}`);
if (!peerCache) {
const p = await client.getInputEntity(username);
peerCache = JSON.stringify(p.toJSON());
await cache.set(`telegram:inputEntity:${username}`, peerCache);
}
const peerData = JSON.parse(peerCache, (k, v) => (k === 'channelId' || k === 'accessHash' ? returnBigInt(v) : v));
const peer = new Api.InputPeerChannel(peerData);
const entity = await client.getEntity(peer);
let attachments: string[] = [];