Embedded RocketBot in ALL ROOMS

pull/240/head
Gabriel Engel 11 years ago
parent 81f54cc053
commit 73ea68cd47
  1. 1
      .meteor/packages
  2. 1
      .meteor/versions
  3. 16
      client/methods/sendMessage.coffee
  4. 2
      client/methods/typingStatus.coffee
  5. 38
      packages/rocketchat-hubot/hubot.coffee
  6. 32
      packages/rocketchat-lib/lib/settings.coffee
  7. 1
      packages/rocketchat-lib/package.js
  8. 71
      packages/rocketchat-lib/server/sendMessage.coffee
  9. 4
      server/methods/canAccessRoom.coffee
  10. 1
      server/startup/indexes.coffee

@ -13,7 +13,6 @@ email
http
jquery
less
markdown
meteor-platform
reactive-var
service-configuration

@ -58,7 +58,6 @@ less@1.0.14
livedata@1.0.13
localstorage@1.0.3
logging@1.0.7
markdown@1.0.4
matb33:collection-hooks@0.7.13
meteor@1.1.6
meteor-developer@1.1.3

@ -3,24 +3,14 @@ Meteor.methods
if not Meteor.userId()
throw new Meteor.Error 203, t('User_logged_out')
Tracker.nonreactive ->
if _.trim(message.msg) isnt ''
message.ts = new Date(Date.now() + TimeSync.serverOffset())
message.u =
_id: Meteor.userId()
username: Meteor.user().username
message.html = message.msg
if _.trim(message.html) isnt ''
message.html = _.escapeHTML message.html
message = RocketChat.callbacks.run 'beforeSaveMessage', message
message.html = message.html.replace /\n/gm, '<br/>'
ChatMessage.upsert
rid: message.rid
t: 't'
,
$set: message
$unset:
t: 1
expireAt: 1
ChatMessage.insert message

@ -11,8 +11,6 @@ Meteor.methods
if start
msgData =
'$set':
expireAt: moment().add(30, 'seconds').toDate()
'$setOnInsert':
msg: '...'
'u._id': Meteor.userId()

@ -12,8 +12,6 @@ path = Npm.require('path')
# Log messages?
DEBUG = true
rocketUser = Meteor.users.findOne({username: 'rocketbot'})
# Monkey-patch Hubot to support private messages
Hubot.Response::priv = (strings...) ->
@robot.adapter.priv @envelope, strings...
@ -36,6 +34,7 @@ class Robot extends Hubot.Robot
@topic = bind @topic
@error = bind @error
@catchAll = bind @catchAll
@user = Meteor.users.findOne {username: @name}, fields: username: 1
loadAdapter: -> false
hear: (regex, callback) -> super regex, Meteor.bindEnvironment callback
respond: (regex, callback) -> super regex, Meteor.bindEnvironment callback
@ -55,13 +54,8 @@ class RocketChatAdapter extends Hubot.Adapter
send: (envelope, strings...) ->
console.log envelope, strings
sendHelper @robot, envelope, strings, (string) =>
console.log "send #{envelope.user.rid}: #{string} (#{envelope.user.id})" if DEBUG
return @priv envelope, string if envelope.message.private
RocketChat.sendMessage rocketUser._id,
u:
username: "rocketbot"
msg: string
rid: envelope.user.rid
console.log "send #{envelope.room}: #{string} (#{envelope.user.id})" if DEBUG
RocketChat.sendMessage RocketBot.user, { msg: string }, { _id: envelope.room }
# Public: Raw method for sending emote data back to the chat source.
#
@ -74,8 +68,6 @@ class RocketChatAdapter extends Hubot.Adapter
console.log "emote #{envelope.rid}: #{string} (#{envelope.u.username})" if DEBUG
return @priv envelope, "*** #{string} ***" if envelope.message.private
Meteor.call "sendMessage",
u:
username: "rocketbot"
msg: string
rid: envelope.rid
action: true
@ -83,13 +75,13 @@ class RocketChatAdapter extends Hubot.Adapter
# Priv: our extension -- send a PM to user
priv: (envelope, strings...) ->
sendHelper @robot, envelope, strings, (string) ->
console.log "priv #{envelope.user.rid}: #{string} (#{envelope.user.id})" if DEBUG
console.log "priv #{envelope.room}: #{string} (#{envelope.user.id})" if DEBUG
Meteor.call "sendMessage",
u:
username: "rocketbot"
to: "#{envelope.user.id}"
msg: string
rid: envelope.user.rid
rid: envelope.room
# Public: Raw method for building a reply and sending it back to the chat
# source. Extend this.
@ -133,24 +125,16 @@ class RocketChatAdapter extends Hubot.Adapter
class RocketBotReceiver
constructor: (message) ->
if message.u.username is 'rocketbot'
return message
RocketBotUser = new Hubot.User(message.u.username, rid: message.rid)
RocketBotTextMessage = new Hubot.TextMessage(RocketBotUser, message.msg, message._id)
console.log {rid: message.rid, username:message.u.username}
# RocketChat.addUserToRoom rocketUser._id, {rid: message.rid, username:message.u.username}
RocketBot.adapter.receive RocketBotTextMessage
# console.log 'message: ', message if DEBUG
# console.log 'RocketBot: ', RocketBot if DEBUG
if message.u.username isnt RocketBot.name
RocketBotUser = new Hubot.User(message.u.username, room: message.rid)
RocketBotTextMessage = new Hubot.TextMessage(RocketBotUser, message.msg, message._id)
RocketBot.adapter.receive RocketBotTextMessage
return message
class HubotScripts
constructor: (robot) ->
hello = Npm.require 'hubot-scripts/src/scripts/hello.coffee'
hello robot
Npm.require('hubot-scripts/src/scripts/hello.coffee')(robot)
# # load all scripts in scripts/
# console.log path.resolve '.'
# scriptPath = path.resolve __dirname, 'scripts'

