This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.js
637 lines (506 loc) · 16.1 KB
/
client.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
/*
This file is a part of libertysoil.org website
Copyright (C) 2015 Loki Education (Social Enterprise)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import fetch from 'isomorphic-fetch';
import FormData from 'form-data';
import { format as format_url, parse as parse_url } from 'url';
import { stringify } from 'querystring';
import { extend, merge as mergeObj } from 'lodash';
export default class ApiClient
{
host;
serverReq = null;
constructor(host, serverReq = null) {
this.host = host;
this.serverReq = serverReq;
}
apiUrl(relativeUrl) {
return `${this.host}${relativeUrl}`;
}
apiUrlForFetch(relativeUrl, query = {}) {
const urlObj = parse_url(this.apiUrl(relativeUrl));
urlObj.query = mergeObj(urlObj.query, query);
return format_url(urlObj);
}
async handleResponseError(response) {
if (!response.ok) {
let json, errorMessage;
errorMessage = response.statusText;
try {
json = await response.json();
errorMessage = json.error || errorMessage;
} catch (e) {
throw new Error(errorMessage);
}
throw new Error(errorMessage);
}
}
async get(relativeUrl, query = {}) {
let defaultHeaders = {};
if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
defaultHeaders = { Cookie: this.serverReq.headers['cookie'] };
}
const response = await fetch(
this.apiUrlForFetch(relativeUrl, query),
{
credentials: 'same-origin',
headers: defaultHeaders
}
);
await this.handleResponseError(response);
return response;
}
async head(relativeUrl, query = {}) {
let defaultHeaders = {};
if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
defaultHeaders = { Cookie: this.serverReq.headers['cookie'] };
}
return fetch(
this.apiUrlForFetch(relativeUrl, query),
{
credentials: 'same-origin',
method: 'HEAD',
headers: defaultHeaders
}
);
}
async del(relativeUrl) {
let defaultHeaders = {};
if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
defaultHeaders = { Cookie: this.serverReq.headers['cookie'] };
}
const response = await fetch(
this.apiUrlForFetch(relativeUrl),
{
credentials: 'same-origin',
method: 'DELETE',
headers: defaultHeaders
}
);
await this.handleResponseError(response);
return response;
}
async post(relativeUrl, data = null) {
let headers = {
'Content-Type': 'application/x-www-form-urlencoded'
},
body;
if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
headers = {
Cookie: this.serverReq.headers['cookie'],
'Content-Type': 'application/x-www-form-urlencoded'
};
}
if (data !== null) {
body = stringify(data);
}
const response = await fetch(
this.apiUrl(relativeUrl),
{
credentials: 'same-origin',
method: 'POST',
headers,
body
}
);
await this.handleResponseError(response);
return response;
}
/*
*post without setting content type
*/
async postMultipart(relativeUrl, data = null) {
let headers = {},
body;
if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
headers = {
Cookie: this.serverReq.headers['cookie']
};
}
if (data !== null) {
body = data;
}
const response = await fetch(
this.apiUrl(relativeUrl),
{
credentials: 'same-origin',
method: 'POST',
headers,
body
}
);
await this.handleResponseError(response);
return response;
}
async postJSON(relativeUrl, data = null) {
let headers = {
'Content-Type': 'application/json'
},
body;
if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
headers = {
Cookie: this.serverReq.headers['cookie'],
'Content-Type': 'application/json'
};
}
if (data !== null) {
body = JSON.stringify(data);
}
const response = await fetch(
this.apiUrl(relativeUrl),
{
credentials: 'same-origin',
method: 'POST',
headers,
body
}
);
await this.handleResponseError(response);
return response;
}
async subscriptions(offset = 0) {
const response = await this.get(`/api/v1/posts?offset=${offset}`);
return await response.json();
}
async checkUserExists(username) {
const result = await this.head(`/api/v1/user/${username}`);
return result.ok;
}
async checkEmailTaken(email) {
const result = await this.head(`/api/v1/user/email/${email}`);
return result.ok;
}
async getAvailableUsername(username) {
const response = await this.get(`/api/v1/user/available-username/${username}`);
const json = await response.json();
return json.username;
}
async userInfo(username) {
const response = await this.get(`/api/v1/user/${username}`);
return await response.json();
}
async checkSchoolExists(name) {
const result = await this.head(`/api/v1/school/${name}`);
return result.ok;
}
async getSchool(school_name) {
const response = await this.get(`/api/v1/school/${school_name}`);
return await response.json();
}
async schools(query = {}) {
const response = await this.get('/api/v1/schools', query);
return await response.json();
}
async schoolsAlphabet() {
const response = await this.get('/api/v1/schools-alphabet');
return await response.json();
}
async userPosts(username, query = {}) {
const response = await this.get(`/api/v1/posts/user/${username}`, query);
return await response.json();
}
async relatedPosts(postId) {
const response = await this.get(`/api/v1/post/${postId}/related-posts`);
const json = await response.json();
if (!response.ok) {
throw new Error(json.error);
}
return json;
}
async userTags() {
const response = await this.get(`/api/v1/user/tags`);
return await response.json();
}
async userLikedPosts() {
const response = await this.get(`/api/v1/posts/liked`);
return await response.json();
}
async schoolPosts(schoolUrlName) {
const response = await this.get(`/api/v1/posts/school/${schoolUrlName}`);
return await response.json();
}
async getLikedPosts(username) {
const response = await this.get(`/api/v1/posts/liked/${username}`);
return await response.json();
}
async userFavouredPosts() {
const response = await this.get(`/api/v1/posts/favoured`);
return await response.json();
}
async getFavouredPosts(username) {
const response = await this.get(`/api/v1/posts/favoured/${username}`);
return await response.json();
}
async tagPosts(tag) {
const response = await this.get(`/api/v1/posts/tag/${tag}`);
return await response.json();
}
async geotagPosts(geotagUrlName) {
const response = await this.get(`/api/v1/posts/geotag/${geotagUrlName}`);
return await response.json();
}
async city(city_id) {
const response = await this.get(`/api/v1/city/${city_id}`);
return await response.json();
}
async countries() {
const response = await this.get(`/api/v1/countries/`);
return await response.json();
}
async country(country_code) {
const response = await this.get(`/api/v1/country/${country_code}`);
return await response.json();
}
async like(postId) {
const response = await this.post(`/api/v1/post/${postId}/like`);
return await response.json();
}
async unlike(postId) {
const response = await this.post(`/api/v1/post/${postId}/unlike`);
return await response.json();
}
async likeHashtag(name) {
const response = await this.post(`/api/v1/tag/${name}/like`);
return await response.json();
}
async unlikeHashtag(name) {
const response = await this.post(`/api/v1/tag/${name}/unlike`);
return await response.json();
}
async likeSchool(urlName) {
const response = await this.post(`/api/v1/school/${urlName}/like`);
return await response.json();
}
async unlikeSchool(urlName) {
const response = await this.post(`/api/v1/school/${urlName}/unlike`);
return await response.json();
}
async likeGeotag(urlName) {
const response = await this.post(`/api/v1/geotag/${urlName}/like`);
return await response.json();
}
async unlikeGeotag(urlName) {
const response = await this.post(`/api/v1/geotag/${urlName}/unlike`);
return await response.json();
}
async fav(postId) {
const response = await this.post(`/api/v1/post/${postId}/fav`);
return await response.json();
}
async unfav(postId) {
const response = await this.post(`/api/v1/post/${postId}/unfav`);
return await response.json();
}
async follow(userName) {
const response = await this.post(`/api/v1/user/${userName}/follow`);
return await response.json();
}
async ignoreUser(userName) {
const response = await this.post(`/api/v1/user/${userName}/ignore`);
return await response.json();
}
async updateUser(user) {
const response = await this.postJSON(`/api/v1/user`, user);
return await response.json();
}
async changePassword(old_password, new_password) {
const response = await this.postJSON(`/api/v1/user/password`, { old_password, new_password });
if (response.status !== 200) {
const e = extend(new Error(response.statusText), { response: await response.json() });
throw e;
}
return response.json();
}
async resetPassword(email) {
const response = await this.postJSON(`/api/v1/resetpassword`, { email });
return await response.json();
}
async newPassword(hash, password, password_repeat) {
const response = await this.postJSON(`/api/v1/newpassword/${hash}`, { password, password_repeat });
return await response.json();
}
async unfollow(userName) {
const response = await this.post(`/api/v1/user/${userName}/unfollow`);
return await response.json();
}
async registerUser(userData) {
const response = await this.post(`/api/v1/users`, userData);
return await response.json();
}
async login(loginData) {
const response = await this.post(`/api/v1/session`, loginData);
return await response.json();
}
async userSuggestions() {
const response = await this.get(`/api/v1/suggestions/personalized`);
return await response.json();
}
async userRecentHashtags() {
const response = await this.get('/api/v1/user/recent-hashtags');
return await response.json();
}
async userRecentSchools() {
const response = await this.get('/api/v1/user/recent-schools');
return await response.json();
}
async userRecentGeotags() {
const response = await this.get('/api/v1/user/recent-geotags');
return await response.json();
}
async initialSuggestions() {
const response = await this.get(`/api/v1/suggestions/initial`);
return await response.json();
}
async postInfo(uuid) {
const response = await this.get(`/api/v1/post/${uuid}`);
return await response.json();
}
async createPost(type, data) {
data.type = type;
const response = await this.postJSON(`/api/v1/posts`, data);
return await response.json();
}
async updatePost(uuid, data) {
const response = await this.postJSON(`/api/v1/post/${uuid}`, data);
return await response.json();
}
async deletePost(uuid) {
const response = await this.del(`/api/v1/post/${uuid}`);
return await response.json();
}
async updateGeotag(uuid, data) {
const response = await this.postJSON(`/api/v1/geotag/${uuid}`, data);
return await response.json();
}
async updateHashtag(uuid, data) {
const response = await this.postJSON(`/api/v1/tag/${uuid}`, data);
return await response.json();
}
async updateSchool(uuid, data) {
const response = await this.postJSON(`/api/v1/school/${uuid}`, data);
return await response.json();
}
async pickpoint(options) {
const response = await this.get('/api/v1/pickpoint', options);
return await response.json();
}
async search(query) {
const response = await this.get(`/api/v1/search/${query}`);
return await response.json();
}
async tagCloud() {
const response = await this.get('/api/v1/tag-cloud');
return await response.json();
}
async searchHashtags(query) {
const response = await this.get(`/api/v1/tags/search/${query}`);
return await response.json();
}
async followTag(name) {
const response = await this.post(`/api/v1/tag/${name}/follow`);
return await response.json();
}
async unfollowTag(name) {
const response = await this.post(`/api/v1/tag/${name}/unfollow`);
return await response.json();
}
async schoolCloud() {
const response = await this.get('/api/v1/school-cloud');
return await response.json();
}
async searchSchools(query) {
const response = await this.get(`/api/v1/schools/search/${query}`);
return await response.json();
}
async followSchool(name) {
const response = await this.post(`/api/v1/school/${name}/follow`);
return await response.json();
}
async unfollowSchool(name) {
const response = await this.post(`/api/v1/school/${name}/unfollow`);
return await response.json();
}
async geotagCloud() {
const response = await this.get('/api/v1/geotag-cloud');
return await response.json();
}
async followGeotag(urlName) {
const response = await this.post(`/api/v1/geotag/${urlName}/follow`);
return await response.json();
}
async unfollowGeotag(urlName) {
const response = await this.post(`/api/v1/geotag/${urlName}/unfollow`);
return await response.json();
}
async checkGeotagExists(name) {
const result = await this.head(`/api/v1/geotag/${name}`);
return result.ok;
}
async getGeotag(urlName) {
const response = await this.get(`/api/v1/geotag/${urlName}`);
return await response.json();
}
async getHashtag(name) {
const response = await this.get(`/api/v1/tag/${name}`);
return await response.json();
}
async searchGeotags(query) {
const response = await this.get(`/api/v1/geotags/search/${query}`);
return await response.json();
}
async getQuotes() {
const response = await this.get('/api/v1/quotes');
return await response.json();
}
async uploadImage(images) {
const data = new FormData;
images.forEach((image) => {
data.append("files", image);
});
const response = await this.postMultipart('/api/v1/upload', data);
return await response.json();
}
async processImage(id, transforms, derived_id = null) {
const response = await this.postJSON('/api/v1/image', {
original_id: id,
transforms: JSON.stringify(transforms),
derived_id
});
return await response.json();
}
async createComment(postId, text) {
const response = await this.post(`/api/v1/post/${postId}/comments`, {
text
});
return await response.json();
}
async deleteComment(postId, commentId) {
const response = await this.del(`/api/v1/post/${postId}/comment/${commentId}`);
return await response.json();
}
async saveComment(postId, commentId, text) {
const response = await this.post(`/api/v1/post/${postId}/comment/${commentId}`, {
text
});
return await response.json();
}
async subscribeToPost(postId) {
const response = await this.post(`/api/v1/post/${postId}/subscribe`);
return await response.json();
}
async unsubscribeFromPost(postId) {
const response = await this.post(`/api/v1/post/${postId}/unsubscribe`);
return await response.json();
}
}