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

Fix message deletion and fix render of previous message #359

Merged
merged 1 commit into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions client/lib/RoomManager.coffee
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,6 +27,7 @@
sub.stop()

msgStream.removeListener rid
deleteMsgStream.removeListener rid

openedRooms[rid].ready = false
openedRooms[rid].active = false
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions client/lib/chatMessages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 ''
Expand Down
9 changes: 9 additions & 0 deletions client/methods/deleteMessage.coffee
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 1 addition & 11 deletions client/methods/updateMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion client/views/app/room.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions server/methods/deleteMessage.coffee
Original file line number Diff line number Diff line change
@@ -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 }
22 changes: 1 addition & 21 deletions server/methods/updateMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 14 additions & 12 deletions server/stream/messages.coffee
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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: [
Expand All @@ -27,8 +40,6 @@ Meteor.startup ->
ets:
$gt: new Date()
]
_deleted:
$ne: true

options = {}

Expand All @@ -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()
13 changes: 7 additions & 6 deletions server/stream/streamBroadcast.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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