Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] View pinned message's attachment #10214

Merged
merged 6 commits into from
May 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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