-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
383 lines (330 loc) · 10.5 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
/**
* InfraNodus is a non-linear reading device.
*
* Inspired from ThisIsLike.Com and KnowNodes (now Rhizi) we
* want to create a basic interface for rich edge annotation of graphs
* for collaborative use.
*
* This open source, free software is available under MIT license.
* It is provided as is, with no guarantees and no liabilities.
*
* You are very welcome to reuse this code if you keep this notice.
*
* Written by Dmitry Paranyushkin | Nodus Labs and hopefully you also...
* www.noduslabs.com | info AT noduslabs DOT com
*
*
* In some parts the code from the book "Node.js in Action" is used,
* (c) 2014 Manning Publications Co.
* by Marc Harter, T.J. Holowaychuk, Nathan Rajlich
* Any source code files provided as a supplement to the book are freely
* available to the public for download. Reuse of the code is permitted,
* in whole or in part, including the creation of derivative works, provided
* that you acknowledge that you are using it and identify the source:
* title, publisher and year.
*/
var api = require('./routes/api')
var api2 = require('./routes/api2')
var express = require('express')
var oauths = require('./routes/evernote')
var entries = require('./routes/entries')
var Entry = require('./lib/entry')
var page = require('./lib/middleware/page')
var validate = require('./lib/middleware/validate')
var user = require('./lib/middleware/user')
var register = require('./routes/register')
var login = require('./routes/login')
var main = require('./routes/main')
var messages = require('./lib/messages')
var http = require('http')
var path = require('path')
var bodyParser = require('body-parser')
var favicon = require('serve-favicon')
var morgan = require('morgan')
var session = require('express-session')
var cookieParser = require('cookie-parser')
var methodOverride = require('method-override')
var errorhandler = require('errorhandler')
var serveStatic = require('serve-static')
var multer = require('multer')
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './tmp');
},
filename: function (req, file, cb) {
cb(null , file.originalname);
}
});
var upload = multer({ storage: storage })
var options = require('./options')
var pass = require('./lib/pass')
var passport = require('passport')
var settings = require('./routes/settings')
var imports = require('./routes/imports')
var importRss = require('./routes/importrss')
var importGoogle = require('./routes/importgoogle')
var app = express()
var server = http.Server(app)
var io = require('socket.io')(server)
app.set('port', process.env.PORT || 3000)
app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')
app.use(favicon(path.join(__dirname, 'public/images', 'favicon-32x32.png')))
app.use(methodOverride())
app.use(session({
secret: options.cookie_secret,
resave: true,
saveUninitialized: true
}))
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(morgan('dev'))
app.use(cookieParser(options.cookie_secret))
app.use(serveStatic(path.join(__dirname, 'public')));
app.use(passport.initialize())
app.use(passport.session())
app.get('/', main.render)
// This makes sure that when someone accesses external /api2 they are authenticated first
app.use('/api2', api2.auth)
app.use(user)
app.use(messages)
// First we declare all the static paths in the script
app.get('/signup', register.form)
app.get('/recover', register.recover)
app.post('/recover', register.generatehash)
app.post('/signup', register.submit)
app.get('/login', pass.checkLogin, login.form)
app.get('/reset/:user/:timestamp/:hash', register.reset)
app.post('/reset', register.reset)
// POST /login
// This is an alternative implementation that uses a custom callback to
// acheive the same functionality.
app.post('/login', login.submit)
app.get('/logout', login.logout)
app.get('/post', entries.form)
app.post(
'/post',
pass.ensureAuthenticated,
validate.isLoggedIn(),
validate.isToDelete(),
validate.getContextForEntry('entry[body]'),
entries.submit
)
app.post('/context', pass.ensureAuthenticated, validate.changeContextPrivacy())
// Internal API to get nodes and statements for user's own nodes - no need to check user ID
app.get('/api/user/nodes/:context?', validate.getContextsList(), api.nodes)
app.get('/api/user/statements/:context?', api.entries)
// Internal API to get nodes and statements for somebody else's nodes in context
app.get(
'/api/public/nodes/:user?/:context?',
validate.getUserID(),
validate.getContextPrivacy(),
validate.getContextsList(),
api.nodes
)
app.get(
'/api/public/statements/:user?/:context?',
validate.getUserID(),
validate.getContextPrivacy(),
api.entries
)
app.get('/api/:user/lda/:type/:context?', validate.getUserID(), api.entriesLDA)
// Get connected texts through internal connector function
app.get(
'/api/connectedcontexts/',
validate.getUserID(),
api.connectedcontextsoutside
)
app.get(
'/api/:user/connectedcontexts/',
validate.getUserID(),
api.connectedcontexts
)
// External API to get nodes and statements
app.get('/api2/user/nodes/:context?', api2.nodes)
app.get('/api2/user/statements/:context?', validate.getUserID(), api2.entries)
// For posting through API POST parameters:
// entry[body] is the statement,
// context is the context (optional, default: private),
// statementid is the ID of a statement to edit / delete (optional)
// submit = 'edit' to edit, delete = 'delete' to delete (optional)
app.post(
'/api2/post',
validate.isToDelete(),
validate.getContextForEntry('entry[body]'),
entries.submit
)
app.get(
'/settings',
pass.ensureAuthenticated,
validate.getContextsList(),
settings.render
)
app.post('/settings', pass.ensureAuthenticated, settings.modify)
app.get(
'/import',
pass.ensureAuthenticated,
validate.getContextsList(),
imports.render
)
app.get(
'/import/files',
pass.ensureAuthenticated,
validate.getContextsList(),
imports.renderFiles
)
// backward compatibility
app.get(
'/google',
pass.ensureAuthenticated,
validate.getContextsList(),
importGoogle.renderGoogle
)
app.get(
'/import/google',
pass.ensureAuthenticated,
validate.getContextsList(),
importGoogle.renderGoogle
)
app.get(
'/import/youtube',
pass.ensureAuthenticated,
validate.getContextsList(),
imports.renderYouTube
)
app.get(
'/import/evernote',
pass.ensureAuthenticated,
validate.getContextsList(),
imports.renderEvernote
)
app.get(
'/importurl',
pass.ensureAuthenticated,
validate.getContextsList(),
imports.renderURL
)
app.get(
'/importrss',
pass.ensureAuthenticated,
validate.getContextsList(),
importRss.renderRSS
)
app.get(
'/apps',
pass.ensureAuthenticated,
validate.getContextsList(),
imports.renderApps
)
app.get(
'/twitter',
pass.ensureAuthenticated,
validate.getContextsList(),
imports.renderTwitter
)
app.post('/import', pass.ensureAuthenticated, upload.single('uploadedFile'), imports.submit)
app.post('/importrss', pass.ensureAuthenticated, importRss.submitRSS)
app.post('/importgoogle', pass.ensureAuthenticated, importGoogle.submitGoogle)
app.get('/evernote_oauth', oauths.oauth)
app.get('/evernote_oauth_callback', oauths.oauth_callback)
app.get('/evernote_clear', oauths.clear)
// From here we declare all the dynamic paths at the first level of the content tree
app.get(
'/:user/edit',
pass.ensureAuthenticated,
validate.getContextsList(),
entries.list
)
app.get(
'/:user/:context?/edit',
pass.ensureAuthenticated,
validate.getContextPrivacy(),
validate.getContextsList(),
entries.list
)
app.get(
'/:user/:context?',
pass.checkUser,
validate.getUserID(),
validate.getContextPrivacy(),
validate.getContextsList(),
entries.list
)
// Errors and bad requests
var routes = require('./routes')
app.use(routes.notfound)
app.use(routes.error)
app.use(routes.badrequest)
// development only
if ('development' == app.get('env')) {
app.use(errorhandler())
}
if (process.env.ERROR_ROUTE) {
app.get('/dev/error', function(req, res, next) {
var err = new Error('database connection failed')
err.type = 'database'
next(err)
})
}
server.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'))
})
// could be var chat = to make it recursive
io.on('connection', function(socket) {
console.log('a user socket connected')
socket.on('disconnect', function(data) {
console.log('user disconnected')
// let's count how many people are left in a room
var room = io.sockets.adapter.rooms[socket.room]
var people_remaining = 1
// TODO that shouldn't be called if not needed
if (room != undefined) {
people_remaining = room.length
}
console.log('users left')
console.log(room)
console.log(people_remaining)
// Notify the other person in the chat room
// that his partner has left
socket.broadcast.to(socket.room).emit('leave', {
boolean: true,
people: people_remaining,
})
})
socket.on('chat message', function(msg) {
console.log('message: ' + msg)
io.sockets.in(socket.room).emit('chat message', msg)
// io.emit('chat message', msg);
})
socket.on('delete message', function(msg) {
console.log('message: ' + msg)
io.sockets.in(socket.room).emit('delete message', msg)
// io.emit('chat message', msg);
})
socket.on('node click', function(msg) {
socket.broadcast.to(socket.room).emit('node click', msg)
})
socket.on('node delete', function(msg) {
io.sockets.in(socket.room).emit('node delete', msg)
})
socket.on('node add', function(msg) {
io.sockets.in(socket.room).emit('node add', msg)
})
socket.on('graph reset', function(msg) {
io.sockets.in(socket.room).emit('graph reset', msg)
})
socket.on('login', function(data) {
// Use the socket object to store data. Each client gets
// their own unique socket object
socket.username = data.user
socket.room = data.id
// Add the client to the room
socket.join(data.id)
// Send the startChat event to all the people in the
// room, along with a list of people that are in it.
socket.to(data.id).emit('startChat', {
boolean: true,
id: data.id,
})
})
})