forked from youiest/youiest-unionize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
304 lines (248 loc) · 7.55 KB
/
lib.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
var WIModel = function(options){
return {
"_id": options._id,
"inbox": [],
"outbox": [],
"follow": [],
"big": [],
"seen": [],
"vote": [],
"feed": [],
"recommend": [],
"profile": {
"profile_picture": "http://i.imgur.com/vaCjg.jpg"
}
}
}
var getimages = function(options){
return {
"XP": 92,
"YP": 5,
"_id": options._id,
"clientUpdate": true,
"cycleComplete": false,
"from": "nicolsondsouza",
"imageId": "eRH8i6MTWJG9XtsptfTGqfe34JLs2ZDmvbfrShs3cdyi3JZLtqo",
"image_low": "http://i.imgur.com/P96QpsF.jpg",
"journey": "Arra",
"key": "",
"profile_picture": "http://i.imgur.com/vaCjg.jpg",
"startTime": 1431521396599,
"to": "eRH8i6MTWJG9XtsptfTGqfe34JLs2ZDmvbfrShs3cdyi3JZLtqo",
"userId": "nicolsondsouza",
"source": "instagram"
}
}
Unionize = {};
WI = new Mongo.Collection("wi");
W = new Mongo.Collection("w");
log = console.log.bind(console);
// var keys = {};
// keys.outbox = "inbox";
// keys.follow = "follower";
// Unionize.keys = keys;
Unionize.hooks = {};
Unionize.hooks.outbox = function(){
}
// this.modModifier = {};
// modModifier.outbox = function(modifier, userId) {
// var from, inserted, new_key, old_key, to;
// old_key = 'outbox';
// new_key = 'sending';
// if (old_key !== new_key) {
// smite(modifier, 'needs a new agenda', eval(s));
// smite(eval(Object.defineProperty(modifier.$push, new_key, Object.getOwnPropertyDescriptor(modifier.$push, old_key))));
// smite(eval(delete modifier.$push[old_key], 'deleted key', eval(s)));
// }
// smite('did we insert into W?', modifier, modifier.$push, from = modifier.$push.sending.from, to = modifier.$push.sending.to, eval(s));
// inserted = W.insert({
// to: to,
// from: from
// });
// smite(inserted, 'how long did the insert hook take? usually 30ms', eval(s));
// return modifier;
// };
Unionize.getUTC = function(){
return new Date().getTime();
}
Unionize.exists = function(userId){
return WI.find(userId).count()
}
Unionize.prepare = function(userId){
if(Unionize.exists(userId) == 0){
var wiModel = new WIModel({"_id": userId});
WI.insert(wiModel);
return false;
}
return true;
}
Unionize.validateDocs = function(docs){
if(!docs)
throw new Meteor.Error("Please check information provided undefined", "404");
if(!docs.from){
throw new Meteor.Error("Source is not defined from", "404");
}
if(!docs.to)
throw new Meteor.Error("Target is not defined to", "404");
}
Unionize.connect = function(docs){
Unionize.validateDocs(docs);
Unionize.prepare(docs.from);
docs.startTime = Unionize.getUTC();
docs.journey = [{"onConnect": Unionize.getUTC()- docs.startTime}];
var update = {};
update["outbox"] = docs;
WI.update(docs.from,{$push: update});
}
Unionize.sampleFollow = function(){
WI.update(userId, {$push: {"follow": {
"_id": Random.id(),
"from": "eliasmoosman",
"to": "nicolsondsouza",
"image_low": "http://i.imgur.com/EAyLXgp.jpg"
}}});
}
Unionize.samplefeed = function(){
WI.update(userId, {$push: {"feed": {
"_id": Random.id(),
"from": "eliasmoosman",
"to": "nicolsondsouza",
"image_low": "http://i.imgur.com/EAyLXgp.jpg"
}}});
}
Unionize.sampleInbox = function(docs){
var update = new getimages({"_id": Random.id()});
update.source = "";
WI.update(userId,{$push: {"inbox":update}});
}
Unionize.getNewInagesInbox = function(docs){
var update = new getimages({"_id": Random.id()});
WI.update(userId,{$push: {"inbox":update}});
}
Unionize.getNewInagesRecommend = function(docs){
var update = new getimages({"_id": Random.id()});
WI.update(userId,{$push: {"recommend":update}});
}
// depricated
// Unionize.connectF = function(docs){
// Unionize.validateDocs(docs);
// Unionize.prepare(docs.from);
// docs.startTime = Unionize.getUTC();
// docs.journey = [{"onConnect": Unionize.getUTC()- docs.startTime}];
// WI.update(docs.from,{$push: {"follow": docs}});
// }
// hooks
// Unionize.onWUpdateHookFollow = function(userId, docs){
// }
// WI.insert.before(function(docs){
// });
// WI.insert.after(function(docs){
// });
// log(W.before.insert())
// W.before.insert(function(userId, docs){
// // Unionize.onWInsertHook(userId, docs);
// });
WI.before.update(function(userId, doc, fieldNames, modifier, options){
try{
// var fieldName, modifier, _i, _len;
// for (_i = 0, _len = fieldNames.length; _i < _len; _i++) {
// fieldName = fieldNames[_i];
// if (_.has(afterModifier, fieldName)) {
// // smite(fieldName, 'spinning afterModifier', eval(s));
// modifier = afterModifier[fieldName](modifier, doc, userId);
// }
// }
log(fieldNames, modifier)
var key = fieldNames[0];
console.log(key)
if(key && Unionize.hooks[key] && modifier["$push"]){ // && modifier["$push"][key]
var docs = modifier["$push"][key];
if(docs.cycleComplete)
return;
if(key == "inbox"){
if(docs.source == "instagram"){
Unionize.hooks["frominstagram"](userId, docs);
}
else if(docs.source == "facebook"){
Unionize.hooks["fromFacebook"](userId, docs);
}else{
Unionize.hooks["inbox"](userId, docs);
}
}else{
Unionize.hooks[key](userId, docs);
}
// modifier["$push"][key] = Unionize.onWUpdateHook(userId, docs, keys[key]);
docs = modifier["$push"][key];
// docs.journey.push({"onInsertWIInbox": Unionize.getUTC() - docs.startTime});
}
return docs;
// else if(fieldNames[0] == "follow"){
// modifier["$push"].follow = Unionize.onWUpdateHookFollow(userId, modifier["$push"].follow);
// var docs = modifier["$push"].follow;
// }
}
catch(error){
console.error(error);
}
});
// W.after.update(function(){
// });
Unionize.hooks.outbox = Unionize.onWUpdateHook = function(userId, docs, key){
log("Unionize.onWInsertHook");
// log(docs.clientUpdate,Meteor.isServer)
if(docs.clientUpdate && Meteor.isServer)
return docs;
if(Meteor.isClient){
if(!Unionize.exists(docs.to))
return docs;
docs.clientUpdate = true;
}
Unionize.prepare(docs.to);
// docs.journey.push({"onWUpdateHook": Unionize.getUTC()- docs.startTime});
// console.log(docs._id,Meteor.isClient,Meteor.isServer)
docs.key = key;
docs.cycleComplete = true;
W.insert(docs);
// docs.journey.push({"onInsertW": Unionize.getUTC()- docs.startTime});
var update = {};
update["inbox"] = docs;
WI.update(docs.to,{$push: update});
// docs.journey.push({"onInsertWIInbox": Unionize.getUTC()- docs.startTime});
// if(WI.find(docs.to).count()){
// // log("to updated");
// }
return docs;
// replicated on W collection
}
Unionize.hooks.inbox = function(userId, docs, key){
log("hooks_inbox");
// log(userId, docs, key);
}
Unionize.hooks.feed = function(userId, docs, key){
log("hooks_feed");
// log(userId, docs, key);
}
Unionize.hooks.follow = function(userId, docs, key){
log("hooks_follows");
// log(userId, docs, key);
}
Unionize.hooks.recommend = function(userId, docs, key){
log("hooks_recommended");
// log(userId, docs, key);
}
Unionize.hooks.seen = function(userId, docs, key){
log("hooks_seen");
// log(userId, docs, key);
}
Unionize.hooks.vote = function(userId, docs, key){
log("hooks_vote");
// log(userId, docs, key);
}
Unionize.hooks.fromFacebook = function(userId, docs, key){
log("hooks_fromFacebook");
// log(userId, docs, key);
}
Unionize.hooks.frominstagram = function(userId, docs, key){
log("hooks_fromFacebook");
// log(userId, docs, key);
}