@ -0,0 +1,32 @@
###
# Setting hooks provide an easy way to add extra steps to common operations.
# @namespace RocketChat.settings
###
RocketChat.settings = {}
###
# Setting priorities
###
RocketChat.settings.priority =
HIGH: -1000
MEDIUM: 0
LOW: 1000
###
# Add a setting function to a hook
# @param {String} hook - The name of the hook
# @param {Function} setting - The setting function
###
RocketChat.settings.add = (hook, setting, priority) ->
# if setting array doesn't exist yet, initialize it
priority ?= RocketChat.settings.priority.MEDIUM
unless _.isNumber priority
priority = RocketChat.settings.priority.MEDIUM
setting.priority = priority
RocketChat.settings[hook] ?= []
RocketChat.settings[hook].push setting
return
RocketChat.settings.startup = (hook, setting, priority) ->
return

@ -17,6 +17,7 @@ Package.onUse(function(api) {
api.addFiles('lib/underscore.string.coffee', ['server', 'client']);
api.addFiles('lib/core.coffee', ['server', 'client']);
api.addFiles('lib/callbacks.coffee', ['server', 'client']);
api.addFiles('server/sendMessage.coffee', ['server']);
api.export(['RocketChat'], ['server', 'client']);
});

@ -1,28 +1,49 @@
RocketChat.sendMessage = (userId, message) ->
room = Meteor.call 'canAccessRoom', message.rid, userId
RocketChat.sendMessage = (user, message, room) ->
if not room
if not user or not message or not room._id
return false
console.log '[methods] sendMessage -> '.green, 'userId:', userId, 'arguments:', arguments
console.log '[functions] RocketChat.sendMessage -> '.green, 'arguments:', arguments
message.ts = new Date()
message.u = Meteor.users.findOne userId, fields: username: 1
message.u = _.pick user, ['_id','username']
message.rid = room._id
if urls = message.msg.match /([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\w]+)?\??([-\+=&!:;%@\/\.\,\w]+)?#?([\w]+)?)?/g
message.urls = urls.map (url) -> url: url
message = RocketChat.callbacks.run 'beforeSaveMessage', message
message._id = ChatMessage.insert message
###
Defer other updates as their return is not interesting to the user
###
###
Defer other updated as their return is not interesting to the user
Execute all callbacks
###
Meteor.defer ->
RocketChat.callbacks.run 'afterSaveMessage', message
###
Remove the typing record
###
Meteor.defer ->
ChatMessage.remove
rid: message.rid
t: 't'
'u._id': message.u._id
###
Update all the room activity tracker fields
###
Meteor.defer ->
###
Update all the room activity tracker fields
###
ChatRoom.update
# only subscriptions to the same room
rid: message.rid
@ -34,15 +55,20 @@ RocketChat.sendMessage = (userId, message) ->
$inc:
msgs: 1
###
Increment unread couter if direct messages
###
Meteor.defer ->
# increment unread couter if direct messages
if room.t is 'd'
if not room.t? or room.t is 'd'
###
Update the other subscriptions
###
ChatSubscription.update
# only subscriptions to the same room
rid: message.rid
# only direct messages subscriptions
t: 'd'
# not the msg owner
'u._id':
$ne: message.u._id
@ -100,25 +126,4 @@ RocketChat.sendMessage = (userId, message) ->
# make sure we alert all matching subscription
multi: true
###
Save the message. If there was already a typing record, update it.
###
message._id = ChatMessage.insert message
Meteor.defer ->
ChatMessage.remove
rid: message.rid
t: 't'
'u._id': message.u._id
Meteor.defer ->
RocketChat.callbacks.run 'afterSaveMessage', message
Meteor.methods
sendMessage: (message) ->
if not Meteor.userId()
throw new Meteor.Error('invalid-user', "[methods] sendMessage -> Invalid user")
RocketChat.sendMessage Meteor.userId(), message
return message

@ -10,7 +10,7 @@ Meteor.methods
unless rid
throw new Meteor.Error 'invalid-room', '[methods] canAccessRoom -> Cannot access empty room'
room = ChatRoom.findOne rid, { fields: { usernames: 1, t: 1 } }
room = ChatRoom.findOne rid, { fields: { usernames: 1, t: 1, name: 1 } }
if room.t is 'c'
canAccess = true
@ -20,4 +20,4 @@ Meteor.methods
if canAccess isnt true
return false
else
return room
return _.pick room, ['_id', 't', 'name']

@ -11,4 +11,5 @@ Meteor.startup ->
try ChatSubscription._ensureIndex { 'ts': 1 } catch e then console.log e
try ChatMessage._ensureIndex { 'rid': 1, 'ts': 1 } catch e then console.log e
try ChatMessage._ensureIndex { 'rid': 1, 't': 1, 'u._id': 1 } catch e then console.log e
try ChatMessage._ensureIndex { 'expireAt': 1 }, { expireAfterSeconds: 0 } catch e then console.log e

Loading…
Cancel
Save