From afbb394221c1d2dfddb26b6280527784bd05b9ec Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Tue, 28 Jul 2015 16:55:28 -0300 Subject: [PATCH] Fix message deletion and fix render of previous message --- client/lib/RoomManager.coffee | 16 +++++++++++++--- client/lib/chatMessages.coffee | 9 ++++----- client/methods/deleteMessage.coffee | 9 +++++++++ client/methods/updateMessage.coffee | 12 +----------- client/views/app/room.coffee | 3 ++- server/methods/deleteMessage.coffee | 12 ++++++++++++ server/methods/updateMessage.coffee | 22 +--------------------- server/stream/messages.coffee | 26 ++++++++++++++------------ server/stream/streamBroadcast.coffee | 13 +++++++------ 9 files changed, 63 insertions(+), 59 deletions(-) create mode 100644 client/methods/deleteMessage.coffee create mode 100644 server/methods/deleteMessage.coffee diff --git a/client/lib/RoomManager.coffee b/client/lib/RoomManager.coffee index 50755c3823e3..1db1142dec92 100644 --- a/client/lib/RoomManager.coffee +++ b/client/lib/RoomManager.coffee @@ -1,8 +1,17 @@ +Meteor.startup -> + ChatMessage.find().observe + removed: (record) -> + recordBefore = ChatMessage.findOne {ts: {$lt: record.ts}}, {sort: {ts: -1}} + if recordBefore? + ChatMessage.update {_id: recordBefore._id}, {$set: {tick: new Date}} + + @RoomManager = new class defaultTime = 600000 # 10 minutes openedRooms = {} subscription = null msgStream = new Meteor.Stream 'messages' + deleteMsgStream = new Meteor.Stream 'delete-message' onlineUsers = new ReactiveVar {} Dep = new Tracker.Dependency @@ -18,6 +27,7 @@ sub.stop() msgStream.removeListener rid + deleteMsgStream.removeListener rid openedRooms[rid].ready = false openedRooms[rid].active = false @@ -63,11 +73,11 @@ openedRooms[rid].active = true msgStream.on rid, (msg) -> - if msg._deleted? - return ChatMessage.remove _id: msg._id - ChatMessage.upsert { _id: msg._id }, msg + deleteMsgStream.on rid, (msg) -> + ChatMessage.remove _id: msg._id + computation.invalidate() return { diff --git a/client/lib/chatMessages.coffee b/client/lib/chatMessages.coffee index a8aa18c90894..78c962f24dcd 100644 --- a/client/lib/chatMessages.coffee +++ b/client/lib/chatMessages.coffee @@ -86,11 +86,10 @@ #Meteor.call 'onClientBeforeSendMessage', {} Meteor.call 'sendMessage', msgObject - deleteMsg = (element) -> - id = element.getAttribute("id") - Meteor.call 'deleteMessage', { id: id }, (error, result) -> - if error - return Errors.throw error.reason + deleteMsg = (message) -> + Meteor.call 'deleteMessage', message, (error, result) -> + if error + return Errors.throw error.reason update = (id, rid, input) -> if _.trim(input.value) isnt '' diff --git a/client/methods/deleteMessage.coffee b/client/methods/deleteMessage.coffee new file mode 100644 index 000000000000..525a41f64051 --- /dev/null +++ b/client/methods/deleteMessage.coffee @@ -0,0 +1,9 @@ +Meteor.methods + deleteMessage: (message) -> + if not Meteor.userId() + throw new Meteor.Error 203, t('general.User_logged_out') + + Tracker.nonreactive -> + ChatMessage.remove + _id: message._id + 'u._id': Meteor.userId() diff --git a/client/methods/updateMessage.coffee b/client/methods/updateMessage.coffee index 477a08def69f..688b23d9593b 100644 --- a/client/methods/updateMessage.coffee +++ b/client/methods/updateMessage.coffee @@ -14,14 +14,4 @@ Meteor.methods , $set: ets: message.ets - msg: message.msg - - deleteMessage: (message) -> - if not Meteor.userId() - throw new Meteor.Error 203, t('general.User_logged_out') - - Tracker.nonreactive -> - - ChatMessage.remove - _id: message.id - 'u._id': Meteor.userId() + msg: message.msg \ No newline at end of file diff --git a/client/views/app/room.coffee b/client/views/app/room.coffee index 3a71fbb92578..5549afe17e6e 100644 --- a/client/views/app/room.coffee +++ b/client/views/app/room.coffee @@ -431,6 +431,7 @@ Template.room.events Session.set('showUserInfo', $(e.currentTarget).data('username')) 'click .delete-message': (event) -> + message = @_arguments[1] msg = event.currentTarget.parentNode.parentNode return if msg.classList.contains("system") swal { @@ -445,7 +446,7 @@ Template.room.events html: false }, -> swal t('Deleted'), t('Your_entry_has_been_deleted'), 'success' - ChatMessages.deleteMsg(msg) + ChatMessages.deleteMsg(message) 'click .start-video': (event) -> _id = Template.instance().data._id diff --git a/server/methods/deleteMessage.coffee b/server/methods/deleteMessage.coffee new file mode 100644 index 000000000000..3fc661054a64 --- /dev/null +++ b/server/methods/deleteMessage.coffee @@ -0,0 +1,12 @@ +Meteor.methods + deleteMessage: (message) -> + if not Meteor.userId() + throw new Meteor.Error('invalid-user', "[methods] deleteMessage -> Invalid user") + + console.log '[methods] deleteMessage -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments + + ChatMessage.remove + _id: message._id + 'u._id': Meteor.userId() + + deleteMsgStream.emit message.rid, { _id: message._id } diff --git a/server/methods/updateMessage.coffee b/server/methods/updateMessage.coffee index 50a50410e341..1370fd50a349 100644 --- a/server/methods/updateMessage.coffee +++ b/server/methods/updateMessage.coffee @@ -19,24 +19,4 @@ Meteor.methods $set: message # Meteor.defer -> - # RocketChat.callbacks.run 'afterSaveMessage', ChatMessage.findOne(message.id) - - deleteMessage: (message) -> - if not Meteor.userId() - throw new Meteor.Error('invalid-user', "[methods] deleteMessage -> Invalid user") - - console.log '[methods] deleteMessage -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments - - ChatMessage.update - _id: message.id - 'u._id': Meteor.userId() - , - $set: - _deleted: true - - # userId = Meteor.userId() - # Meteor.setTimeout -> - # ChatMessage.remove - # _id: message.id - # 'u._id': userId - # , 10000 + # RocketChat.callbacks.run 'afterSaveMessage', ChatMessage.findOne(message.id) \ No newline at end of file diff --git a/server/stream/messages.coffee b/server/stream/messages.coffee index 168daaca8c1d..848d7e5264f9 100644 --- a/server/stream/messages.coffee +++ b/server/stream/messages.coffee @@ -1,4 +1,5 @@ -msgStream = new Meteor.Stream 'messages' +@msgStream = new Meteor.Stream 'messages' +@deleteMsgStream = new Meteor.Stream 'delete-message' msgStream.permissions.write (eventName) -> console.log('stream.permissions.write', this.userId); @@ -18,6 +19,18 @@ msgStream.permissions.read (eventName) -> catch e return false + +deleteMsgStream.permissions.write (eventName) -> + return false + +deleteMsgStream.permissions.read (eventName) -> + try + canAccess = Meteor.call 'canAccessRoom', eventName, this.userId + + return !!canAccess + catch e + return false + Meteor.startup -> filter = $or: [ @@ -27,8 +40,6 @@ Meteor.startup -> ets: $gt: new Date() ] - _deleted: - $ne: true options = {} @@ -38,12 +49,3 @@ Meteor.startup -> changed: (record) -> msgStream.emit record.rid, record - - ChatMessage.find({ _deleted: true }, { fields: { rid: 1, _id: 1 } }).observeChanges - added: (_id, record) -> - msgStream.emit record.rid, { _id: _id, _deleted: true } - changed: (_id, record) -> - msgStream.emit record.rid, { _id: _id, _deleted: true } - ChatMessage.remove - _id: message.id - 'u._id': Meteor.userId() diff --git a/server/stream/streamBroadcast.coffee b/server/stream/streamBroadcast.coffee index 1954d690fce1..7fd13ad79182 100644 --- a/server/stream/streamBroadcast.coffee +++ b/server/stream/streamBroadcast.coffee @@ -49,10 +49,11 @@ Meteor.startup -> + config = + 'typing': typingStream + 'deleteMsgStream': deleteMsgStream + if webrtc - startStreamBroadcast - 'webrtc.stream': webrtc.stream - 'typing': typingStream - else - startStreamBroadcast - 'typing': typingStream + config['webrtc.stream'] = webrtc.stream + + startStreamBroadcast config