Skip to content

Commit

Permalink
Merge branch 'develop' into fix/apps-contextual-bar-close-danger
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Jan 10, 2023
2 parents 7e436ba + 5774aca commit 6a16817
Show file tree
Hide file tree
Showing 35 changed files with 543 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const subscriptionOptions = {
async function validateRoomMessagePermissionsAsync(
room: IRoom | null,
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] },
extraData: Record<string, any>,
extraData?: Record<string, any>,
): Promise<void> {
if (!room) {
throw new Error('error-invalid-room');
Expand Down Expand Up @@ -48,7 +48,7 @@ async function validateRoomMessagePermissionsAsync(
export async function canSendMessageAsync(
rid: IRoom['_id'],
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] },
extraData: Record<string, any>,
extraData?: Record<string, any>,
): Promise<IRoom> {
const room = await Rooms.findOneById(rid);
if (!room) {
Expand All @@ -62,14 +62,14 @@ export async function canSendMessageAsync(
export function canSendMessage(
rid: IRoom['_id'],
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] },
extraData: Record<string, any>,
extraData?: Record<string, any>,
): IRoom {
return Promise.await(canSendMessageAsync(rid, { uid, username, type }, extraData));
}
export function validateRoomMessagePermissions(
room: IRoom,
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] },
extraData: Record<string, any>,
extraData?: Record<string, any>,
): void {
return Promise.await(validateRoomMessagePermissionsAsync(room, { uid, username, type }, extraData));
}
4 changes: 2 additions & 2 deletions apps/meteor/app/autotranslate/server/autotranslate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class TranslationProviderRegistry {
return TranslationProviderRegistry.enabled ? TranslationProviderRegistry.getActiveProvider()?.getSupportedLanguages(target) : undefined;
}

