Skip to content

Commit 9275fb3

Browse files
committed
feat: move channel updated, visibility and deleted events to llc
1 parent 8e50703 commit 9275fb3

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

package/src/__tests__/offline-support/offline-feature.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import { useChannelsContext } from '../../contexts/channelsContext/ChannelsConte
1313
import { getOrCreateChannelApi } from '../../mock-builders/api/getOrCreateChannel';
1414
import { queryChannelsApi } from '../../mock-builders/api/queryChannels';
1515
import { useMockedApis } from '../../mock-builders/api/useMockedApis';
16+
import dispatchChannelDeletedEvent from '../../mock-builders/event/channelDeleted';
17+
import dispatchChannelHiddenEvent from '../../mock-builders/event/channelHidden';
1618
import dispatchChannelTruncatedEvent from '../../mock-builders/event/channelTruncated';
1719
import dispatchChannelUpdatedEvent from '../../mock-builders/event/channelUpdated';
20+
// import dispatchChannelVisibleEvent from '../../mock-builders/event/channelVisible';
1821
import dispatchConnectionChangedEvent from '../../mock-builders/event/connectionChanged';
1922
import dispatchMemberAddedEvent from '../../mock-builders/event/memberAdded';
2023
import dispatchMemberRemovedEvent from '../../mock-builders/event/memberRemoved';
@@ -441,6 +444,61 @@ export const Generic = () => {
441444
});
442445
});
443446

447+
it('should remove the channel from DB if the channel is deleted', async () => {
448+
useMockedApis(chatClient, [queryChannelsApi(channels)]);
449+
450+
renderComponent();
451+
act(() => dispatchConnectionChangedEvent(chatClient));
452+
await waitFor(() => expect(screen.getByTestId('channel-list')).toBeTruthy());
453+
const removedChannel = channels[getRandomInt(0, channels.length - 1)].channel;
454+
act(() => dispatchChannelDeletedEvent(chatClient, removedChannel));
455+
await waitFor(async () => {
456+
const channelIdsOnUI = screen
457+
.queryAllByLabelText('list-item')
458+
.map((node) => node._fiber.pendingProps.testID);
459+
expect(channelIdsOnUI.includes(removedChannel.cid)).toBeFalsy();
460+
await expectCIDsOnUIToBeInDB(screen.queryAllByLabelText);
461+
462+
const channelsRows = await BetterSqlite.selectFromTable('channels');
463+
const matchingRows = channelsRows.filter((c) => c.id === removedChannel.id);
464+
465+
const messagesRows = await BetterSqlite.selectFromTable('messages');
466+
const matchingMessagesRows = messagesRows.filter((m) => m.cid === removedChannel.cid);
467+
468+
expect(matchingRows.length).toBe(0);
469+
expect(matchingMessagesRows.length).toBe(0);
470+
});
471+
});
472+
473+
it('should correctly mark the channel as hidden in the db', async () => {
474+
useMockedApis(chatClient, [queryChannelsApi(channels)]);
475+
476+
renderComponent();
477+
act(() => dispatchConnectionChangedEvent(chatClient));
478+
await waitFor(() => expect(screen.getByTestId('channel-list')).toBeTruthy());
479+
const hiddenChannel = channels[getRandomInt(0, channels.length - 1)].channel;
480+
act(() => dispatchChannelHiddenEvent(chatClient, hiddenChannel));
481+
await waitFor(async () => {
482+
const channelIdsOnUI = screen
483+
.queryAllByLabelText('list-item')
484+
.map((node) => node._fiber.pendingProps.testID);
485+
expect(channelIdsOnUI.includes(hiddenChannel.cid)).toBeFalsy();
486+
await expectCIDsOnUIToBeInDB(screen.queryAllByLabelText);
487+
488+
const channelsRows = await BetterSqlite.selectFromTable('channels');
489+
const matchingRows = channelsRows.filter((c) => c.id === hiddenChannel.id);
490+
491+
const messagesRows = await BetterSqlite.selectFromTable('messages');
492+
const matchingMessagesRows = messagesRows.filter((m) => m.cid === hiddenChannel.cid);
493+
494+
expect(matchingRows.length).toBe(1);
495+
expect(matchingRows[0].hidden).toBeTruthy();
496+
expect(matchingMessagesRows.length).toBe(
497+
chatClient.activeChannels[hiddenChannel.cid].state.messages.length,
498+
);
499+
});
500+
});
501+
444502
it('should add the channel to DB when user is added as member', async () => {
445503
useMockedApis(chatClient, [queryChannelsApi(channels)]);
446504

package/src/components/Chat/hooks/handleEventToSyncDB.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { upsertChannelData } from '../../../store/apis/upsertChannelData';
1212
// import { SqliteClient } from '../../../store/SqliteClient';
1313
// import { PreparedQueries } from '../../../store/types';
1414

15+
// eslint-disable-next-line require-await
1516
export const handleEventToSyncDB = async (event: Event, client: StreamChat, flush?: boolean) => {
1617
const { type } = event;
1718
console.log('client', !!client);
@@ -190,8 +191,8 @@ export const handleEventToSyncDB = async (event: Event, client: StreamChat, flus
190191
// }
191192

192193
if (
193-
type === 'channel.updated' ||
194-
type === 'channel.visible' ||
194+
// type === 'channel.updated' ||
195+
// type === 'channel.visible' ||
195196
type === 'notification.added_to_channel' ||
196197
type === 'notification.message_new'
197198
) {
@@ -204,8 +205,8 @@ export const handleEventToSyncDB = async (event: Event, client: StreamChat, flus
204205
}
205206

206207
if (
207-
type === 'channel.hidden' ||
208-
type === 'channel.deleted' ||
208+
// type === 'channel.hidden' ||
209+
// type === 'channel.deleted' ||
209210
type === 'notification.removed_from_channel'
210211
) {
211212
if (event.channel) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default (client, channel = {}) => {
2+
client.dispatchEvent({
3+
channel,
4+
cid: channel.cid,
5+
type: 'channel.visible',
6+
});
7+
};

0 commit comments

Comments
 (0)