Skip to content

Commit

Permalink
handles pin for replies and quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dzilla committed Apr 6, 2018
1 parent f41b7a8 commit 3125102
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/rocketchat-message-pin/server/pinMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Meteor.methods({
'ts': originalMessage.ts
}];

const recursiveRemove = (msg, deep = 1) => {
const recursiveRemove = (attachmentMsg, deep = 1) => {
const msg = attachmentMsg;
if (msg) {
if ('attachments' in msg && msg.attachments !== null && deep < RocketChat.settings.get('Message_QuoteChainLimit')) {
msg.attachments.map((nestedMsg) => recursiveRemove(nestedMsg, deep + 1));
Expand All @@ -60,9 +61,28 @@ Meteor.methods({
return msg;
};

const shouldAdd = (attachment) => {
let toAdd = true;
for (let i = 0; i < attachments.length; i++ && toAdd) {
if (attachments[i].message_link) {
if (attachments[i].message_link === attachment.message_link) {
toAdd = false;
}
}
}
return toAdd;
};

if (Array.isArray(originalMessage.attachments)) {
attachments.push(...originalMessage.attachments);
attachments[attachments.length - 1] = recursiveRemove(attachments[attachments.length - 1]);
for (const attachment of originalMessage.attachments) {
if (attachment.message_link) {
if (shouldAdd(attachment)) {
attachments.push(recursiveRemove(attachment));
}
} else {
attachments.push(recursiveRemove(attachment));
}
}
}

return RocketChat.models.Messages.createWithTypeRoomIdMessageAndUser('message_pinned', originalMessage.rid, '', me, {attachments});
Expand Down

0 comments on commit 3125102

Please sign in to comment.