Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh committed Sep 26, 2023
1 parent 2462d5c commit 06a3ebb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
21 changes: 12 additions & 9 deletions apps/meteor/app/lib/server/startup/mentionUserNotInChannel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { api } from '@rocket.chat/core-services';
import type { IMessage } from '@rocket.chat/core-typings';
import { isDirectMessageRoom, isEditedMessage, isRoomFederated } from '@rocket.chat/core-typings';
import { Subscriptions, Rooms } from '@rocket.chat/models';
import type { ActionsBlock } from '@rocket.chat/ui-kit';
Expand All @@ -14,13 +15,14 @@ import { hasAtLeastOnePermissionAsync, hasPermissionAsync } from '../../../autho
const permissionsToAddUserToRoom = ['add-user-to-joined-room', 'add-user-to-any-c-room', 'add-user-to-any-p-room'];

const APP_ID = 'mention-core';
const getBlocks = (commaSeparatedMentions: string) => {
const getBlocks = (mentions: IMessage['mentions'], messageId: string) => {
const strigifiedMentions = JSON.stringify(mentions);
return {
addUsersBlock: {
type: 'button',
appId: APP_ID,
blockId: 'actionBlock',
value: commaSeparatedMentions,
blockId: messageId,
value: strigifiedMentions,
actionId: 'add-users',
text: {
type: 'plain_text',
Expand All @@ -30,7 +32,8 @@ const getBlocks = (commaSeparatedMentions: string) => {
dismissBlock: {
type: 'button',
appId: APP_ID,
blockId: 'actionBlock',
blockId: messageId,
value: strigifiedMentions,
actionId: 'dismiss',
text: {
type: 'plain_text',
Expand All @@ -40,8 +43,8 @@ const getBlocks = (commaSeparatedMentions: string) => {
dmBlock: {
type: 'button',
appId: APP_ID,
value: commaSeparatedMentions,
blockId: 'actionBlock',
value: strigifiedMentions,
blockId: messageId,
actionId: 'share-message',
text: {
type: 'plain_text',
Expand Down Expand Up @@ -89,8 +92,8 @@ callbacks.add(
const canDMUsers = await hasPermissionAsync(message.u._id, 'create-d'); // TODO: Perhaps check if user has DM with mentioned user (might be too expensive)

const usernames = mentionsUsersNotInChannel.map(({ username }) => username);
const actionBlocks = getBlocks(usernames.join(','));

const actionBlocks = getBlocks(mentionsUsersNotInChannel, message._id);
console.log(actionBlocks);
const elements: ActionsBlock['elements'] = [
canAddUsers && actionBlocks.addUsersBlock,
(canAddUsers || canDMUsers) && actionBlocks.dismissBlock,
Expand All @@ -109,7 +112,7 @@ callbacks.add(
type: 'section',
text: {
type: 'mrkdwn',
text: i18n.t(messageLabel, { mentions: `@${usernames.join(', @')}` }), // TODO: i18n
text: i18n.t(messageLabel, { mentions: `@${usernames.join(', @')}` }),
},
} as const,
Boolean(elements.length) &&
Expand Down
70 changes: 49 additions & 21 deletions apps/meteor/server/modules/core-apps/mention.module.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,72 @@
import { api } from '@rocket.chat/core-services';
import type { IUiKitCoreApp } from '@rocket.chat/core-services';
import type { IMessage } from '@rocket.chat/core-typings';

// import { VideoConf } from '@rocket.chat/core-services';
import { addUsersToRoomMethod } from '../../../app/lib/server/methods/addUsersToRoom';
// import { i18n } from '../../lib/i18n';
import { i18n } from '../../lib/i18n';

const retrieveMentionsFromPayload = (stringifiedMentions: string): Exclude<IMessage['mentions'], undefined> => {
try {
const mentions = JSON.parse(stringifiedMentions);
console.log('mentions', mentions);
if (!Array.isArray(mentions) || !mentions.length || !('username' in mentions[0])) {
throw new Error('Invalid payload');
}
return mentions;
} catch (error) {
throw new Error('Invalid payload');
}
};

export class MentionModule implements IUiKitCoreApp {
appId = 'mention-core';

async blockAction(payload: any): Promise<any> {
const {
// triggerId,
actionId,
payload: { value: commaSeparatedUsernames },
// user: { _id: userId },
payload: { value: stringifiedMentions },
} = payload;

console.log('payload', payload);

const mentions = retrieveMentionsFromPayload(stringifiedMentions);

const usernames = mentions.map(({ username }) => username);

if (actionId === 'dismiss') {
// TODO: Remove actions from ephemeral message
// TODO: Update message after interaction.
// You mentioned Rachel Berry, but they’re not in this room. (if mentioned user is added to room remove actions)
console.log('ignore');
// do nothing button
void api.broadcast('notify.ephemeralMessage', payload.user._id, payload.room, {
msg: i18n.t('You_mentioned___mentions__but_theyre_not_in_this_room', {
mentions: `@${usernames.join(', @')}`,
}),
_id: payload.message,
mentions,
});
return;
}

const usernames = commaSeparatedUsernames.split(',');
if (actionId === 'add-users') {
// TODO: Remove actions from ephemeral message
console.log('add-users');
void addUsersToRoomMethod(payload.user._id, { rid: payload.room, users: usernames }, payload.user);
// add users to channel
void addUsersToRoomMethod(payload.user._id, { rid: payload.room, users: usernames as string[] }, payload.user);
void api.broadcast('notify.ephemeralMessage', payload.user._id, payload.room, {
msg: i18n.t('You_mentioned___mentions__but_theyre_not_in_this_room', {
mentions: `@${usernames.join(', @')}`,
}),
_id: payload.message,
mentions,
});
return;
}

if (actionId === 'share-message') {
// TODO: Remove actions from ephemeral message
// TODO: update ephemeral message to have the following key
// You_mentioned___mentions__but_theyre_not_in_this_room_You_let_them_know_via_dm
console.log('share-message');
// let them know button
// const messagePayload =
// mentions.forEach(

// );
void api.broadcast('notify.ephemeralMessage', payload.user._id, payload.room, {
msg: i18n.t('You_mentioned___mentions__but_theyre_not_in_this_room_You_let_them_know_via_dm', {
mentions: `@${usernames.join(', @')}`,
}),
_id: payload.message,
mentions,
});
}
}
}

0 comments on commit 06a3ebb

Please sign in to comment.