UNPKG

10.9 kBJavaScriptView Raw
1'use strict';
2const co = require('co');
3const http = require('./utilities/http');
4const getUserIdByName = require('./utilities/getIdByName');
5const variables = require('./variables');
6const httpSettings = http.SETTINGS;
7
8function profile(user_id, token, content, skip) {
9 return co(function *resolveUsername() {
10 if(isNaN(user_id)) {
11 user_id = yield getUserIdByName(user_id);
12 }
13
14 if(content == undefined) {content="all"}
15 if(skip == undefined) {skip=0}
16
17 const url = `${variables['API']}/users/${user_id}`;
18 let parameters = { app: 3, plat: 2 };
19 if (content != null && content != undefined) {
20 parameters = { app: 3, plat: 2, content };
21 }
22 if (skip != null && skip != undefined) {
23 parameters = { app: 3, plat: 2, skip };
24 }
25 if (content != null && content!= undefined && skip != null && skip != undefined) {
26 parameters = { app: 3, plat: 2, content, skip };
27 }
28 if (token != null && token != undefined) {
29 const token_id = token["id"];
30 const token_key = token["key"];
31 const user_id = token["user_id"];
32 parameters = {
33 app: 3,
34 plat: 2,
35 content,
36 skip,
37 token_id, token_key, user_id
38 };
39 }
40
41 return http
42 .GET(url, parameters)
43 .then(data => data.profile);
44 });
45}
46
47function rant(rant_id, token) {
48 return co(function *getrant() {
49 const url = `${variables['API']}/devrant/rants/${rant_id}`;
50 let parameters = { app: 3 };
51 console.log(token)
52 if (token != null && token != undefined) {
53 const token_id = token["id"];
54 const token_key = token["key"];
55 const user_id = token["user_id"];
56 parameters = {
57 app: 3,
58 token_id, token_key, user_id
59 };
60 }
61
62 return http
63 .GET(url, parameters);
64 });
65}
66
67function rants(sort, limit, skip, prev_set, token, range) {
68 const url = `${variables['API']}/devrant/rants`;
69
70 let parameters = {
71 app: 3,
72 sort, limit, skip, prev_set
73 };
74
75 if(range != undefined && range != null) {
76 parameters = {
77 app: 3,
78 sort, limit, skip, prev_set, range
79 };
80 }
81
82 if (token != null && token != undefined) {
83 const token_id = token["id"];
84 const token_key = token["key"];
85 const user_id = token["user_id"];
86 parameters = {
87 app: 3,
88 sort, limit, skip, token_id, token_key, user_id, prev_set
89 };
90 if(range != undefined && range != null) {
91 parameters = {
92 app: 3,
93 sort, limit, skip, range, token_id, token_key, user_id, prev_set
94 };
95 }
96 }
97
98 return http
99 .GET(url, parameters)
100 .then(data => data);
101}
102
103function search(query) {
104 const url = `${variables['API']}/devrant/search`;
105 const parameters = {
106 app: 3,
107 term: query
108 };
109
110 return http
111 .GET(url, parameters)
112 .then(data => data.results);
113}
114
115function getFrequentSearchTerms() {
116 const url = `${variables['API']}/devrant/search/tags`;
117 const parameters = {
118 app: 3,
119 plat: 3
120 };
121
122 return http
123 .GET(url, parameters)
124 .then(data => data.tags);
125}
126
127function login(email, passwd) {
128 const url = `${variables['API']}/users/auth-token`;
129 const parameters = {
130 app: 3,
131 username: email,
132 password: passwd,
133 plat: 3
134 };
135
136 return http
137 .POST(url, parameters);
138}
139
140function postRant(rant, tags, type, token, imagePath) {
141 const url = `${variables['API']}/devrant/rants`;
142
143 if(type == undefined) {type = 1}
144
145 const token_id = token["id"];
146 const token_key = token["key"];
147 const user_id = token["user_id"];
148
149 const parameters = {
150 app: 3,
151 plat: 3,
152 rant: rant,
153 tags: tags,
154 type: type,
155 token_id: token_id,
156 token_key: token_key,
157 user_id: user_id
158 };
159
160 if(imagePath !== undefined && imagePath !== null) {
161 return http.POST_FILE(url, parameters, imagePath);
162 } else {
163 return http.POST(url, parameters);
164 }
165}
166
167function editRant(text, tags, rant_id, token, imagePath) {
168 const url = `${variables['API']}/rants/${rant_id}`;
169
170 const token_id = token["id"];
171 const token_key = token["key"];
172 const user_id = token["user_id"];
173
174 const parameters = {
175 app: 3,
176 plat: 2,
177 rant: text,
178 tags: tags,
179 token_id: token_id,
180 token_key: token_key,
181 user_id: user_id
182 };
183
184 if(imagePath !== undefined && imagePath !== null) {
185 return http.POST_FILE(url, parameters, imagePath);
186 } else {
187 return http.POST(url, parameters);
188 }
189}
190
191function postComment(text, rant_id, token, imagePath) {
192 const url = `${variables['API']}/devrant/rants/${rant_id}/comments`;
193
194 const token_id = token["id"];
195 const token_key = token["key"];
196 const user_id = token["user_id"];
197
198 const parameters = {
199 app: 3,
200 plat: 2,
201 comment: text,
202 token_id: token_id,
203 token_key: token_key,
204 user_id: user_id
205 };
206
207 if(imagePath !== undefined && imagePath !== null) {
208 return http.POST_FILE(url, parameters, imagePath);
209 } else {
210 return http.POST(url, parameters);
211 }
212}
213
214function editComment(text, comment_id, token, imagePath) {
215 const url = `${variables['API']}/comments/${comment_id}`;
216
217 const token_id = token["id"];
218 const token_key = token["key"];
219 const user_id = token["user_id"];
220
221 const parameters = {
222 app: 3,
223 plat: 2,
224 comment: text,
225 token_id: token_id,
226 token_key: token_key,
227 user_id: user_id
228 };
229
230 if(imagePath !== undefined && imagePath !== null) {
231 return http.POST_FILE(url, parameters, imagePath);
232 } else {
233 return http.POST(url, parameters);
234 }
235}
236
237function favorite(isfav, rant_id, token) {
238 const url = `${variables['API']}/devrant/rants/${rant_id}/favorite`;
239 if(isfav === false) {
240 const url = `${variables['API']}/devrant/rants/${rant_id}/unfavorite`;
241 }
242 const token_id = token["id"];
243 const token_key = token["key"];
244 const user_id = token["user_id"];
245
246 const parameters = {
247 app: 3,
248 plat: 2,
249 token_id: token_id,
250 token_key: token_key,
251 user_id: user_id
252 };
253
254 return http
255 .POST(url, parameters);
256}
257
258function subscribe(bool, userToSubscribe, token) {
259 const url = `${variables['API']}/users/${userToSubscribe}/subscribe`;
260 const token_id = token["id"];
261 const token_key = token["key"];
262 const user_id = token["user_id"];
263
264 const parameters = {
265 app: 3,
266 plat: 2,
267 token_id: token_id,
268 token_key: token_key,
269 user_id: user_id
270 };
271
272 if (!bool) {
273 return http.DELETE(url, parameters);
274 }
275
276 return http
277 .POST(url, parameters);
278}
279
280function deleteRant(rant_id, token) {
281 const url = `${variables['API']}/devrant/rants/${rant_id}`;
282 const token_id = token["id"];
283 const token_key = token["key"];
284 const user_id = token["user_id"];
285
286 const parameters = {
287 app: 3,
288 plat: 2,
289 token_id: token_id,
290 token_key: token_key,
291 user_id: user_id
292 };
293
294 return http.DELETE(url, parameters);
295}
296
297function deleteComment(comment_id, token) {
298 const url = `${variables['API']}/comments/${comment_id}`;
299 const token_id = token["id"];
300 const token_key = token["key"];
301 const user_id = token["user_id"];
302
303 const parameters = {
304 app: 3,
305 plat: 2,
306 token_id: token_id,
307 token_key: token_key,
308 user_id: user_id
309 };
310
311 return http.DELETE(url, parameters);
312}
313
314function vote(vote, rant_id, token) {
315 const url = `${variables['API']}/devrant/rants/${rant_id}/vote`;
316
317 const token_id = token["id"];
318 const token_key = token["key"];
319 const user_id = token["user_id"];
320
321 const parameters = {
322 app: 3,
323 plat: 3,
324 vote: vote,
325 token_id: token_id,
326 token_key: token_key,
327 user_id: user_id
328 };
329
330 return http
331 .POST(url, parameters);
332}
333
334function voteComment(vote, comment_id, token) {
335 const url = `${variables['API']}/comments/${comment_id}/vote`;
336
337 const token_id = token["id"];
338 const token_key = token["key"];
339 const user_id = token["user_id"];
340
341 const parameters = {
342 app: 3,
343 token_id: token_id,
344 token_key: token_key,
345 user_id: user_id,
346 vote: vote,
347 };
348
349 return http
350 .POST(url, parameters);
351}
352
353function notifications(token, last_time) {
354 if(last_time === undefined) { last_time = 0; }
355 const url = `${variables['API']}/users/me/notif-feed`;
356 const parameters = {
357 app: 3,
358 token_id: token["id"],
359 token_key: token["key"],
360 user_id: token["user_id"],
361 last_time: last_time,
362 plat: 2,
363 ext_prof: 1,
364 };
365
366 return http
367 .GET(url, parameters)
368}
369
370function clearNotifications(token) {
371 const url = `${variables['API']}/users/me/notif-feed`;
372 const parameters = {
373 app: 3,
374 token_id: token["id"],
375 token_key: token["key"],
376 user_id: token["user_id"],
377 plat: 2,
378 };
379
380 return http
381 .DELETE(url, parameters)
382}
383
384function collabs(sort, limit, skip, token) {
385 if(sort == undefined)
386 sort = 'recent';
387 const url = `${variables['API']}/devrant/collabs`;
388
389 let parameters = {
390 app: 3,
391 sort, limit, skip
392 };
393
394 if (token != null && token != undefined) {
395 const token_id = token["id"];
396 const token_key = token["key"];
397 const user_id = token["user_id"];
398 parameters = {
399 app: 3,
400 sort, limit, skip, token_id, token_key, user_id
401 };
402 }
403
404 return http
405 .GET(url, parameters)
406 .then(data => data.rants);
407}
408
409function stories(range, sort, limit, skip, token) {
410 if(range == undefined)
411 range = 'week';
412 if(sort == undefined)
413 sort = 'recent';
414 // sort = ['recent', 'top']
415 // range = ['day', 'week', 'month', 'all']
416 const url = `${variables['API']}/devrant/story-rants`;
417
418 let parameters = {
419 app: 3,
420 range, sort, limit, skip
421 };
422
423 if (token != null && token != undefined) {
424 const token_id = token["id"];
425 const token_key = token["key"];
426 const user_id = token["user_id"];
427 parameters = {
428 app: 3,
429 range, sort, limit, skip, token_id, token_key, user_id
430 };
431 }
432
433 return http
434 .GET(url, parameters)
435 .then(data => data.rants);
436}
437
438function weekly(week, sort, limit, skip, token) {
439 if(sort == undefined)
440 sort = 'recent';
441 // sort = ['algo', recent', 'top']
442 // week = <week_number>
443 const url = `${variables['API']}/devrant/weekly-rants`;
444
445 let parameters = {
446 app: 3,
447 week, sort, limit, skip
448 };
449
450 if (token != null && token != undefined) {
451 const token_id = token["id"];
452 const token_key = token["key"];
453 const user_id = token["user_id"];
454 parameters = {
455 app: 3,
456 week, sort, limit, skip, token_id, token_key, user_id
457 };
458 }
459
460 return http
461 .GET(url, parameters)
462 .then(data => data.rants);
463}
464
465function listWeekly(token) {
466 const url = `${variables['API']}/devrant/weekly-list`;
467
468 let parameters = {
469 app: 3,
470 plat: 2,
471 };
472
473 if (token != null && token != undefined) {
474 const token_id = token["id"];
475 const token_key = token["key"];
476 const user_id = token["user_id"];
477 parameters = {
478 app: 3,
479 plat: 2,
480 token_id, token_key, user_id
481 };
482 }
483
484 return http
485 .GET(url, parameters)
486 .then(data => data.weeks);
487}
488
489
490function surpriseRant(token) {
491 const url = `${variables['API']}/devrant/rants/surprise`;
492 const token_id = token["id"];
493 const token_key = token["key"];
494 const user_id = token["user_id"];
495
496 const parameters = {
497 app: 3,
498 plat: 2,
499 token_id: token_id,
500 token_key: token_key,
501 user_id: user_id
502 };
503
504 return http.GET(url, parameters);
505}
506
507module.exports = {
508 httpSettings,
509 profile,
510 rant,
511 rants,
512 search,
513 login,
514 postComment,
515 postRant,
516 vote,
517 voteComment,
518 surpriseRant,
519 notifications,
520 clearNotifications,
521 collabs,
522 stories,
523 weekly,
524 listWeekly,
525 favorite,
526 subscribe,
527 deleteRant,
528 deleteComment,
529 getFrequentSearchTerms,
530 editComment,
531 editRant
532}