From bda6e000c7bdde00ba3809c26a6235f5c5e86cd8 Mon Sep 17 00:00:00 2001 From: Martin Schoeler Date: Mon, 3 Apr 2017 16:06:57 -0300 Subject: [PATCH 1/4] Convert Message Pin Package to JS --- .../client/actionButton.coffee | 87 ----------------- .../client/actionButton.js | 97 +++++++++++++++++++ .../client/lib/PinnedMessage.coffee | 1 - .../client/lib/PinnedMessage.js | 1 + .../client/pinMessage.coffee | 30 ------ .../client/pinMessage.js | 38 ++++++++ .../client/tabBar.coffee | 13 --- .../rocketchat-message-pin/client/tabBar.js | 16 +++ .../client/views/pinnedMessages.coffee | 40 -------- .../client/views/pinnedMessages.js | 70 +++++++++++++ packages/rocketchat-message-pin/package.js | 19 ++-- .../server/pinMessage.coffee | 77 --------------- .../server/pinMessage.js | 86 ++++++++++++++++ .../server/publications/pinnedMessages.coffee | 23 ----- .../server/publications/pinnedMessages.js | 26 +++++ .../server/settings.coffee | 3 - .../rocketchat-message-pin/server/settings.js | 12 +++ .../server/startup/indexes.coffee | 3 - .../server/startup/indexes.js | 9 ++ 19 files changed, 364 insertions(+), 287 deletions(-) delete mode 100644 packages/rocketchat-message-pin/client/actionButton.coffee create mode 100644 packages/rocketchat-message-pin/client/actionButton.js delete mode 100644 packages/rocketchat-message-pin/client/lib/PinnedMessage.coffee create mode 100644 packages/rocketchat-message-pin/client/lib/PinnedMessage.js delete mode 100644 packages/rocketchat-message-pin/client/pinMessage.coffee create mode 100644 packages/rocketchat-message-pin/client/pinMessage.js delete mode 100644 packages/rocketchat-message-pin/client/tabBar.coffee create mode 100644 packages/rocketchat-message-pin/client/tabBar.js delete mode 100644 packages/rocketchat-message-pin/client/views/pinnedMessages.coffee create mode 100644 packages/rocketchat-message-pin/client/views/pinnedMessages.js delete mode 100644 packages/rocketchat-message-pin/server/pinMessage.coffee create mode 100644 packages/rocketchat-message-pin/server/pinMessage.js delete mode 100644 packages/rocketchat-message-pin/server/publications/pinnedMessages.coffee create mode 100644 packages/rocketchat-message-pin/server/publications/pinnedMessages.js delete mode 100644 packages/rocketchat-message-pin/server/settings.coffee create mode 100644 packages/rocketchat-message-pin/server/settings.js delete mode 100644 packages/rocketchat-message-pin/server/startup/indexes.coffee create mode 100644 packages/rocketchat-message-pin/server/startup/indexes.js diff --git a/packages/rocketchat-message-pin/client/actionButton.coffee b/packages/rocketchat-message-pin/client/actionButton.coffee deleted file mode 100644 index 4b5861f5bbc0..000000000000 --- a/packages/rocketchat-message-pin/client/actionButton.coffee +++ /dev/null @@ -1,87 +0,0 @@ -import toastr from 'toastr' -Meteor.startup -> - RocketChat.MessageAction.addButton - id: 'pin-message' - icon: 'icon-pin' - i18nLabel: 'Pin_Message' - context: [ - 'pinned' - 'message' - 'message-mobile' - ] - action: (event, instance) -> - message = @_arguments[1] - message.pinned = true - Meteor.call 'pinMessage', message, (error, result) -> - if error - return handleError(error) - validation: (message) -> - if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })? - return false - else if message.pinned or not RocketChat.settings.get('Message_AllowPinning') - return false - - return RocketChat.authz.hasAtLeastOnePermission 'pin-message', message.rid - order: 20 - - RocketChat.MessageAction.addButton - id: 'unpin-message' - icon: 'icon-pin rotate-45' - i18nLabel: 'Unpin_Message' - context: [ - 'pinned' - 'message' - 'message-mobile' - ] - action: (event, instance) -> - message = @_arguments[1] - message.pinned = false - Meteor.call 'unpinMessage', message, (error, result) -> - if error - return handleError(error) - validation: (message) -> - if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })? - return false - else if not message.pinned or not RocketChat.settings.get('Message_AllowPinning') - return false - - return RocketChat.authz.hasAtLeastOnePermission 'pin-message', message.rid - order: 21 - - RocketChat.MessageAction.addButton - id: 'jump-to-pin-message' - icon: 'icon-right-hand' - i18nLabel: 'Jump_to_message' - context: [ - 'pinned' - ] - action: (event, instance) -> - message = @_arguments[1] - RocketChat.MessageAction.hideDropDown() - RoomHistoryManager.getSurroundingMessages(message, 50) - validation: (message) -> - if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })? - return false - - return true - order: 100 - - RocketChat.MessageAction.addButton - id: 'permalink-pinned' - icon: 'icon-link' - i18nLabel: 'Permalink' - classes: 'clipboard' - context: [ - 'pinned' - ] - action: (event, instance) -> - message = @_arguments[1] - RocketChat.MessageAction.hideDropDown() - $(event.currentTarget).attr('data-clipboard-text', RocketChat.MessageAction.getPermaLink(message._id)); - toastr.success(TAPi18n.__('Copied')) - validation: (message) -> - if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })? - return false - - return true - order: 101 diff --git a/packages/rocketchat-message-pin/client/actionButton.js b/packages/rocketchat-message-pin/client/actionButton.js new file mode 100644 index 000000000000..826f33f9fbe0 --- /dev/null +++ b/packages/rocketchat-message-pin/client/actionButton.js @@ -0,0 +1,97 @@ +import toastr from 'toastr'; + +Meteor.startup(function() { + RocketChat.MessageAction.addButton({ + id: 'pin-message', + icon: 'icon-pin', + i18nLabel: 'Pin_Message', + context: ['pinned', 'message', 'message-mobile'], + + action() { + const message = this._arguments[1]; + message.pinned = true; + return Meteor.call('pinMessage', message, function(error) { + if (error) { + return handleError(error); + } + }); + }, + + validation(message) { + if (RocketChat.models.Subscriptions.findOne({ rid: message.rid }) == null) { + return false; + } else if (message.pinned || !RocketChat.settings.get('Message_AllowPinning')) { + return false; + } + return RocketChat.authz.hasAtLeastOnePermission('pin-message', message.rid); + }, + order: 20 + }); + + RocketChat.MessageAction.addButton({ + id: 'unpin-message', + icon: 'icon-pin rotate-45', + i18nLabel: 'Unpin_Message', + context: ['pinned', 'message', 'message-mobile'], + action() { + const message = this._arguments[1]; + message.pinned = false; + return Meteor.call('unpinMessage', message, function(error) { + if (error) { + return handleError(error); + } + }); + }, + + validation(message) { + if (RocketChat.models.Subscriptions.findOne({ rid: message.rid }) == null) { + return false; + } else if (!message.pinned || !RocketChat.settings.get('Message_AllowPinning')) { + return false; + } + return RocketChat.authz.hasAtLeastOnePermission('pin-message', message.rid); + }, + order: 21 + }); + + RocketChat.MessageAction.addButton({ + id: 'jump-to-pin-message', + icon: 'icon-right-hand', + i18nLabel: 'Jump_to_message', + context: ['pinned'], + action() { + const message = this._arguments[1]; + RocketChat.MessageAction.hideDropDown(); + return RoomHistoryManager.getSurroundingMessages(message, 50); + }, + validation(message) { + if (RocketChat.models.Subscriptions.findOne({ rid: message.rid }) == null) { + return false; + } + return true; + }, + order: 100 + }); + + return RocketChat.MessageAction.addButton({ + id: 'permalink-pinned', + icon: 'icon-link', + i18nLabel: 'Permalink', + classes: 'clipboard', + context: ['pinned'], + action() { + const message = this._arguments[1]; + RocketChat.MessageAction.hideDropDown(); + $(event.currentTarget).attr('data-clipboard-text', RocketChat.MessageAction.getPermaLink(message._id)); + return toastr.success(TAPi18n.__('Copied')); + }, + + validation(message) { + if (RocketChat.models.Subscriptions.findOne({ rid: message.rid }) == null) { + return false; + } + return true; + }, + order: 101 + }); +}); diff --git a/packages/rocketchat-message-pin/client/lib/PinnedMessage.coffee b/packages/rocketchat-message-pin/client/lib/PinnedMessage.coffee deleted file mode 100644 index 4b4849c8acde..000000000000 --- a/packages/rocketchat-message-pin/client/lib/PinnedMessage.coffee +++ /dev/null @@ -1 +0,0 @@ -@PinnedMessage = new Mongo.Collection 'rocketchat_pinned_message' diff --git a/packages/rocketchat-message-pin/client/lib/PinnedMessage.js b/packages/rocketchat-message-pin/client/lib/PinnedMessage.js new file mode 100644 index 000000000000..a6c9b072f915 --- /dev/null +++ b/packages/rocketchat-message-pin/client/lib/PinnedMessage.js @@ -0,0 +1 @@ +this.PinnedMessage = new Mongo.Collection('rocketchat_pinned_message'); diff --git a/packages/rocketchat-message-pin/client/pinMessage.coffee b/packages/rocketchat-message-pin/client/pinMessage.coffee deleted file mode 100644 index 0151c21ab638..000000000000 --- a/packages/rocketchat-message-pin/client/pinMessage.coffee +++ /dev/null @@ -1,30 +0,0 @@ -Meteor.methods - pinMessage: (message) -> - if not Meteor.userId() - return false - - if not RocketChat.settings.get 'Message_AllowPinning' - return false - - if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })? - return false - - ChatMessage.update - _id: message._id - , - $set: { pinned: true } - - unpinMessage: (message) -> - if not Meteor.userId() - return false - - if not RocketChat.settings.get 'Message_AllowPinning' - return false - - if not RocketChat.models.Subscriptions.findOne({ rid: message.rid })? - return false - - ChatMessage.update - _id: message._id - , - $set: { pinned: false } diff --git a/packages/rocketchat-message-pin/client/pinMessage.js b/packages/rocketchat-message-pin/client/pinMessage.js new file mode 100644 index 000000000000..f863ac6009ed --- /dev/null +++ b/packages/rocketchat-message-pin/client/pinMessage.js @@ -0,0 +1,38 @@ +Meteor.methods({ + pinMessage(message) { + if (!Meteor.userId()) { + return false; + } + if (!RocketChat.settings.get('Message_AllowPinning')) { + return false; + } + if (RocketChat.models.Subscriptions.findOne({ rid: message.rid }) == null) { + return false; + } + return ChatMessage.update({ + _id: message._id + }, { + $set: { + pinned: true + } + }); + }, + unpinMessage(message) { + if (!Meteor.userId()) { + return false; + } + if (!RocketChat.settings.get('Message_AllowPinning')) { + return false; + } + if (RocketChat.models.Subscriptions.findOne({ rid: message.rid }) == null) { + return false; + } + return ChatMessage.update({ + _id: message._id + }, { + $set: { + pinned: false + } + }); + } +}); diff --git a/packages/rocketchat-message-pin/client/tabBar.coffee b/packages/rocketchat-message-pin/client/tabBar.coffee deleted file mode 100644 index f96377abdad6..000000000000 --- a/packages/rocketchat-message-pin/client/tabBar.coffee +++ /dev/null @@ -1,13 +0,0 @@ -Meteor.startup -> - Tracker.autorun -> - if RocketChat.settings.get 'Message_AllowPinning' - RocketChat.TabBar.addButton({ - groups: ['channel', 'group', 'direct'], - id: 'pinned-messages', - i18nTitle: 'Pinned_Messages', - icon: 'icon-pin', - template: 'pinnedMessages', - order: 10 - }) - else - RocketChat.TabBar.removeButton 'pinned-messages' diff --git a/packages/rocketchat-message-pin/client/tabBar.js b/packages/rocketchat-message-pin/client/tabBar.js new file mode 100644 index 000000000000..253d4f22b21b --- /dev/null +++ b/packages/rocketchat-message-pin/client/tabBar.js @@ -0,0 +1,16 @@ +Meteor.startup(function() { + return Tracker.autorun(function() { + if (RocketChat.settings.get('Message_AllowPinning')) { + return RocketChat.TabBar.addButton({ + groups: ['channel', 'group', 'direct'], + id: 'pinned-messages', + i18nTitle: 'Pinned_Messages', + icon: 'icon-pin', + template: 'pinnedMessages', + order: 10 + }); + } else { + return RocketChat.TabBar.removeButton('pinned-messages'); + } + }); +}); diff --git a/packages/rocketchat-message-pin/client/views/pinnedMessages.coffee b/packages/rocketchat-message-pin/client/views/pinnedMessages.coffee deleted file mode 100644 index f99ec368d472..000000000000 --- a/packages/rocketchat-message-pin/client/views/pinnedMessages.coffee +++ /dev/null @@ -1,40 +0,0 @@ -Template.pinnedMessages.helpers - hasMessages: -> - return PinnedMessage.find({ rid: @rid }, { sort: { ts: -1 } }).count() > 0 - - messages: -> - return PinnedMessage.find { rid: @rid }, { sort: { ts: -1 } } - - message: -> - return _.extend(this, { customClass: 'pinned' }) - - hasMore: -> - return Template.instance().hasMore.get() - -Template.pinnedMessages.onCreated -> - @hasMore = new ReactiveVar true - @limit = new ReactiveVar 50 - @autorun => - data = Template.currentData() - @subscribe 'pinnedMessages', data.rid, @limit.get(), => - if PinnedMessage.find({ rid: data.rid }).count() < @limit.get() - @hasMore.set false - -Template.pinnedMessages.events - 'click .message-cog': (e, t) -> - e.stopPropagation() - e.preventDefault() - message_id = $(e.currentTarget).closest('.message').attr('id') - RocketChat.MessageAction.hideDropDown() - t.$("\##{message_id} .message-dropdown").remove() - message = PinnedMessage.findOne message_id - actions = RocketChat.MessageAction.getButtons message, 'pinned' - el = Blaze.toHTMLWithData Template.messageDropdown, { actions: actions } - t.$("\##{message_id} .message-cog-container").append el - dropDown = t.$("\##{message_id} .message-dropdown") - dropDown.show() - - 'scroll .content': _.throttle (e, instance) -> - if e.target.scrollTop >= e.target.scrollHeight - e.target.clientHeight && instance.hasMore.get() - instance.limit.set(instance.limit.get() + 50) - , 200 diff --git a/packages/rocketchat-message-pin/client/views/pinnedMessages.js b/packages/rocketchat-message-pin/client/views/pinnedMessages.js new file mode 100644 index 000000000000..5af8ad3ff881 --- /dev/null +++ b/packages/rocketchat-message-pin/client/views/pinnedMessages.js @@ -0,0 +1,70 @@ +/* globals PinnedMessage */ +Template.pinnedMessages.helpers({ + hasMessages() { + return PinnedMessage.find({ + rid: this.rid + }, { + sort: { + ts: -1 + } + }).count() > 0; + }, + messages() { + return PinnedMessage.find({ + rid: this.rid + }, { + sort: { + ts: -1 + } + }); + }, + message() { + return _.extend(this, { + customClass: 'pinned' + }); + }, + hasMore() { + return Template.instance().hasMore.get(); + } +}); + +Template.pinnedMessages.onCreated(function() { + this.hasMore = new ReactiveVar(true); + this.limit = new ReactiveVar(50); + return this.autorun((function(_this) { + return function() { + const data = Template.currentData(); + return _this.subscribe('pinnedMessages', data.rid, _this.limit.get(), function() { + if (PinnedMessage.find({ + rid: data.rid + }).count() < _this.limit.get()) { + return _this.hasMore.set(false); + } + }); + }; + })(this)); +}); + +Template.pinnedMessages.events({ + 'click .message-cog'(e, t) { + let actions, dropDown, el, message, message_id; + e.stopPropagation(); + e.preventDefault(); + message_id = $(e.currentTarget).closest('.message').attr('id'); + RocketChat.MessageAction.hideDropDown(); + t.$(`\#${ message_id } .message-dropdown`).remove(); + message = PinnedMessage.findOne(message_id); + actions = RocketChat.MessageAction.getButtons(message, 'pinned'); + el = Blaze.toHTMLWithData(Template.messageDropdown, { + actions + }); + t.$(`\#${ message_id } .message-cog-container`).append(el); + dropDown = t.$(`\#${ message_id } .message-dropdown`); + return dropDown.show(); + }, + 'scroll .content': _.throttle(function(e, instance) { + if (e.target.scrollTop >= e.target.scrollHeight - e.target.clientHeight && instance.hasMore.get()) { + return instance.limit.set(instance.limit.get() + 50); + } + }, 200) +}); diff --git a/packages/rocketchat-message-pin/package.js b/packages/rocketchat-message-pin/package.js index 30414b03156b..b9daa1c4ef9f 100644 --- a/packages/rocketchat-message-pin/package.js +++ b/packages/rocketchat-message-pin/package.js @@ -7,7 +7,6 @@ Package.describe({ Package.onUse(function(api) { api.use([ 'mongo', - 'coffeescript', 'ecmascript', 'underscore', 'less', @@ -17,20 +16,20 @@ Package.onUse(function(api) { api.use('templating', 'client'); api.addFiles([ - 'client/lib/PinnedMessage.coffee', - 'client/actionButton.coffee', + 'client/lib/PinnedMessage.js', + 'client/actionButton.js', 'client/messageType.js', - 'client/pinMessage.coffee', - 'client/tabBar.coffee', + 'client/pinMessage.js', + 'client/tabBar.js', 'client/views/pinnedMessages.html', - 'client/views/pinnedMessages.coffee', + 'client/views/pinnedMessages.js', 'client/views/stylesheets/messagepin.less' ], 'client'); api.addFiles([ - 'server/settings.coffee', - 'server/pinMessage.coffee', - 'server/publications/pinnedMessages.coffee', - 'server/startup/indexes.coffee' + 'server/settings.js', + 'server/pinMessage.js', + 'server/publications/pinnedMessages.js', + 'server/startup/indexes.js' ], 'server'); }); diff --git a/packages/rocketchat-message-pin/server/pinMessage.coffee b/packages/rocketchat-message-pin/server/pinMessage.coffee deleted file mode 100644 index f2049c584652..000000000000 --- a/packages/rocketchat-message-pin/server/pinMessage.coffee +++ /dev/null @@ -1,77 +0,0 @@ -Meteor.methods - pinMessage: (message, pinnedAt) -> - if not Meteor.userId() - throw new Meteor.Error('error-invalid-user', "Invalid user", { method: 'pinMessage' }) - - if not RocketChat.settings.get 'Message_AllowPinning' - throw new Meteor.Error 'error-action-not-allowed', 'Message pinning not allowed', { method: 'pinMessage', action: 'Message_pinning' } - - room = RocketChat.models.Rooms.findOneById(message.rid) - - if Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) is -1 - return false - - originalMessage = RocketChat.models.Messages.findOneById message._id - - if not originalMessage?._id? - throw new Meteor.Error 'error-invalid-message', 'Message you are pinning was not found', { method: 'pinMessage', action: 'Message_pinning' } - - # If we keep history of edits, insert a new message to store history information - if RocketChat.settings.get 'Message_KeepHistory' - RocketChat.models.Messages.cloneAndSaveAsHistoryById message._id - - me = RocketChat.models.Users.findOneById Meteor.userId() - - originalMessage.pinned = true - originalMessage.pinnedAt = pinnedAt || Date.now - originalMessage.pinnedBy = - _id: Meteor.userId() - username: me.username - - originalMessage = RocketChat.callbacks.run 'beforeSaveMessage', originalMessage - - RocketChat.models.Messages.setPinnedByIdAndUserId originalMessage._id, originalMessage.pinnedBy, originalMessage.pinned - - 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 - ] - - unpinMessage: (message) -> - if not Meteor.userId() - throw new Meteor.Error('error-invalid-user', "Invalid user", { method: 'unpinMessage' }) - - if not RocketChat.settings.get 'Message_AllowPinning' - throw new Meteor.Error 'error-action-not-allowed', 'Message pinning not allowed', { method: 'unpinMessage', action: 'Message_pinning' } - - room = RocketChat.models.Rooms.findOneById(message.rid) - - if Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) is -1 - return false - - originalMessage = RocketChat.models.Messages.findOneById message._id - - if not originalMessage?._id? - throw new Meteor.Error 'error-invalid-message', 'Message you are unpinning was not found', { method: 'unpinMessage', action: 'Message_pinning' } - - # If we keep history of edits, insert a new message to store history information - if RocketChat.settings.get 'Message_KeepHistory' - RocketChat.models.Messages.cloneAndSaveAsHistoryById originalMessage._id - - me = RocketChat.models.Users.findOneById Meteor.userId() - - originalMessage.pinned = false - originalMessage.pinnedBy = - _id: Meteor.userId() - username: me.username - - originalMessage = RocketChat.callbacks.run 'beforeSaveMessage', originalMessage - - RocketChat.models.Messages.setPinnedByIdAndUserId originalMessage._id, originalMessage.pinnedBy, originalMessage.pinned - - - # Meteor.defer -> - # RocketChat.callbacks.run 'afterSaveMessage', RocketChat.models.Messages.findOneById(message.id) diff --git a/packages/rocketchat-message-pin/server/pinMessage.js b/packages/rocketchat-message-pin/server/pinMessage.js new file mode 100644 index 000000000000..8906e247a86b --- /dev/null +++ b/packages/rocketchat-message-pin/server/pinMessage.js @@ -0,0 +1,86 @@ +Meteor.methods({ + pinMessage(message, pinnedAt) { + if (!Meteor.userId()) { + throw new Meteor.Error('error-invalid-user', 'Invalid user', { + method: 'pinMessage' + }); + } + if (!RocketChat.settings.get('Message_AllowPinning')) { + throw new Meteor.Error('error-action-not-allowed', 'Message pinning not allowed', { + method: 'pinMessage', + action: 'Message_pinning' + }); + } + const room = RocketChat.models.Rooms.findOneById(message.rid); + if (Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) === -1) { + return false; + } + let originalMessage = RocketChat.models.Messages.findOneById(message._id); + if (!(originalMessage && originalMessage._id)) { + throw new Meteor.Error('error-invalid-message', 'Message you are pinning was not found', { + method: 'pinMessage', + action: 'Message_pinning' + }); + } + if (RocketChat.settings.get('Message_KeepHistory')) { + RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id); + } + const me = RocketChat.models.Users.findOneById(Meteor.userId()); + originalMessage.pinned = true; + originalMessage.pinnedAt = pinnedAt || Date.now; + originalMessage.pinnedBy = { + _id: Meteor.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 + } + ] + }); + }, + unpinMessage(message) { + if (!Meteor.userId()) { + throw new Meteor.Error('error-invalid-user', 'Invalid user', { + method: 'unpinMessage' + }); + } + if (!RocketChat.settings.get('Message_AllowPinning')) { + throw new Meteor.Error('error-action-not-allowed', 'Message pinning not allowed', { + method: 'unpinMessage', + action: 'Message_pinning' + }); + } + const room = RocketChat.models.Rooms.findOneById(message.rid); + + if (Array.isArray(room.usernames) && room.usernames.indexOf(Meteor.user().username) === -1) { + return false; + } + + let originalMessage = RocketChat.models.Messages.findOneById(message._id); + + if (!(originalMessage && originalMessage._id)) { + throw new Meteor.Error('error-invalid-message', 'Message you are unpinning was not found', { + method: 'unpinMessage', + action: 'Message_pinning' + }); + } + if (RocketChat.settings.get('Message_KeepHistory')) { + RocketChat.models.Messages.cloneAndSaveAsHistoryById(originalMessage._id); + } + const me = RocketChat.models.Users.findOneById(Meteor.userId()); + originalMessage.pinned = false; + originalMessage.pinnedBy = { + _id: Meteor.userId(), + username: me.username + }; + originalMessage = RocketChat.callbacks.run('beforeSaveMessage', originalMessage); + return RocketChat.models.Messages.setPinnedByIdAndUserId(originalMessage._id, originalMessage.pinnedBy, originalMessage.pinned); + } +}); diff --git a/packages/rocketchat-message-pin/server/publications/pinnedMessages.coffee b/packages/rocketchat-message-pin/server/publications/pinnedMessages.coffee deleted file mode 100644 index efa77f81456c..000000000000 --- a/packages/rocketchat-message-pin/server/publications/pinnedMessages.coffee +++ /dev/null @@ -1,23 +0,0 @@ -Meteor.publish 'pinnedMessages', (rid, limit=50) -> - unless this.userId - return this.ready() - - publication = @ - - user = RocketChat.models.Users.findOneById this.userId - unless user - return this.ready() - - cursorHandle = RocketChat.models.Messages.findPinnedByRoom(rid, { sort: { ts: -1 }, limit: limit }).observeChanges - added: (_id, record) -> - publication.added('rocketchat_pinned_message', _id, record) - - changed: (_id, record) -> - publication.changed('rocketchat_pinned_message', _id, record) - - removed: (_id) -> - publication.removed('rocketchat_pinned_message', _id) - - @ready() - @onStop -> - cursorHandle.stop() diff --git a/packages/rocketchat-message-pin/server/publications/pinnedMessages.js b/packages/rocketchat-message-pin/server/publications/pinnedMessages.js new file mode 100644 index 000000000000..e6cda8b3b34b --- /dev/null +++ b/packages/rocketchat-message-pin/server/publications/pinnedMessages.js @@ -0,0 +1,26 @@ +Meteor.publish('pinnedMessages', function(rid, limit = 50) { + if (!this.userId) { + return this.ready(); + } + const publication = this; + + const user = RocketChat.models.Users.findOneById(this.userId); + if (!user) { + return this.ready(); + } + const cursorHandle = RocketChat.models.Messages.findPinnedByRoom(rid, { sort: { ts: -1 }, limit }).observeChanges({ + added(_id, record) { + return publication.added('rocketchat_pinned_message', _id, record); + }, + changed(_id, record) { + return publication.changed('rocketchat_pinned_message', _id, record); + }, + removed(_id) { + return publication.removed('rocketchat_pinned_message', _id); + } + }); + this.ready(); + return this.onStop(function() { + return cursorHandle.stop(); + }); +}); diff --git a/packages/rocketchat-message-pin/server/settings.coffee b/packages/rocketchat-message-pin/server/settings.coffee deleted file mode 100644 index 29181cffd2ad..000000000000 --- a/packages/rocketchat-message-pin/server/settings.coffee +++ /dev/null @@ -1,3 +0,0 @@ -Meteor.startup -> - RocketChat.settings.add 'Message_AllowPinning', true, { type: 'boolean', group: 'Message', public: true } - RocketChat.models.Permissions.upsert('pin-message', { $setOnInsert: { roles: ['owner', 'moderator', 'admin'] } }); diff --git a/packages/rocketchat-message-pin/server/settings.js b/packages/rocketchat-message-pin/server/settings.js new file mode 100644 index 000000000000..01ccaafbc5ae --- /dev/null +++ b/packages/rocketchat-message-pin/server/settings.js @@ -0,0 +1,12 @@ +Meteor.startup(function() { + RocketChat.settings.add('Message_AllowPinning', true, { + type: 'boolean', + group: 'Message', + 'public': true + }); + return RocketChat.models.Permissions.upsert('pin-message', { + $setOnInsert: { + roles: ['owner', 'moderator', 'admin'] + } + }); +}); diff --git a/packages/rocketchat-message-pin/server/startup/indexes.coffee b/packages/rocketchat-message-pin/server/startup/indexes.coffee deleted file mode 100644 index c008b0c36577..000000000000 --- a/packages/rocketchat-message-pin/server/startup/indexes.coffee +++ /dev/null @@ -1,3 +0,0 @@ -Meteor.startup -> - Meteor.defer -> - RocketChat.models.Messages.tryEnsureIndex { 'pinnedBy._id': 1 }, { sparse: 1 } diff --git a/packages/rocketchat-message-pin/server/startup/indexes.js b/packages/rocketchat-message-pin/server/startup/indexes.js new file mode 100644 index 000000000000..bd3f4103554c --- /dev/null +++ b/packages/rocketchat-message-pin/server/startup/indexes.js @@ -0,0 +1,9 @@ +Meteor.startup(function() { + return Meteor.defer(function() { + return RocketChat.models.Messages.tryEnsureIndex({ + 'pinnedBy._id': 1 + }, { + sparse: 1 + }); + }); +}); From 8063714c25526c52aaa89a98ca0d7817a2ecd248 Mon Sep 17 00:00:00 2001 From: Martin Schoeler Date: Mon, 3 Apr 2017 16:42:30 -0300 Subject: [PATCH 2/4] fix not saved file --- .../client/views/pinnedMessages.js | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/rocketchat-message-pin/client/views/pinnedMessages.js b/packages/rocketchat-message-pin/client/views/pinnedMessages.js index 5af8ad3ff881..24e7dc483527 100644 --- a/packages/rocketchat-message-pin/client/views/pinnedMessages.js +++ b/packages/rocketchat-message-pin/client/views/pinnedMessages.js @@ -31,35 +31,34 @@ Template.pinnedMessages.helpers({ Template.pinnedMessages.onCreated(function() { this.hasMore = new ReactiveVar(true); this.limit = new ReactiveVar(50); - return this.autorun((function(_this) { + return this.autorun(() => { return function() { const data = Template.currentData(); - return _this.subscribe('pinnedMessages', data.rid, _this.limit.get(), function() { + return this.subscribe('pinnedMessages', data.rid, this.limit.get(), function() { if (PinnedMessage.find({ rid: data.rid - }).count() < _this.limit.get()) { - return _this.hasMore.set(false); + }).count() < this.limit.get()) { + return this.hasMore.set(false); } }); }; - })(this)); + }); }); Template.pinnedMessages.events({ 'click .message-cog'(e, t) { - let actions, dropDown, el, message, message_id; e.stopPropagation(); e.preventDefault(); - message_id = $(e.currentTarget).closest('.message').attr('id'); + const message_id = $(e.currentTarget).closest('.message').attr('id'); RocketChat.MessageAction.hideDropDown(); t.$(`\#${ message_id } .message-dropdown`).remove(); - message = PinnedMessage.findOne(message_id); - actions = RocketChat.MessageAction.getButtons(message, 'pinned'); - el = Blaze.toHTMLWithData(Template.messageDropdown, { + const message = PinnedMessage.findOne(message_id); + const actions = RocketChat.MessageAction.getButtons(message, 'pinned'); + const el = Blaze.toHTMLWithData(Template.messageDropdown, { actions }); t.$(`\#${ message_id } .message-cog-container`).append(el); - dropDown = t.$(`\#${ message_id } .message-dropdown`); + const dropDown = t.$(`\#${ message_id } .message-dropdown`); return dropDown.show(); }, 'scroll .content': _.throttle(function(e, instance) { From 0ec692c92c1ba5ac21686b861e57caf08ba23ca5 Mon Sep 17 00:00:00 2001 From: Martin Schoeler Date: Tue, 4 Apr 2017 10:08:33 -0300 Subject: [PATCH 3/4] fix review --- .../client/views/pinnedMessages.js | 18 ++++++++---------- .../server/pinMessage.js | 6 ++++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/rocketchat-message-pin/client/views/pinnedMessages.js b/packages/rocketchat-message-pin/client/views/pinnedMessages.js index 24e7dc483527..f735c4419c5a 100644 --- a/packages/rocketchat-message-pin/client/views/pinnedMessages.js +++ b/packages/rocketchat-message-pin/client/views/pinnedMessages.js @@ -32,16 +32,14 @@ Template.pinnedMessages.onCreated(function() { this.hasMore = new ReactiveVar(true); this.limit = new ReactiveVar(50); return this.autorun(() => { - return function() { - const data = Template.currentData(); - return this.subscribe('pinnedMessages', data.rid, this.limit.get(), function() { - if (PinnedMessage.find({ - rid: data.rid - }).count() < this.limit.get()) { - return this.hasMore.set(false); - } - }); - }; + const data = Template.currentData(); + return this.subscribe('pinnedMessages', data.rid, this.limit.get(), function() { + if (PinnedMessage.find({ + rid: data.rid + }).count() < this.limit.get()) { + return this.hasMore.set(false); + } + }); }); }); diff --git a/packages/rocketchat-message-pin/server/pinMessage.js b/packages/rocketchat-message-pin/server/pinMessage.js index 8906e247a86b..defb7727b51c 100644 --- a/packages/rocketchat-message-pin/server/pinMessage.js +++ b/packages/rocketchat-message-pin/server/pinMessage.js @@ -16,12 +16,13 @@ Meteor.methods({ return false; } let originalMessage = RocketChat.models.Messages.findOneById(message._id); - if (!(originalMessage && originalMessage._id)) { + if (originalMessage == null || originalMessage._id == null) { throw new Meteor.Error('error-invalid-message', 'Message you are pinning was not found', { method: 'pinMessage', action: 'Message_pinning' }); } + //If we keep history of edits, insert a new message to store history information if (RocketChat.settings.get('Message_KeepHistory')) { RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id); } @@ -65,12 +66,13 @@ Meteor.methods({ let originalMessage = RocketChat.models.Messages.findOneById(message._id); - if (!(originalMessage && originalMessage._id)) { + if (originalMessage == null || originalMessage._id == null) { throw new Meteor.Error('error-invalid-message', 'Message you are unpinning was not found', { method: 'unpinMessage', action: 'Message_pinning' }); } + //If we keep history of edits, insert a new message to store history information if (RocketChat.settings.get('Message_KeepHistory')) { RocketChat.models.Messages.cloneAndSaveAsHistoryById(originalMessage._id); } From 00ead2ad6d9e1f9db3274f309db256b755aa6727 Mon Sep 17 00:00:00 2001 From: Martin Schoeler Date: Tue, 4 Apr 2017 10:33:18 -0300 Subject: [PATCH 4/4] update pinnedmessages.js --- packages/rocketchat-message-pin/client/views/pinnedMessages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-message-pin/client/views/pinnedMessages.js b/packages/rocketchat-message-pin/client/views/pinnedMessages.js index f735c4419c5a..b6a4a38ce2b6 100644 --- a/packages/rocketchat-message-pin/client/views/pinnedMessages.js +++ b/packages/rocketchat-message-pin/client/views/pinnedMessages.js @@ -33,7 +33,7 @@ Template.pinnedMessages.onCreated(function() { this.limit = new ReactiveVar(50); return this.autorun(() => { const data = Template.currentData(); - return this.subscribe('pinnedMessages', data.rid, this.limit.get(), function() { + return this.subscribe('pinnedMessages', data.rid, this.limit.get(), () => { if (PinnedMessage.find({ rid: data.rid }).count() < this.limit.get()) {