Skip to content

Commit

Permalink
fix(route/telegram): improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony committed Nov 9, 2024
1 parent 7bec9c5 commit 566d3d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions lib/routes/telegram/tglib/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import InvalidParameterError from '@/errors/types/invalid-parameter';
import { client, decodeMedia, getClient, getFilename, getMediaLink, streamDocument, streamThumbnail } from './client';
import { returnBigInt as bigInt } from 'telegram/Helpers';
import { HTMLParser } from 'telegram/extensions/html';
import { DataItem } from '@/types';
import type { Api } from 'telegram';

function parseRange(range, length) {
if (!range) {
Expand Down Expand Up @@ -111,24 +113,25 @@ async function getMedia(ctx) {
}

export default async function handler(ctx) {
const { username } = ctx.req.param();
const client = await getClient();

const item = [];
const chat = await client.getInputEntity(ctx.req.param('username'));
const item: DataItem[] = [];
const chat = (await client.getInputEntity(username)) as Api.InputPeerChannel;
const channelInfo = await client.getEntity(chat);

if (channelInfo.className !== 'Channel') {
throw new Error(`${ctx.req.param('username')} is not a channel`);
throw new Error(`${username} is not a channel`);
}

let attachments = [];
let attachments: string[] = [];
const messages = await client.getMessages(chat, { limit: 50 });

for (const message of messages) {
if (message.media) {
// messages that have no text are shown as if they're one post
// because in TG only 1 attachment per message is possible
attachments.push(getMediaLink(ctx, chat, ctx.req.param('username'), message));
attachments.push(getMediaLink(ctx, chat, username, message));
}
if (message.text !== '') {
let description = attachments.join('\n');
Expand All @@ -144,19 +147,19 @@ export default async function handler(ctx) {
title,
description,
pubDate: new Date(message.date * 1000).toUTCString(),
link: `https://t.me/s/${ctx.req.param('username')}/${message.id}`,
author: `${channelInfo.title} (@${ctx.req.param('username')})`,
link: `https://t.me/s/${username}/${message.id}`,
author: `${channelInfo.title} (@${username})`,
});
}
}

return {
title: channelInfo.title,
language: null,
link: `https://t.me/${ctx.req.param('username')}`,
link: `https://t.me/${username}`,
item,
allowEmpty: ctx.req.param('id') === 'allow_empty',
description: `@${ctx.req.param('username')} on Telegram`,
description: `@${username} on Telegram`,
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/routes/telegram/tglib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function ExpandInlineBytes(bytes) {
return real;
}

function getMediaLink(ctx, channel, channelName, message) {
function getMediaLink(ctx, channel: Api.InputPeerChannel, channelName: string, message: Api.Message) {
const base = `${ctx.protocol}://${ctx.host}/telegram/channel/${channelName}`;
const src = base + `${channel.channelId}_${message.id}`;

Expand All @@ -102,7 +102,7 @@ function getMediaLink(ctx, channel, channelName, message) {
linkText += ` (${humanFileSize(x.document.size)})`;
return `<a href="${src}" target="_blank"><img src="${src}?thumb" alt=""/><br/>${linkText}</a>`;
}
return;
return '';
}
function getFilename(x) {
if (x instanceof Api.MessageMediaDocument) {
Expand Down

0 comments on commit 566d3d0

Please sign in to comment.