-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.js
1241 lines (1199 loc) · 40.5 KB
/
utility.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
var app = require('./app');
var http = require('http');
var fs = require('fs');
var random = require('secure_random');
var bcrypt = require('bcrypt-nodejs');
var mr = require('./mapreduce');
var config = require('./config');
//Takes in array and returns new one with duplicate entries removed
function arrNoDupe(a) {
var temp = {};
for (var i = 0; i < a.length; i++)
temp[a[i]] = true;
var r = [];
for (var k in temp)
r.push(k);
return r;
}
//clears changes array. Happens immediately after a read.
var clearChanges = exports.clearChanges = function(obj){
var ch = obj.changes;
for(var o in ch){
for(var a in ch[o]){
if(ch[o][a]) ch[o][a].length = 0;
}
}
}
//returns date in mm/dd/yyyy format
//TODO: Evaluate if this is good enough
var getDate = exports.getDate = function(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
today = mm+'/'+dd+'/'+yyyy;
return today;
}
//Default conflict resolution, returns latest sibling
var last_write_wins = exports.last_write_wins = function(siblings){
console.log('last_write_wins()');
function siblingLastModifiedSort(a, b) {
if(!a.metadata.last_modified || new Date(a.metadata.last_modified) < new Date(b.metadata.last_modified)) {
return 1;
}
else {
return -1;
}
}
siblings.sort(siblingLastModifiedSort);
return siblings[0];
}
//Conflict resolution for gamepins
var pin_resolve = exports.pin_resolve = function(siblings){
if(!siblings[0].data.posterId){
console.log('pin_resolve called on non pin: calling default resolver');
return last_write_wins(siblings);
}
console.log('pin_resolve');
function siblingLastModifiedSort(a, b) {
if(!a.metadata.last_modified || new Date(a.metadata.last_modified) < new Date(b.metadata.last_modified))
return 1;
else
return -1;
}
//sort by timestamp to get last written user at siblings[0]
siblings.sort(siblingLastModifiedSort);
var net_changes = { likedBy: {add:[], remove:[], edit:[]},
comments: {add:[], remove:[], edit:[]}
};
//join sibling changes into net changes
for(var s = 0; s < siblings.length; s++){
console.log(siblings[s].data.comments);
net_changes.likedBy.add = net_changes.likedBy.add.concat(siblings[s].data.changes.likedBy.add);
net_changes.likedBy.remove = net_changes.likedBy.remove.concat(siblings[s].data.changes.likedBy.remove);
net_changes.comments.add = net_changes.comments.add.concat(siblings[s].data.changes.comments.add);
net_changes.comments.remove = net_changes.comments.remove.concat(siblings[s].data.changes.comments.remove);
}
//Add LikedBy
siblings[0].data.likedBy = siblings[0].data.likedBy.concat(net_changes.likedBy.add);
siblings[0].data.likedBy = arrNoDupe(siblings[0].data.likedBy);
//Delete LikedBy
for(var p in net_changes.likedBy.remove){
if(siblings[0].data.likedBy.indexOf(net_changes.likedBy.remove[p]) !== -1)
siblings[0].data.likedBy.splice(siblings[0].data.likedBy.indexOf(net_changes.likedBy.remove[p]), 1);
}
//Add Comments
siblings[0].data.comments = siblings[0].data.comments.concat(net_changes.comments.add);
siblings[0].data.comments = arrNoDupe(siblings[0].data.comments);
//Delete Comments
for(var p in net_changes.comments.remove){
if(siblings[0].data.comments.indexOf(net_changes.comments.remove[p]) !== -1)
siblings[0].data.comments.splice(siblings[0].data.comments.indexOf(net_changes.comments.remove[p]), 1);
}
return siblings[0];
}
//Conflict resolution for user
var user_resolve = exports.user_resolve = function(siblings){
//Check to make sure our obj is a user, else call default resolver
if(!siblings[0].data.email){
console.log('user_resolve called on non user: calling default resolver');
return last_write_wins(siblings);
}
console.log('user_resolve()');
function siblingLastModifiedSort(a, b) {
if(!a.metadata.last_modified || new Date(a.metadata.last_modified) < new Date(b.metadata.last_modified))
return 1;
else
return -1;
}
siblings.sort(siblingLastModifiedSort);
var net_changes = { posts: {add:[], remove:[], edit:[]},
likes: {add:[], remove:[], edit:[]},
followers: {add:[], remove:[]},
following: {add:[], remove:[]},
};
//join sibling changes into net changes
for(var s = 0; s < siblings.length; s++){
net_changes.posts.add = net_changes.posts.add.concat(siblings[s].data.changes.posts.add);
net_changes.posts.remove = net_changes.posts.remove.concat(siblings[s].data.changes.posts.remove);
net_changes.likes.add = net_changes.likes.add.concat(siblings[s].data.changes.likes.add);
net_changes.likes.remove = net_changes.likes.remove.concat(siblings[s].data.changes.likes.remove);
net_changes.followers.add = net_changes.followers.add.concat(siblings[s].data.changes.followers.add);
net_changes.followers.remove = net_changes.followers.remove.concat(siblings[s].data.changes.followers.remove);
net_changes.following.add = net_changes.following.add.concat(siblings[s].data.changes.following.add);
net_changes.following.remove = net_changes.following.remove.concat(siblings[s].data.changes.following.remove);
}
//resolve posts
siblings[0].data.posts = siblings[0].data.posts.concat(net_changes.posts.add);
siblings[0].data.posts = arrNoDupe(siblings[0].data.posts);
for(var p in net_changes.posts.remove){
if(siblings[0].data.posts.indexOf(net_changes.posts.remove[p]) !== -1)
siblings[0].data.posts.splice(siblings[0].data.posts.indexOf(net_changes.posts.remove[p], 1));
}
//resolve likes
siblings[0].data.likes = siblings[0].data.likes.concat(net_changes.likes.add);
siblings[0].data.likes = arrNoDupe(siblings[0].data.likes);
for(var p in net_changes.posts.remove){
if(siblings[0].data.likes.indexOf(net_changes.likes.remove[p]) !== -1)
siblings[0].data.likes.splice(siblings[0].data.likes.indexOf(net_changes.likes.remove[p]), 1);
}
//resolve followers
siblings[0].data.followers = siblings[0].data.followers.concat(net_changes.followers.add);
siblings[0].data.followers = arrNoDupe(siblings[0].data.followers);
for(var p in net_changes.followers.remove){
if(siblings[0].data.followers.indexOf(net_changes.followers.remove[p]) !== -1)
siblings[0].data.followers.splice(siblings[0].data.followers.indexOf(net_changes.followers.remove[p]), 1);
}
//resolve following
siblings[0].data.following = siblings[0].data.following.concat(net_changes.following.add);
siblings[0].data.following = arrNoDupe(siblings[0].data.following);
for(var p in net_changes.following.remove){
if(siblings[0].data.following.indexOf(net_changes.following.remove[p]) !== -1)
siblings[0].data.following.splice(siblings[0].data.following.indexOf(net_changes.following.remove[p]), 1);
}
return siblings[0];
}
//12 categories
var categories = ['Action & Adventure', 'Arcade', 'Board & Card', 'Casino & Gambling',
'Educational', 'Family & Kids', 'Music & Rhythm', 'Puzzle',
'Racing', 'Role Playing', 'Simulation', 'Sports', 'Strategy', 'Trivia & Word'];
//save image
var clearPosts = exports.clearPosts = function(user_key){
app.riak.bucket('users').objects.get(user_key, user_resolve, function(err, obj){
if(err) console.log(err);
obj.data.posts = [];
obj.save(function(err, saved){
if(err) console.log(err);
console.log("Post references cleared from: " + saved.key);
});
});
}
var clearTony = exports.clearTony = function(){
app.riak.bucket('users').objects.get('dtonys@gmail.com-groups', function(err, obj){
if(err) return res.json({error: "fetch groups error"});
obj.data['Action & Adventure'] = [];
obj.save(function(err, data){
if(err) return res.json({ error: "ClearTony save Failed" });
console.log('clearTony success');
});
});
}
var putImage = exports.putImage = function(req, res){
console.log('getImg');
var options = {
host: 'https://identity.api.rackspacecloud.com/v1.0',
port: 80,
path: '/1.0',
method: 'GET',
headers: {
'X-Auth-User': 'happyspace',
'X-Auth-Key': '1b5a100b899c44633dbda1aa93ea6237'
}
};
var R = http.request(options, function(response) {
});
R.end();
}
var fetchImg = exports.fetchImg = function(req, res){
app.riak.bucket('images').objects.get('testImg', function(err, obj){
var base64data = new Buffer(obj.data).toString('base64');
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Length', base64data.length);
res.end(base64data);
console.log(base64data);
return res.send(base64data);
//return res.json({ img: base64data });
//console.log(err);
//console.log(obj);
});
}
/* create an individual user, split into 3 parts: user, user-groups, user-activity
* user@gmail.com contains all basic user data
* user@gmail.com-groups contains a list of groups and posts that belong to them
* user@gmail.com-activity is a feed of events executed by this user recently
*/
var createUser = exports.createUser = function(key, uname, categories){
//create user
var usrId;
var exists;
console.log('createUser');
var data = {
email: key,
passHash: bcrypt.hashSync('pass'),
username: uname,
fbConnect: false,
favCat: categories,
profileImg: '',
gender: null,
bio: null,
dateJoined: getDate(),
posts: [],
likes: [],
followers: [],
following: [],
changes:{ posts: {add: [], remove: []},
likes: {add: [], remove: []},
followers: {add: [], remove: []},
following: {add: [], remove: []}
}
}
//create/overwrite user
var new_usr = app.riak.bucket('users').objects.new(key, data);
//index username so we can query via username instead of email
new_usr.addToIndex('username', uname);
app.riak.bucket('users').objects.get(key, user_resolve, function(err, obj) {
if(err){
//if user doesn't exist, create new user
if(err.status_code === 404){
new_usr.save(function(err, saved){
console.log('User ' + key + ' created');
next();
});
}
else{
console.log('Get user error: ' + err);
}
}
//if existing user found, fetch that user's vec clock and overwrite with new_usr
else if(obj){
exists = true;
new_usr.metadata.vclock = obj.metadata.vclock;
new_usr.save(function(err, saved){
console.log('User ' + key + ' found and overwritten');
next();
});
}
});
//create/overwrite user-groups
function next(){
//create one group per favCat
var groups = {};
var group_key = key + '-groups';
for(f in new_usr.data.favCat){
groups[new_usr.data.favCat[f]] = [];
}
var usr_groups = app.riak.bucket('users').objects.new(group_key, groups);
app.riak.bucket('users').objects.get(group_key, function(err, obj) {
if(err && err.status_code === 404){
usr_groups.save(function(err, data){
console.log('Groups object ' + group_key + ' created');
next2();
});
}
else if(obj){
usr_groups.metadata.vclock = obj.metadata.vclock;
usr_groups.save(function(err, data){
console.log('Groups object ' + group_key + ' found and overwritten');
next2();
});
}
});
}
//create/overwrite user-activity
function next2(){
var activity = {evtIds:[]};
var activity_key = key + '-activity';
var usr_activity = app.riak.bucket('users').objects.new(activity_key, activity);
app.riak.bucket('users').objects.get(activity_key, function(err, obj) {
if(err && err.status_code === 404){
usr_activity.save(function(err, saved){
console.log('Activity queue ' + activity_key + ' created');
console.log('createUser complete!');
next3();
});
}
else if(obj){
usr_activity.metadata.vclock = obj.metadata.vclock;
usr_activity.save(function(err, saved){
console.log('Groups object ' + activity_key + ' found and overwritten');
console.log('createUser complete!');
next3();
});
}
});
}
//store email -> username into table for easy reference
function next3(){
var usr_ref = app.riak.bucket('userReference').objects.new(key, {username: uname});
usr_ref.save(function(err, obj){
console.log('user Reference saved!');
console.log(obj);
});
}
}
var getUser = exports.getUser = function(key){
app.riak.bucket('users').objects.get(key, function(err, obj){
if(err) return;
console.log(obj);
});
}
var getUserbyIndex = exports.getUserbyIndex = function(uname){
app.riak.bucket('users').search.twoi('user1', 'username', function(err, keys) {
if(err) return;
console.log(keys);
app.riak.bucket('users').objects.get(keys, user_resolve, function(err, obj){
if(err) return;
console.log(obj);
});
});
}
var createGamepin = exports.createGamepin = function(owner, uname, cat, desc){
var pinId;
generateId(function(id){
console.log(id);
pinID = id;
next();
});
function next(){
var value = {
posterId: owner,
posterName: uname,
likedBy: [],
repinVia: null,
category: cat,
content: null,
sourceUrl: null,
gameName: null,
publisher: null,
description: desc,
datePosted: null,
groupId: null,
returnAll: 'y',
comments: [],
changes:{ likedBy: {add:[], remove:[]},
comments:{add:[], remove:[] }
}
};
//create/overwrite gamepin
var gamepin = app.riak.bucket('gamepins').objects.new(pinID, value);
app.riak.bucket('gamepins').objects.get(pinID, user_resolve, function(err, obj) {
if(err && err.status_code === 404){
gamepin.save(function(err, saved){
console.log('Gamepin ' + pinID + ' created');
link(saved.data.posterId, saved.key);
});
}
else if(obj !== null){
gamepin.metadata.vclock = obj.metadata.vclock;
gamepin.save(function(err, saved){
console.log('Gamepin ' + pinID + 'overwritten!');
link(saved.data.posterId, saved.key);
});
}
});
}
}
//generate n users. Range s to n-1.
var generateUsers = exports.generateUsers = function(s, n, callback){
var userArray = [];
var userIds = [];
var clock = s;
for(var i = s; i < n; i++){
userIds.push('user' + i + '@gmail.com');
//user schema
userArray.push(
{ email: 'user' + i + '@gmail.com',
passHash: bcrypt.hashSync('user'+i),
//passHash: 'user'+i,
name: 'user' + i,
fbConnect: i%2==0 ? true : false,
favCat: null,
profileImg: '/images/profile/profile'+i%10+'.png',
gender: null,
bio:null,
dateJoined:null,
currXP:0,
nextXP:100,
level:1,
posts:[],
likes:[],
followers:[],
following:[],
recentActivity:[],
changes:{ posts: {add:[], remove:[]},
likes: {add:[], remove:[]},
followers: {add:[], remove:[]},
following: {add:[], remove:[]},
}
}
);
}
//set random favorite category
for(var i = s; i < n; i++){
(function(i){
random.getRandomInt(0, 11, function(err, rand) {
userArray[i].favCat = categories[rand];
if(i === n-1) next();
});
})(i);
}
//create new user. If one exists with given key, overwrite it.
function next(){
//Returns an array of err objects for users not found, and objs for found users
app.riak.bucket('users').objects.get(userIds, user_resolve, function(err, objs){
var sub;
//if nodiak gives us a single object, convert that into an array with 1 element.
if(err && Object.prototype.toString.call( err ) !== '[object Array]')
err = [err];
//loop through all not found keys and create objects for these
for(var e = 0; err && e < err.length; e++){
if(err[e].status_code === 404){
sub = err[e].data.substring(err[e].data.indexOf('r') + 1, err[e].data.indexOf('@'));
key = parseInt(sub, 10);
var new_usr = app.riak.bucket('users').objects.new(err[e].data, userArray[key]);
new_usr.data.dateJoined = getDate();
new_usr.save(function(err, saved){
console.log('User not found. New user created: ');
console.log(saved.data.email);
});
}
}
//if nodiak gives us a single object, convert that into an array with 1 element
if(objs && Object.prototype.toString.call( objs ) !== '[object Array]')
objs = [objs];
//loop through all found objects and overwrite them
for(var o = 0; objs && o < objs.length; o++){
clearChanges(objs[o]);
sub = objs[o].key.substring(objs[o].key.indexOf('r') + 1, objs[o].key.indexOf('@'));
key = parseInt(sub, 10);
if(objs[o].siblings) console.log('siblings found and resolved');
var merge_user = app.riak.bucket('users').objects.new(objs[o].key, userArray[key]);
merge_user.metadata.vclock = objs[o].metadata.vclock;
merge_user.save(function(err, saved){
console.log('User updated');
console.log(saved.data.email);
});
}
callback();
});
}
}
//generate n pins. range s to n-1
var generatePins = exports.generatePins = function(s, n){
var pinArray = [];
var pinIds = [];
var clock;
var user_keys = [];
//get user keys
mr.listKeys('users', function(results){
user_keys = results.data;
console.log(user_keys);
if(user_keys.length > 0) next();
else{
console.log('No users in db. Unable to generate pins without owners.');
return 0;
}
});
function next(){
var clock = s;
for(var i = s; i < n; i++){
pinIds.push(100 + i);
//gamepin schema
pinArray.push(
{ posterId: null,
posterName: null,
likedBy: [],
repinVia: null,
category: null,
content: null,
sourceUrl: null,
gameName: null,
publisher: null,
description: 'This is pin ' + i,
datePosted: null,
groupId: null,
returnAll: 'y',
comments: [],
changes:{ likedBy: {add:[], remove:[]},
comments:{add:[], remove:[] }
}
}
);
}
//set random category
for(var i = 0; i < (n-s); i++){
(function(i){
random.getRandomInt(0, 11, function(err, rand) {
pinArray[i].category = categories[rand];
if(i === (n-s)-1) next2();
});
})(i);
}
}
//set random posterId
function next2(){
clock = 0;
console.log('next2');
for(var i = 0; i < (n-s); i++){
pinArray[i].posterName = 'Test';
//secure_random cannot deal with range 0 to 0, hence this case
if(user_keys.length-1 === 0){
pinArray[i].posterId = user_keys[0];
if(i === n-1) next3();
}
else{
(function(i){
random.getRandomInt(0, user_keys.length-1, function(err, rand) {
pinArray[i].posterId = user_keys[rand];
if(i === (n-s)-1) next3();
});
})(i);
}
}
}
//create new pin. If one exists with current key, overwrite it.
function next3(){
clock = s;
console.log(pinIds);
app.riak.bucket('gamepins').objects.get(pinIds, pin_resolve, function(err, objs){
//if nodiak gives us a single object, convert that into an array with 1 element.
if(err && Object.prototype.toString.call( err ) !== '[object Array]')
err = [err];
//loop through all not found keys and create objects for these
for(var e = 0; err && e < err.length; e++){
if(err[e].status_code === 404){
var new_pin = app.riak.bucket('gamepins').objects.new(err[e].data, pinArray[err[e].data - 100]);
new_pin.data.datePosted = getDate();
new_pin.save(function(err, saved){
console.log(err);
console.log(saved);
console.log('new gamepin created');
link(saved.data.posterId, saved.key);
});
}
}
//if nodiak gives us a single object, convert that into an array with 1 element.
if(objs && Object.prototype.toString.call( objs ) !== '[object Array]')
objs = [objs];
//loop through all found objects and overwrite them
for(var o = 0; objs && o < objs.length; o++){
clearChanges(objs[o]);
var merge_pin = app.riak.bucket('gamepin').objects.new(objs[o].key, pinArray[objs[o].key - 100]);
merge_pin.metadata.vclock = objs[o].metadata.vclock;
merge_pin.save(function(err, saved){
console.log('gamepin updated');
link(saved.data.posterId, saved.key);
});
}
});
}
//fetch owner and add this post to his posts list
function userLink(ownerId, postId){
var update_user = app.riak.bucket('users').objects.new(ownerId);
update_user.fetch(user_resolve, function(err, obj){
if(err){
console.log("Fetch User: " + err);
return;
}
//clear changes
clearChanges(obj);
if(obj.siblings) console.log('siblings found and resolved');
console.log(obj);
update_user.data.posts.push(postId);
update_user.data.changes.posts.add.push(postId);
update_user.save(function(err, saved){
console.log('link');
});
});
}
}
//adds gamepin to user's posts
var link = exports.link = function(userId, pinId){
console.log("userId: " + userId + " pinID: " + pinId);
usr = app.riak.bucket('users').objects.new(userId);
usr.fetch(user_resolve, function(err, obj){
if(err){
console.log('err');
console.log(err);
}
clearChanges(obj);
console.log('userId: '+userId + ' pinId:' + pinId);
console.log('before: [' + obj.data.posts + ']');
//add this pin to the user object
if(obj.data.posts.indexOf(pinId) === -1){
obj.data.posts.push(pinId);
obj.data.changes.posts.add.push(pinId);
console.log('happening');
}
obj.save(function(err, saved){
console.log('after: [' +saved.data.posts + ']');
console.log('linked '+ pinId + 'to ' + userId);
});
});
}
//clear all posts from a user
var clearLinks = exports.clearLinks = function(userId){
usr = app.riak.bucket('users').objects.new(userId);
usr.fetch(user_resolve, function(err, obj){
if(err){
console.log("Fetch User: " + err);
return;
}
//clear changes
clearChanges(obj);
obj.data.posts = [];
obj.save(function(err, saved){
console.log('after: [' +saved.data.posts + ']');
});
});
}
//Like a pin. Sets up 2 way link between gamepin and user.
var like = exports.like = function(userId, pinId){
//check if pin exists
app.riak.bucket('gamepins').object.exists(pinId, function(err, result){
if(err){
console.log('Pin Exists: ' + err);
return;
}
if(result) next();
else console.log('Error: pin ' + pinId + 'not found in db');
});
//add pinId to user's likes[]
function next(){
usr = app.riak.bucket('users').objects.new(userId);
usr.fetch(user_resolve, function(err, obj){
if(err){
console.log('Fetch User:' + err);
return;
}
clearChanges(obj);
console.log(userId + ' liked pin #'+pinId);
if(obj.data.likes.indexOf(pinId) === -1){
obj.data.likes.push(pinId);
obj.data.changes.likes.add.push(pinId);
}
else
console.log('Error: '+ userId + 'already liked this pin');
obj.save(function(err, saved){
console.log('like added to '+userId+': [' +saved.data.likes + ']');
next2();
});
});
}
//add like to the gamepin's likedBy[]
function next2(){
var pin = app.riak.bucket('gamepins').object.new(pinId);
pin.fetch(pin_resolve, function(err, obj){
if(err){
console.log("Fetch Pin: " +err);
return;
}
//clear changes
clearChanges(obj);
if(obj.data.likedBy.indexOf(userId) === -1){
obj.data.likedBy.push(userId);
obj.data.changes.likedBy.add.push(userId);
}
else
console.log('Error: ' + userId + 'already in pin '+ pinId + 's likedBy list');
obj.save(function(err, saved){
console.log("gamepin's likedBy list [" + saved.data.likedBy + "]");
});
});
}
}
//Unlike a pin. Removes 2 way between gamepin and user.
var unlike = exports.unlike = function(userId, pinId){
//check if pin exists
app.riak.bucket('gamepins').object.exists(pinId, function(err, result){
if(err){
console.log('GamePin Exists: ' + err);
return;
}
if(result) next();
else console.log('Error: pin ' + pinId + 'not found in db');
});
//remove pinId from user's likes
function next(){
usr = app.riak.bucket('users').objects.new(userId);
usr.fetch(user_resolve, function(err, obj){
if(err){
console.log('Fetch User:' + err);
return;
}
//clear changes
clearChanges(obj);
console.log(userId + ' unliked pin #'+pinId);
if(obj.data.likes.indexOf(pinId) !== -1){
obj.data.likes.splice(obj.data.likes.indexOf(pinId), 1);
obj.data.changes.likes.remove.push(pinId);
}
obj.save(function(err, saved){
console.log('like removed from '+userId+': [' +saved.data.likes + ']');
next2();
});
});
}
//remove userId from pin's likedBy
function next2(){
pin = app.riak.bucket('gamepins').objects.new(pinId);
pin.fetch(pin_resolve, function(err, obj){
if(err){
console.log('Fetch Pin:' + err);
return;
}
//clear changes
clearChanges(obj);
console.log('Pin #'+pinId + ' removed ' +userId+ 'from likedBy list');
if(obj.data.likedBy.indexOf(userId) !== -1){
obj.data.likedBy.splice(obj.data.likedBy.indexOf(userId), 1);
obj.data.changes.likedBy.remove.push(userId);
}
obj.save(function(err, saved){
console.log('result liked list: [' + saved.data.likedBy + ']');
});
});
}
}
//Follow sets up 2 way link from user to user.
var follow = exports.follow = function(sourceId, targetId){
var src = app.riak.bucket('users').objects.new(sourceId);
var targ = app.riak.bucket('users').objects.new(targetId);
//source user adds target to following
src.fetch(user_resolve, function(err, obj){
if(err){
console.log("Fetch User: " + err);
return;
}
clearChanges(obj);
if(sourceId === targetId){
console.log("Error: cannot follow yourself");
return;
}
if(obj.data.following.indexOf(targetId) === -1){
obj.data.following.push(targetId);
obj.data.changes.following.add.push(targetId);
obj.save(function(err,saved){
console.log(sourceId + " following ["+ saved.data.following +"]");
next();
});
}
else{
console.log("User " + targetId + " aready on following list");
return;
}
});
function next(){
//target user adds source to followers
targ.fetch(user_resolve, function(err, obj){
if(err){
console.log("Fetch User " + err);
return;
}
clearChanges(obj);
if(sourceId === targetId){
console.log("Error: cannot follow yourself");
return;
}
if(obj.data.followers.indexOf(sourceId) === -1){
obj.data.followers.push(sourceId);
obj.data.changes.followers.add.push(sourceId);
obj.save(function(err, saved){
console.log(targetId + " followers ["+ saved.data.followers +"]");
});
}
else{
console.log("User " + sourceId + " aready on followers list");
return;
}
});
}
}
//Unfollow remotes 2 way link from user to user.
var unfollow = exports.unfollow = function(sourceId, targetId){
var src = app.riak.bucket('users').objects.new(sourceId);
var targ = app.riak.bucket('users').objects.new(targetId);
//source user removes target from following
src.fetch(user_resolve, function(err, obj){
if(err){
console.log("Fetch User: " + err);
return;
}
clearChanges(obj);
if(sourceId === targetId){
console.log("Error: cannot unfollow yourself");
return;
}
if(obj.data.following.indexOf(targetId) !== -1){
obj.data.following.splice(obj.data.following.indexOf(targetId), 1);
obj.data.changes.following.remove.push(targetId);
obj.save(function(err,saved){
console.log(sourceId + " following ["+ saved.data.following +"]");
next();
});
}
else{
console.log("User " + targetId + " not on following list");
return;
}
});
function next(){
//target user removes source from followers
targ.fetch(user_resolve, function(err, obj){
if(err){
console.log("Fetch User: " + err);
return;
}
clearChanges(obj);
if(sourceId === targetId){
console.log("Error: cannot unfollow yourself");
return;
}
if(obj.data.followers.indexOf(sourceId) !== -1){
obj.data.followers.splice(obj.data.followers.indexOf(sourceId), 1);
obj.data.changes.followers.remove.push(sourceId);
obj.save(function(err, saved){
console.log(targetId + " followers ["+ saved.data.followers +"]");
});
}
else{
console.log("User " + sourceId + " not on followers list");
return;
}
});
}
}
//Create new gamepin or overwrite existing one.
var postPin = exports.postPin = function(pinId, pinData){
//check if pin exists
app.riak.bucket('gamepins').object.exists(pinId, function(err, result){
if(err){
console.log('Fetch Pin: ' + err);
return;
}
if(result){
overwrite();
console.log('Pin already exists in db. Overwriting pin #' + pinId);
}
else{
create();
console.log('Creating new pin # ' + pinId);
}
});
function create(){
new_pin = app.riak.bucket('gamepins').object.new(pinId, pinData);
new_pin.data.datePosted = getDate();
new_pin.save(function(err, saved){
console.log(saved.data.posterId +' posted pin #'+pinId);
link(saved.data.posterId, pinId);
});
}
function overwrite(){
var old_pin = app.riak.bucket('gamepins').object.new(pinId);
var new_pin = app.riak.bucket('gamepins').object.new(pinId, pinData);
old_pin.fetch(pin_resolve, function(err, obj){
if(err){
console.log('Fetch Pin: ' + err);
return;
}
clearChanges(obj);
new_pin.metadata.vclock = obj.metadata.vclock;
new_pin.save(function(err, saved){
console.log('Pin #' + pinId + 'overwritten');
link(saved.data.posterId, pinId);
});
});
}
}
//same as postPin, but given a repinVia param
var repin = exports.repin = function(pinId, pinData){
//check if pin exists
app.riak.bucket('gamepins').object.exists(pinId, function(err, result){
if(err){
console.log('Pin Exists: ' + err);
return;
}
if(!result) next();
else console.log('Error: pin ' + pinId + 'already exists in db');
});
function next(){
new_pin = app.riak.bucket('gamepins').object.new(pinId, pinData);
new_pin.save(function(err, saved){
console.log(saved.data.posterId +' repinned pin #'+pinId+ 'via '+saved.data.repinVia);
link(saved.data.posterId, pinId);
});
}
}
//TODO
/*editPin = exports.editPin = function(pinId, pinData){
//check if pin exists
app.riak.bucket('gamepins').object.exists(pinId, function(err, result){
if(err){
console.log('Pin Exists: ' + err);
return;
}
if(result) next();
else console.log('Error: pin ' + pinId + 'does not exist in db');
});
function next(){
var old_pin = app.riak.bucket('gamepins').object.new(pinId);
var new_pin = app.riak.bucket('gamepins').object.new(pinId, pinData);
old_pin.fetch(pin_resolve, function(err, obj){
if(err){
console.log('Fetch Pin: ' + err);
return;
}
clearChanges(obj);
new_pin.metadata.vclock = obj.metadata.vclock;
new_pin.save(function(err, saved){
console.log('Pin #' + pinId + 'overwritten');
});
});
}
}*/
//Remove poster reference, like references, and then delete the gamepin
deletePin = exports.deletePin = function(pinId){
old_pin = app.riak.bucket('gamepins').object.new(pinId);
old_pin.fetch(pin_resolve, function(err, pin_obj){
if(err){
console.log("Fetch Pin: " + err);
return;
}
clearChanges(pin_obj);
usr = app.riak.bucket('users').object.new(pin_obj.data.posterId);
//remove reference from pin owner
usr.fetch(user_resolve, function(err, usr_obj){
if(err){
console.log("Fetch User: " + err);
//if pin owner does not exist, delete the pin anyways
if(err.status_code === 404) next();
return;
}
clearChanges(usr_obj);
if(usr_obj.data.posts.indexOf(pinId) === -1){
console.log("Error: User does not own this pin");
return;