Skip to content

Commit 0d1031c

Browse files
committed
Allow owners/moderators to react on read only messages
1 parent 79de34e commit 0d1031c

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

app/reactions/client/init.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ Template.room.events({
1818
const user = Meteor.user();
1919
const room = Rooms.findOne({ _id: rid });
2020

21-
if (room.ro && !room.reactWhenReadOnly) {
22-
if (!Array.isArray(room.unmuted) || room.unmuted.indexOf(user.username) === -1) {
23-
return false;
24-
}
25-
}
26-
27-
if (Array.isArray(room.muted) && room.muted.indexOf(user.username) !== -1) {
21+
if (roomTypes.readOnly(room._id, user._id)) {
2822
return false;
2923
}
3024

@@ -70,26 +64,19 @@ Meteor.startup(function() {
7064
EmojiPicker.open(event.currentTarget, (emoji) => Meteor.call('setReaction', `:${ emoji }:`, msg._id));
7165
},
7266
condition({ msg: message, u: user, room, subscription }) {
73-
7467
if (!room) {
7568
return false;
7669
}
7770

78-
if (room.ro && !room.reactWhenReadOnly && roomTypes.readOnly(room._id, user._id)) {
79-
if (!Array.isArray(room.unmuted) || room.unmuted.indexOf(user.username) === -1) {
80-
return false;
81-
}
82-
}
83-
84-
if (Array.isArray(room.muted) && room.muted.indexOf(user.username) !== -1) {
71+
if (!subscription) {
8572
return false;
8673
}
8774

88-
if (!subscription) {
75+
if (message.private) {
8976
return false;
9077
}
9178

92-
if (message.private) {
79+
if (roomTypes.readOnly(room._id, user._id)) {
9380
return false;
9481
}
9582

app/reactions/client/methods/setReaction.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import _ from 'underscore';
44
import { Messages, Rooms, Subscriptions } from '../../../models';
55
import { callbacks } from '../../../callbacks';
66
import { emoji } from '../../../emoji';
7+
import { roomTypes } from '../../../utils/client';
78

89
Meteor.methods({
910
setReaction(reaction, messageId) {
@@ -16,25 +17,19 @@ Meteor.methods({
1617
const message = Messages.findOne({ _id: messageId });
1718
const room = Rooms.findOne({ _id: message.rid });
1819

19-
if (room.ro && !room.reactWhenReadOnly) {
20-
if (!Array.isArray(room.unmuted) || room.unmuted.indexOf(user.username) === -1) {
21-
return false;
22-
}
23-
}
24-
25-
if (Array.isArray(room.muted) && room.muted.indexOf(user.username) !== -1) {
20+
if (message.private) {
2621
return false;
2722
}
2823

29-
if (!Subscriptions.findOne({ rid: message.rid })) {
24+
if (!emoji.list[reaction]) {
3025
return false;
3126
}
3227

33-
if (message.private) {
28+
if (roomTypes.readOnly(room._id, user._id)) {
3429
return false;
3530
}
3631

37-
if (!emoji.list[reaction]) {
32+
if (!Subscriptions.findOne({ rid: message.rid })) {
3833
return false;
3934
}
4035

app/reactions/server/setReaction.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Notifications } from '../../notifications';
88
import { callbacks } from '../../callbacks';
99
import { emoji } from '../../emoji';
1010
import { isTheLastMessage, msgStream } from '../../lib';
11+
import { hasPermission } from '../../authorization/server/functions/hasPermission';
1112

1213
const removeUserReaction = (message, reaction, username) => {
1314
message.reactions[reaction].usernames.splice(message.reactions[reaction].usernames.indexOf(username), 1);
@@ -17,16 +18,17 @@ const removeUserReaction = (message, reaction, username) => {
1718
return message;
1819
};
1920

20-
export function setReaction(room, user, message, reaction, shouldReact) {
21+
async function setReaction(room, user, message, reaction, shouldReact) {
2122
reaction = `:${ reaction.replace(/:/g, '') }:`;
2223

2324
if (!emoji.list[reaction] && EmojiCustom.findByNameOrAlias(reaction).count() === 0) {
2425
throw new Meteor.Error('error-not-allowed', 'Invalid emoji provided.', { method: 'setReaction' });
2526
}
2627

27-
if (room.ro && !room.reactWhenReadOnly) {
28-
if (!Array.isArray(room.unmuted) || room.unmuted.indexOf(user.username) === -1) {
29-
return false;
28+
if (room.ro === true && (!room.reactWhenReadOnly && !hasPermission(user._id, 'post-readonly', room._id))) {
29+
// Unless the user was manually unmuted
30+
if (!(room.unmuted || []).includes(user.username)) {
31+
throw new Error('You can\'t send messages because the room is readonly.');
3032
}
3133
}
3234

@@ -38,8 +40,6 @@ export function setReaction(room, user, message, reaction, shouldReact) {
3840
msg: TAPi18n.__('You_have_been_muted', {}, user.language),
3941
});
4042
return false;
41-
} if (!Subscriptions.findOne({ rid: message.rid })) {
42-
return false;
4343
}
4444

4545
const userAlreadyReacted = Boolean(message.reactions) && Boolean(message.reactions[reaction]) && message.reactions[reaction].usernames.indexOf(user.username) !== -1;
@@ -92,18 +92,18 @@ Meteor.methods({
9292
setReaction(reaction, messageId, shouldReact) {
9393
const user = Meteor.user();
9494

95-
const message = Messages.findOneById(messageId);
96-
97-
const room = Meteor.call('canAccessRoom', message.rid, Meteor.userId());
98-
9995
if (!user) {
10096
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setReaction' });
10197
}
10298

99+
const message = Messages.findOneById(messageId);
100+
103101
if (!message) {
104102
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setReaction' });
105103
}
106104

105+
const room = Meteor.call('canAccessRoom', message.rid, Meteor.userId());
106+
107107
if (!room) {
108108
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setReaction' });
109109
}

0 commit comments

Comments
 (0)