Skip to content

Commit 81883a4

Browse files
authored
fix: remove blocked messages from offline db (#1666)
Blocked messages where not cleared from offline DB. We need to clear them before we attempt to send a new message making sure that they don't appear on the UI.
1 parent 547cd2f commit 81883a4

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/channel_state.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
deleteUserMessages as _deleteUserMessages,
1616
addToMessageList,
1717
formatMessage,
18+
isBlockedMessage,
1819
} from './utils';
1920
import { DEFAULT_MESSAGE_SET_PAGINATION } from './constants';
2021

@@ -798,6 +799,12 @@ export class ChannelState {
798799
(message) => message.type !== 'error',
799800
);
800801

802+
const blockedMessages = this.latestMessages.filter(isBlockedMessage);
803+
// We need to hard delete the blocked messages from the offline database.
804+
for (const message of blockedMessages) {
805+
this._channel.getClient().offlineDb?.hardDeleteMessage({ id: message.id });
806+
}
807+
801808
this.latestMessages = filteredMessages;
802809
}
803810

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,13 @@ export const runDetached = <T>(
13421342

13431343
promise.catch(onError);
13441344
};
1345+
1346+
export const isBlockedMessage = (message: LocalMessage) =>
1347+
message.type === 'error' &&
1348+
(message.moderation_details?.action === 'MESSAGE_RESPONSE_ACTION_REMOVE' ||
1349+
message.moderation?.action === 'remove');
1350+
1351+
export const isBouncedMessage = (message: LocalMessage) =>
1352+
message.type === 'error' &&
1353+
(message?.moderation_details?.action === 'MESSAGE_RESPONSE_ACTION_BOUNCE' ||
1354+
message?.moderation?.action === 'bounce');

test/unit/channel_state.test.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ import { DEFAULT_MESSAGE_SET_PAGINATION } from '../../src/constants';
99
import { generateUUIDv4 as uuidv4 } from '../../src/utils';
1010

1111
import { vi, describe, beforeEach, afterEach, it, expect } from 'vitest';
12+
import { MockOfflineDB } from './offline-support/MockOfflineDB';
1213

1314
const toISOString = (timestampMs) => new Date(timestampMs).toISOString();
1415

1516
describe('ChannelState addMessagesSorted', function () {
1617
let state;
18+
let client;
1719

18-
beforeEach(() => {
19-
const client = new StreamChat();
20-
client.userID = 'userId';
20+
beforeEach(async () => {
21+
client = new StreamChat();
22+
const offlineDb = new MockOfflineDB({ client });
23+
24+
client.setOfflineDBApi(offlineDb);
25+
await client.offlineDb.init(client.userID);
2126
const channel = new Channel(client, 'type', 'id', {});
2227
client._addChannelConfig({ cid: channel.cid, config: {} });
2328
state = new ChannelState(channel);
@@ -277,6 +282,36 @@ describe('ChannelState addMessagesSorted', function () {
277282
expect(state.latestMessages[2].id).to.be.equal('14');
278283
});
279284

285+
it('should remove blocked messages from the latest messages from the offline database', () => {
286+
state.addMessagesSorted(
287+
[
288+
generateMsg({
289+
id: '12',
290+
date: toISOString(1200),
291+
type: 'error',
292+
moderation_details: { action: 'MESSAGE_RESPONSE_ACTION_REMOVE' },
293+
}),
294+
generateMsg({
295+
id: '13',
296+
date: toISOString(1300),
297+
type: 'error',
298+
moderation: { action: 'remove' },
299+
}),
300+
generateMsg({ id: '14', date: toISOString(1400) }),
301+
],
302+
false,
303+
false,
304+
true,
305+
'latest',
306+
);
307+
expect(state.latestMessages.length).to.be.equal(3);
308+
state.filterErrorMessages();
309+
expect(state.latestMessages.length).to.be.equal(1);
310+
expect(client.offlineDb.hardDeleteMessage).toHaveBeenCalledTimes(2);
311+
expect(client.offlineDb.hardDeleteMessage).toHaveBeenCalledWith({ id: '12' });
312+
expect(client.offlineDb.hardDeleteMessage).toHaveBeenCalledWith({ id: '13' });
313+
});
314+
280315
it('adds message page sorted', () => {
281316
// load first page
282317
state.addMessagesSorted(

0 commit comments

Comments
 (0)