Skip to content

Commit

Permalink
[FIX] Broadcast/ Read only issues (#10835)
Browse files Browse the repository at this point in the history
[FIX] Broadcast channels were showing reply button for deleted messages and generating wrong reply links some times
  • Loading branch information
ggazzo authored and rodrigok committed May 21, 2018
1 parent 202bb31 commit 3ffd9e6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
33 changes: 25 additions & 8 deletions packages/rocketchat-lib/client/MessageAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
import _ from 'underscore';
import moment from 'moment';
import toastr from 'toastr';
const call = (method, ...args) => new Promise((resolve, reject) => {
Meteor.call(method, ...args, function(err, data) {
if (err) {
return reject(err);
}
resolve(data);
});
});

const success = function success(fn) {
return function(error, result) {
Expand Down Expand Up @@ -93,14 +101,23 @@ RocketChat.MessageAction = new class {
return this.buttons.set({});
}

getPermaLink(msgId) {
const roomData = ChatSubscription.findOne({
rid: Session.get('openedRoom')
async getPermaLink(msgId) {
if (!msgId) {
throw new Error('invalid-parameter');
}

const msg = RocketChat.models.Messages.findOne(msgId) || await call('getSingleMessage', msgId);
if (!msg) {
throw new Error('message-not-found');
}
const roomData = RocketChat.models.Rooms.findOne({
_id: msg.rid
});
let routePath = document.location.pathname;
if (roomData) {
routePath = RocketChat.roomTypes.getRouteLink(roomData.t, roomData);

if (!roomData) {
throw new Error('room-not-found');
}
const routePath = RocketChat.roomTypes.getRouteLink(roomData.t, roomData);
return `${ Meteor.absoluteUrl().replace(/\/$/, '') + routePath }?msg=${ msgId }`;
}
};
Expand Down Expand Up @@ -220,9 +237,9 @@ Meteor.startup(function() {
label: 'Permalink',
classes: 'clipboard',
context: ['message', 'message-mobile'],
action(event) {
async action(event) {
const message = this._arguments[1];
const permalink = RocketChat.MessageAction.getPermaLink(message._id);
const permalink = await RocketChat.MessageAction.getPermaLink(message._id);
if (Meteor.isCordova) {
cordova.plugins.clipboard.copy(permalink);
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-message-pin/client/actionButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ Meteor.startup(function() {
label: 'Permalink',
classes: 'clipboard',
context: ['pinned'],
action(event) {
async action(event) {
const message = this._arguments[1];
$(event.currentTarget).attr('data-clipboard-text', RocketChat.MessageAction.getPermaLink(message._id));
$(event.currentTarget).attr('data-clipboard-text', await RocketChat.MessageAction.getPermaLink(message._id));
toastr.success(TAPi18n.__('Copied'));
},
condition(message) {
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-message-star/client/actionButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ Meteor.startup(function() {
label: 'Permalink',
classes: 'clipboard',
context: ['starred'],
action(event) {
async action(event) {
const message = this._arguments[1];
$(event.currentTarget).attr('data-clipboard-text', RocketChat.MessageAction.getPermaLink(message._id));
$(event.currentTarget).attr('data-clipboard-text', await RocketChat.MessageAction.getPermaLink(message._id));
toastr.success(TAPi18n.__('Copied'));
},
condition(message) {
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-message/client/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Template.message.helpers({
},
broadcast() {
const instance = Template.instance();
return this.u._id !== Meteor.userId() && instance.room && instance.room.broadcast;
return !this.t && this.u._id !== Meteor.userId() && instance.room && instance.room.broadcast;
},
isIgnored() {
return this.ignored;
Expand Down
20 changes: 14 additions & 6 deletions packages/rocketchat-ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import s from 'underscore.string';
import moment from 'moment';
import toastr from 'toastr';

const reply = id => id && `[ ](${ RocketChat.MessageAction.getPermaLink(id) }) `;

this.ChatMessages = class ChatMessages {
init(node) {
this.editing = {};
Expand Down Expand Up @@ -179,7 +177,7 @@ this.ChatMessages = class ChatMessages {
* * @param {Element} input DOM element
* * @param {function?} done callback
*/
send(rid, input, done = function() {}) {
async send(rid, input, done = function() {}) {
if (s.trim(input.value) !== '') {
readMessage.enable();
readMessage.readNow();
Expand All @@ -190,8 +188,7 @@ this.ChatMessages = class ChatMessages {
const mentionUser = $(input).data('mention-user') || false;

if (reply !== undefined) {
const url = RocketChat.MessageAction.getPermaLink(reply._id);
msg = `[ ](${ url }) `;
msg = `[ ](${ await RocketChat.MessageAction.getPermaLink(reply._id) }) `;
const roomInfo = RocketChat.models.Rooms.findOne(reply.rid, { fields: { t: 1 } });
if (roomInfo.t !== 'd' && reply.u.username !== Meteor.user().username && mentionUser) {
msg += `@${ reply.u.username } `;
Expand Down Expand Up @@ -404,10 +401,21 @@ this.ChatMessages = class ChatMessages {
}

restoreText(rid) {
const text = reply(FlowRouter.getQueryParam('reply')) || localStorage.getItem(`messagebox_${ rid }`);
const text = localStorage.getItem(`messagebox_${ rid }`);
if (typeof text === 'string' && this.input) {
this.input.value = text;
}
const msgId = FlowRouter.getQueryParam('reply');
if (!msgId) {
return;
}
const message = RocketChat.models.Messages.findOne(msgId);
if (message) {
return this.$input.data('reply', message).trigger('dataChange');
}
Meteor.call('getSingleMessage', msgId, (err, msg) => {
return !err && this.$input.data('reply', msg).trigger('dataChange');
});
}

keyup(rid, event) {
Expand Down

0 comments on commit 3ffd9e6

Please sign in to comment.