Skip to content

Commit e099a27

Browse files
Packages: rocket:lib and rocket:me; Created 'sendMessage' hook
1 parent b250c51 commit e099a27

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed

.meteor/packages

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ rocket:file
5050
pauli:accounts-linkedin
5151
iframely:oembed
5252
pierreeric:rxfavico
53+
rocket:lib
54+
rocket:me

.meteor/versions

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ [email protected]
9494
9595
9696
97+
98+
9799
98100
99101
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# https://github.com/TelescopeJS/Telescope/blob/master/packages/telescope-lib/lib/callbacks.js
2+
3+
###
4+
# Callback hooks provide an easy way to add extra steps to common operations.
5+
# @namespace Rocket.callbacks
6+
###
7+
Rocket.callbacks = {}
8+
9+
###
10+
# Callback priorities
11+
###
12+
Rocket.callbacks.priority =
13+
HIGH: -1
14+
MEDIUM: 0
15+
LOW: 1
16+
17+
###
18+
# Add a callback function to a hook
19+
# @param {String} hook - The name of the hook
20+
# @param {Function} callback - The callback function
21+
###
22+
23+
Rocket.callbacks.add = (hook, callback, priority) ->
24+
# if callback array doesn't exist yet, initialize it
25+
priority ?= Rocket.callbacks.priority.MEDIUM
26+
unless _.isNumber priority
27+
priority = Rocket.callbacks.priority.MEDIUM
28+
callback.priority = priority
29+
Rocket.callbacks[hook] ?= []
30+
Rocket.callbacks[hook].push callback
31+
return
32+
33+
###
34+
# Remove a callback from a hook
35+
# @param {string} hook - The name of the hook
36+
# @param {string} functionName - The name of the function to remove
37+
###
38+
39+
Rocket.callbacks.remove = (hookName, callbackName) ->
40+
Rocket.callbacks[hookName] = _.reject Rocket.callbacks[hookName], (callback) ->
41+
callback.name is callbackName
42+
return
43+
44+
###
45+
# Successively run all of a hook's callbacks on an item
46+
# @param {String} hook - The name of the hook
47+
# @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
48+
# @param {Object} [constant] - An optional constant that will be passed along to each callback
49+
# @returns {Object} Returns the item after it's been through all the callbacks for this hook
50+
###
51+
52+
Rocket.callbacks.run = (hook, item, constant) ->
53+
callbacks = Rocket.callbacks[hook]
54+
if !!callbacks?.length
55+
# if the hook exists, and contains callbacks to run
56+
_.sortBy(callbacks, (callback) -> return callback.priority or Rocket.callbacks.priority.MEDIUM).reduce (result, callback) ->
57+
# console.log(callback.name);
58+
callback result, constant
59+
, item
60+
else
61+
# else, just return the item unchanged
62+
item
63+
64+
###
65+
# Successively run all of a hook's callbacks on an item, in async mode (only works on server)
66+
# @param {String} hook - The name of the hook
67+
# @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
68+
# @param {Object} [constant] - An optional constant that will be passed along to each callback
69+
###
70+
71+
Rocket.callbacks.runAsync = (hook, item, constant) ->
72+
callbacks = Rocket.callbacks[hook]
73+
if Meteor.isServer and !!callbacks?.length
74+
# use defer to avoid holding up client
75+
Meteor.defer ->
76+
# run all post submit server callbacks on post object successively
77+
_.sortBy(callbacks, (callback) -> return callback.priority or Rocket.callbacks.priority.MEDIUM).forEach (callback) ->
78+
# console.log(callback.name);
79+
callback item, constant
80+
return
81+
return
82+
else
83+
return item
84+
return

packages/rocket-lib/lib/core.coffee

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
###
2+
# Kick off the global namespace for Rocket.
3+
# @namespace Rocket
4+
###
5+
6+
Rocket = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This will add underscore.string methods to Underscore.js
2+
# except for include, contains, reverse and join that are
3+
# dropped because they collide with the functions already
4+
# defined by Underscore.js.
5+
_.mixin(s.exports())

packages/rocket-lib/package.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Package.describe({
2+
name: 'rocket:lib',
3+
version: '0.0.1',
4+
summary: 'Rocket libraries',
5+
git: ''
6+
});
7+
8+
Package.onUse(function(api) {
9+
api.versionsFrom('1.0');
10+
11+
api.use([
12+
'coffeescript',
13+
'underscore',
14+
'underscorestring:underscore.string'
15+
]);
16+
17+
api.addFiles('lib/underscore.string.coffee', 'server');
18+
api.addFiles('lib/core.coffee', 'server');
19+
api.addFiles('lib/callbacks.coffee', 'server');
20+
21+
api.export(['Rocket'], ['server']);
22+
});
23+
24+
Package.onTest(function(api) {
25+
26+
});

packages/rocket-me/me.coffee

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
###
2+
# Me is a named function that will replace /me commands
3+
# @param {Object} doc - The message object
4+
###
5+
6+
class Me
7+
constructor: (doc) ->
8+
# If message starts with /me, replace it for text formatting
9+
if doc.message.indexOf('/me') is 0
10+
doc.message = '######' + Meteor.user().name + doc.message.replace('/me', '')
11+
return doc
12+
13+
Rocket.callbacks.add 'sendMessage', Me

packages/rocket-me/package.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Package.describe({
2+
name: 'rocket:me',
3+
version: '0.0.1',
4+
summary: 'Message pre-processor that will translate /me commands',
5+
git: ''
6+
});
7+
8+
Package.onUse(function(api) {
9+
api.versionsFrom('1.0');
10+
11+
api.use([
12+
'coffeescript',
13+
14+
]);
15+
16+
api.addFiles('me.coffee', 'server');
17+
});
18+
19+
Package.onTest(function(api) {
20+
21+
});

server/methods/sendMessage.coffee

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Meteor.methods
4545
if mentions.length is 0
4646
mentions = undefined
4747

48+
msg = Rocket.callbacks.run 'sendMessage', msg
49+
4850
ChatMessage.upsert messageFilter,
4951
$set:
5052
'u._id': Meteor.userId()

0 commit comments

Comments
 (0)