Comment Threads: Fix sometimes missing top message

This commit is contained in:
Alexander Zinchuk 2021-05-11 23:18:05 +03:00
parent f26f30ccb9
commit a70efbbd08
4 changed files with 15 additions and 21 deletions

View File

@ -103,26 +103,11 @@ export async function fetchMessages({
const chats = result.chats.map((c) => buildApiChatFromPreview(c)).filter<ApiChat>(Boolean as any); const chats = result.chats.map((c) => buildApiChatFromPreview(c)).filter<ApiChat>(Boolean as any);
const threadInfos = messages.map(({ threadInfo }) => threadInfo).filter<ApiThreadInfo>(Boolean as any); const threadInfos = messages.map(({ threadInfo }) => threadInfo).filter<ApiThreadInfo>(Boolean as any);
// Not sure if there is an easier way to do this
let firstMessageId: number | undefined;
if (result.messages.length) {
if (result instanceof GramJs.messages.Messages) {
firstMessageId = result.messages[result.messages.length - 1].id;
} else if (pagination.offsetId && result.offsetIdOffset) {
const offsetIdIndex = result.messages.findIndex((m) => m.id === pagination.offsetId);
const lastIndex = result.messages.length - offsetIdIndex;
if (lastIndex + result.offsetIdOffset >= result.count) {
firstMessageId = result.messages[result.messages.length - 1].id;
}
}
}
return { return {
messages, messages,
users, users,
chats, chats,
threadInfos, threadInfos,
firstMessageId,
}; };
} }
@ -702,6 +687,8 @@ export async function requestThreadInfoUpdate({
invokeRequest(new GramJs.messages.GetReplies({ invokeRequest(new GramJs.messages.GetReplies({
peer: buildInputPeer(chat.id, chat.accessHash), peer: buildInputPeer(chat.id, chat.accessHash),
msgId: threadId, msgId: threadId,
offsetId: 1,
addOffset: -1,
limit: 1, limit: 1,
})), })),
]); ]);
@ -724,6 +711,9 @@ export async function requestThreadInfoUpdate({
lastReadInboxMessageId: topMessageResult.readInboxMaxId, lastReadInboxMessageId: topMessageResult.readInboxMaxId,
messagesCount: (repliesResult instanceof GramJs.messages.ChannelMessages) ? repliesResult.count : undefined, messagesCount: (repliesResult instanceof GramJs.messages.ChannelMessages) ? repliesResult.count : undefined,
}, },
firstMessageId: repliesResult && 'messages' in repliesResult && repliesResult.messages.length
? repliesResult.messages[0].id
: undefined,
}); });
const chats = topMessageResult.chats.map((c) => buildApiChatFromPreview(c)).filter<ApiChat>(Boolean as any); const chats = topMessageResult.chats.map((c) => buildApiChatFromPreview(c)).filter<ApiChat>(Boolean as any);

View File

@ -174,6 +174,7 @@ export type ApiUpdateThreadInfo = {
chatId: number; chatId: number;
threadId: number; threadId: number;
threadInfo: Partial<ApiThreadInfo>; threadInfo: Partial<ApiThreadInfo>;
firstMessageId?: number;
}; };
export type ApiUpdateScheduledMessageSendSucceeded = { export type ApiUpdateScheduledMessageSendSucceeded = {

View File

@ -576,7 +576,7 @@ async function loadViewportMessages(
} }
const { const {
messages, users, chats, threadInfos, firstMessageId, messages, users, chats, threadInfos,
} = result; } = result;
const byId = buildCollectionByKey(messages, 'id'); const byId = buildCollectionByKey(messages, 'id');
@ -592,10 +592,6 @@ async function loadViewportMessages(
global = addChats(global, buildCollectionByKey(chats, 'id')); global = addChats(global, buildCollectionByKey(chats, 'id'));
global = updateThreadInfos(global, chatId, threadInfos); global = updateThreadInfos(global, chatId, threadInfos);
if (firstMessageId) {
global = replaceThreadParam(global, chatId, threadId, 'firstMessageId', firstMessageId);
}
let listedIds = selectListedIds(global, chatId, threadId); let listedIds = selectListedIds(global, chatId, threadId);
const outlyingIds = selectOutlyingIds(global, chatId, threadId); const outlyingIds = selectOutlyingIds(global, chatId, threadId);

View File

@ -210,7 +210,9 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
} }
case 'updateThreadInfo': { case 'updateThreadInfo': {
const { chatId, threadId, threadInfo } = update; const {
chatId, threadId, threadInfo, firstMessageId,
} = update;
const currentThreadInfo = selectThreadInfo(global, chatId, threadId); const currentThreadInfo = selectThreadInfo(global, chatId, threadId);
const newTheadInfo = { const newTheadInfo = {
@ -223,6 +225,11 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
} }
global = updateThreadInfo(global, chatId, threadId, newTheadInfo as ApiThreadInfo); global = updateThreadInfo(global, chatId, threadId, newTheadInfo as ApiThreadInfo);
if (firstMessageId) {
global = replaceThreadParam(global, chatId, threadId, 'firstMessageId', firstMessageId);
}
setGlobal(global); setGlobal(global);
break; break;