Packages: rocket:lib and rocket:me; Created 'sendMessage' hook

pull/155/head
Marcelo Schmidt 10 years ago
parent b250c51267
commit e099a270b3
  1. 2
      .meteor/packages
  2. 2
      .meteor/versions
  3. 84
      packages/rocket-lib/lib/callbacks.coffee
  4. 6
      packages/rocket-lib/lib/core.coffee
  5. 5
      packages/rocket-lib/lib/underscore.string.coffee
  6. 26
      packages/rocket-lib/package.js
  7. 13
      packages/rocket-me/me.coffee
  8. 21
      packages/rocket-me/package.js
  9. 2
      server/methods/sendMessage.coffee

@ -50,3 +50,5 @@ rocket:file
pauli:accounts-linkedin
iframely:oembed
pierreeric:rxfavico
rocket:lib
rocket:me

@ -94,6 +94,8 @@ reactive-var@1.0.5
reload@1.1.3
retry@1.0.3
rocket:file@0.0.1
rocket:lib@0.0.1
rocket:me@0.0.1
routepolicy@1.0.5
service-configuration@1.0.4
session@1.1.0

@ -0,0 +1,84 @@
# https://github.com/TelescopeJS/Telescope/blob/master/packages/telescope-lib/lib/callbacks.js
###
# Callback hooks provide an easy way to add extra steps to common operations.
# @namespace Rocket.callbacks
###
Rocket.callbacks = {}
###
# Callback priorities
###
Rocket.callbacks.priority =
HIGH: -1
MEDIUM: 0
LOW: 1
###
# Add a callback function to a hook
# @param {String} hook - The name of the hook
# @param {Function} callback - The callback function
###
Rocket.callbacks.add = (hook, callback, priority) ->
# if callback array doesn't exist yet, initialize it
priority ?= Rocket.callbacks.priority.MEDIUM
unless _.isNumber priority
priority = Rocket.callbacks.priority.MEDIUM
callback.priority = priority
Rocket.callbacks[hook] ?= []
Rocket.callbacks[hook].push callback
return
###
# Remove a callback from a hook
# @param {string} hook - The name of the hook
# @param {string} functionName - The name of the function to remove
###
Rocket.callbacks.remove = (hookName, callbackName) ->
Rocket.callbacks[hookName] = _.reject Rocket.callbacks[hookName], (callback) ->
callback.name is callbackName
return
###
# Successively run all of a hook's callbacks on an item
# @param {String} hook - The name of the hook
# @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
# @param {Object} [constant] - An optional constant that will be passed along to each callback
# @returns {Object} Returns the item after it's been through all the callbacks for this hook
###
Rocket.callbacks.run = (hook, item, constant) ->
callbacks = Rocket.callbacks[hook]
if !!callbacks?.length
# if the hook exists, and contains callbacks to run
_.sortBy(callbacks, (callback) -> return callback.priority or Rocket.callbacks.priority.MEDIUM).reduce (result, callback) ->
# console.log(callback.name);
callback result, constant
, item
else
# else, just return the item unchanged
item
###
# Successively run all of a hook's callbacks on an item, in async mode (only works on server)
# @param {String} hook - The name of the hook
# @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
# @param {Object} [constant] - An optional constant that will be passed along to each callback
###
Rocket.callbacks.runAsync = (hook, item, constant) ->
callbacks = Rocket.callbacks[hook]
if Meteor.isServer and !!callbacks?.length
# use defer to avoid holding up client
Meteor.defer ->
# run all post submit server callbacks on post object successively
_.sortBy(callbacks, (callback) -> return callback.priority or Rocket.callbacks.priority.MEDIUM).forEach (callback) ->
# console.log(callback.name);
callback item, constant
return
return
else
return item
return

@ -0,0 +1,6 @@
###
# Kick off the global namespace for Rocket.
# @namespace Rocket
###
Rocket = {}

@ -0,0 +1,5 @@
# This will add underscore.string methods to Underscore.js
# except for include, contains, reverse and join that are
# dropped because they collide with the functions already
# defined by Underscore.js.
_.mixin(s.exports())

@ -0,0 +1,26 @@
Package.describe({
name: 'rocket:lib',
version: '0.0.1',
summary: 'Rocket libraries',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'coffeescript',
'underscore',
'underscorestring:underscore.string'
]);
api.addFiles('lib/underscore.string.coffee', 'server');
api.addFiles('lib/core.coffee', 'server');
api.addFiles('lib/callbacks.coffee', 'server');
api.export(['Rocket'], ['server']);
});
Package.onTest(function(api) {
});

@ -0,0 +1,13 @@
###
# Me is a named function that will replace /me commands
# @param {Object} doc - The message object
###
class Me
constructor: (doc) ->
# If message starts with /me, replace it for text formatting
if doc.message.indexOf('/me') is 0
doc.message = '######' + Meteor.user().name + doc.message.replace('/me', '')
return doc
Rocket.callbacks.add 'sendMessage', Me

@ -0,0 +1,21 @@
Package.describe({
name: 'rocket:me',
version: '0.0.1',
summary: 'Message pre-processor that will translate /me commands',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'coffeescript',
'rocket:lib@0.0.1'
]);
api.addFiles('me.coffee', 'server');
});
Package.onTest(function(api) {
});

@ -45,6 +45,8 @@ Meteor.methods
if mentions.length is 0
mentions = undefined
msg = Rocket.callbacks.run 'sendMessage', msg
ChatMessage.upsert messageFilter,
$set:
'u._id': Meteor.userId()

Loading…
Cancel
Save