Skip to content

Commit

Permalink
[NEW] View pinned message's attachment (#10214)
Browse files Browse the repository at this point in the history
* displays pinned file's attachments

* handles pin for replies and quotes

* fix review
  • Loading branch information
c0dzilla authored and rodrigok committed May 18, 2018
1 parent d2001ca commit 89ff78a
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions packages/rocketchat-message-pin/server/pinMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
const recursiveRemove = (msg, deep = 1) => {
if (!msg) {
return;
}

if (deep > RocketChat.settings.get('Message_QuoteChainLimit')) {
delete msg.attachments;
return msg;
}

msg.attachments = Array.isArray(msg.attachments) ? msg.attachments.map(
nestedMsg => recursiveRemove(nestedMsg, deep + 1)
) : null;

return msg;
};

const shouldAdd = (attachments, attachment) => !attachments.some(({message_link}) => message_link && message_link === attachment.message_link);

Meteor.methods({
pinMessage(message, pinnedAt) {
if (!Meteor.userId()) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'pinMessage'
});
Expand Down Expand Up @@ -31,27 +51,48 @@ Meteor.methods({
RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id);
}

const me = RocketChat.models.Users.findOneById(Meteor.userId());
const me = RocketChat.models.Users.findOneById(userId);

originalMessage.pinned = true;
originalMessage.pinnedAt = pinnedAt || Date.now;
originalMessage.pinnedBy = {
_id: Meteor.userId(),
_id: userId,
username: me.username
};

originalMessage = RocketChat.callbacks.run('beforeSaveMessage', originalMessage);

RocketChat.models.Messages.setPinnedByIdAndUserId(originalMessage._id, originalMessage.pinnedBy, originalMessage.pinned);

return RocketChat.models.Messages.createWithTypeRoomIdMessageAndUser('message_pinned', originalMessage.rid, '', me, {
attachments: [
{
'text': originalMessage.msg,
'author_name': originalMessage.u.username,
'author_icon': getAvatarUrlFromUsername(originalMessage.u.username),
'ts': originalMessage.ts
const attachments = [];

if (Array.isArray(originalMessage.attachments)) {
originalMessage.attachments.forEach(attachment => {
if (!attachment.message_link || shouldAdd(attachments, attachment)) {
attachments.push(attachment);
}
]
});
});
}

return RocketChat.models.Messages.createWithTypeRoomIdMessageAndUser(
'message_pinned',
originalMessage.rid,
'',
me,
{
attachments: [
{
text: originalMessage.msg,
author_name: originalMessage.u.username,
author_icon: getAvatarUrlFromUsername(
originalMessage.u.username
),
ts: originalMessage.ts,
attachments: recursiveRemove(attachments)
}
]
}
);
},
unpinMessage(message) {
if (!Meteor.userId()) {
Expand Down

0 comments on commit 89ff78a

Please sign in to comment.