fix(route/telegram): improve typing
This commit is contained in:
@@ -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) {
|
||||
@@ -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');
|
||||
@@ -144,8 +147,8 @@ 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})`,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -153,10 +156,10 @@ export default async function handler(ctx) {
|
||||
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`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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}`;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user