-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTR.js
86 lines (74 loc) · 2.23 KB
/
TR.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
'use strict';
let denodeify = require('denodeify');
let request = require('request').defaults({jar: true});
let get = denodeify(request.get, function (err, resp, body) {
return [err, body];
});
let post = denodeify(request.post, function (err, resp, body) {
return [err, body];
});
class TR {
//async
static async login(username, password) {
let body = await post({url: 'https://www.trustroots.org/api/auth/signin', form:{username: username, password: password}});
let jsonBody = JSON.parse(body);
let isNotLoggedIn = jsonBody && jsonBody.hasOwnProperty('message') && jsonBody.message === 'Unknown user or invalid password';
let isLoggedIn = jsonBody && jsonBody.hasOwnProperty('_id');
if(isNotLoggedIn) {
let e = new Error('login not successful');
e.status = 403;
throw e;
}
else if (isLoggedIn) {
return;
}
else {
let e = new Error(jsonBody.message || 'other error');
e.status = 500;
throw e;
}
}
//async
static logout() {
}
//show all existing hosts
static hosts() {
}
static async user(username) {
let body = await get({url: `https://www.trustroots.org/api/users/${username}`});
let jsonBody = JSON.parse(body);
if(jsonBody && jsonBody.hasOwnProperty('message') && jsonBody.message === 'Not found.') {
let e = new Error('Not found.');
e.status = 404;
throw e;
}
return ({
id: jsonBody._id,
username: jsonBody.username,
name: jsonBody.displayName,
created: new Date(jsonBody.created),
gender: jsonBody.gender
});
}
static async contacts(id) {
let body = await get({url: `https://www.trustroots.org/api/contacts/${id}`});
let jsonBody = JSON.parse(body);
let contacts = [];
for(let rawContact of jsonBody){
//include only confirmed and not deleted
if(rawContact && rawContact.confirmed === true) {
let contact = rawContact.user;
delete rawContact.user;
let finalContact = {
id: contact._id,
username: contact.username,
name: contact.displayName,
created: new Date(rawContact.created)
}
contacts.push(finalContact);
}
}
return contacts;
}
}
module.exports = TR;