Skip to content

Commit

Permalink
share-message
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh committed Sep 28, 2023
1 parent 859461c commit eabac32
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 2 additions & 3 deletions apps/meteor/app/lib/server/startup/mentionUserNotInChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ 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';
// import { Meteor } from 'meteor/meteor';
import moment from 'moment';
// import _ from 'underscore';

import { callbacks } from '../../../../lib/callbacks';
import { isTruthy } from '../../../../lib/isTruthy';
Expand Down Expand Up @@ -93,7 +91,6 @@ callbacks.add(

const usernames = mentionsUsersNotInChannel.map(({ username }) => username);
const actionBlocks = getBlocks(mentionsUsersNotInChannel, message._id);
console.log(actionBlocks);
const elements: ActionsBlock['elements'] = [
canAddUsers && actionBlocks.addUsersBlock,
(canAddUsers || canDMUsers) && actionBlocks.dismissBlock,
Expand All @@ -104,6 +101,8 @@ callbacks.add(
? 'You_mentioned___mentions__but_theyre_not_in_this_room'
: 'You_mentioned___mentions__but_theyre_not_in_this_room_You_can_ask_a_room_admin_to_add_them';

// TODO: Mentions style
// TODO: Use real name setting
void api.broadcast('notify.ephemeralMessage', message.u._id, message.rid, {
msg: '',
mentions: mentionsUsersNotInChannel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const ForwardMessageModal = ({ onClose, permalink, message }: ForwardMessageProp
const optionalMessage = '';
const curMsg = await prependReplies(optionalMessage, [message]);

// TODO: chat.postMessage accepts an array of rooms
return Promise.all(
rooms.map(async (roomId) => {
const sendPayload = {
Expand Down
34 changes: 28 additions & 6 deletions apps/meteor/server/modules/core-apps/mention.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { api } from '@rocket.chat/core-services';
import type { IUiKitCoreApp } from '@rocket.chat/core-services';
import type { IMessage } from '@rocket.chat/core-typings';
import { Subscriptions } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { processWebhookMessage } from '../../../app/lib/server/functions/processWebhookMessage';
import { addUsersToRoomMethod } from '../../../app/lib/server/methods/addUsersToRoom';
import { i18n } from '../../lib/i18n';
import { roomCoordinator } from '../../lib/rooms/roomCoordinator';

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');
}
Expand All @@ -24,9 +27,8 @@ export class MentionModule implements IUiKitCoreApp {
async blockAction(payload: any): Promise<any> {
const {
actionId,
payload: { value: stringifiedMentions },
payload: { value: stringifiedMentions, blockId: referenceMessageId },
} = payload;
console.log('payload', payload);

const mentions = retrieveMentionsFromPayload(stringifiedMentions);

Expand Down Expand Up @@ -56,10 +58,30 @@ export class MentionModule implements IUiKitCoreApp {
}

if (actionId === 'share-message') {
// const messagePayload =
// mentions.forEach(
const sub = await Subscriptions.findOneByRoomIdAndUserId(payload.room, payload.user._id);
// this should exist since the event is fired from withing the room (e.g the user sent a message)
if (!sub) {
throw new Error('Failed to retrieve room information');
}

const roomPath = roomCoordinator.getRouteLink(sub.t, { rid: sub.rid });
if (!roomPath) {
throw new Error('Failed to retrieve path to room');
}

const link = new URL(Meteor.absoluteUrl(roomPath));
link.searchParams.set('msg', referenceMessageId);
const text = `[ ](${link.toString()})`;

// forwards message to all DMs
await processWebhookMessage(
{
roomId: mentions.map(({ _id }) => _id),
text,
},
payload.user,
);

// );
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(', @')}`,
Expand Down

0 comments on commit eabac32

Please sign in to comment.