Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 6d9b5c4

Browse files
author
Ray Arayilakath
committed
Refactored Several Classes (and lint)
1 parent 31f153a commit 6d9b5c4

File tree

7 files changed

+302
-328
lines changed

7 files changed

+302
-328
lines changed

src/classes/Comment.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default class Comment {
8787
async createCommentOnPost(body, postId) {
8888
if (!global.cookies) {
8989
throw new Error('ReplAPI.it: Not logged in.');
90-
} else if (['RayhanADev'].contains(global.initVariables.username)) {
90+
} else if (['RayhanADev'].includes(global.initVariables.username)) {
9191
if (typeof body !== 'string') {
9292
throw new Error(
9393
`Body must be of type string. Got type ${typeof title}.`,
@@ -134,7 +134,7 @@ export default class Comment {
134134
async createCommentOnComment(body, commentId) {
135135
if (!global.cookies) {
136136
throw new Error('ReplAPI.it: Not logged in.');
137-
} else if (['RayhanADev'].contains(global.initVariables.username)) {
137+
} else if (['RayhanADev'].includes(global.initVariables.username)) {
138138
if (typeof body !== 'string') {
139139
throw new Error(
140140
`Body must be of type string. Got type ${typeof title}.`,
@@ -181,7 +181,7 @@ export default class Comment {
181181
async updateComment(id, body) {
182182
if (!global.cookies) {
183183
throw new Error('ReplAPI.it: Not logged in.');
184-
} else if (['RayhanADev'].contains(global.initVariables.username)) {
184+
} else if (['RayhanADev'].includes(global.initVariables.username)) {
185185
if (typeof id !== 'number') {
186186
throw new Error(
187187
`Title must be of type number. Got type ${typeof title}.`,
@@ -228,7 +228,7 @@ export default class Comment {
228228
async deleteComment(id) {
229229
if (!global.cookies) {
230230
throw new Error('ReplAPI.it: Not logged in.');
231-
} else if (['RayhanADev'].contains(global.initVariables.username)) {
231+
} else if (['RayhanADev'].includes(global.initVariables.username)) {
232232
if (typeof id !== 'number') {
233233
throw new Error(
234234
`Id must be of type number. Got type ${typeof title}.`,
@@ -265,7 +265,7 @@ export default class Comment {
265265
async createCommentVote(id) {
266266
if (!global.cookies) {
267267
throw new Error('ReplAPI.it: Not logged in.');
268-
} else if (['RayhanADev'].contains(global.initVariables.username)) {
268+
} else if (['RayhanADev'].includes(global.initVariables.username)) {
269269
if (typeof id !== 'number') {
270270
throw new Error(
271271
`Id must be of type number. Got type ${typeof title}.`,
@@ -306,7 +306,7 @@ export default class Comment {
306306
async deleteCommentVote(id) {
307307
if (!global.cookies) {
308308
throw new Error('ReplAPI.it: Not logged in.');
309-
} else if (['RayhanADev'].contains(global.initVariables.username)) {
309+
} else if (['RayhanADev'].includes(global.initVariables.username)) {
310310
if (typeof id !== 'number') {
311311
throw new Error(
312312
`Id must be of type number. Got type ${typeof title}.`,

src/classes/Custom.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ export class CustomDataQuery {
1111
}
1212

1313
async getData() {
14-
const { queryName } = this;
15-
const { customQuery } = this;
16-
const { customVariables } = this;
14+
const { queryName, customQuery, customVariables } = this;
1715

1816
const specialQueryVariables = {
1917
since: 'KarmaSince',

src/classes/Leaderboard.js

Lines changed: 63 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,71 @@
1-
import fetch from 'node-fetch'
1+
import fetch from 'node-fetch';
22

3-
import headers from '../utils/headers.js'
4-
import constants from '../utils/constants.js'
3+
import headers from '../utils/headers.js';
4+
import constants from '../utils/constants.js';
55

66
export default class Leaderboard {
7-
async leaderboardData(after, count, since) {
8-
if (!after) after = '';
9-
if (!count) count = 10;
10-
if (!since) {
11-
let output = [];
7+
async leaderboardData(after = '', count = 10, since) {
8+
let query = '';
9+
let variables = {};
10+
if (since) {
11+
query = `
12+
query Leaderboard($after: String!, $count: Int!, $since: KarmaSince!) {
13+
leaderboard(after: $after, count: $count, since: $since) {
14+
items { ${constants.userAttributes} }
15+
pageInfo {
16+
nextCursor
17+
}
18+
}
19+
}`;
20+
variables = {
21+
after,
22+
count,
23+
since,
24+
};
25+
} else {
26+
query = `
27+
query Leaderboard($after: String!, $count: Int!) {
28+
leaderboard(after: $after, count: $count) {
29+
items { ${constants.userAttributes} }
30+
pageInfo {
31+
nextCursor
32+
}
33+
}
34+
}`;
35+
variables = {
36+
after,
37+
count,
38+
};
39+
}
1240

13-
async function recurse(after) {
14-
if (after === null) return;
41+
const output = [];
1542

16-
let info = await fetch(variables.graphql, {
17-
method: 'POST',
18-
headers,
19-
body: JSON.stringify({
20-
query: `
21-
query Leaderboard($after: String!, $count: Int!) {
22-
leaderboard(after: $after, count: $count) {
23-
items { ${variables.userAttributes} }
24-
pageInfo {
25-
nextCursor
26-
}
27-
}
28-
}`,
29-
variables: JSON.stringify({
30-
after: after,
31-
count: count
32-
})
33-
})
34-
})
35-
.then(res => res.json());
43+
async function recurse(recurseAfter) {
44+
if (recurseAfter === null) return;
3645

37-
if (!info.data.leaderboard) {
38-
throw new Error(`Cannot fetch leaderboard`);
39-
} else {
40-
info.data.leaderboard.items.forEach(user => {
41-
output.push(user);
42-
});
43-
if (output.length != count) {
44-
await recurse(info.data.leaderboard.pageInfo.nextCursor);
45-
}
46-
}
47-
}
46+
const info = await fetch(constants.graphql, {
47+
method: 'POST',
48+
headers,
49+
body: JSON.stringify({
50+
query,
51+
variables: JSON.stringify(variables),
52+
}),
53+
})
54+
.then((res) => res.json());
4855

49-
await recurse(after);
50-
return output;
51-
} else {
52-
let output = [];
56+
if (!info.data.leaderboard) {
57+
throw new Error('Cannot fetch leaderboard');
58+
} else {
59+
info.data.leaderboard.items.forEach((user) => {
60+
output.push(user);
61+
});
62+
if (output.length !== count) {
63+
await recurse(info.data.leaderboard.pageInfo.nextCursor);
64+
}
65+
}
66+
}
5367

54-
async function recurse(after) {
55-
if (after === null) return;
56-
57-
let info = await fetch(variables.graphql, {
58-
method: 'POST',
59-
headers,
60-
body: JSON.stringify({
61-
query: `
62-
query Leaderboard($after: String!, $count: Int!, $since: KarmaSince!) {
63-
leaderboard(after: $after, count: $count, since: $since) {
64-
items { ${variables.userAttributes} }
65-
pageInfo {
66-
nextCursor
67-
}
68-
}
69-
}`,
70-
variables: JSON.stringify({
71-
after: after,
72-
count: count,
73-
since: since
74-
})
75-
})
76-
})
77-
.then(res => res.json());
78-
79-
if (!info.data.leaderboard) {
80-
throw new Error(`Cannot fetch leaderboard`);
81-
} else {
82-
info.data.leaderboard.items.forEach(user => {
83-
output.push(user);
84-
});
85-
if (output.length != count) {
86-
await recurse(info.data.leaderboard.pageInfo.nextCursor);
87-
}
88-
}
89-
}
90-
91-
await recurse(after);
92-
return output;
93-
}
94-
}
95-
}
68+
await recurse(after);
69+
return output;
70+
}
71+
}

src/classes/Login.js

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
import fetch from 'node-fetch'
1+
import fetch from 'node-fetch';
22

3-
import headers from '../utils/headers.js'
4-
import constants from '../utils/constants.js'
3+
import headers from '../utils/headers.js';
4+
import constants from '../utils/constants.js';
55

66
async function _getCookies(username, password) {
7-
if (['RayhanADev'].includes(global.initVariables.username)) {
8-
let info = await fetch(constants.login, {
9-
method: 'POST',
10-
headers,
11-
body: JSON.stringify({
12-
username: username,
13-
password: password,
14-
captcha: global.initVariables.captcha.token,
15-
hCaptchaSiteKey: '7f7c5b9f-8cff-49f3-ab09-5666dca1104b'
16-
})
17-
}).then(res => res.headers.raw()['set-cookie'][1]);
7+
if (['RayhanADev'].includes(global.initVariables.username)) {
8+
const info = await fetch(constants.login, {
9+
method: 'POST',
10+
headers,
11+
body: JSON.stringify({
12+
username,
13+
password,
14+
captcha: global.initVariables.captcha.token,
15+
hCaptchaSiteKey: '7f7c5b9f-8cff-49f3-ab09-5666dca1104b',
16+
}),
17+
}).then((res) => res.headers.raw()['set-cookie'][1]);
1818

19-
if (!info) {
20-
throw new Error(`Couldn't fetch cookie data.`);
21-
} else {
22-
return info;
23-
}
24-
} else {
25-
throw new Error(
26-
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`
27-
);
28-
}
19+
if (!info) {
20+
throw new Error('Couldn\'t fetch cookie data.');
21+
} else {
22+
return info;
23+
}
24+
} else {
25+
throw new Error(
26+
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`,
27+
);
28+
}
2929
}
3030

3131
export default class Login {
32-
async withCredentials(password) {
33-
if (['RayhanADev'].includes(global.initVariables.username)) {
34-
global.cookies = await _getCookies(global.initVariables.username, password);
35-
} else {
36-
throw new Error(
37-
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`
38-
);
39-
}
40-
}
41-
32+
async withCredentials(password) {
33+
if (['RayhanADev'].includes(global.initVariables.username)) {
34+
global.cookies = await _getCookies(global.initVariables.username, password);
35+
} else {
36+
throw new Error(
37+
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`,
38+
);
39+
}
40+
}
41+
4242
async withSID(sid) {
43-
if (['RayhanADev'].includes(global.initVariables.username)) {
44-
global.cookies = sid;
45-
} else {
46-
throw new Error(
47-
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`
48-
);
49-
}
50-
}
51-
}
43+
if (['RayhanADev'].includes(global.initVariables.username)) {
44+
global.cookies = sid;
45+
} else {
46+
throw new Error(
47+
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`,
48+
);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)