Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .maestro/tests/room/room-last-message-thread-50-plus.yaml
Original file line number Diff line number Diff line change
@@ -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
58 changes: 47 additions & 11 deletions app/lib/methods/loadMessagesForRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,60 @@ import updateMessages from './updateMessages';
import { generateLoadMoreId } from './helpers/generateLoadMoreId';

const COUNT = 50;
const COUNT_LIMIT = COUNT * 10;

Comment thread
OtavioStasiak marked this conversation as resolved.
async function load({ rid: roomId, latest, t }: { rid: string; latest?: Date; t: RoomTypes }): Promise<IMessage[]> {
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<void> {
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;
}
Comment thread
OtavioStasiak marked this conversation as resolved.

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);
}
Comment thread
OtavioStasiak marked this conversation as resolved.
}
return data.messages as IMessage[];

const startTimestamp = latest ? new Date(latest).toISOString() : undefined;
await fetchBatch(startTimestamp);
return allMessages;
Comment thread
OtavioStasiak marked this conversation as resolved.
}

export function loadMessagesForRoom(args: {
Expand All @@ -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,
Expand Down
Loading