From dd9dab98dfb72337f251c4aa3454b4c00bfa0d72 Mon Sep 17 00:00:00 2001 From: haosdent Date: Tue, 14 Jul 2015 12:30:54 +0800 Subject: [PATCH] Fix minor bugs in irc plugin. --- packages/rocketchat-irc/irc.server.coffee | 26 +++-- server/methods/createChannel.coffee | 11 +- server/methods/joinRoom.coffee | 4 +- server/methods/leaveRoom.coffee | 15 +-- server/methods/receiveMessage.coffee | 124 ---------------------- 5 files changed, 35 insertions(+), 145 deletions(-) delete mode 100644 server/methods/receiveMessage.coffee diff --git a/packages/rocketchat-irc/irc.server.coffee b/packages/rocketchat-irc/irc.server.coffee index 3dd79377228f..9e05a7940d28 100644 --- a/packages/rocketchat-irc/irc.server.coffee +++ b/packages/rocketchat-irc/irc.server.coffee @@ -19,6 +19,7 @@ async = (f, args...) -> class IrcClient constructor: (@loginReq) -> @user = @loginReq.user + @user.username = @user.name ircClientMap[@user._id] = this @ircPort = IRC_PORT @ircHost = IRC_HOST @@ -157,8 +158,7 @@ class IrcClient ircSendMessageCache.set cacheKey, timestamp console.log '[irc] onReceiveMessage -> '.yellow, 'source:', source, 'target:', target, 'content:', content - @createUserWhenNotExist source - source = Meteor.users.findOne {name: source} + source = @createUserWhenNotExist source if target[0] == '#' room = ChatRoom.findOne {name: target.substring 1} else @@ -188,9 +188,6 @@ class IrcClient for member in appendMembers @createUserWhenNotExist member - Meteor.users.update {name: member}, - $set: - status: 'online' update = $pull: @@ -248,7 +245,7 @@ class IrcClient @joinRoom(room) joinRoom: (room) -> - if room.t isnt 'c' + if room.t isnt 'c' or room.name == 'general' return if @isJoiningRoom @@ -304,15 +301,23 @@ class IrcClient status: 'offline' createUserWhenNotExist: (name) -> - user = Meteor.users.findOne {name: name}, fields: name: 1 + user = Meteor.users.findOne {name: name} unless user console.log '[irc] createNotExistUser ->'.yellow, 'userName:', name Meteor.call 'registerUser', - email: "#{name}@rocketchat.org", - pass: 'rocketchat', + email: "#{name}@rocketchat.org" + pass: 'rocketchat' name: name + Meteor.users.update {name: name}, + $set: + status: 'online' + username: name + user = Meteor.users.findOne {name: name} + return user + createDirectRoomWhenNotExist: (source, target) -> + console.log '[irc] createDirectRoomWhenNotExist -> '.yellow, 'source:', source, 'target:', target rid = [source._id, target._id].sort().join('') now = new Date() ChatRoom.upsert @@ -346,9 +351,12 @@ IrcClient.getByUid = (uid) -> return ircClientMap[uid] IrcClient.create = (login) -> + unless login.user? + return login unless login.user._id of ircClientMap ircClient = new IrcClient login return async ircClient.connect + return login diff --git a/server/methods/createChannel.coffee b/server/methods/createChannel.coffee index 4fd3b69ddabc..187e89fe66a1 100644 --- a/server/methods/createChannel.coffee +++ b/server/methods/createChannel.coffee @@ -9,8 +9,9 @@ Meteor.methods console.log '[methods] createChannel -> '.green, 'userId:', Meteor.userId(), 'arguments:', arguments now = new Date() + user = Meteor.user() - members.push Meteor.user().username + members.push user.username # name = s.slugify name @@ -22,9 +23,9 @@ Meteor.methods msgs: 0 u: _id: Meteor.userId() - username: Meteor.user().username + username: user.username - RocketChat.callbacks.run 'beforeCreateChannel', Meteor.user(), room + RocketChat.callbacks.run 'beforeCreateChannel', user, room # create new room rid = ChatRoom.insert room @@ -44,14 +45,14 @@ Meteor.methods _id: member._id username: username - if username is Meteor.user().username + if username is user.username sub.ls = now ChatSubscription.insert sub Meteor.defer -> - RocketChat.callbacks.run 'afterCreateChannel', Meteor.user(), room + RocketChat.callbacks.run 'afterCreateChannel', user, room return { rid: rid diff --git a/server/methods/joinRoom.coffee b/server/methods/joinRoom.coffee index b511fe9332b5..fb12ca5b860d 100644 --- a/server/methods/joinRoom.coffee +++ b/server/methods/joinRoom.coffee @@ -45,6 +45,8 @@ Meteor.methods _id: user._id username: user.username - RocketChat.callbacks.run 'afterJoinRoom', user, room + Meteor.defer -> + + RocketChat.callbacks.run 'afterJoinRoom', user, room return true diff --git a/server/methods/leaveRoom.coffee b/server/methods/leaveRoom.coffee index 72d913a1c771..b0f6b5d81195 100644 --- a/server/methods/leaveRoom.coffee +++ b/server/methods/leaveRoom.coffee @@ -7,12 +7,13 @@ Meteor.methods throw new Meteor.Error 300, 'Usuário não logado' room = ChatRoom.findOne rid + user = Meteor.user() - RocketChat.callbacks.run 'beforeLeaveRoom', Meteor.user(), room + RocketChat.callbacks.run 'beforeLeaveRoom', user, room update = $pull: - usernames: Meteor.user().username + usernames: user.username ChatSubscription.update { rid: rid }, $set: @@ -20,8 +21,8 @@ Meteor.methods , multi: true - if room.t isnt 'c' and room.usernames.indexOf(Meteor.user().username) isnt -1 - removedUser = Meteor.user() + if room.t isnt 'c' and room.usernames.indexOf(user.username) isnt -1 + removedUser = user ChatMessage.insert rid: rid @@ -33,7 +34,7 @@ Meteor.methods username: removedUser.username if room.u? and room.u._id is Meteor.userId() - newOwner = _.without(room.usernames, Meteor.user().username)[0] + newOwner = _.without(room.usernames, user.username)[0] if newOwner? newOwner = Meteor.users.findOne username: newOwner @@ -48,4 +49,6 @@ Meteor.methods ChatRoom.update rid, update - RocketChat.callbacks.run 'afterLeaveRoom', Meteor.user(), room + Meteor.defer -> + + RocketChat.callbacks.run 'afterLeaveRoom', user, room diff --git a/server/methods/receiveMessage.coffee b/server/methods/receiveMessage.coffee deleted file mode 100644 index e5cfb59007e6..000000000000 --- a/server/methods/receiveMessage.coffee +++ /dev/null @@ -1,124 +0,0 @@ -Meteor.methods - receiveMessage: (message) -> - if message.u?._id? - else if message.u?.username? - message.u = Meteor.users.findOne {username: message.u.username}, fields: username: 1 - else - return false - - room = ChatRoom.findOne message.rid, { fields: { usernames: 1, t: 1 } } - - if not room - return false - - console.log '[methods] receiveMessage -> '.green, 'userId:', message.u._id, 'arguments:', arguments - - message.ts = new Date() - - if urls = message.msg.match /([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\w]+)?\??([-\+=&!:;%@\/\.\,\w]+)?#?([\w]+)?)?/g - message.urls = urls - - message = RocketChat.callbacks.run 'beforeReceiveMessage', message - - ### - Defer other updated as their return is not interesting to the user - ### - Meteor.defer -> - - ### - Update all the room activity tracker fields - ### - ChatRoom.update - # only subscriptions to the same room - rid: message.rid - , - # update the last message timestamp - $set: - lm: message.ts - # increate the messages counter - $inc: - msgs: 1 - - - # increment unread couter if direct messages - if room.t is 'd' - ### - Update the other subscriptions - ### - ChatSubscription.update - # only subscriptions to the same room - rid: message.rid - # not the msg owner - 'u._id': - $ne: message.u._id - , - $set: - # alert de user - alert: true - # open the room for the user - open: true - # increment unread couter - $inc: - unread: 1 - - else - message.mentions?.forEach (mention) -> - console.log mention - ### - Update all other subscriptions of mentioned users to alert their owners and incrementing - the unread counter for mentions and direct messages - ### - ChatSubscription.update - # only subscriptions to the same room - rid: message.rid - # the mentioned user - 'u._id': mention._id - , - $set: - # alert de user - alert: true - # open the room for the user - open: true - # increment unread couter - $inc: - unread: 1 - - ### - Update all other subscriptions to alert their owners but witout incrementing - the unread counter, as it is only for mentions and direct messages - ### - ChatSubscription.update - # only subscriptions to the same room - rid: message.rid - # only the ones that have not been alerted yet - alert: false - # not the msg owner - 'u._id': - $ne: message.u._id - , - $set: - # alert de user - alert: true - # open the room for the user - open: true - , - # make sure we alert all matching subscription - multi: true - - ### - Save the message. If there was already a typing record, update it. - ### - ChatTyping.upsert - rid: message.rid - $and: [{ 'u._id': message.u._id }] - , - $set: message - $unset: - expireAt: 1 - - Meteor.defer -> - - message._id = Random.id() - RocketChat.callbacks.run 'afterReceiveMessage', message - -