Skip to content

Commit

Permalink
refactor: make roomcoordinator.roomName async (#28609)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Mar 24, 2023
1 parent 5da5a17 commit 1ac2768
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 28 deletions.
10 changes: 5 additions & 5 deletions apps/meteor/app/lib/server/functions/notifications/email.js
Expand Up @@ -21,10 +21,10 @@ Meteor.startup(() => {
});
});

function getEmailContent({ message, user, room }) {
async function getEmailContent({ message, user, room }) {
const lng = (user && user.language) || settings.get('Language') || 'en';

const roomName = escapeHTML(`#${roomCoordinator.getRoomName(room.t, room)}`);
const roomName = escapeHTML(`#${await roomCoordinator.getRoomName(room.t, room)}`);
const userName = escapeHTML(settings.get('UI_Use_Real_Name') ? message.u.name || message.u.username : message.u.username);

const roomDirectives = roomCoordinator.getRoomDirectives(room.t);
Expand Down Expand Up @@ -117,7 +117,7 @@ function generateNameEmail(name, email) {
return `${String(name).replace(/@/g, '%40').replace(/[<>,]/g, '')} <${email}>`;
}

export function getEmailData({ message, receiver, sender, subscription, room, emailAddress, hasMentionToUser }) {
export async function getEmailData({ message, receiver, sender, subscription, room, emailAddress, hasMentionToUser }) {
const username = settings.get('UI_Use_Real_Name') ? message.u.name || message.u.username : message.u.username;
let subjectKey = 'Offline_Mention_All_Email';

Expand All @@ -129,9 +129,9 @@ export function getEmailData({ message, receiver, sender, subscription, room, em

const emailSubject = Mailer.replace(settings.get(subjectKey), {
user: username,
room: roomCoordinator.getRoomName(room.t, room),
room: await roomCoordinator.getRoomName(room.t, room),
});
const content = getEmailContent({
const content = await getEmailContent({
message,
user: receiver,
room,
Expand Down
Expand Up @@ -55,7 +55,7 @@ export async function getPushData({
},
roomName:
settings.get('Push_show_username_room') && roomCoordinator.getRoomDirectives(room.t).isGroupChat(room)
? `#${roomCoordinator.getRoomName(room.t, room, userId)}`
? `#${await roomCoordinator.getRoomName(room.t, room, userId)}`
: '',
username,
message: messageText,
Expand Down
9 changes: 4 additions & 5 deletions apps/meteor/app/lib/server/lib/sendNotificationsOnMessage.js
Expand Up @@ -146,11 +146,11 @@ export const sendNotification = async ({
isThread,
})
) {
receiver.emails.some((email) => {
for await (const email of receiver.emails) {
if (email.verified) {
queueItems.push({
type: 'email',
data: getEmailData({
data: await getEmailData({
message,
receiver,
sender,
Expand All @@ -161,10 +161,9 @@ export const sendNotification = async ({
}),
});

return true;
break;
}
return false;
});
}
}

if (queueItems.length) {
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/definition/IRoomTypeConfig.ts
Expand Up @@ -100,7 +100,7 @@ export interface IRoomTypeServerDirectives {

allowRoomSettingChange: (room: IRoom, setting: ValueOf<typeof RoomSettingsEnum>) => boolean;
allowMemberAction: (room: IRoom, action: ValueOf<typeof RoomMemberActions>, userId?: IUser['_id']) => Promise<boolean>;
roomName: (room: IRoom, userId?: string) => string | undefined;
roomName: (room: IRoom, userId?: string) => Promise<string | undefined>;
isGroupChat: (room: IRoom) => boolean;
canBeDeleted: (hasPermission: (permissionId: string, rid?: string) => Promise<boolean> | boolean, room: IRoom) => Promise<boolean>;
preventRenaming: () => boolean;
Expand All @@ -111,7 +111,7 @@ export interface IRoomTypeServerDirectives {
sender: AtLeast<IUser, '_id' | 'name' | 'username'>,
notificationMessage: string,
userId: string,
) => { title: string | undefined; text: string };
) => Promise<{ title: string | undefined; text: string }>;
getMsgSender: (senderId: IRocketChatRecord['_id']) => Promise<IRocketChatRecord | undefined>;
includeInRoomSearch: () => boolean;
getReadReceiptsExtraData: (message: IMessage) => Partial<ReadReceipt>;
Expand Down
12 changes: 6 additions & 6 deletions apps/meteor/server/lib/rooms/roomCoordinator.ts
Expand Up @@ -14,7 +14,7 @@ class RoomCoordinatorServer extends RoomCoordinator {
async allowMemberAction(_room: IRoom, _action: ValueOf<typeof RoomMemberActions>, _userId?: IUser['_id']): Promise<boolean> {
return false;
},
roomName(_room: IRoom, _userId?: string): string {
async roomName(_room: IRoom, _userId?: string): Promise<string> {
return '';
},
isGroupChat(_room: IRoom): boolean {
Expand All @@ -35,13 +35,13 @@ class RoomCoordinatorServer extends RoomCoordinator {
canAccessUploadedFile(_params: { rc_uid: string; rc_rid: string; rc_token: string }): boolean {
return false;
},
getNotificationDetails(
async getNotificationDetails(
room: IRoom,
sender: AtLeast<IUser, '_id' | 'name' | 'username'>,
notificationMessage: string,
userId: string,
): { title: string | undefined; text: string } {
const title = `#${this.roomName(room, userId)}`;
): Promise<{ title: string | undefined; text: string }> {
const title = `#${await this.roomName(room, userId)}`;
const name = settings.get<boolean>('UI_Use_Real_Name') ? sender.name : sender.username;

const text = `${name}: ${notificationMessage}`;
Expand Down Expand Up @@ -83,8 +83,8 @@ class RoomCoordinatorServer extends RoomCoordinator {
return Object.keys(this.roomTypes).filter((key) => (this.roomTypes[key].directives as IRoomTypeServerDirectives).includeInDashboard());
}

getRoomName(roomType: string, roomData: IRoom, userId?: string): string {
return this.getRoomDirectives(roomType).roomName(roomData, userId) ?? '';
async getRoomName(roomType: string, roomData: IRoom, userId?: string): Promise<string> {
return (await this.getRoomDirectives(roomType).roomName(roomData, userId)) ?? '';
}

setRoomFind(roomType: string, roomFind: Required<Pick<IRoomTypeServerDirectives, 'roomFind'>>['roomFind']): void {
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/server/lib/rooms/roomTypes/direct.ts
Expand Up @@ -54,7 +54,7 @@ roomCoordinator.add(DirectMessageRoomType, {
}
},

roomName(room, userId?) {
async roomName(room, userId?) {
const subscription = ((): { fname?: string; name?: string } | undefined => {
if (room.fname || room.name) {
return {
Expand Down Expand Up @@ -91,7 +91,7 @@ roomCoordinator.add(DirectMessageRoomType, {
return (room?.uids?.length || 0) > 2;
},

getNotificationDetails(room, sender, notificationMessage, userId) {
async getNotificationDetails(room, sender, notificationMessage, userId) {
const useRealName = settings.get<boolean>('UI_Use_Real_Name');

if (this.isGroupChat(room)) {
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/server/lib/rooms/roomTypes/livechat.ts
Expand Up @@ -22,15 +22,15 @@ roomCoordinator.add(LivechatRoomType, {
return ([RoomMemberActions.INVITE, RoomMemberActions.JOIN] as Array<ValueOf<typeof RoomMemberActions>>).includes(action);
},

roomName(room, _userId?) {
async roomName(room, _userId?) {
return room.name || room.fname || (room as any).label;
},

canAccessUploadedFile({ rc_token: token, rc_rid: rid }) {
return token && rid && !!Promise.await(LivechatRooms.findOneOpenByRoomIdAndVisitorToken(rid, token));
},

getNotificationDetails(room, _sender, notificationMessage, userId) {
async getNotificationDetails(room, _sender, notificationMessage, userId) {
const title = `[Omnichannel] ${this.roomName(room, userId)}`;
const text = notificationMessage;

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/lib/rooms/roomTypes/private.ts
Expand Up @@ -43,7 +43,7 @@ roomCoordinator.add(PrivateRoomType, {
}
},

roomName(room, _userId?) {
async roomName(room, _userId?) {
if (room.prid || isRoomFederated(room)) {
return room.fname;
}
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/lib/rooms/roomTypes/public.ts
Expand Up @@ -43,7 +43,7 @@ roomCoordinator.add(PublicRoomType, {
}
},

roomName(room, _userId?) {
async roomName(room, _userId?) {
if (room.prid || isRoomFederated(room)) {
return room.fname;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/server/lib/rooms/roomTypes/voip.ts
Expand Up @@ -8,11 +8,11 @@ import { roomCoordinator } from '../roomCoordinator';
const VoipRoomType = getVoipRoomType(roomCoordinator);

roomCoordinator.add(VoipRoomType, {
roomName(room, _userId?) {
async roomName(room, _userId?) {
return room.name || room.fname || (room as any).label;
},

getNotificationDetails(room, _sender, notificationMessage, userId) {
async getNotificationDetails(room, _sender, notificationMessage, userId) {
const title = `[Omnichannel] ${this.roomName(room, userId)}`;
const text = notificationMessage;

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/routes/avatar/room.js
Expand Up @@ -56,7 +56,7 @@ export const roomAvatar = Meteor.bindEnvironment(async function (req, res /* , n
return FileUpload.get(file, req, res);
}

const roomName = roomCoordinator.getRoomName(room.t, room, uid);
const roomName = await roomCoordinator.getRoomName(room.t, room, uid);

setCacheAndDispositionHeaders(req, res);

Expand Down

0 comments on commit 1ac2768

Please sign in to comment.