-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
91 lines (78 loc) · 2.57 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
load('vertx.js');
// Web Server Configuration
var webServerConf = {
port: 8080,
host: 'localhost',
bridge: true,
inbound_permitted: [
{ address: 'vertx.basicauthmanager.login' },
{ address: 'vertx.mongopersistor' },
{ address: 'messages.incoming' },
{ address: 'messages.primestream' },
],
outbound_permitted: [
{ address_re: 'messages.outgoing\\..+' }
]};
// Deploy Web Server Module
vertx.deployModule('vertx.web-server-v1.0', webServerConf);
// MongoDB Persistor Configuration
var mongoConf = {
db_name: 'twitx'
}
// Deploy MongoDB Persistor Module
vertx.deployModule('vertx.mongo-persistor-v1.1', mongoConf);
// Authentication Manager Configuration
var securityConf = {
user_collection: 'profiles'
};
// Deploy Authentication Manager Module
vertx.deployModule('vertx.auth-mgr-v1.0', securityConf);
var eb = vertx.eventBus;
// Handle incoming messages from the browser...
var messageHandler = function(message) {
message.timestamp = new Date();
eb.send('vertx.mongopersistor', { action: 'save',
collection: 'messages',
document: message });
eb.send('vertx.mongopersistor', { action : 'find',
collection: 'profiles',
matcher: { following: message.username }
},
function (reply) {
// This should really be on a worker...refactor!
for (var i = 0; i < reply.results.length; i++) {
// Stream the message to each person that's following me...
eb.publish('messages.outgoing.'+reply.results[i].username, message);
}
// Send the message to me as well...
eb.publish('messages.outgoing.'+message.username, message);
});
}
eb.registerHandler('messages.incoming', messageHandler);
function loadUserProfile(username, callback) {
eb.send('vertx.mongopersistor',
{ action: 'findone',
collection: 'profiles',
matcher: { username: username }
},
callback);
}
// Prime the message stream after loading user profiles...
eb.registerHandler('messages.primestream', function(message, replier) {
loadUserProfile(message.username, function(reply) {
if (reply.status === 'ok') {
eb.send('vertx.mongopersistor',
{ action: 'find',
collection: 'messages',
matcher: {
username: { "$in": reply.result.following }
},
sort: {timestamp: -1},
limit: 10 },
function(queryReply) {
eb.publish('messages.outgoing.'+reply.result.username, queryReply)
});
replier(reply);
}
});
});