Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 8 additions & 27 deletions apps/meteor/client/views/root/hooks/useLoadMissedMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { useConnectionStatus } from '@rocket.chat/ui-contexts';
import { useEffect, useRef } from 'react';

import { LegacyRoomManager, upsertMessage } from '../../../../app/ui-utils/client';
import { sdk } from '../../../../app/utils/client/lib/SDKClient';
import { mapMessageFromApi } from '../../../lib/utils/mapMessageFromApi';
import { callWithErrorHandling } from '../../../lib/utils/callWithErrorHandling';
import { Messages, Subscriptions } from '../../../stores';

/**
Expand All @@ -22,33 +21,15 @@ const loadMissedMessages = async (rid: IRoom['_id']): Promise<void> => {
}

try {
const { result } = await sdk.rest.get('/v1/chat.syncMessages', {
roomId: rid,
lastUpdate: lastMessage.ts.toISOString(),
});

if (result?.updated?.length) {
// TODO(ddp-removal): this should move to `/v1/chat.syncMessages` so reconnect
// also reconciles edits/deletions made while offline, but that endpoint queries
// by `_updatedAt` (+ trash collection) and is currently too slow for this path.
// Evaluate/optimize the query before migrating; for now keep the DDP method.
const result = await callWithErrorHandling('loadMissedMessages', rid, lastMessage.ts);
if (result) {
const subscription = Subscriptions.state.find((record) => record.rid === rid);
// `/v1/chat.syncMessages` returns everything changed since `lastUpdate` by
// `_updatedAt`, which includes edits to older messages. We only want to
// upsert messages that are genuinely new (created after our newest loaded
// message) or that are already loaded (so edits stay in sync), otherwise we
// would inject stale messages into the room history.
await Promise.all(
result.updated
.map(mapMessageFromApi)
.filter((msg) => msg.ts.getTime() > lastMessage.ts.getTime() || Messages.state.has(msg._id))
.map((msg) => upsertMessage({ msg, subscription })),
);
await Promise.all(Array.from(result).map((msg) => upsertMessage({ msg, subscription })));
}

// Drop messages that were deleted while the connection was down, but only if
// they are currently loaded.
result?.deleted?.forEach(({ _id }) => {
if (Messages.state.has(_id)) {
Messages.state.delete(_id);
}
});
} catch (error) {
console.error('Error loading missed messages:', error);
}
Expand Down
2 changes: 0 additions & 2 deletions apps/meteor/server/methods/loadMissedMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { canAccessRoomIdAsync } from '../../app/authorization/server/functions/canAccessRoom';
import { methodDeprecationLogger } from '../../app/lib/server/lib/deprecationWarningLogger';

declare module '@rocket.chat/ddp-client' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -16,7 +15,6 @@ declare module '@rocket.chat/ddp-client' {

Meteor.methods<ServerMethods>({
async loadMissedMessages(rid, start) {
methodDeprecationLogger.method('loadMissedMessages', '9.0.0', '/v1/chat.syncMessages');
check(rid, String);
check(start, Date);

Expand Down
Loading