diff --git a/.maestro/tests/room/room-last-message-thread-50-plus.yaml b/.maestro/tests/room/room-last-message-thread-50-plus.yaml new file mode 100644 index 00000000000..f49bb0a35d0 --- /dev/null +++ b/.maestro/tests/room/room-last-message-thread-50-plus.yaml @@ -0,0 +1,32 @@ +appId: chat.rocket.reactnative +name: Open room with last message as thread with more than 50 messages +onFlowStart: + - runFlow: '../../helpers/setup.yaml' +tags: + - test-12 + +--- +- runFlow: + file: '../../helpers/login-with-deeplink.yaml' + env: + USERNAME: ${output.account.adminUser} + PASSWORD: ${output.account.adminPassword} + +# should open a room where the last message is a thread containing more than 50 messages. +- runFlow: + file: '../../helpers/navigate-to-room.yaml' + env: + ROOM: 'maestro_test_load_threads' +- extendedWaitUntil: + visible: + text: '.*message 50.*' + timeout: 60000 +- extendedWaitUntil: + visible: + id: 'thread-count-55' + timeout: 60000 +- scrollUntilVisible: + element: + id: 'message-content-message 1' + direction: UP + timeout: 60000 diff --git a/app/lib/methods/loadMessagesForRoom.ts b/app/lib/methods/loadMessagesForRoom.ts index e2ab367454f..501ed034f26 100644 --- a/app/lib/methods/loadMessagesForRoom.ts +++ b/app/lib/methods/loadMessagesForRoom.ts @@ -10,24 +10,60 @@ import updateMessages from './updateMessages'; import { generateLoadMoreId } from './helpers/generateLoadMoreId'; const COUNT = 50; +const COUNT_LIMIT = COUNT * 10; async function load({ rid: roomId, latest, t }: { rid: string; latest?: Date; t: RoomTypes }): Promise { - let params = { roomId, count: COUNT } as { roomId: string; count: number; latest?: string }; - if (latest) { - params = { ...params, latest: new Date(latest).toISOString() }; - } - const apiType = roomTypeToApiType(t); if (!apiType) { return []; } - // RC 0.48.0 - const data = await sdk.get(`${apiType}.history`, params); - if (!data.success) { - return []; + const allMessages: IMessage[] = []; + let mainMessagesCount = 0; + + async function fetchBatch(lastTs?: string): Promise { + if (allMessages.length >= COUNT_LIMIT) { + return; + } + + const params = { roomId, count: COUNT, ...(lastTs && { latest: lastTs }) }; + + let data; + switch (apiType) { + case 'channels': + data = await sdk.get('channels.history', params); + break; + case 'groups': + data = await sdk.get('groups.history', params); + break; + case 'im': + data = await sdk.get('im.history', params); + break; + default: + return; + } + + if (!data?.success || !data.messages?.length) { + return; + } + + const batch = data.messages as IMessage[]; + allMessages.push(...batch); + + const mainMessagesInBatch = batch.filter(message => !message.tmid); + mainMessagesCount += mainMessagesInBatch.length; + + const needsMoreMainMessages = mainMessagesCount < COUNT; + + if (needsMoreMainMessages) { + const lastMessage = batch[batch.length - 1]; + await fetchBatch(lastMessage.ts as string); + } } - return data.messages as IMessage[]; + + const startTimestamp = latest ? new Date(latest).toISOString() : undefined; + await fetchBatch(startTimestamp); + return allMessages; } export function loadMessagesForRoom(args: { @@ -42,7 +78,7 @@ export function loadMessagesForRoom(args: { if (data?.length) { const lastMessage = data[data.length - 1]; const lastMessageRecord = await getMessageById(lastMessage._id as string); - if (!lastMessageRecord && data.length === COUNT) { + if (!lastMessageRecord && (data.length === COUNT || data.length >= COUNT_LIMIT)) { const loadMoreMessage = { _id: generateLoadMoreId(lastMessage._id as string), rid: lastMessage.rid,