This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
/
sumo_db.js
160 lines (137 loc) · 4.54 KB
/
sumo_db.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
'use strict';
/* global User */
(function(exports) {
var API_V1_BASE = 'https://support.allizom.org/api/1/';
var API_V2_BASE = 'https://support.allizom.org/api/2/';
var PRODUCT = 'firefox-os';
function get_token() {
var endpoint = API_V1_BASE + 'users/get_token';
var data = {
username: window.user.username,
password: window.user.password
};
return request(endpoint, 'POST', data).then(function(response) {
var json = JSON.parse(response);
return json.token;
});
}
function request(url, method, data, headers) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open(method, url);
req.setRequestHeader('Content-Type', 'application/json');
for (var field in headers) {
req.setRequestHeader(field, headers[field]);
}
req.onload = function() {
if (req.status >= 200 && req.status < 300) {
resolve(req.response);
} else {
reject(Error(req.statusText));
}
};
req.onerror = function() {
reject(Error('Network Error'));
};
req.send(JSON.stringify(data));
});
}
function request_with_auth(url, method, data) {
return User.get_credentials().then(function(credentials) {
return request(url, method, data, {
authorization: 'Token ' + credentials.token
});
});
}
var SumoDB = {
post_question: function(text) {
var endpoint = API_V2_BASE + 'question/';
endpoint += '?format=json'; // TODO bug 1088014
var data = {
title: text,
product: 'firefox-os',
content: ' ',
topic: 'basic-features'
};
return request_with_auth(endpoint, 'POST', data)
.then(function(response) {
return JSON.parse(response);
}
);
},
post_answer: function(question_id, text) {
var endpoint = API_V2_BASE + 'answer/';
endpoint += '?format=json'; // TODO bug 1088014
var data = {
question: question_id,
content: text
};
return request_with_auth(endpoint, 'POST', data)
.then(function(response) {
return JSON.parse(response);
}
);
},
/**
* Get list of questions for the current user
*/
get_my_questions: function(user) {
var endpoint = API_V2_BASE + 'question/';
endpoint += '?product=' + PRODUCT;
endpoint += '&creator=' + user.username;
endpoint += '&locale=' + user.locale;
endpoint += '&format=json'; // TODO bug 1088014
return request(endpoint, 'GET').then(function(response) {
return JSON.parse(response).results;
});
},
get_question: function(question_id) {
var endpoint = API_V2_BASE + 'question/';
endpoint += question_id + '/';
endpoint += '?format=json'; // TODO bug 1088014
return request(endpoint, 'GET').then(function(response) {
return JSON.parse(response);
});
},
get_answers_for_question: function(question_id) {
var endpoint = API_V2_BASE + 'answer/';
endpoint += '?question=' + question_id;
endpoint += '&format=json'; // TODO bug 1088014
return request(endpoint, 'GET').then(function(response) {
return JSON.parse(response).results;
});
},
get_unanswered_questions: function() {
var endpoint = API_V2_BASE + 'question/';
endpoint += '?product=' + PRODUCT;
endpoint += '&is_solved=0';
endpoint += '&format=json'; // TODO bug 1088014
return request(endpoint, 'GET').then(function(response) {
return JSON.parse(response).results;
});
},
create_user: function() {
var endpoint = API_V2_BASE + 'user/generate';
endpoint += '?format=json'; // TODO bug 1088014
return request(endpoint, 'POST').then(function(response) {
return JSON.parse(response);
});
},
update_user: function(user_data) {
return User.get_credentials().then(function(credentials) {
var endpoint = API_V2_BASE + 'user/';
endpoint += credentials.username + '/';
endpoint += '?format=json'; // TODO bug 1088014
user_data.username = credentials.username;
user_data.password = credentials.password;
return request_with_auth(endpoint, 'PUT', user_data);
}).then(JSON.parse);
},
get_user: function(username) {
var endpoint = API_V2_BASE + 'user/' + username + '/';
endpoint += '?format=json'; // TODO bug 1088014
return request(endpoint, 'GET').then(JSON.parse);
}
};
exports.SumoDB = SumoDB;
})(window);