forked from oxen-io/session-file-server-old
/
streams.js
410 lines (401 loc) · 16.7 KB
/
streams.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
var redis = require('redis');
var nconf = require('nconf');
//require('longjohn')
module.exports = {
cache: null,
// we still need to be able to communicate pumps
dispatcher: null,
redisClient: null,
redisListenClient: null,
reloadTimer: null,
openPings: {},
// needs to happen after cache is set
init: function(redisClientOptions) {
// FIXME: support
this.redisClient = redis.createClient(redisClientOptions);
this.redisListenClient = redis.createClient(redisClientOptions);
var ref = this;
ref.redisListenClient.subscribe("AppDotNetWS"); // WS to altapi
ref.redisListenClient.subscribe("AppDotNetAPI"); // altapi to altpi communication
ref.redisListenClient.on("message", ref.handleEvent);
// set up initial listeners (wait for the camintejs connect)
setTimeout(function() {
ref.loadSubscriptions();
}, 1000);
/*
setInterval(function() {
// just incase another thread updates
// maybe check database count
// or we can have a scoreboard
// we just plug into the redis bus
ref.loadSubscriptions();
}, 30*1000)
*/
},
checkConnections: function() {
var ref = this;
this.cache.getAllUserStreams(function(res) {
// send pings
var wrap = {
meta: {
//connection_id: stream.connection_id,
type: 'ping'
},
data: { id: Date.now() }
}
for(var i in res.userStreams) {
var stream = res.userStreams[i];
ref.checkConnection(stream, wrap);
}
})
},
expireConnections: function() {
var ref = this;
this.cache.getAllUserStreams(function(res) {
for(var i in res.userStreams) {
var stream = res.userStreams[i];
ref.checkExpire(stream);
}
})
},
loadSubscriptions: function() {
//console.log('streams::loadSubscriptions - start');
// query user_streamsubscriptions for all listeners
// update our dispatcher data structure that our general pump uses
// we need to make a key, so it's easy for pump to look up if there's any connections we need to send to
// we need to do the heavy lifting here..
var ref = this;
this.cache.getAllUserStreams(function(res) {
//console.log('streams::loadSubscriptions - res', res);
var tokenLookup = {};
for(var i in res.tokens) {
var tokenObj = res.tokens[i];
tokenLookup[tokenObj.id] = tokenObj;
}
var subLookup = {};
for(var i in res.subs) {
var sub = res.subs[i];
if (subLookup[sub.user_stream_id] === undefined) {
subLookup[sub.user_stream_id] = [];
}
subLookup[sub.user_stream_id].push(sub);
}
ref.dispatcher.pumps = {};
console.log('streams::loadSubscriptions -', res.userStreams.length, 'streams');
for(var i in res.userStreams) {
var stream = res.userStreams[i];
//console.log('streams::loadSubscriptions -', subLookup[stream.id].length, 'subcriptions for', stream.id, 'streamid');
// FIXME: remove any existing listeners for this connection
// FIXME: set up new listeners for this connection
// send a ping too
for(var j in subLookup[stream.id]) {
var sub = subLookup[stream.id][j];
// threaded doesn't generate multiple events
// because the creation only happens on one thread
switch(sub.stream) {
case '/posts/stream':
// we can listen to user posts
// so lets get a list of everyone we're following
// FIXME: update this list when we follow/unfollow a user
// subscribe this connection_string_id to all user_posts we need
ref.cache.getFollowing(stream.userid, {}, function(followings, err) {
console.log('streams::loadSubscriptions -', stream.userid, 'is following', followings.length, 'users', stream.connection_id);
for(var k in followings) {
var following = followings[k];
var key = 'user.'+following.followsid+'.post';
if (ref.dispatcher.pumps[key] === undefined) {
ref.dispatcher.pumps[key] = [];
}
ref.dispatcher.pumps[key].push(stream.connection_id);
}
});
break;
case '/channels':
// get a list of channels you're subbged to
ref.cache.getUserSubscriptions(stream.userid, {}, function(subscriptions, err) {
for(var k in subscriptions) {
var sub = subscriptions[k];
var key = 'channel.'+sub.channelid;
if (ref.dispatcher.pumps[key] === undefined) {
ref.dispatcher.pumps[key] = [];
}
ref.dispatcher.pumps[key].push(stream.connection_id);
}
});
break;
case '/users/me/posts':
var key = 'user.'+stream.userid+'.post';
if (ref.dispatcher.pumps[key] === undefined) {
ref.dispatcher.pumps[key] = [];
}
ref.dispatcher.pumps[key].push(stream.connection_id);
break;
default:
if (sub.stream.match(/^\/channels\/\d+\/messages/)) {
var match = sub.stream.match(/^\/channels\/(\d+)\/messages/);
var channelid = match[1];
//console.log('streams::loadSubscriptions - \/channels\/\d+\/messages channelid', channelid);
var key = 'channel.'+channelid+'.message';
if (ref.dispatcher.pumps[key] === undefined) {
ref.dispatcher.pumps[key] = [];
}
ref.dispatcher.pumps[key].push(stream.connection_id);
} else {
console.log('streams::loadSubscriptions - implement me', sub.stream);
}
break;
}
}
}
// , ref.dispatcher.pumps
var keys = []
for(var key in ref.dispatcher.pumps) {
keys.push([key, ref.dispatcher.pumps[key].length])
}
console.log('streams::loadSubscriptions - final keys', keys.length);
ref.checkConnections();
ref.expireConnections();
});
},
loadSubscription: function(stream) {
},
checkConnection: function(stream, wrap) {
var ref=this;
wrap.meta.connection_id = stream.connection_id;
if (ref.openPings[stream.connection_id] === undefined) {
ref.openPings[stream.connection_id] = []
}
ref.openPings[stream.connection_id].push(wrap.data.id)
var idx = ref.openPings[stream.connection_id].indexOf(wrap.data.id)
ref.handlePublish(stream.connection_id, wrap);
setTimeout(function() {
//console.log('checking pings for', stream.connection_id, ref.openPings[stream.connection_id].length);
var cur = ref.openPings[stream.connection_id].indexOf(wrap.data.id);
if (cur != -1) {
//console.log(wrap.data.id, 'ping is still outstanding for', stream.connection_id, 'state', stream.connection_closed_at);
if (stream.connection_closed_at == null) {
console.log('streams::checkConnections - marking offline', stream.connection_id);
module.exports.cache.userStreamUpdate(stream.connection_id, {
connection_closed_at: new Date()
}, function(userStream, err) {
//console.log('userStream updated')
})
}
} else {
// if has connection_closed_at, we need to clear it
//console.log('streams::checkConnections - ', stream.connection_id, 'state', stream.connection_closed_at);
if (stream.connection_closed_at != null) {
console.log('streams::checkConnections - marking online', stream.connection_id);
module.exports.cache.userStreamUpdate(stream.connection_id, {
connection_closed_at: null
}, function(userStream, err) {
//console.log('userStream updated')
})
}
}
}, 30*1000);
},
checkExpire: function(stream) {
// ignore if no auto or online
if (!stream.auto_delete || stream.connection_closed_at == null) {
//console.log('stream::checkExpire - skipping, AD:', stream.auto_delete, 'state', stream.connection_closed_at);
return;
}
var now = Date.now();
var thenD = new Date(stream.connection_closed_at);
var then = thenD.getTime();
var diff = now - then;
//console.log('streams::checkExpire - diff', diff, 'ms')
if (diff < 10*60*1000) {
return;
}
console.log('streams::checkExpire - disconnected over 10mins ago', stream.connection_id, '@', stream.id);
this.cache.deleteUserStream(stream.id);
},
handlePublish: function(connectionId, data) {
//console.log('streams::handlePublish - start', connectionId, data);
//try {
this.redisClient.publish(connectionId, JSON.stringify(data));
/*
} catch(e) {
console.log('cant publish', e)
} */
},
pendingReloads: 0,
handleEvent: function(channel, message) {
// these could be HTTP requests
//console.log('streams::handleEvent - chan', channel, 'msg', message);
var ref = module.exports
if (channel == 'AppDotNetAPI') {
var pkt = JSON.parse(message)
//console.log('streams::handleEvent - AppDotNetAPI - message', pkt)
if (pkt.act == 'sub') {
// uid,sid,sub {
// user_stream_id
// stream
// params
// id
// }
// time delay this for Xms just incase more come in
ref.pendingReloads++
//console.log('streams::handleEvent - AppDotNetAPI - ask for reload')
// cancel any pending timer, add another Xms
if (ref.reloadTimer !== null) clearTimeout(ref.reloadTimer)
ref.reloadTimer = setTimeout(function() {
console.log('streams::handleEvent - AppDotNetAPI - reloading, estimated new subs:', ref.pendingReloads)
ref.loadSubscriptions()
ref.pendingReloads = 0
}, 100)
}
return
}
if (message.match(/^disconnect_/)) {
var parts = message.split(/_/);
var connectionId = parts[1];
//console.log(connectionId, 'disconnected, write to cache');
module.exports.cache.userStreamUpdate(connectionId, {
connection_closed_at: new Date()
}, function() {
//console.log('userStream updated')
})
} else if (message.match(/^pong_/)){
var parts = message.split(/_/);
var connectionId = parts[1];
var timestamp = parts[2];
if (!module.exports.openPings[connectionId]) {
// not really an error, just means we restarted...
//console.log('streams::handleEvent - cant find any open pings for', connectionId);
return;
}
var idx = module.exports.openPings[connectionId].indexOf(timestamp);
if (idx) {
module.exports.openPings[connectionId].splice(idx, 1);
//console.log('streams::handleEvent -', connectionId, 'is connected');
} else {
console.error('streams::handleEvent - no openping for', timestamp, 'for', connectionId, 'we have', module.exports.openPings[connectionId]);
}
} else {
console.error('streams::handleEvent - chan', channel, 'msg', message);
}
},
// called in middleware
handleSubscription: function(req, res) {
var connId = req.query.connection_id;
var accessToken = req.token;
var connToken = null;
var ref = this;
function sendErrResponse(code, err) {
var resObj={
"meta": {
"code": code,
"error_message": err
}
};
res.status(code).type('application/json').send(JSON.stringify(resObj));
}
//console.log('streams::handleSubscription - handling',req.path);
this.redisClient.get("token_"+connId, function(err, reply) {
if (!reply) {
sendErrResponse(500, "no such connection")
console.log('streams::handleSubscription - reply is empty', reply);
return;
}
connToken = reply.toString();
//console.log('connToken', connToken);
if (connToken != accessToken) {
console.log('streams::handleSubscription - connToken does not match accessToken', connToken, accessToken);
sendErrResponse(401, "token mismatch")
return;
}
ref.redisClient.get("autoDelete_"+connId, function(err, reply) {
var autoDel = reply.toString(); // FIXME: look up in redis
// convert string to token numeric id and get user id
ref.cache.getAPIUserToken(accessToken, function(tokenObj, err) {
//console.log('app::handleSubscription - tokenObj', tokenObj);
if (err || !tokenObj) {
sendErrResponse(401, "invalid token")
console.log('streams::handleSubscription - token not found', accessToken, err);
return;
}
ref.cache.findOrCreateUserStream(connId, tokenObj.id, tokenObj.userid, autoDel, function(userStream, err) {
if (err || !userStream) {
sendErrResponse(500, "cant create userStream")
console.log('streams::handleSubscription - userStream could not be created', connId, tokenObj.id, tokenObj.userid, autoDel, err);
return;
}
//console.log('app::handleSubscription - userStream', userStream);
//console.log('app::handleSubscription - userStream.id', userStream.id);
// FIXME: apply/adjust to all mount points
// update auto_delete state
var userStreamSubscriptionEndpoints = {
//'/users/me/following': {},
//'/users/me/followers': {},
'/users/me/posts': {},
//'/users/me/mentions': {},
'/posts/stream': {},
//'/posts/stream/unified': {},
'/channels': {}, // (includes new messages for channels you're subscribed to)
//'/users/me/files': {},
//'/token': {}, // (includes updates for both the token and the user objects of the current user)
//'/posts/:post_id/replies': {},
//'/channels/:post_id/subscribers': {},
'/channels/:post_id/messages': {},
}
var matchedPoint = req.path
/*
if (req.path.match(/^\/posts\/\d+\/replies/)) {
matchedPoint = '/posts/:post_id/replies'
} else
if (req.path.match(/^\/channels\/\d+\/subscribers/)) {
matchedPoint = '/channels/:post_id/subscribers'
} else
*/
if (req.path.match(/^\/channels\/\d+\/messages/)) {
matchedPoint = '/channels/:post_id/messages'
}
var validEndpoint = userStreamSubscriptionEndpoints[matchedPoint]
if (!validEndpoint) {
console.log('app::handleSubscription - endpoint', matchedPoint, 'is not subscribable/implemented yet');
sendErrResponse(500, "unsubscribable endpoint")
return;
}
//console.log('app::handleSubscription - checking for subscribable endpoints', req.path, userStream);
// check user_streamsubscriptions
// make sure user_stream_id, stream doesn't already exist
// if exists make sure params are updated
// otherwise create it
//console.log('app::handleSubscription - streamId', userStream.id);
ref.cache.findOrCreateUserSubscription(userStream.id, req.path, JSON.stringify(req.apiParams), function(subscription, err) {
if (err || !subscription) {
console.log('streams::handleSubscription - subscription could not be created', userStream.id, req.path, err);
sendErrResponse(500, "cant create subscription")
return;
}
if (subscription.stream != req.path) {
console.log('streams::handleSubscription - subscription could not be created, path doesnt match', subscription.stream, '!=', req.path);
}
//console.log('streams::handleSubscription - connection', connId, 'endpoint', req.path, 'subscription', subscription)
// poke the dispatch engine to reload state from db
//ref.loadSubscriptions();
//console.log('streams::handleSubscription - publish');
var data = {
act: "sub",
uid: tokenObj.userid,
sid: userStream.id,
sub: subscription
}
ref.redisClient.publish('AppDotNetAPI', JSON.stringify(data));
var resObj={
"meta": {
"code": 200,
"error_message": "Implemented"
}
};
res.status(200).type('application/json').send(JSON.stringify(resObj));
})
});
});
});
})
}
};