Skip to content

Commit

Permalink
feat(conversation-list-item/matrix-client): handle message previews f…
Browse files Browse the repository at this point in the history
…or reaction events (#2343)
  • Loading branch information
domw30 authored Oct 8, 2024
1 parent 8db77e1 commit 80f4edc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/lib/chat/chat-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,35 @@ describe(adminMessageText, () => {
expect(adminText).toEqual('Courtney was removed as moderator by admin');
});
});

describe(AdminMessageType.REACTION, () => {
it('returns default message if admin user id not found', () => {
const state = getState('current-user', {});
const adminText = adminMessageText(
{
message: 'some message',
isAdmin: true,
admin: { type: AdminMessageType.REACTION, userId: 'unknown-user-id' },
} as Message,
state
);

expect(adminText).toEqual('some message');
});

it('translates message if admin user id is found', () => {
const state = getState('current-user', { 'admin-user-id': { id: 'admin-user-id', firstName: 'Courtney' } });
const message = {
message: 'some message',
isAdmin: true,
admin: { type: AdminMessageType.REACTION, userId: 'admin-user-id', amount: '10' },
} as any;

const adminText = adminMessageText(message, state);

expect(adminText).toEqual('Courtney reacted with 10 MEOW');
});
});
});

describe(getMessagePreview, () => {
Expand Down
11 changes: 11 additions & 0 deletions src/lib/chat/chat-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,23 @@ export function adminMessageText(message: Message, state: RootState) {
return translateMemberSetAsModerator(message.admin, state) ?? text;
case AdminMessageType.MEMBER_REMOVED_AS_MODERATOR:
return translateMemberRemovedAsModerator(message.admin, state) ?? text;
case AdminMessageType.REACTION:
return translateReaction(message.admin, user, state) ?? text;

default:
return text;
}
}

function translateReaction(admin: { userId?: string; amount?: string }, currentUser, state: RootState) {
if (admin.userId === currentUser.id) {
return `You reacted with ${admin.amount} MEOW`;
}

const user = denormalizeUser(admin.userId, state);
return user?.firstName ? `${user.firstName} reacted with ${admin.amount} MEOW` : null;
}

function translateJoinedZero(admin: { inviteeId?: string; inviterId?: string }, currentUser, state: RootState) {
const isCurrentUserInvitee = admin.inviteeId === currentUser.id;
if (isCurrentUserInvitee) {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/chat/matrix-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ export class MatrixClient implements IChatClient {

case EventType.RoomPowerLevels:
return mapEventToAdminMessage(event);

case EventType.Reaction:
return mapEventToAdminMessage(event);
default:
return null;
}
Expand Down Expand Up @@ -1505,7 +1508,7 @@ export class MatrixClient implements IChatClient {
.filter((event) => {
if (found) return false;

if (event.type === EventType.RoomMessage) {
if (event.type === EventType.RoomMessage || event.type === CustomEventType.ROOM_POST) {
found = true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/chat/matrix/chat-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,17 @@ function getAdminDataFromEventType(type, content, sender, targetUserId, previous
return { type: AdminMessageType.CONVERSATION_STARTED, userId: sender };
case EventType.RoomPowerLevels:
return getRoomPowerLevelsChangedAdminData(content, previousContent);
case EventType.Reaction:
return getRoomReactionAdminData(content, sender);
default:
return null;
}
}

function getRoomReactionAdminData(content, sender) {
return { type: AdminMessageType.REACTION, userId: sender, amount: content.amount };
}

function getRoomMemberAdminData(content, targetUserId) {
switch (content.membership) {
case MembershipStateType.Leave:
Expand Down
1 change: 1 addition & 0 deletions src/store/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export enum AdminMessageType {
MEMBER_ADDED_TO_CONVERSATION = 'MEMBER_ADDED_TO_CONVERSATION',
MEMBER_SET_AS_MODERATOR = 'MEMBER_SET_AS_MODERATOR',
MEMBER_REMOVED_AS_MODERATOR = 'MEMBER_REMOVED_AS_MODERATOR',
REACTION = 'REACTION',
}

export enum MessageSendStatus {
Expand Down

0 comments on commit 80f4edc

Please sign in to comment.