From f5cc0ecf438f90712a551f4917856ae165c603bb Mon Sep 17 00:00:00 2001 From: Avrumy Date: Wed, 15 May 2024 14:36:56 +0300 Subject: [PATCH 1/4] Fix handling uppercase hashtags --- src/handlers/hashtag/new-message.ts | 20 +++++++++++++------- src/utils.ts | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/handlers/hashtag/new-message.ts b/src/handlers/hashtag/new-message.ts index 89d0999..77d5c4f 100644 --- a/src/handlers/hashtag/new-message.ts +++ b/src/handlers/hashtag/new-message.ts @@ -3,6 +3,7 @@ import { GrammyError } from 'grammy'; import Group from '../../models/group'; import { Group as IGroup } from '../../typings/db'; import Message from '../../models/message'; +import { lowerCaseObject } from '../../utils'; export const handleNewMessage: HashtagHandler = async ( ctx, @@ -29,18 +30,20 @@ export const handleNewMessage: HashtagHandler = async ( } const sentChannels: number[] = []; + const [chatTags, tagKeyMap] = lowerCaseObject(chat.tags); for (const tag of tags) { - if (!chat.tags[tag]) { + if (!chatTags[tag]) { continue; } // Convert to array for backwards compatibility - if (!Array.isArray(chat.tags[tag])) { - chat.tags[tag] = [chat.tags[tag] as unknown as number]; + if (!Array.isArray(chatTags[tag])) { + chatTags[tag] = [chatTags[tag] as unknown as number]; + chat.tags[tagKeyMap[tag]] = chatTags[tag]; } - for (const channelID of chat.tags[tag]) { + for (const channelID of chatTags[tag]) { if (sentChannels.includes(channelID)) { continue; } @@ -72,10 +75,13 @@ export const handleNewMessage: HashtagHandler = async ( throw error; } - if (chat.tags[tag].length === 1) { - delete chat.tags[tag]; + if (chat.tags[tagKeyMap[tag]].length === 1) { + delete chat.tags[tagKeyMap[tag]]; } else { - chat.tags[tag].splice(chat.tags[tag].indexOf(channelID), 1); + chat.tags[tagKeyMap[tag]].splice( + chat.tags[tagKeyMap[tag]].indexOf(channelID), + 1, + ); } chat.markModified('tags'); diff --git a/src/utils.ts b/src/utils.ts index 8f16575..0611eda 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -78,3 +78,21 @@ export const formatLikeKeyboard = ( return keyboard; }; + +// returns an array where the first item is the object with all the keys lowercase +// and the second item is a map of the lowercase keys to the original keys +export const lowerCaseObject = ( + object: T, +): [T, { [k: string]: string }] => { + return Object.entries(object).reduce<[T, { [k: string]: string }]>( + ([result, keyMap], [key, value]) => { + const lowerCaseKey = key.toLowerCase(); + + result[lowerCaseKey as keyof T] = value as T[keyof T]; + keyMap[lowerCaseKey] = key; + + return [result, keyMap]; + }, + [{} as T, {} as { [k: string]: string }], + ); +}; From 258f1e1c5d97dc0f689115897124acebb9cb1a38 Mon Sep 17 00:00:00 2001 From: Avrumy Date: Wed, 15 May 2024 18:37:43 +0300 Subject: [PATCH 2/4] Switch to `copyMessage` to prevent `Bad Request: message caption is too long` --- src/handlers/hashtag/edited-message.ts | 4 +- src/handlers/hashtag/index.ts | 2 + src/handlers/hashtag/new-message.ts | 3 +- src/handlers/hashtag/utils.ts | 55 ++++++++++---------------- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/handlers/hashtag/edited-message.ts b/src/handlers/hashtag/edited-message.ts index abb15d0..e91937f 100644 --- a/src/handlers/hashtag/edited-message.ts +++ b/src/handlers/hashtag/edited-message.ts @@ -11,6 +11,7 @@ export const handleEditedMessage: HashtagHandler = async ( message, entities, text, + textIsCaption, hashtagEntities, tags, ) => { @@ -41,6 +42,7 @@ export const handleEditedMessage: HashtagHandler = async ( message, entities, text, + textIsCaption, hashtagEntities, tags, ); @@ -140,7 +142,7 @@ export const handleEditedMessage: HashtagHandler = async ( channelID: channelMessage.channel_id, message, text, - entities, + textIsCaption, }); await new Message({ diff --git a/src/handlers/hashtag/index.ts b/src/handlers/hashtag/index.ts index c4e5661..0b55b9e 100644 --- a/src/handlers/hashtag/index.ts +++ b/src/handlers/hashtag/index.ts @@ -46,6 +46,7 @@ composer message, entities, text, + Boolean(message.caption), hashtagEntities, tags, ); @@ -55,6 +56,7 @@ composer message, entities, text, + Boolean(message.caption), hashtagEntities, tags, ); diff --git a/src/handlers/hashtag/new-message.ts b/src/handlers/hashtag/new-message.ts index 77d5c4f..15a137b 100644 --- a/src/handlers/hashtag/new-message.ts +++ b/src/handlers/hashtag/new-message.ts @@ -10,6 +10,7 @@ export const handleNewMessage: HashtagHandler = async ( message, entities, text, + textIsCaption, hashtagEntities, tags, ) => { @@ -55,7 +56,7 @@ export const handleNewMessage: HashtagHandler = async ( channelID, message, text, - entities, + textIsCaption, }); sentChannels.push(channelID); diff --git a/src/handlers/hashtag/utils.ts b/src/handlers/hashtag/utils.ts index 328ed0e..29d2943 100644 --- a/src/handlers/hashtag/utils.ts +++ b/src/handlers/hashtag/utils.ts @@ -1,5 +1,5 @@ import { Context, InlineKeyboard } from 'grammy'; -import { MessageEntity, Message as TMessage } from '@grammyjs/types'; +import { MessageEntity, MessageId, Message as TMessage } from '@grammyjs/types'; import { Group as IGroup } from '../../typings/db'; import { formatLikeKeyboard } from '../../utils'; @@ -9,6 +9,7 @@ export interface HashtagHandler { message: TMessage, entities: MessageEntity[], text: string, + textIsCaption: boolean, hashtagEntities: MessageEntity[], tags: string[], ): Promise; @@ -61,15 +62,15 @@ export const sendMessage = async ({ channelID, message, text, - entities, + textIsCaption, }: { ctx: Context; chat: IGroup; channelID: number; message: TMessage; text: string; - entities: MessageEntity[]; -}): Promise => { + textIsCaption: boolean; +}): Promise => { // Use `!== false` in case it's `undefined` if (!chat.settings || chat.settings.forwards !== false) { return await ctx.api.forwardMessage( @@ -85,43 +86,27 @@ export const sendMessage = async ({ ? message.chat.username : `c/${chatId}`; + const addReplyMarkup = text.length <= (textIsCaption ? 1024 : 4096); const replyMarkup = getReplyMarkup({ chat, directLink, message_id: message.message_id, }); - if (message.audio) { - return await ctx.api.sendAudio(channelID, message.audio.file_id, { - reply_markup: replyMarkup, - caption: text, - caption_entities: entities, - }); - } else if (message.document) { - return await ctx.api.sendDocument(channelID, message.document.file_id, { - reply_markup: replyMarkup, - caption: text, - caption_entities: entities, - }); - } else if (message.photo) { - const photos = [...message.photo]; - const fileId = photos.pop()!.file_id; + const copiedMessage = await ctx.api.copyMessage( + channelID, + message.chat.id, + message.message_id, + addReplyMarkup ? { reply_markup: replyMarkup } : undefined, + ); - return await ctx.api.sendPhoto(channelID, fileId, { - reply_markup: replyMarkup, - caption: text, - caption_entities: entities, - }); - } else if (message.video) { - return await ctx.api.sendVideo(channelID, message.video.file_id, { - reply_markup: replyMarkup, - caption: text, - caption_entities: entities, - }); - } else { - return await ctx.api.sendMessage(channelID, text, { - reply_markup: replyMarkup, - entities, - }); + if (!addReplyMarkup) { + await ctx.api.editMessageReplyMarkup( + channelID, + copiedMessage.message_id, + { reply_markup: replyMarkup }, + ); } + + return copiedMessage; }; From 73075359798ced13be3d1df8dad204459076e894 Mon Sep 17 00:00:00 2001 From: Avrumy Date: Wed, 15 May 2024 18:39:05 +0300 Subject: [PATCH 3/4] Don't allow enabling likes anymore --- src/handlers/settings.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/handlers/settings.ts b/src/handlers/settings.ts index aea2010..c682163 100644 --- a/src/handlers/settings.ts +++ b/src/handlers/settings.ts @@ -25,7 +25,7 @@ const generateMarkup = (chat: IGroup) => { const comments = chat.settings!.comments === true; // Default false const likes = chat.settings!.likes === true; // Default false - return new InlineKeyboard() + const keyboard = new InlineKeyboard() .text( buttonText`Forwards ${forwards}`, buttonData('forwards', forwards), @@ -37,8 +37,13 @@ const generateMarkup = (chat: IGroup) => { buttonText`Comments ${comments}`, buttonData('comments', comments), ) - .row() - .text(buttonText`Likes ${likes}`, buttonData('likes', likes)); + .row(); + + if (chat.settings!.likes) { + keyboard.text(buttonText`Likes ${likes}`, buttonData('likes', likes)); + } + + return keyboard; }; const updateSettings = ( From 0731fddcea56e7a17d8e68bf1256078996351ef9 Mon Sep 17 00:00:00 2001 From: Avrumy Date: Wed, 15 May 2024 18:39:48 +0300 Subject: [PATCH 4/4] Fix the comments link --- src/handlers/discussion.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/discussion.ts b/src/handlers/discussion.ts index 12f3ea6..d7af89d 100644 --- a/src/handlers/discussion.ts +++ b/src/handlers/discussion.ts @@ -56,7 +56,7 @@ composer const replyMarkup = getReplyMarkup({ chat, directLink, - commentsLink: `https://t.me/c/${chatId}/1?thread=${ctx.msg.message_id}`, + commentsLink: `https://t.me/c/${chatId}/${ctx.msg.message_id}?thread=${ctx.msg.message_id}`, message_id: message.message_id, plus, minus,