Skip to content

Commit

Permalink
refactor(matrix-client): optimize room tags fetching by using in-memo…
Browse files Browse the repository at this point in the history
…ry data instead of network requests (#2740)
  • Loading branch information
domw30 authored Mar 4, 2025
1 parent 3ace500 commit 53660d9
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 57 deletions.
4 changes: 0 additions & 4 deletions src/lib/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,6 @@ export async function removeRoomFromLabel(roomId: string, label: string) {
return await chat.get().matrix.removeRoomFromLabel(roomId, label);
}

export async function getRoomTags(conversations: Partial<Channel>[]) {
return await chat.get().matrix.getRoomTags(conversations);
}

export async function createUnencryptedConversation(
users: User[],
name: string,
Expand Down
35 changes: 0 additions & 35 deletions src/lib/chat/matrix-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const getSdkClient = (sdkClient = {}) => ({
invite: jest.fn().mockResolvedValue({}),
setRoomTag: jest.fn().mockResolvedValue({}),
deleteRoomTag: jest.fn().mockResolvedValue({}),
getRoomTags: jest.fn().mockResolvedValue({}),
...sdkClient,
});

Expand Down Expand Up @@ -1681,40 +1680,6 @@ describe('matrix client', () => {
});
});

describe('getRoomTags', () => {
it('returns correct tags for all rooms', async () => {
const conversations = [
{ id: 'room1', labels: [] },
{ id: 'room2', labels: [] },
];

const getRoomTags = jest
.fn()
.mockResolvedValueOnce({ tags: { 'm.favorite': {}, 'm.mute': {}, 'm.work': {}, 'm.family': {} } })
.mockResolvedValueOnce({ tags: {} });

const client = subject({ createClient: jest.fn(() => getSdkClient({ getRoomTags })) });

await client.connect(null, 'token');
await client.getRoomTags(conversations);

expect(getRoomTags).toHaveBeenCalledWith('room1');
expect(getRoomTags).toHaveBeenCalledWith('room2');
expect(conversations).toEqual([
{
id: 'room1',
labels: [
'm.favorite',
'm.mute',
'm.work',
'm.family',
],
},
{ id: 'room2', labels: [] },
]);
});
});

describe('getPostMessageReactions', () => {
it('returns the correct reactions for a room', async () => {
const mockGetRoom = jest.fn().mockReturnValue({
Expand Down
14 changes: 2 additions & 12 deletions src/lib/chat/matrix-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1227,17 +1227,6 @@ export class MatrixClient implements IChatClient {
return await Promise.all(matches.map((r) => this.mapConversation(r)));
}

async getRoomTags(conversations: Partial<Channel>[]): Promise<void> {
const tags = conversations.map(async (conversation) => {
featureFlags.enableTimerLogs && console.time(`xxxgetRoomTags${conversation.id}`);
const result = await this.matrix.getRoomTags(conversation.id);
conversation.labels = Object.keys(result.tags);
featureFlags.enableTimerLogs && console.timeEnd(`xxxgetRoomTags${conversation.id}`);
});

await Promise.all(tags);
}

async addRoomToLabel(roomId: string, label: string): Promise<void> {
await this.waitForConnection();
await this.matrix.setRoomTag(roomId, label);
Expand Down Expand Up @@ -1593,6 +1582,7 @@ export class MatrixClient implements IChatClient {
const avatarUrl = this.getRoomAvatar(room);
const createdAt = this.getRoomCreatedAt(room);
const groupType = this.getRoomGroupType(room);
const roomTags = Object.keys(room.tags || {});

featureFlags.enableTimerLogs && console.time(`xxxgetUpToLatestUserMessageFromRoom${room.roomId}`);
const messages = await this.getUpToLatestUserMessageFromRoom(room);
Expand Down Expand Up @@ -1620,7 +1610,7 @@ export class MatrixClient implements IChatClient {
conversationStatus: ConversationStatus.CREATED,
adminMatrixIds: admins,
moderatorIds: mods,
labels: [],
labels: roomTags,
isSocialChannel,
// this isn't the best way to get the zid as it relies on the name format, but it's a quick fix
zid: isSocialChannel ? name?.split('://')[1] : null,
Expand Down
4 changes: 1 addition & 3 deletions src/store/channels-list/saga.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { testSaga } from 'redux-saga-test-plan';
import { call } from 'redux-saga/effects';
import * as matchers from 'redux-saga-test-plan/matchers';
import { chat, getRoomTags } from '../../lib/chat';
import { chat } from '../../lib/chat';

import {
fetchConversations,
Expand Down Expand Up @@ -59,7 +59,6 @@ describe('channels list saga', () => {
[matchers.call.fn(chatClient.getConversations), MOCK_CONVERSATIONS],
[matchers.call.fn(mapToZeroUsers), null],
[matchers.call.fn(getUserReadReceiptPreference), null],
[matchers.call.fn(getRoomTags), null],
]);
}

Expand Down Expand Up @@ -136,7 +135,6 @@ describe('channels list saga', () => {
[matchers.call.fn(chatClient.getConversations), MOCK_CONVERSATIONS],
[matchers.call.fn(mapToZeroUsers), null],
[matchers.call.fn(getUserReadReceiptPreference), null],
[matchers.call.fn(getRoomTags), null],
])
.withReducer(rootReducer, { channelsList: { value: [] } } as RootState)
.fork(loadSecondaryConversationData, [...MOCK_CONVERSATIONS])
Expand Down
4 changes: 1 addition & 3 deletions src/store/channels-list/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getDeepProperty from 'lodash.get';
import uniqBy from 'lodash.uniqby';
import { fork, put, call, take, all, select, spawn } from 'redux-saga/effects';
import { receive, denormalizeConversations, setStatus } from '.';
import { batchDownloadFiles, chat, downloadFile, getRoomTags } from '../../lib/chat';
import { batchDownloadFiles, chat, downloadFile } from '../../lib/chat';

import { AsyncListStatus } from '../normalized';
import { toLocalChannel, mapChannelMembers, mapChannelMessages } from './utils';
Expand Down Expand Up @@ -176,13 +176,11 @@ export function* fetchConversations() {

export function* loadSecondaryConversationData(conversations) {
yield put(setIsSecondaryConversationDataLoaded(false));
yield call(getRoomTags, conversations);
yield call(parseProfileImagesForMembers, conversations);

const receiveCalls = conversations.map((conversation) =>
call(receiveChannel, {
id: conversation.id,
labels: conversation.labels,
otherMembers: conversation.otherMembers,
memberHistory: conversation.memberHistory,
})
Expand Down

0 comments on commit 53660d9

Please sign in to comment.