-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapi.js
337 lines (311 loc) · 10 KB
/
api.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
import cheerio from 'cheerio-without-node-native';
import config from '../config/default.json';
/**
* Get item from given ID
* @param {String} itemId The ID of the item to fetch
* @return {Promise} Returns a promise
*/
const getItem = itemId =>
fetch(`${config.api}/item/${itemId}.json`)
.then(response => response.json())
.catch(error => error);
/**
* Given a list of IDs, get each item for that ID
* @param {Number[]} itemIds An array of item IDs
* @return {Promise[]} An array of Promises resolved
* with Promise.all
*/
const getItems = (page, limit, itemIds) => {
let slicedItems = itemIds;
if (page && limit) {
const beginning = (page - 1) * limit;
const end = beginning + limit;
slicedItems = itemIds.slice(beginning, end);
}
// Map each itemId to an Array of Promises
return slicedItems.map(item => getItem(item).then(post => post));
};
/**
* Gets a users information from their username
* @param {String} username The users username
* @return {Object} The users data
*/
const getUser = username =>
fetch(`${config.api}/user/${username}.json`)
.then(response => response.json())
.then(responseJson => responseJson)
.catch(error => null);
/**
* Get the URL needed to upvote
* @param {String} itemId The item ID to upvote
* @return {Promise} Returns a promise that
* resolves with the upvote URL
*/
const getUpvoteUrl = itemId =>
fetch(`${config.base}/item?id=${itemId}`, {
mode: 'no-cors',
credentials: 'include',
})
.then(response => response.text())
.then(responseText => {
const document = cheerio.load(responseText);
return document(`#up_${itemId}`).attr('href');
});
/**
* Get the URL needed to unvote
* @param {String} itemId The item ID to upvote
* @return {Promise} Returns a promise that
* resolves with the unvote URL
*/
const getUnvoteUrl = itemId =>
fetch(`${config.base}/item?id=${itemId}`, {
mode: 'no-cors',
credentials: 'include',
})
.then(response => response.text())
.then(responseText => {
const document = cheerio.load(responseText);
return document(`#un_${itemId}`).attr('href');
});
/**
* Upvote an item
* @param {String} itemId The item ID to upvote
* @return {Promise} Returns a promise that
* resolves true if upvoted, else false
*/
const upvote = itemId =>
getUpvoteUrl(itemId)
.then(upvoteUrl =>
fetch(`${config.base}/${upvoteUrl}`, {
mode: 'no-cors',
credentials: 'include',
}),
)
.then(response => response.text())
.then(responseText => true)
.catch(error => false);
/**
* Unvote an item
* @param {String} itemId The item ID to upvote
* @return {Promise} Returns a promise that
* resolves true if unvoted, else false
*/
const unvote = itemId =>
getUnvoteUrl(itemId)
.then(upvoteUrl =>
fetch(`${config.base}/${upvoteUrl}`, {
mode: 'no-cors',
credentials: 'include',
}),
)
.then(response => response.text())
.then(responseText => true)
.catch(error => false);
/**
* Login a user
* @param {String} username The users username
* @param {String} password The users password
* @return {Promise} Returns a promise that
* resolves true if logged in, else false
*/
const login = (username, password) => {
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
});
return fetch(`${config.base}/login`, {
method: 'POST',
headers,
body: `acct=${username}&pw=${password}&goto=news`,
mode: 'no-cors',
credentials: 'include',
})
.then(response => response.text())
.then(responseText => !/Bad Login/i.test(responseText));
};
/**
* Get the URL needed to logout
* @return {Promise} Returns a promise that
* resolves with the logout URL
*/
const getLogoutUrl = () =>
fetch(`${config.base}/news`, {
mode: 'no-cors',
credentials: 'include',
})
.then(response => response.text())
.then(responseText => {
const document = cheerio.load(responseText);
return document('#logout').attr('href');
});
/**
* Logout a user
* @return {Promise} Returns a promise that
* resolves true if logged out, else false
*/
const logout = () =>
getLogoutUrl()
.then(logoutUrl =>
fetch(`${config.base}/${logoutUrl}`, {
mode: 'no-cors',
credentials: 'include',
}),
)
.then(response => response.text())
.then(responseText => true)
.catch(error => false);
/**
* Get the URL needed to comment
* @param {String} itemId The item ID to comment on
* @return {Promise} Returns a promise that
* resolves with the comment URL
*/
const getCommentUrl = itemId =>
fetch(`${config.base}/item?id=${itemId}`, {
mode: 'no-cors',
credentials: 'include',
})
.then(response => response.text())
.then(responseText => {
const document = cheerio.load(responseText);
return document('input[name=hmac]').attr('value');
});
/**
* Reply to a story or a comment
* @param {String} itemId The item ID to comment on
* @param {String} reply The text content of the reply
* @return {Promise} Returns a promise that
* resolves true if commented, else false
*/
const comment = (itemId, reply) =>
getCommentUrl(itemId)
.then(commentUrl => {
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
});
return fetch(`${config.base}/comment`, {
method: 'POST',
headers,
body: `parent=${itemId}&goto=item?id=${itemId}&hmac=${commentUrl}&text=${reply}`,
mode: 'no-cors',
credentials: 'include',
});
})
.then(response => response.text())
.then(responseText => true)
.catch(error => false);
/**
* Convert nested comments into an array of comments
* where child comment is simply the next element
* in the array. Is recursive
* @param {Object} comments An object containing nested comments
* @param {Object[]} commentsArray An array containing flattened comments
* @return {Object[]} An array containing all flattened comments
*/
const flatten = (comments, commentsArray) => {
for (const key in comments) {
const currentComment = comments[key];
if (comments.hasOwnProperty(key) && !currentComment.dead && !currentComment.deleted) {
commentsArray.push(currentComment);
if (currentComment.kids && currentComment.kids.length > 0) {
flatten(currentComment.kids, commentsArray);
}
}
}
return commentsArray;
};
/**
* Get all comments from an array of comment IDs
* @param {Number[]} commentIds An array of all comment IDs
* @return {Promise} A promise that resolves with
* all comments
*/
const getComments = commentIds =>
new Promise((resolve, reject) => {
const comments = commentIds.map(id => getChildComment(id, 0).then(comment => comment));
Promise.all(comments)
.then(allComments => {
resolve(flatten(allComments, []));
})
.catch(error => {
reject(error);
});
});
/**
* A recursive function to get child comments of
* a parent comment
* @param {Number} parentId The parent ID to get the child comment
* @param {Number} level The level of nesting
* @return {Promise} A promise containing all child comments
*/
const getChildComment = (parentId, level) =>
new Promise((resolve, reject) => {
getItem(parentId)
.then(comment => {
comment.level = level;
comment.open = true;
comment.hidden = false;
if (comment.kids && comment.kids.length > 0) {
const results = comment.kids.map(id => getChildComment(id, level + 1).then(c => c));
Promise.all(results).then(kids => {
resolve({ ...comment, kids });
});
} else {
resolve(comment);
}
})
.catch(error => {
reject(error);
});
});
/**
* Toggles comment visibility when a comment
* is clicked on
* @param {Object[]} comments An array of comment objects
* @param {Number} id The id of the comment clicked on
* @param {Number} level The level of the comment clicked on
* @return {Object[]} The new array of comment objects
* with certain comments hidden/closed
*/
const toggleComments = (comments, id, level) => {
comments.forEach((comment, index) => {
if (comment.id === id) {
// Toggle content visibility, but still show comment header
comment.open = !comment.open;
for (i = index + 1; i < comments.length; i++) {
// If comment is a children of comment clicked on
if (comments[i].level > level) {
// If clicked on comment is being closed
if (!comment.open) {
// Hide every child comment
comments[i].hidden = true;
} else if (comment.open) {
// Child comment has a parent, check if the parent is closed
if (comments[i].parent) {
for (k = 0; k < comments.length; k++) {
// Found the parent
if (comments[k].id === comments[i].parent) {
// If the parent is closed
if (!comments[k].open) {
// Hide the child
comments[i].hidden = true;
} else {
comments[i].hidden = false;
}
}
}
} else {
// No parent so unhide it
comments[i].hidden = false;
}
}
} else {
break;
}
}
}
});
return comments;
};
export { getItem, getItems, upvote, unvote, login, comment, getUser, logout, getComments, toggleComments };