-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
1111 lines (799 loc) · 26 KB
/
index.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
made by qwazwsx/thisisatesttoseehowl
https://github.com/qwazwsx/c4-cams
.
*/
/* ########################################### */
/* REQUIREMENTS AND VARS */
var MongoClient = require('mongodb').MongoClient; //mongoDB client for databases
//var mongoUrl = "mongodb://localhost:27017/"; //location of mongoDB server
var mongoUrl = process.env.MONGODB_URI || "mongodb://localhost:27017"; //location of mongoDB server
var uuidv4 = require('uuid/v4'); //generates uuid's for documents
var decay = require('decay'); //get decay for sorting by up&down votes
var wilsonScore = decay.wilsonScore(); //use wilson type of scoring (reddit comments "best" sorting)
var sortBy = require('sort-array'); //helps when sorting arrays of posts
var express = require('express'); //get express for API
var RateLimit = require('express-rate-limit'); //get ratelimiter for express
var app = express(); //get express server
var http = require('http'); //http for checking if URLS are working
var { URL } = require('url'); //parse URLS
var server = require('http').Server(app); //server for socket.io
var io = require('socket.io')(server); //socket.io
var isOnline = require('is-online'); //check if server is connected to the internet
var inspector = require('inspector'); //dev tools inspector
var views = []; //tracks users for view counts
var reports = []; //tracks users for reports
var topPosts = [] //list of sorted posts, updates every 10 min
var cache = [];
var port = process.env.PORT || 3000; // set our port (defaults to 8081 if env var isnt set)
var dbName = process.env.dbName || "heroku_m7sf9mbv";
var db; //database connection
var dbo;
var time = 0;
//incremented on every dead url, if it gets too high check if the server has a connection
var offlineCheck = 0;
var socketConnections = [];
//setup port
server.listen(port, function(){
console.log('listening on *:' + port);
});
io.on('connection', function(socket){
//add connection to array
socketConnections.push({ id: socket.id, ip: socket.conn.remoteAddress });
socket.on('disconnect', function() {
//remove connection from array
var index = socketConnections.findIndex(function(x) { return x.id === socket.id});
if (index > -1) {
socketConnections.splice(index, 1);
}
});
})
//connect to database
MongoClient.connect(mongoUrl, function(err, connection) {
console.log('[INFO] CONNECTING TO DB')
if (err){
console.log('error connecting to db');
throw err;
}
//set database object to global var
db = connection;
dbo = db.db(dbName);
sort();
registerApiRoutes();
});
//catch shutdown signal and disconnect from DB
//its JS why do I have to write code to support different platforms?
//https://stackoverflow.com/a/14861513/6088533
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function () {
process.emit("SIGINT");
});
}
process.on("SIGINT", function () {
//graceful shutdown
db.close().then(function() {
console.log('got shutdown signal, closing database connection');
//close inspector/devtools
inspector.close();
process.exit();
});
});
/* ########################################### */
/* SERVER SETUP */
//enable ratelimiting
app.enable('trust proxy');
var apiLimiter = new RateLimit({
windowMs: 60*1000, // 1 minute
max: 120,
delayMs: 0 // disabled
});
app.use('/c4-cams/api/', apiLimiter);
var apiLimiterHard = new RateLimit({
windowMs: 10*60*1000, // 1 minute
max: 3,
delayMs: 0, // disabled
skipFailedRequests: true
});
app.use('/c4-cams/api/add', apiLimiterHard);
//re-calculate top posts every 5 min
setInterval(function(){
sort();
},5*60*1000);
//reset "rate-limiting" for views and reports
setInterval(function(){
views = [];
reports = [];
},30*60*1000);
/* ########################################### */
/* API */
//func containing all API endpoints and their code
//not properly tabbed on purpose, no need to
function registerApiRoutes(){
//################################################
//add camera to list
app.post('/c4-cams/api/add', function (req, res) {
addCamera(req.query.url).then(function(data){
if (data.error !== undefined){
//if url isnt valid or already exists
res.status(400).send(data);
}else{
res.send(data);
}
});
});
//################################################
//sends ordered list of posts
app.get('/c4-cams/api/top', function (req, res) {
//return cached array
res.send(topPosts);
});
//################################################
//responds pong
//delay: ms to delay response
app.get('/c4-cams/api/ping', function (req, res) {
setTimeout(function(){res.send('pong')},req.query.delay);
});
//################################################
//query params
//type: 0 - search by camera object uuid, 1 - search by short url, 2 - search by full url
//query: what you are seaching for
app.post('/c4-cams/api/find', function (req, res) {
var ip = req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || req.client.remoteAddress;
var results = 'unset';
//if query isnt empty
if (req.query.query !== undefined){
//if searching by uuid
if (req.query.type == 0){
search('cams',2,{ _id: req.query.query}).then(function(send){
results = send;
respond();
});
}else{
//if searching by short url
if (req.query.type == 1){
search('cams',2,{ url: req.query.query }).then(function(send){
results = send;
respond();
});
}else{
//if searching by full url
if (req.query.type == 2){
search('cams',2,{ urlFull: req.query.query }).then(function(send){
results = send;
respond();
});
}else{
//if type is invalid
res.status(400).send({error: 'parameter \'type\' not set correctly', code: 0});
results = 'error'
}
}
}
function respond(){
//if results diddnt error
if (results !== 'error' ){
//if nothing was found
if (results[0] == undefined){
res.status(400).send({message: 'no cams found', code: 2});
}else{
//if results were found
//account for view and add vote data
view(results,ip).then(function(data) {
res.send(data)
});
}
}
}
}else{
//if query is empty
res.status(400).send({error: 'parameter \'query\' not set correctly', code: 1});
}
});
//################################################
//query params
//uuid
//errors
//0 - you have already reported this camera
app.post('/c4-cams/api/report', function (req, res) {
var ip = req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || req.client.remoteAddress ;
report(req.query.uuid,ip,true).then(function(send){
if (send.error !== undefined){
res.status(400).send(send);
}else{
res.send(send);
}
});
});
//################################################
//query params
//uuid: uuid of a camera object
//errors
//0 - uuid doesnt exist
//1 - user already upvoted
app.post('/c4-cams/api/upvote', function (req, res) {
var ip = req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || req.client.remoteAddress ;
upvote(req.query.uuid,ip).then(function(data){
if (data.error !== undefined){
res.status(400).send(data);
}else{
res.send(data);
}
});
});
app.post('/c4-cams/api/unvote', function (req, res) {
var ip = req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || req.client.remoteAddress ;
unvote(req.query.uuid,ip).then(function(data){
if (data.error !== undefined){
res.status(400).send(data);
}else{
res.send(data);
}
});
});
//################################################
//query params
//uuid: uuid of a camera object
//errors
//0 - uuid doesnt exist
//1 - user already upvoted
app.post('/c4-cams/api/downvote', function (req, res) {
var ip = req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || req.client.remoteAddress ;
downvote(req.query.uuid,ip).then(function(data){
if (data.error !== undefined){
res.status(400).send(data);
}else{
res.send(data);
}
});
});
//################################################
//returns a random camera object
app.get('/c4-cams/api/random', function (req, res) {
var ip = req.headers["X-Forwarded-For"] || req.headers["x-forwarded-for"] || req.client.remoteAddress;
random(ip,req.query.id).then(function(data) {
res.send(data);
});
});
}
//use routes
app.use('/c4-cams/', express.static('static/random'));
app.use('/c4-cams/perma', express.static('static/perma'));
app.use('/c4-cams/random', express.static('static/random'));
app.use('/c4-cams/top', express.static('static/top'));
app.use('/c4-cams', express.static('static'));
app.use('/', express.static('static/random'));
app.use('/perma', express.static('static/perma'));
app.use('/random', express.static('static/random'));
app.use('/top', express.static('static/top'));
app.use('/info', express.static('static/info'));
/* ########################################### */
/* FUNCTIONS */
//get a random camera from the database, check that is works, and make a document for it
//ip: ip of requester
//socketId: optional, improves socketIO messages
function random(ip,socketId) {
return new Promise(function(resolve,reject){
//get random document from database
dbo.collection('camera_list').aggregate([{ $sample: { size: 1 } }]).toArray(function(err, cameraObj) {
if (err) throw err;
//check if url can be reached
checkUrl(cameraObj[0].url).then(function(urlCheck){
//if the url returns an error
if (urlCheck.error){
//console.log('[INFO] url dead '+ cameraObj[0].url);
//if given socketId is actually connected
if (socketId !== undefined){
if (io.sockets.connected[socketId] !== undefined){
//emit message directly to user
io.sockets.connected[socketId].emit('fail', {error: 'dead url', code: '0', urlFull: cameraObj[0].url, url: cameraObj[0].url});
}
//io.clients[socketId].send({error: 'dead url', code: '0', url: cameraObj[0].url})
}
//add error flag to doc
update('camera_list',cameraObj[0],{ $inc: { errors: 1 } } ).then(function(data) {
//if there are 2 or more errors
if (data.errors >= 2){
//remove that cam from the list
remove('camera_list',{url: cameraObj[0].url}).then(function(err,res) {
console.log('[INFO] removed cam from list '+cameraObj[0].url)
console.log(socketId)
});
}
})
//try again with another url
random(ip,socketId).then(function(data) {
resolve(data)
});
}else{
//if url returns OK
load(cameraObj[0].url,ip).then(function(data){
var tempData = data;
tempData.time = urlCheck.time;
resolve(data);
});
};
});
});
});
}
//returns status code of given url
//if error response will have error set to true
function checkUrl(url){
return new Promise(function(resolve,reject){
//setup options
var options = new URL(url);
options.method = 'HEAD'
//make the request
var beforeTime = time;
var req = http.request(options, function(r) {
//return the response code on no errors
if (r.statusCode == 200){
var totalTime = time - beforeTime
resolve({success: true, code: r.statusCode, time: totalTime });
}else{
resolve({ error: true, message: 'request succeeded but response code was non-200, response code:'+r.statusCode });
}
});
req.on('error', function(err) {
//on error return error flag
offlineCheck++;
if (offlineCheck >= 15){
isOnline().then(online => {
if (!online){
//server has no connection
process.exit(1);
}else{
offlineCheck = 0;
}
//=> true
});
}
resolve({ error: true, message: err });
});
req.setTimeout(3000, function() {
//timeout and return error flag
offlineCheck++;
if (offlineCheck >= 15){
isOnline().then(online => {
if (!online){
//server has no connection
process.exit(1);
}else{
offlineCheck = 0;
}
//=> true
});
}
req.abort();
resolve({ error: true, message: 'timeout' });
});
req.end();
});
}
setInterval(function() {
time = time + 100;
},100)
//add a camera to the list
//url: full url?
function addCamera(url){
return new Promise(function(resolve,reject){
if (url == undefined){
resolve({error: 'url not set', code: 2});
}else{
//search for the given url
search('camera_list',2,{url: url}).then(function(data){
//if it doesnt already exist
if (data == ''){
//if it is a link
if (url.indexOf('http') !== -1){
checkUrl(url).then(function(urlCheck){
if (urlCheck.error == undefined){
//add it to the list
add('camera_list',{url: url}).then(function(){
//create a doc for it
createDoc(url).then(function(data){
//return OK
resolve({uuid: data, response: 'OK', code: 200});
});
});
}else{
resolve({error: 'url is either not reachable to the web or not valid', code: 2});
}
})
}else{
resolve({error: 'not valid url', code: 0});
}
}else{
//if url already exists
resolve({error: 'url already exists', code: 1});
}
});
}
});
}
//gets all data from database and sorts using wilson type sorting
//read more here: https://github.com/clux/decay#1-wilson-score
//sets var sortedPosts
//sortedPosts gets returned when the top list is needed
//this function should be ran at an interval 5min?? 10 min? 30min?
var unsortedPosts = [];
var score;
var posts;
function sort(){
return new Promise(function(resolve,reject){
//get all cams
console.log
dbo.collection('cams').find({}).toArray(function(err, data) {
if (err) throw err;
unsortedPosts = [];
//loop over all cams and score them
for (var i = 0; i < data.length; i++){
//calc score with decay
score = wilsonScore(data[i].upvotes, data[i].downvotes);
//add this to an array
unsortedPosts[i] = [];
unsortedPosts[i].score = score;
unsortedPosts[i].data = data[i];
}
//sort scored posts
posts = sortBy(unsortedPosts,'score').reverse();
sortedPosts = [];
//remove score from data
for (var i = 0; i < posts.length; i++){
sortedPosts[i] = posts[i].data;
}
//only get top 50 results
topPosts = sortedPosts.slice(0, 50);;
console.log('[INFO] top posts calculated')
resolve();
//console.log(sortedPosts);
});
});
}
//report a post as dead
//uuid: uuid of cam
//ip: ip of requester
function report(uuid,ip,outside){
return new Promise(function(resolve,reject){
//search for the referenced post
search('cams', 2, { _id: uuid}).then(function(data){
if (data == ''){
//if the post doesnt exist
resolve({error: 'camera not found', code: 1});
}else{
//if it exists
//if report isnt already in temp array
if (reports[uuid] == undefined){
reports[uuid] = [];
};
//if the ip hasnt already reported this post
if (reports[uuid].indexOf(ip) == -1){
//if there are 2 or more errors
if (data[0].reports >= 2){
//remove that cam from the list
remove('camera_list',{ _id:uuid }).then(function(err,res) {
console.log('[INFO] removed cam from camera_list '+data[0].url)
});
remove('cams',{ _id:uuid }).then(function(err,res) {
console.log('[INFO] removed cam from cams '+data[0].url)
});
}else{
//if there are less than 2 reports
//if outside report flag is set only report one fith the amount
if (outside == true){
update('cams', { _id:uuid }, { $inc: { reports: 0.2 } } );
console.log('[INFO] external report')
}else{
//if report comes internally report with more weight
update('cams', { _id:uuid }, { $inc: { reports: 1 } } );
}
}
//keep track of reports
reports[uuid].push(ip);
//return
resolve({message:'OK'});
}else{
resolve({error: 'you have already reported this camera', code: 0});
}
}
});
});
}
//upvote a post
//uuid: uuid of post
//ip: ip of requester
function upvote(uuid,ip){
return new Promise(function(resolve,reject){
//search in vote records for upvotes on this ip on this post
search('votes', 2, { _id: uuid}).then(function(data){
//if uuid doesnt exist
if (data == ''){
resolve({error: 'uuid doesnt exist', code: 0});
}else{
//if user diddnt upvote the post already
if (data[0].upvoters.indexOf(ip) == -1){
//add upvote
update('cams', { _id: uuid }, { $inc: {upvotes: 1} } );
//add ip to upvoters list
update('votes', { _id: uuid }, { $push: {upvoters: ip} } );
//if user downvoted before
if (data[0].downvoters.indexOf(ip) !== -1){
//take away the downvote
update('cams', { _id: uuid }, { $inc: {downvotes: -1} } );
//remove ip from downvoters list
update('votes', { _id: uuid }, { $pull: {downvoters: ip} } );
};
resolve({message:'OK'});
}else{
resolve({error: 'user already upvoted', code: 1});
};
};
});
});
}
//downvote a post
//uuid: uuid of post
//ip: ip of requester
function downvote(uuid,ip){
return new Promise(function(resolve,reject){
//search in vote records for upvotes on this ip on this post
search('votes', 2, { _id: uuid}).then(function(data){
//if uuid doesnt exist
if (data == ''){
resolve({error: 'uuid doesnt exist', code: 0});
}else{
//if user diddnt downvote the post already
if (data[0].downvoters.indexOf(ip) == -1){
//add downvote
update('cams', { _id: uuid }, { $inc: {downvotes: 1} } );
//add ip to downvoters list
update('votes', { _id: uuid }, { $push: {downvoters: ip} } );
//if user upvoted before
if (data[0].upvoters.indexOf(ip) !== -1){
//take away the upvote
update('cams', { _id: uuid }, { $inc: {upvotes: -1} } );
//remove ip from upvoters list
update('votes', { _id: uuid }, { $pull: {upvoters: ip} } );
};
resolve({message:'OK'});
}else{
resolve({error: 'user already downvoted', code: 1});
};
};
});
});
}
function unvote(uuid,ip){
return new Promise(function(resolve,reject){
//search in vote records for votes on this ip on this post
search('votes', 2, { _id: uuid}).then(function(data){
if (data == ''){
resolve({error: 'uuid not valid', code: 1})
}else{
//if they downvoted before
if (data[0].downvoters.indexOf(ip) !== -1){
//take away the downvote
update('cams', { _id: uuid }, { $inc: {downvotes: -1} } );
//remove ip from downvoters list
update('votes', { _id: uuid }, { $pull: {downvoters: ip} } );
};
//if they upvoted before
if (data[0].upvoters.indexOf(ip) !== -1){
//take away the upvote
update('cams', { _id: uuid }, { $inc: {upvotes: -1} } );
//remove ip from upvoters list
update('votes', { _id: uuid }, { $pull: {upvoters: ip} } );
};
resolve({message:'OK'})
}
});
});
}
//add view to camera and add voting data
//searchData: data from cams (see load() func)
//ip: ip of requester
function view(searchData,ip){
return new Promise(function(resolve,reject){
//if the camera exists in the database add view count
//console.log('[INFO] PAGE EXISTS');
//if viewers array for this cam doesnt exist make it
if (views[searchData[0]._id] == undefined){
views[searchData[0]._id] = [];
};
//if ip havent already viewed push ip to array and count view
if (views[searchData[0]._id].indexOf(ip) == -1){
views[searchData[0]._id].push(ip)
//add view to the cam
update('cams', { _id: searchData[0]._id }, { $inc: { views: 1 } })
};
//check if IP has voted before
checkVote(searchData[0]._id, ip).then(function(vote) {
//add that result to data
searchData[0].vote = vote;
resolve(searchData[0]);
});
});
}
//to be called on page load
//url: full url
//ip: ip of requester
function load(url,ip,permaData){
//if load() is being called from the /api/find endpoint
if (url == undefined && ip == undefined && permaData !== undefined){
}
return new Promise(function(resolve,reject){
//check if url already is in database
search('cams',0,url).then(function(searchData){
if (searchData == ''){
//if url isnt in the database add it and restart the function
console.log('[INFO] creating page ' + url)
createDoc(url).then(function(){
load(url,ip).then(function(data){
resolve(data);
});
})
}else{
//change view data and check vote database if the IP has voted before
view(searchData,ip).then(function(searchData) {
resolve(searchData);
})
};
}).catch(function(err){
throw err;
});
});
};
function checkVote(uuid,ip){
return new Promise(function(resolve,reject){
search('votes', 2, { _id: uuid}).then(function(data){
//if they downvoted before
if (data[0].downvoters.indexOf(ip) !== -1){
resolve('downvote');
}else{
//if they upvoted before
if (data[0].upvoters.indexOf(ip) !== -1){
resolve('upvote');
}else{
resolve('unvoted');
};
};
});
});
}
//placeholder function
//will eventualy serve JSON content
//create a new doc from url
//url: full url
function createDoc(url){
//add / incase url doesnt have / at the end (it breaks regex)
//url = url + '/'
return new Promise(function(resolve,reject){
//generate a uuid
var uuid = uuidv4();
//check if uuid is already used
search('cams',1,uuid).then(function(data){
if (data !== ''){
//if uuid isnt already used add it to the database
add('cams',{
_id:uuid,
url: /(:\/\/)(.+?)(?=\/)/g.exec(url)[0].replace('://',''),
urlFull: url,
upvotes: 0,
downvotes: 0,
reports: 0,
views: 0
});
add('votes', { _id:uuid, upvoters: [], downvoters: []} ).then(function(data){
resolve(uuid);
});
}else{
//if uuid is already used retry
console.log('UUID is already used, buy a lottery ticket');
createDoc(url);
};
});
});
};
//check if camera is already in the database
//collection: collection to search in
//mode: 0 = search by url. 1 = search by uuid. 2 = input your own query object
//input: full url
function search(collection,mode,input){
return new Promise(function(resolve,reject){
//not as elegant as /(?<=:\/\/)(.+?)(?=\/)/g but positive lookbehinds aren't supported
if (mode == 0){
//cut long url to short version
var query = { url: /(:\/\/)(.+?)(?=\/)/g.exec(input)[0].replace('://','') };
}else{
if (mode == 1){
//search for uuid
var query = { uuid: input };
}else{
if (mode == 2){
//custom search query
var query = input;
};