forked from jvimedia/AppDotNetAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
404 lines (376 loc) · 13.2 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
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
/**
* Based losely off ohe
*/
var path = require('path');
var nconf = require('nconf');
// Look for a config file
var config_path = path.join(__dirname, '/config.json');
// and a model file
var config_model_path = path.join(__dirname, '/config.models.json');
nconf.argv().env('__').file({file: config_path}).file('model', {file: config_model_path});
/** set up express framework */
var express = require('express');
var app = express();
var Cookies = require( "cookies" );
var bodyParser = require('body-parser');
/** get file io imported */
var fs = require('fs');
/** ohe libs */
/** for getting app_token */
var auth = require('./ohe/auth');
/** for building app stream */
var adnstream = require('./ohe/adnstream');
/** pull configuration from config into variables */
var apiroot = nconf.get('uplink:apiroot') || 'https://api.app.net';
var upstream_client_id=nconf.get('uplink:client_id') || 'NotSet';
var upstream_client_secret=nconf.get('uplink:client_secret') || 'NotSet';
var webport = nconf.get('web:port') || 7070;
var api_client_id= nconf.get('web:api_client_id') || '';
var auth_base=nconf.get('auth:base') || 'https://account.app.net/oauth/';
var auth_client_id=nconf.get('auth:client_id') || upstream_client_id;
var auth_client_secret=nconf.get('auth:client_secret') || upstream_client_secret;
// Todo: make these modular load modules from config file
// Todo: general parameters
// Todo: expiration models and configuration
// Todo: end error object
var proxy = null
if (upstream_client_id!='NotSet') {
proxy = require('./dataaccess.proxy.js');
}
var db=require('./dataaccess.caminte.js');
var cache=require('./dataaccess.base.js');
var dispatcher=require('./dispatcher.js');
var dialects=[];
// Todo: message queue
// initialize chain
if (proxy) db.next=proxy;
cache.next=db;
dispatcher.cache=cache;
dispatcher.notsilent=!(nconf.get('uplink:silent') || false);
dispatcher.appConfig = {
provider: nconf.get('pomf:provider') || 'pomf.cat',
provider_url: nconf.get('pomf:provider_url') || 'https://pomf.cat/',
}
console.log('configuring app as', dispatcher.appConfig)
// app.net defaults
dispatcher.config=nconf.get('dataModel:config') || {
"text": {
"uri_template_length": {
"post_id": 9,
"message_id": 12
}
},
"user": {
"annotation_max_bytes": 8192,
"text_max_length": 256
},
"file": {
"annotation_max_bytes": 8192
},
"post": {
"annotation_max_bytes": 8192,
"text_max_length": 256
},
"message": {
"annotation_max_bytes": 8192,
"text_max_length": 2048
},
"channel": {
"annotation_max_bytes": 8192
}
};
console.log('configuring adn settings as', dispatcher.config)
if (proxy) {
// set up proxy object
proxy.apiroot=apiroot;
proxy.dispatcher=dispatcher; // upload dispatcher
}
/** set up query parameters */
// all Boolean (0 or 1) and prefixed by include_
var generalParams=['muted','deleted','directed_posts','machine','starred_by','reposters','annotations','post_annotations','user_annotations','html','marker','read','recent_messages','message_annotations','inactive','incomplete','private','file_annotations'];
// Stream Faceting allows you to filter and query a user's personalized stream or unified stream with an interface similar to our Post Search API. If you use stream faceting, the API will only return recent posts in a user's stream.
// Boolean (0 or 1)
var streamFacetParams=['has_oembed_photo'];
var pageParams=['since_id','before_id','count','last_read','last_read_inclusive','marker','marker_inclusive'];
var channelParams=['channel_types'];
var fileParams=['file_types'];
/** need this for POST parsing */
// heard this writes to /tmp and doesn't scale.. need to confirm if current versions have this problem
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.all('/*', function(req, res, next){
origin = req.get('Origin') || '*';
res.set('Access-Control-Allow-Origin', origin);
res.set('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
res.set('Access-Control-Expose-Headers', 'Content-Length');
res.set('Access-Control-Allow-Credentials', 'true');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization'); // add the list of headers your site allows.
if ('OPTIONS' == req.method) {
var ts=new Date().getTime();
console.log('app.js - OPTIONS requests served in', (ts-res.start)+'ms');
return res.sendStatus(200);
}
next();
});
/**
* Set up middleware to check for prettyPrint
* This is run on each incoming request
*/
app.use(function(req, res, next) {
res.start=new Date().getTime();
res.path=req.path;
//console.dir(req); // super express debug
var token=null;
if (req.get('Authorization') || req.query.access_token) {
if (req.query.access_token) {
//console.log('app.js - Authquery',req.query.access_token);
req.token=req.query.access_token;
// probably should validate the token here
/*
console.log('app.js - getUserClientByToken',req.token);
dispatcher.getUserClientByToken(req.token, function(usertoken, err) {
if (usertoken==null) {
console.log('Invalid query token (Server restarted on clients?...): '+req.query.access_token+' err: '+err);
req.token=null;
if (req.get('Authorization')) {
//console.log('Authorization: '+req.get('Authorization'));
// Authorization Bearer <YOUR ACCESS TOKEN>
req.token=req.get('Authorization').replace('Bearer ', '');
}
} else {
token=usertoken;
console.log('token marked valid');
}
});
*/
} else {
//console.log('authheader');
if (req.get('Authorization')) {
//console.log('Authorization: '+req.get('Authorization'));
// Authorization Bearer <YOUR ACCESS TOKEN>
req.token=req.get('Authorization').replace('Bearer ', '');
/*
dispatcher.getUserClientByToken(req.token, function(usertoken, err) {
if (usertoken==null) {
console.log('Invalid header token (Server restarted on clients?...): '+req.token);
req.token=null;
} else {
token=usertoken;
}
});
*/
}
}
}
// debug incoming requests
if (dispatcher.notsilent && upstream_client_id!='NotSet') {
process.stdout.write("\n");
}
// not any of these
if (!(req.path.match(/\/channels/) || req.path.match(/\/posts\/\d+\/replies/) ||
req.path.match(/users\/@[A-z]+\/posts/))) {
console.log('Request for '+req.path);
}
// set defaults
// Defaults to false except when you specifically request a Post from a muted user or when you specifically request a muted user's stream.
var generalParams={};
generalParams.muted=false;
generalParams.deleted=true;
// Defaults to false for "My Stream" and true everywhere else.
generalParams.directed_posts=true;
generalParams.machine=false;
generalParams.starred_by=false;
generalParams.reposters=false;
// map to bool but handle '0' and '1' just like 'true' and 'false'
function stringToBool(str) {
if (str === '0' || str === 'false' || str === 'null' || str === 'undefined') {
return false;
}
// handle ints and bool types too
return str?true:false;
}
generalParams.annotations=false;
if (req.query.include_annotations!==undefined) {
//console.log("Overriding include_annotations to", req.query.include_annotations);
generalParams.annotations=stringToBool(req.query.include_annotations);
}
generalParams.post_annotations=false;
if (req.query.include_post_annotations!==undefined) {
console.log("Overriding include_post_annotations to", req.query.include_post_annotations);
generalParams.post_annotations=stringToBool(req.query.include_post_annotations);
}
generalParams.user_annotations=false;
if (req.query.include_user_annotations!==undefined) {
console.log("Overriding include_user_annotations to", req.query.include_user_annotations);
generalParams.user_annotations=stringToBool(req.query.include_user_annotations);
}
generalParams.html=true;
// channel
generalParams.marker=false;
generalParams.read=true;
generalParams.recent_messages=false;
generalParams.message_annotations=false;
generalParams.inactive=false;
// file
generalParams.incomplete=true;
generalParams.private=true;
generalParams.file_annotations=false;
//
var channelParams={};
channelParams.types='';
if (req.query.channel_types) {
console.log("Overriding channel_types to "+req.query.channel_types);
channelParams.types=req.query.channel_types;
}
var fileParams={};
fileParams.types='';
if (req.query.file_types) {
console.log("Overriding file_types to "+req.query.file_types);
fileParams.types=req.query.channel_types;
}
var stremFacetParams={};
stremFacetParams.has_oembed_photo=false;
var pageParams={};
pageParams.since_id=false;
if (req.query.since_id) {
//console.log("Overriding since_id to "+req.query.since_id);
pageParams.since_id=parseInt(req.query.since_id);
}
pageParams.before_id=false;
if (req.query.before_id) {
console.log("Overriding before_id to "+req.query.before_id);
pageParams.before_id=parseInt(req.query.before_id);
}
pageParams.count=20;
if (req.query.count) {
console.log("Overriding count to "+req.query.count);
pageParams.count=Math.min(Math.max(req.query.count, -200), 200);
}
// stream marker supported endpoints only
pageParams.last_read=false;
pageParams.last_read_inclusive=false;
pageParams.last_marker=false;
pageParams.last_marker_inclusive=false;
// put objects into request
req.apiParams={
generalParams: generalParams,
channelParams: channelParams,
fileParams: fileParams,
stremFacetParams: stremFacetParams,
pageParams: pageParams,
tokenobj: token,
token: req.token,
}
// configure response
res.prettyPrint=req.get('X-ADN-Pretty-JSON') || 0;
// non-ADN spec, ryantharp hack
if (req.query.prettyPrint) {
res.prettyPrint=1;
}
res.JSONP=req.query.callback || '';
req.cookies = new Cookies( req, res);
next();
});
/**
* support both styles of calling API
*/
app.apiroot=apiroot;
app.dispatcher=dispatcher;
/* load dialects from config */
var mounts=nconf.get('web:mounts') || [
{
"destination": "",
"dialect": "appdotnet_official"
},
{
"destination": "/stream/0",
"dialect": "appdotnet_official"
}
];
var dialects={};
for(var i in mounts) {
var mount=mounts[i];
if (dialects[mount.dialect]==undefined) {
// load dialect
console.log("Loading dialect "+mount.dialect);
dialects[mount.dialect]=require('./dialect.'+mount.dialect+'.js');
}
console.log('Mounting '+mount.dialect+' at '+mount.destination+'/');
dialects[mount.dialect](app, mount.destination);
}
// config app (express framework)
app.upstream_client_id=upstream_client_id;
app.upstream_client_secret=upstream_client_secret;
app.auth_client_id=auth_client_id;
app.auth_client_secret=auth_client_secret;
app.webport=webport;
app.apiroot=apiroot;
// only can proxy if we're set up as a client or an auth_base not app.net
console.log('upstream_client_id', upstream_client_id)
if (upstream_client_id!='NotSet' || auth_base!='https://account.app.net/oauth/') {
var oauthproxy=require('./routes.oauth.proxy.js');
oauthproxy.auth_base=auth_base;
oauthproxy.setupoauthroutes(app, cache);
} else {
// Not Cryptographically safe
function generateToken(string_length) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var randomstring = '';
for (var x=0;x<string_length;x++) {
var letterOrNumber = Math.floor(Math.random() * 2);
if (letterOrNumber == 0) {
var newNum = Math.floor(Math.random() * 9);
randomstring += newNum;
} else {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum+1);
}
}
return randomstring;
}
app.get('/oauth/authenticate', function(req, resp) {
resp.redirect(req.query.redirect_uri+'#access_token='+generateToken());
});
}
app.get('/signup', function(req, resp) {
fs.readFile(__dirname+'/templates/signup.html', function(err, data) {
if (err) {
throw err;
}
resp.send(data.toString());
});
});
/** include homepage route */
app.get('/', function(req, resp) {
fs.readFile(__dirname+'/templates/index.html', function(err, data) {
if (err) {
throw err;
}
var body=data.toString();
body=body.replace('{api_client_id}', api_client_id);
body=body.replace('{uplink_client_id}', upstream_client_id);
resp.send(body);
});
});
/**
* Launch the server!
*/
app.listen(webport);
/** set up upstream */
if (upstream_client_id!='NotSet') {
// send events into "dispatcher"
var stream_router = require('./ohe/streamrouter').create_router(app, dispatcher);
// this blocks the API until connected =\
auth.get_app_token(function (token) {
stream_router.stream(token);
});
} else {
console.log("uplink:client_id not set in config. No uplink set up!");
}
// if full data store
// check caches/dates since we were offline for any missing data
// hoover users (from our last max ID to appstream start (dispatcher.first_post))
// hoover posts (from our last max ID to appstream start (dispatcher.first_post))
// hoover stars for all users in db