Skip to content

Commit

Permalink
Map message senders in matrix mode (#1076)
Browse files Browse the repository at this point in the history
* map message senders in matrix mode

* work for real-time messages

* update
  • Loading branch information
ratik21 authored Oct 6, 2023
1 parent 999232c commit 812c153
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/store/messages/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { Events as ChatEvents, getChatBus } from '../chat/bus';
import { Uploadable, createUploadableFile } from './uploadable';
import { chat } from '../../lib/chat';
import { activeChannelIdSelector } from '../chat/selectors';
import { featureFlags } from '../../lib/feature-flags';
import { User } from '../channels';

export interface Payload {
channelId: string;
Expand Down Expand Up @@ -90,6 +92,45 @@ const _isActive = (channelId) => (state) => {

const FETCH_CHAT_CHANNEL_INTERVAL = 60000;

function* getZeroUsersMap() {
const users = yield select((state) => state.normalized.users);
const zeroUsersMap: { [matrixId: string]: User } = {};
for (const user of Object.values(users)) {
zeroUsersMap[(user as User).matrixId] = user as User;
}
// map current user as well
const currentUser = yield select(currentUserSelector());
zeroUsersMap[currentUser.matrixId] = {
userId: currentUser.id,
profileId: currentUser.profileSummary.id,
firstName: currentUser.profileSummary.firstName,
lastName: currentUser.profileSummary.lastName,
profileImage: currentUser.profileSummary.profileImage,
} as User;

return zeroUsersMap;
}

export function* mapMessageSenders(messages) {
if (!featureFlags.enableMatrix) {
return;
}

const zeroUsersMap = yield call(getZeroUsersMap);
messages.forEach((message) => {
const zeroUser = zeroUsersMap[message.sender.userId];
if (zeroUser) {
message.sender = {
userId: zeroUser.userId,
profileId: zeroUser.profileId,
firstName: zeroUser.firstName,
lastName: zeroUser.lastName,
profileImage: zeroUser.profileImage,
};
}
});
}

export function* fetch(action) {
const { channelId, referenceTimestamp } = action.payload;
const channel = yield select(rawChannelSelector(channelId));
Expand All @@ -106,11 +147,13 @@ export function* fetch(action) {
if (referenceTimestamp) {
yield put(receive({ id: channelId, messagesFetchStatus: MessagesFetchState.MORE_IN_PROGRESS }));
messagesResponse = yield call([chatClient, chatClient.getMessagesByChannelId], channelId, referenceTimestamp);
yield call(mapMessageSenders, messagesResponse.messages);
const existingMessages = yield select(rawMessagesSelector(channelId));
messages = [...messagesResponse.messages, ...existingMessages];
} else {
yield put(receive({ id: channelId, messagesFetchStatus: MessagesFetchState.IN_PROGRESS }));
messagesResponse = yield call([chatClient, chatClient.getMessagesByChannelId], channelId);
yield call(mapMessageSenders, messagesResponse.messages);
const existingMessages = yield select(rawMessagesSelector(channelId));
messages = [...existingMessages, ...messagesResponse.messages];
}
Expand Down Expand Up @@ -310,6 +353,7 @@ export function* fetchNewMessages(channelId: string) {
],
channelId
);
yield call(mapMessageSenders, messagesResponse.messages);

yield put(
receive({
Expand Down Expand Up @@ -425,6 +469,7 @@ export function* receiveDelete(action) {

export function* receiveNewMessage(action) {
let { channelId, message } = action.payload;
yield call(mapMessageSenders, [message]);

const channel = yield select(rawChannelSelector(channelId));
const currentMessages = channel?.messages || [];
Expand Down

0 comments on commit 812c153

Please sign in to comment.