From 53827859a200cd8a44b58e1ebeb3db4276e2259a Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sat, 4 Mar 2023 03:16:27 +0100 Subject: [PATCH] Comments: Poll new counters periodically (#2747) --- src/api/gramjs/methods/index.ts | 2 +- src/api/gramjs/methods/messages.ts | 24 ++++++++++++++++++ src/components/middle/MessageList.tsx | 13 ++++++++++ src/global/actions/api/messages.ts | 35 +++++++++++++++++++++++++++ src/global/types.ts | 4 +++ src/lib/gramjs/tl/apiTl.js | 1 + src/lib/gramjs/tl/static/api.json | 3 ++- 7 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/api/gramjs/methods/index.ts b/src/api/gramjs/methods/index.ts index 8f47caec9..2e23ca586 100644 --- a/src/api/gramjs/methods/index.ts +++ b/src/api/gramjs/methods/index.ts @@ -30,7 +30,7 @@ export { fetchPinnedMessages, fetchScheduledHistory, sendScheduledMessages, rescheduleMessage, deleteScheduledMessages, reportMessages, sendMessageAction, fetchSeenBy, fetchSponsoredMessages, viewSponsoredMessage, fetchSendAs, saveDefaultSendAs, fetchUnreadReactions, readAllReactions, fetchUnreadMentions, readAllMentions, transcribeAudio, - closePoll, fetchExtendedMedia, translateText, + closePoll, fetchExtendedMedia, translateText, fetchMessageViews, } from './messages'; export { diff --git a/src/api/gramjs/methods/messages.ts b/src/api/gramjs/methods/messages.ts index 97239a911..743e03638 100644 --- a/src/api/gramjs/methods/messages.ts +++ b/src/api/gramjs/methods/messages.ts @@ -871,6 +871,30 @@ export async function markMessagesRead({ }); } +export async function fetchMessageViews({ + chat, ids, +}: { + chat: ApiChat; ids: number[]; +}) { + const result = await invokeRequest(new GramJs.messages.GetMessagesViews({ + peer: buildInputPeer(chat.id, chat.accessHash), + id: ids, + increment: false, + })); + + if (!result) return undefined; + + return ids.map((id, index) => { + const { views, forwards, replies } = result.views[index]; + return { + id, + views, + forwards, + messagesCount: replies?.replies, + }; + }); +} + export async function requestThreadInfoUpdate({ chat, threadId, originChannelId, }: { diff --git a/src/components/middle/MessageList.tsx b/src/components/middle/MessageList.tsx index 73e41d73e..26acfb9ae 100644 --- a/src/components/middle/MessageList.tsx +++ b/src/components/middle/MessageList.tsx @@ -114,6 +114,7 @@ type StateProps = { }; const MESSAGE_REACTIONS_POLLING_INTERVAL = 15 * 1000; +const MESSAGE_COMMENTS_POLLING_INTERVAL = 15 * 1000; const BOTTOM_THRESHOLD = 50; const UNREAD_DIVIDER_TOP = 10; const UNREAD_DIVIDER_TOP_WITH_TOOLS = 60; @@ -164,6 +165,7 @@ const MessageList: FC = ({ }) => { const { loadViewportMessages, setScrollOffset, loadSponsoredMessages, loadMessageReactions, copyMessagesByIds, + loadMessageViews, } = getActions(); // eslint-disable-next-line no-null/no-null @@ -262,6 +264,17 @@ const MessageList: FC = ({ loadMessageReactions({ chatId, ids }); }, MESSAGE_REACTIONS_POLLING_INTERVAL); + useInterval(() => { + if (!messageIds || !messagesById || threadId !== MAIN_THREAD_ID) { + return; + } + const ids = messageIds.filter((id) => messagesById[id]?.repliesThreadInfo?.isComments); + + if (!ids.length) return; + + loadMessageViews({ chatId, ids }); + }, MESSAGE_COMMENTS_POLLING_INTERVAL); + const loadMoreAround = useMemo(() => { if (type !== 'thread') { return undefined; diff --git a/src/global/actions/api/messages.ts b/src/global/actions/api/messages.ts index 742e9bd5d..f8dfcc704 100644 --- a/src/global/actions/api/messages.ts +++ b/src/global/actions/api/messages.ts @@ -1459,6 +1459,41 @@ addActionHandler('translateMessages', (global, actions, payload): ActionReturnTy return global; }); +addActionHandler('loadMessageViews', async (global, actions, payload): Promise => { + const { chatId, ids } = payload; + + const chat = selectChat(global, chatId); + if (!chat) return; + + const result = await callApi('fetchMessageViews', { + chat, + ids, + }); + + if (!result) return; + + global = getGlobal(); + result.forEach((update) => { + global = updateChatMessage(global, chatId, update.id, { + views: update.views, + forwards: update.forwards, + }); + + const message = selectChatMessage(global, chatId, update.id); + if (!message) return; + + const repliesChatId = message.repliesThreadInfo?.chatId; + const threadId = message.repliesThreadInfo?.threadId; + if (!repliesChatId || !threadId) return; + + global = updateThreadInfo(global, repliesChatId, threadId, { + messagesCount: update.messagesCount, + }); + }); + + setGlobal(global); +}); + function countSortedIds(ids: number[], from: number, to: number) { let count = 0; diff --git a/src/global/types.ts b/src/global/types.ts index e74526093..5a6be8536 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -1666,6 +1666,10 @@ export interface ActionPayloads { chatId: string; offsetId?: number; }; + loadMessageViews: { + chatId: string; + ids: number[]; + }; animateUnreadReaction: { messageIds: number[]; } & WithTabId; diff --git a/src/lib/gramjs/tl/apiTl.js b/src/lib/gramjs/tl/apiTl.js index 7d63f6667..4cd18589f 100644 --- a/src/lib/gramjs/tl/apiTl.js +++ b/src/lib/gramjs/tl/apiTl.js @@ -1196,6 +1196,7 @@ messages.getStickerSet#c8a0ec74 stickerset:InputStickerSet hash:int = messages.S messages.installStickerSet#c78fe460 stickerset:InputStickerSet archived:Bool = messages.StickerSetInstallResult; messages.uninstallStickerSet#f96e55de stickerset:InputStickerSet = Bool; messages.startBot#e6df7378 bot:InputUser peer:InputPeer random_id:long start_param:string = Updates; +messages.getMessagesViews#5784d3e1 peer:InputPeer id:Vector increment:Bool = messages.MessageViews; messages.migrateChat#a2875319 chat_id:long = Updates; messages.searchGlobal#4bc6589a flags:# folder_id:flags.0?int q:string filter:MessagesFilter min_date:int max_date:int offset_rate:int offset_peer:InputPeer offset_id:int limit:int = messages.Messages; messages.getDocumentByHash#b1f2061f sha256:bytes size:long mime_type:string = Document; diff --git a/src/lib/gramjs/tl/static/api.json b/src/lib/gramjs/tl/static/api.json index edc3fce5a..3941ec7a4 100644 --- a/src/lib/gramjs/tl/static/api.json +++ b/src/lib/gramjs/tl/static/api.json @@ -275,5 +275,6 @@ "channels.updatePinnedForumTopic", "channels.deleteTopicHistory", "channels.toggleParticipantsHidden", - "photos.uploadContactProfilePhoto" + "photos.uploadContactProfilePhoto", + "messages.getMessagesViews" ]