diff --git a/lib/routes/telegram/channel-media.ts b/lib/routes/telegram/channel-media.ts index 17bf6807d..34b12f12c 100644 --- a/lib/routes/telegram/channel-media.ts +++ b/lib/routes/telegram/channel-media.ts @@ -64,7 +64,7 @@ function sortThumb(thumb: Api.TypePhotoSize) { return 0; } -function chooseLargestThumb(thumbs: Api.TypePhotoSize[]) { +function chooseLargestThumb(thumbs: Api.TypePhotoSize[]) { thumbs = [...thumbs].sort((a, b) => sortThumb(a) - sortThumb(b)); return thumbs.pop(); } @@ -95,7 +95,7 @@ export async function* streamDocument(client: TelegramClient, obj: Api.Document, requestSize: 512 * 1024, // MAX_CHUNK_SIZE dcId: obj.dcId, offset: undefined, - limit: undefined + limit: undefined, }; if (offset) { iterFileParams.offset = offset; @@ -150,7 +150,9 @@ function streamResponse(c: Context, bodyIter: AsyncGenerator) { aborted = true; }); for await (const chunk of bodyIter) { - if (aborted) { break; } + if (aborted) { + break; + } // console.log(`writing ${chunk.length / 1024}kB`); await stream.write(chunk); } @@ -169,7 +171,7 @@ export const route: Route = { name: 'TELEGRAM_SESSION', optional: false, description: 'Telegram API Authentication', - } + }, ], requirePuppeteer: false, antiCrawler: false, @@ -191,7 +193,7 @@ export const route: Route = { export async function handleMedia(media: Api.TypeMessageMedia, client: TelegramClient, ctx: Context) { if (media instanceof Api.MessageMediaPhoto) { const buf = await client.downloadMedia(media); - return new Response(buf, {headers: {'Content-Type': 'image/jpeg'}}); + return new Response(buf, { headers: { 'Content-Type': 'image/jpeg' } }); } const doc = getDocument(media); @@ -212,9 +214,7 @@ export async function handleMedia(media: Api.TypeMessageMedia, client: TelegramC if (range.length === 0) { ctx.header('Content-Length', doc.size.toString()); - if (!doc.mimeType.startsWith('video/') && - !doc.mimeType.startsWith('audio/') && - !doc.mimeType.startsWith('image/')) { + if (!doc.mimeType.startsWith('video/') && !doc.mimeType.startsWith('audio/') && !doc.mimeType.startsWith('image/')) { ctx.header('Content-Disposition', `attachment; filename="${encodeURIComponent(getFilename(media))}"`); } return streamResponse(ctx, streamDocument(client, doc)); @@ -222,7 +222,7 @@ export async function handleMedia(media: Api.TypeMessageMedia, client: TelegramC const [offset, limit] = range[0]; // console.log(`Range: ${rangeHeader}`); ctx.status(206); // partial content - ctx.header('Content-Length', (limit.subtract(offset).add(1)).toString()); + ctx.header('Content-Length', limit.subtract(offset).add(1).toString()); ctx.header('Content-Range', `bytes ${offset}-${limit}/${doc.size}`); return streamResponse(ctx, streamDocument(client, doc, '', offset, limit)); } diff --git a/lib/routes/telegram/stories.ts b/lib/routes/telegram/stories.ts index d6be0e0fe..7e266d3b2 100644 --- a/lib/routes/telegram/stories.ts +++ b/lib/routes/telegram/stories.ts @@ -3,7 +3,7 @@ import NotFoundError from '@/errors/types/not-found'; import { configureMiddlewares, handleMedia } from '@/routes/telegram/channel-media'; import { Data, DataItem, Route } from '@/types'; import { Context } from 'hono'; -import { Api} from 'telegram'; +import { Api } from 'telegram'; import { getClient, getStory, unwrapMedia } from './tglib/client'; import { getGeoLink, getMediaLink } from './tglib/channel'; @@ -83,7 +83,7 @@ function getMediaAreas(mediaAreas?: Api.TypeMediaArea[]) { export default async function handler(ctx: Context) { const c = await getClient(); - const {username, story} = ctx.req.param(); + const { username, story } = ctx.req.param(); if (!username) { throw new NotFoundError(); } @@ -94,17 +94,19 @@ export default async function handler(ctx: Context) { return await handleMedia(storyItem.media, c, ctx); } - const storiesRes = await c.invoke(new Api.stories.GetPeerStories({peer})); + const storiesRes = await c.invoke(new Api.stories.GetPeerStories({ peer })); const item: DataItem[] = []; for (const story of storiesRes.stories.stories) { - if (!(story instanceof Api.StoryItem)) { // story is deleted (archived) or skipped + if (!(story instanceof Api.StoryItem)) { + // story is deleted (archived) or skipped continue; } const src = `${new URL(ctx.req.url).origin}/telegram/stories/${username}/${story.id}`; const pubDate = new Date(story.date * 1000).toUTCString(); const media = await unwrapMedia(story.media); - if (!media) { // cannot load the story + if (!media) { + // cannot load the story continue; } diff --git a/lib/routes/telegram/tglib/channel.ts b/lib/routes/telegram/tglib/channel.ts index c75b2b3ec..cf44a353c 100644 --- a/lib/routes/telegram/tglib/channel.ts +++ b/lib/routes/telegram/tglib/channel.ts @@ -11,20 +11,22 @@ export function getGeoLink(geo: Api.GeoPoint) { } export async function getPollResults(client, message, m: Api.MessageMediaPoll) { - const resultsUpdateResponse = await client.invoke(new Api.messages.GetPollResults({peer: message.peerId, msgId: message.id})); + const resultsUpdateResponse = await client.invoke(new Api.messages.GetPollResults({ peer: message.peerId, msgId: message.id })); let results: Api.PollResults; if (resultsUpdateResponse?.updates[0] instanceof Api.UpdateMessagePoll) { results = resultsUpdateResponse.updates[0].results as Api.PollResults; } const txt = `

${m.poll.quiz ? 'Quiz' : 'Poll'}: ${m.poll.question.text}

-
+
`; return txt; } @@ -37,7 +39,7 @@ export function getMediaLink(src: string, m: Api.TypeMessageMedia) { return ``; } if (doc && mime.startsWith('video/')) { - const vid = (doc.attributes.find((t) => t instanceof Api.DocumentAttributeVideo) ?? { w: 1080, h: 720 }) as {w: number, h: number}; + const vid = (doc.attributes.find((t) => t instanceof Api.DocumentAttributeVideo) ?? { w: 1080, h: 720 }) as { w: number; h: number }; return ``; } if (doc && mime.startsWith('audio/')) { @@ -128,7 +130,8 @@ export default async function handler(ctx: Context) { text = `Forwarded From: ${getDisplayName(fwdFrom)}: ${text}`; } const media = await unwrapMedia(message.media, message.peerId); - if (message.media instanceof Api.MessageMediaStory && media) { // if successfully loaded the story + if (message.media instanceof Api.MessageMediaStory && media) { + // if successfully loaded the story const storyFrom = await client.getEntity(message.media.peer); text = `Story From: ${getDisplayName(storyFrom)}: ${text}`; } @@ -178,4 +181,4 @@ export default async function handler(ctx: Context) { allowEmpty: ctx.req.param('id') === 'allow_empty', description: `@${username} on Telegram`, }; -}; +} diff --git a/lib/routes/telegram/tglib/client.ts b/lib/routes/telegram/tglib/client.ts index d537d2872..82241a3b8 100644 --- a/lib/routes/telegram/tglib/client.ts +++ b/lib/routes/telegram/tglib/client.ts @@ -33,13 +33,16 @@ export async function getClient(authParams?: UserAuthParams, session?: string) { : undefined, }); - await client.start(Object.assign(authParams ?? {}, { - onError: (err: Error) => { throw new Error('Cannot start TG: ' + err); }, - }) as any); + await client.start( + Object.assign(authParams ?? {}, { + onError: (err: Error) => { + throw new Error('Cannot start TG: ' + err); + }, + }) as any + ); return client; } - export function getFilename(x: Api.TypeMessageMedia) { if (x instanceof Api.MessageMediaDocument) { for (const a of (x.document as Api.Document).attributes) { @@ -61,10 +64,12 @@ export function getDocument(m: Api.TypeMessageMedia) { } export async function getStory(entity: Api.TypeEntityLike, id: number) { - const result = await (await getClient()).invoke( + const result = await ( + await getClient() + ).invoke( new Api.stories.GetStoriesByID({ id: [id], - peer: entity + peer: entity, }) ); return result.stories[0] as Api.StoryItem;