Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into uikit
Browse files Browse the repository at this point in the history
* 'develop' of github.com:RocketChat/Rocket.Chat:
  Revert message properties validation (#16395)
  [FIX] Rooms not being marked as read sometimes (#16397)
  Update apps engine to 1.12.0-beta.2496 (#16398)
  • Loading branch information
ggazzo committed Feb 1, 2020
2 parents c9a156d + c170dde commit 242f820
Show file tree
Hide file tree
Showing 11 changed files with 910 additions and 155 deletions.
2 changes: 1 addition & 1 deletion app/apps/server/bridges/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AppMessageBridge {

const convertedMessage = this.orch.getConverters().get('messages').convertAppMessage(message);

const sentMessage = executeSendMessage(convertedMessage.u._id, convertedMessage, true);
const sentMessage = executeSendMessage(convertedMessage.u._id, convertedMessage);

return sentMessage._id;
}
Expand Down
30 changes: 2 additions & 28 deletions app/lib/server/functions/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Apps } from '../../../apps/server';
import { Markdown } from '../../../markdown/server';
import { isURL, isRelativeURL } from '../../../utils/lib/isURL';
import { FileUpload } from '../../../file-upload/server';
import { Users } from '../../../models/server';

/**
* IMPORTANT
Expand Down Expand Up @@ -144,42 +143,17 @@ const validateMessage = (message) => {
}
};

const validateUserIdentity = (message, _id) => {
if (!message.alias && !message.avatar) {
return;
}
const forbiddenPropsToChangeWhenUserIsNotABot = ['avatar'];
const user = Users.findOneById(_id, { fields: { roles: 1, name: 1 } });
/**
* If the query returns no user, the message has likely
* been sent by a Livechat Visitor, so we don't need to
* validate whether the sender is a bot.
*/
if (!user) {
return;
}
const userIsNotABot = !user.roles.includes('bot');
const messageContainsAnyForbiddenProp = Object.keys(message).some((key) => forbiddenPropsToChangeWhenUserIsNotABot.includes(key));
if (userIsNotABot && (messageContainsAnyForbiddenProp || (typeof message.alias !== 'undefined' && message.alias !== user.name))) {
throw new Error('You are not authorized to change message properties');
}
};

export const sendMessage = function(user, message, room, upsert = false, trustedSender = false) {
export const sendMessage = function(user, message, room, upsert = false) {
if (!user || !message || !room._id) {
return false;
}
const { _id, username, name } = user;

if (!trustedSender) {
validateUserIdentity(message, _id);
}

validateMessage(message);

if (!message.ts) {
message.ts = new Date();
}
const { _id, username, name } = user;
message.u = {
_id,
username,
Expand Down
4 changes: 2 additions & 2 deletions app/lib/server/methods/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { RateLimiter } from '../lib';
import { canSendMessage } from '../../../authorization/server';
import { SystemLogger } from '../../../logger/server';

export function executeSendMessage(uid, message, trustedSender = false) {
export function executeSendMessage(uid, message) {
if (message.tmid && !settings.get('Threads_enabled')) {
throw new Meteor.Error('error-not-allowed', 'not-allowed', {
method: 'sendMessage',
Expand Down Expand Up @@ -73,7 +73,7 @@ export function executeSendMessage(uid, message, trustedSender = false) {
}

metrics.messagesSent.inc(); // TODO This line needs to be moved to it's proper place. See the comments on: https://github.com/RocketChat/Rocket.Chat/pull/5736
return sendMessage(user, message, room, false, trustedSender);
return sendMessage(user, message, room, false);
} catch (error) {
if (error === 'error-not-allowed') {
throw new Meteor.Error('error-not-allowed');
Expand Down
4 changes: 2 additions & 2 deletions app/ui-message/client/blocks/MessageBlock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef, useEffect, useCallback, useMemo } from 'react';
import { UiKitMessage as uiKitMessage, kitContext, UiKitModal as uiKitModal, messageParser, modalParser } from '@rocket.chat/fuselage-ui-kit';
import { UiKitMessage as uiKitMessage, kitContext, UiKitModal as uiKitModal, messageParser, modalParser, UiKitComponent } from '@rocket.chat/fuselage-ui-kit';
import { uiKitText } from '@rocket.chat/ui-kit';
import { Modal, AnimatedVisibility, ButtonGroup, Button, Box } from '@rocket.chat/fuselage';
import { useUniqueId } from '@rocket.chat/fuselage-hooks';
Expand Down Expand Up @@ -130,7 +130,7 @@ export const modalBlockWithContext = ({
action='#'
onSubmit={onSubmit}
>
{uiKitModal(view.blocks)}
<UiKitComponent render={uiKitModal} blocks={view.blocks} />
</Box>
</Modal.Content>
<Modal.Footer>
Expand Down
37 changes: 13 additions & 24 deletions app/ui-utils/client/lib/readMessages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import _ from 'underscore';
import EventEmitter from 'wolfy87-eventemitter';

import { RoomHistoryManager } from './RoomHistoryManager';
Expand All @@ -23,32 +22,15 @@ export const readMessage = new class extends EventEmitter {
constructor() {
super();
this.debug = false;
this.read = _.debounce((force) => this.readNow(force), 2000);
this.enable();
}

readNow(force) {
this.log('--------------');
this.log('readMessage -> readNow init process force:', force);

if ((force !== true) && (this.enabled === false)) {
read(rid = Session.get('openedRoom')) {
if (!this.enabled) {
this.log('readMessage -> readNow canceled by enabled: false');
return;
}

const rid = Session.get('openedRoom');
if (rid == null) {
this.log('readMessage -> readNow canceled, no rid informed');
return;
}

if (force === true) {
this.log('readMessage -> readNow via force rid:', rid);
return Meteor.call('readMessages', rid, () => {
RoomHistoryManager.getRoom(rid).unreadNotLoaded.set(0);
return this.emit(rid);
});
}

const subscription = ChatSubscription.findOne({ rid });
if (subscription == null) {
Expand Down Expand Up @@ -81,8 +63,15 @@ export const readMessage = new class extends EventEmitter {
return;
}

this.log('readMessage -> readNow rid:', rid);
Meteor.call('readMessages', rid, () => {
return this.readNow(rid);
}

readNow(rid = Session.get('openedRoom')) {
if (rid == null) {
this.log('readMessage -> readNow canceled, no rid informed');
return;
}
return Meteor.call('readMessages', rid, () => {
RoomHistoryManager.getRoom(rid).unreadNotLoaded.set(0);
return this.emit(rid);
});
Expand Down Expand Up @@ -177,15 +166,15 @@ Meteor.startup(function() {
.on('blur', () => readMessage.disable())
.on('focus', () => {
readMessage.enable();
readMessage.readNow();
readMessage.read();
})
.on('touchend', () => {
readMessage.enable();
})
.on('keyup', (e) => {
const key = e.which;
if (key === 27) { // ESCAPE KEY
readMessage.readNow(true);
readMessage.readNow();
}
});
});
2 changes: 1 addition & 1 deletion app/ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class ChatMessages {
}

if (msg) {
readMessage.readNow(true);
readMessage.readNow(rid);
$('.message.first-unread').removeClass('first-unread');

const message = await promises.run('onClientBeforeSendMessage', {
Expand Down
12 changes: 8 additions & 4 deletions app/ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,8 @@ Template.room.events({
Session.set(`uploading-cancel-${ this.id }`, true);
},

'click .unread-bar > button.mark-read'() {
readMessage.readNow(true);
'click .unread-bar > button.mark-read'(e, t) {
readMessage.readNow(t.data._id);
},

'click .unread-bar > button.jump-to'(e, t) {
Expand Down Expand Up @@ -1275,9 +1275,13 @@ Template.room.onRendered(function() {
});
}, 300);

const read = _.debounce(function() {
readMessage.read(rid);
}, 500);

this.autorun(() => {
const subscription = Subscriptions.findOne({ rid }, { fields: { alert: 1, unread: 1 } });
readMessage.read();
read();
return subscription && (subscription.alert || subscription.unread) && readMessage.refreshUnreadMark(rid);
});

Expand Down Expand Up @@ -1306,7 +1310,7 @@ Template.room.onRendered(function() {
Rooms.findOne(rid);
const count = this.state.get('count');
if (count === 0) {
return readMessage.read();
return read();
}
readMessage.refreshUnreadMark(rid);
});
Expand Down
3 changes: 3 additions & 0 deletions client/startup/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Tracker } from 'meteor/tracker';
import { Session } from 'meteor/session';
import { TimeSync } from 'meteor/mizzao:timesync';
import { UserPresence } from 'meteor/konecty:user-presence';
import { Accounts } from 'meteor/accounts-base';
import toastr from 'toastr';

import hljs from '../../app/markdown/lib/hljs';
Expand All @@ -21,6 +22,8 @@ if (window.DISABLE_ANIMATION) {
}

Meteor.startup(function() {
Accounts.onLogout(() => Session.set('openedRoom', null));

TimeSync.loggingEnabled = false;

Session.setDefault('AvatarRandom', 0);
Expand Down
Loading

0 comments on commit 242f820

Please sign in to comment.