static translateMessage(message: IMessage, room: IRoom, targetLanguage: string): IMessage | undefined {
static translateMessage(message: IMessage, room: IRoom, targetLanguage?: string): IMessage | undefined {
return TranslationProviderRegistry.enabled
? TranslationProviderRegistry.getActiveProvider()?.translateMessage(message, room, targetLanguage)
: undefined;
Expand Down Expand Up @@ -281,7 +281,7 @@ export abstract class AutoTranslate {
* @param {object} targetLanguage
* @returns {object} unmodified message object.
*/
translateMessage(message: IMessage, room: IRoom, targetLanguage: string): IMessage {
translateMessage(message: IMessage, room: IRoom, targetLanguage?: string): IMessage {
let targetLanguages: string[];
if (targetLanguage) {
targetLanguages = [targetLanguage];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { Match } from 'meteor/check';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import type { IMessage, IRoom, IUser, MessageAttachmentDefault } from '@rocket.chat/core-typings';

import { hasAtLeastOnePermission, canSendMessage } from '../../../authorization/server';
import { Messages, Rooms } from '../../../models/server';
Expand All @@ -10,77 +10,93 @@ import { settings } from '../../../settings/server';
import { callbacks } from '../../../../lib/callbacks';
import { roomCoordinator } from '../../../../server/lib/rooms/roomCoordinator';

const getParentRoom = (rid) => {
const getParentRoom = (rid: IRoom['_id']) => {
const room = Rooms.findOne(rid);
return room && (room.prid ? Rooms.findOne(room.prid, { fields: { _id: 1 } }) : room);
};

const createDiscussionMessage = (rid, user, drid, msg, message_embedded) => {
const createDiscussionMessage = (
rid: IRoom['_id'],
user: IUser,
drid: IRoom['_id'],
msg: IMessage['msg'],
messageEmbedded?: MessageAttachmentDefault,
): IMessage => {
const welcomeMessage = {
msg,
rid,
drid,
attachments: [message_embedded].filter((e) => e),
attachments: [messageEmbedded].filter((e) => e),
};
return Messages.createWithTypeRoomIdMessageAndUser('discussion-created', rid, '', user, welcomeMessage);
return Messages.createWithTypeRoomIdMessageAndUser('discussion-created', rid, '', user, welcomeMessage) as IMessage;
};

const mentionMessage = (rid, { _id, username, name }, message_embedded) => {
const mentionMessage = (
rid: IRoom['_id'],
{ _id, username, name }: Pick<IUser, '_id' | 'name' | 'username'>,
messageEmbedded?: MessageAttachmentDefault,
) => {
const welcomeMessage = {
rid,
u: { _id, username, name },
ts: new Date(),
_updatedAt: new Date(),
attachments: [message_embedded].filter((e) => e),
attachments: [messageEmbedded].filter((e) => e),
};

return Messages.insert(welcomeMessage);
};

const create = ({ prid, pmid, t_name, reply, users, user, encrypted }) => {
type CreateDiscussionProperties = {
prid: IRoom['_id'];
pmid?: IMessage['_id'];
t_name: string;
reply?: string;
users: Array<Exclude<IUser['username'], undefined>>;
user: IUser;
encrypted?: boolean;
};

const create = ({ prid, pmid, t_name: discussionName, reply, users, user, encrypted }: CreateDiscussionProperties) => {
// if you set both, prid and pmid, and the rooms dont match... should throw an error)
let message = false;
let message: undefined | IMessage;
if (pmid) {
message = Messages.findOne({ _id: pmid });
message = Messages.findOne({ _id: pmid }) as IMessage | undefined;
if (!message) {
throw new Meteor.Error('error-invalid-message', 'Invalid message', {
method: 'DiscussionCreation',
});
}
if (prid) {
if (prid !== getParentRoom(message.rid)._id) {
throw new Meteor.Error('error-invalid-arguments', { method: 'DiscussionCreation' });
throw new Meteor.Error('error-invalid-arguments', 'Root message room ID does not match parent room ID ', {
method: 'DiscussionCreation',
});
}
} else {
prid = message.rid;
}
}

if (!prid) {
throw new Meteor.Error('error-invalid-arguments', { method: 'DiscussionCreation' });
throw new Meteor.Error('error-invalid-arguments', 'Missing parent room ID', { method: 'DiscussionCreation' });
}

let p_room;
let parentRoom;
try {
p_room = canSendMessage(prid, { uid: user._id, username: user.username, type: user.type });
parentRoom = canSendMessage(prid, { uid: user._id, username: user.username, type: user.type });
} catch (error) {
throw new Meteor.Error(error.message);
throw new Meteor.Error((error as Error).message);
}

if (p_room.prid) {
if (parentRoom.prid) {
throw new Meteor.Error('error-nested-discussion', 'Cannot create nested discussions', {
method: 'DiscussionCreation',
});
}

if (!Match.Maybe(encrypted, Boolean)) {
throw new Meteor.Error('error-invalid-arguments', 'Invalid encryption state', {
method: 'DiscussionCreation',
});
}

if (typeof encrypted !== 'boolean') {
encrypted = p_room.encrypted;
encrypted = Boolean(parentRoom.encrypted);
}

if (encrypted && reply) {
Expand Down Expand Up @@ -111,42 +127,49 @@ const create = ({ prid, pmid, t_name, reply, users, user, encrypted }) => {
// auto invite the replied message owner
const invitedUsers = message ? [message.u.username, ...users] : users;

const type = roomCoordinator.getRoomDirectives(p_room.t)?.getDiscussionType();
const description = p_room.encrypted ? '' : message.msg;
const topic = p_room.name;
const type = roomCoordinator.getRoomDirectives(parentRoom.t)?.getDiscussionType(parentRoom);
const description = parentRoom.encrypted ? '' : message?.msg;
const topic = parentRoom.name;

if (!type) {
throw new Meteor.Error('error-invalid-type', 'Cannot define discussion room type', {
method: 'DiscussionCreation',
});
}

const discussion = createRoom(
type,
name,
user.username,
[...new Set(invitedUsers)],
user.username as string,
[...new Set(invitedUsers)].filter(Boolean),
false,
{
fname: t_name,
fname: discussionName,
description, // TODO discussions remove
topic, // TODO discussions remove
prid,
encrypted,
},
{
// overrides name validation to allow anything, because discussion's name is randomly generated
nameValidationRegex: /.*/,
nameValidationRegex: '.*',
creator: user._id,
},
);

let discussionMsg;
if (pmid) {
if (p_room.encrypted) {
if (message) {
if (parentRoom.encrypted) {
message.msg = TAPi18n.__('Encrypted_message');
}
mentionMessage(discussion._id, user, attachMessage(message, p_room));
mentionMessage(discussion._id, user, attachMessage(message, parentRoom));

discussionMsg = createDiscussionMessage(message.rid, user, discussion._id, t_name, attachMessage(message, p_room));
discussionMsg = createDiscussionMessage(message.rid, user, discussion._id, discussionName, attachMessage(message, parentRoom));
} else {
discussionMsg = createDiscussionMessage(prid, user, discussion._id, t_name);
discussionMsg = createDiscussionMessage(prid, user, discussion._id, discussionName);
}

callbacks.runAsync('afterSaveMessage', discussionMsg, p_room);
callbacks.runAsync('afterSaveMessage', discussionMsg, parentRoom);

if (reply) {
sendMessage(user, { msg: reply }, discussion);
Expand All @@ -165,7 +188,7 @@ Meteor.methods({
* @param {string[]} users - users to be added
* @param {boolean} encrypted - if the discussion's e2e encryption should be enabled.
*/
createDiscussion({ prid, pmid, t_name, reply, users, encrypted }) {
createDiscussion({ prid, pmid, t_name: discussionName, reply, users, encrypted }: CreateDiscussionProperties) {
if (!settings.get('Discussion_enabled')) {
throw new Meteor.Error('error-action-not-allowed', 'You are not allowed to create a discussion', { method: 'createDiscussion' });
}
Expand All @@ -181,6 +204,6 @@ Meteor.methods({
throw new Meteor.Error('error-action-not-allowed', 'You are not allowed to create a discussion', { method: 'createDiscussion' });
}

return create({ uid, prid, pmid, t_name, reply, users, user: Meteor.user(), encrypted });
return create({ prid, pmid, t_name: discussionName, reply, users, user: Meteor.user() as IUser, encrypted });
},
});
4 changes: 4 additions & 0 deletions apps/meteor/app/emoji/client/lib/EmojiPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export const EmojiPicker = {

return $('.emoji-picker').css(cssProperties);
},
/**
* @param {Element} source
* @param {(emoji: string) => void} callback
*/
async open(source, callback) {
if (!this.initiated) {
await this.init();
Expand Down
8 changes: 0 additions & 8 deletions apps/meteor/app/models/client/models/ChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ class ChatMessageCollection extends Mongo.Collection<IMessage & { ignored?: bool
super(null);
}

setReactions(messageId: IMessage['_id'], reactions: IMessage['reactions']) {
return this.update({ _id: messageId }, { $set: { reactions } });
}

unsetReactions(messageId: IMessage['_id']) {
return this.update({ _id: messageId }, { $unset: { reactions: 1 } });
}

findOneByRoomIdAndMessageId(rid: IRoom['_id'], messageId: IMessage['_id'], options?: Mongo.Options<IMessage>) {
const query = {
rid,
Expand Down
45 changes: 0 additions & 45 deletions apps/meteor/app/reactions/client/init.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,10 @@
import { Meteor } from 'meteor/meteor';
import { Blaze } from 'meteor/blaze';

import { Rooms, Subscriptions } from '../../models/client';
import { MessageAction } from '../../ui-utils';
import { messageArgs } from '../../../client/lib/utils/messageArgs';
import { EmojiPicker } from '../../emoji';
import { roomCoordinator } from '../../../client/lib/rooms/roomCoordinator';

export const EmojiEvents = {
'click .add-reaction'(event) {
event.preventDefault();
event.stopPropagation();
const data = Blaze.getData(event.currentTarget);
const {
msg: { rid, _id: mid, private: isPrivate },
} = messageArgs(data);
const user = Meteor.user();
const room = Rooms.findOne({ _id: rid });

if (!room) {
return false;
}

if (!Subscriptions.findOne({ rid })) {
return false;
}

if (isPrivate) {
return false;
}

if (roomCoordinator.readOnly(room._id, user) && !room.reactWhenReadOnly) {
return false;
}

EmojiPicker.open(event.currentTarget, (emoji) => {
Meteor.call('setReaction', `:${emoji}:`, mid);
});
},

'click .reactions > li:not(.add-reaction)'(event) {
event.preventDefault();

const data = Blaze.getData(event.currentTarget);
const {
msg: { _id: mid },
} = messageArgs(data);
Meteor.call('setReaction', $(event.currentTarget).attr('data-emoji'), mid);
},
};

Meteor.startup(function () {
MessageAction.addButton({
id: 'reaction-message',
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/app/reactions/client/methods/setReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ Meteor.methods({

if (_.isEmpty(message.reactions)) {
delete message.reactions;
Messages.unsetReactions(messageId);
Messages.update({ _id: messageId }, { $unset: { reactions: 1 } });
callbacks.run('unsetReaction', messageId, reaction);
} else {
Messages.setReactions(messageId, message.reactions);
Messages.update({ _id: messageId }, { $set: { reactions: message.reactions } });
callbacks.run('setReaction', messageId, reaction);
}
} else {
Expand All @@ -59,7 +59,7 @@ Meteor.methods({
}
message.reactions[reaction].usernames.push(user.username);

Messages.setReactions(messageId, message.reactions);
Messages.update({ _id: messageId }, { $set: { reactions: message.reactions } });
callbacks.run('setReaction', messageId, reaction);
}
},
Expand Down
8 changes: 4 additions & 4 deletions apps/meteor/app/ui-message/client/message.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
{{/each}}
{{/if}}
{{#if hasAttachments}}
{{> reactAttachments attachments=msg.attachments file=msg.file messageContext=messageContext chatContext=chatContext }}
{{> Attachments attachments=msg.attachments file=msg.file}}
{{/if}}


Expand Down Expand Up @@ -178,15 +178,15 @@
{{/unless}}

{{#if msg.drid}}
{{> DiscussionMetric count=msg.dcount drid=msg.drid lm=msg.dlm openDiscussion=actions.openDiscussion }}
{{> DiscussionMetrics count=msg.dcount drid=msg.drid lm=msg.dlm openDiscussion=actions.openDiscussion }}
{{/if}}

{{#if $and settings.showReplyButton msg.tcount}}
{{> ThreadMetric counter=msg.tcount following=following lm=msg.tlm rid=msg.rid mid=msg._id unread=unread mention=mention all=all openThread=actions.openThread }}
{{> ThreadMetrics counter=msg.tcount following=following lm=msg.tlm rid=msg.rid mid=msg._id unread=unread mention=mention all=all openThread=actions.openThread }}
{{/if}}

{{#if broadcast}}
{{> BroadCastMetric mid=msg._id username=msg.u.username replyBroadcast=actions.replyBroadcast }}
{{> BroadcastMetrics mid=msg._id username=msg.u.username replyBroadcast=actions.replyBroadcast }}
{{/if}}
{{/unless}}
</li>
Expand Down

0 comments on commit 6a16817

Please sign in to comment.