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

Commit 5ce2ee7

Browse files
authored
Merge pull request #4 from RayhanADev/RayhanADev-Leaderboard
Create Leaderboard Functions
2 parents 0ab05a5 + 8c166e8 commit 5ce2ee7

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

src/Leaderboard.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const fetch = require('node-fetch');
2+
3+
const headers = {
4+
'Content-Type': 'application/json',
5+
Accept: 'application/json',
6+
'Accept-Encoding': 'gzip, deflate, br',
7+
Connection: 'keep-alive',
8+
'X-Requested-With': 'REPLAPIit',
9+
Referrer: 'https://repl.it',
10+
Origin: 'https://repl.it'
11+
};
12+
13+
let roleAttributes = `id, name, key, tagline`;
14+
let languageAttributes = `id, displayName, key, category, tagline, icon, isNew`;
15+
16+
let userAttributes = `id, username, url, image, karma, firstName, lastName, fullName, displayName, isLoggedIn, bio, timeCreated, organization { name }, subscription { planId }, languages { ${languageAttributes} }, roles { ${roleAttributes} }`;
17+
let boardAttributes = `id, url, slug, cta, titleCta, bodyCta, buttonCta, description, name, replRequired, isLocked, isPrivate`;
18+
let replAttributes = `id, hostedUrl, title, lang { ${languageAttributes} }, language, timeCreated`;
19+
let commentAttributes = `id, body, voteCount, timeCreated, timeUpdated, user { ${userAttributes} }, url, post { id }, parentComment { id }, comments { id }, isAuthor, canEdit, canVote, canComment, hasVoted, canReport, hasReported, isAnswer, canSelectAsAnswer, canUnselectAsAnswer, preview(removeMarkdown: true, length: 150)`;
20+
let postAttributes = `id, title, body, showHosted, voteCount, commentCount, isPinned, isLocked, timeCreated, timeUpdated, url, user { ${userAttributes} }, board { ${boardAttributes} }, repl { ${replAttributes} }, isAnnouncement, isAuthor, canEdit, canComment, canVote, canPin, canSetType, canChangeBoard, canLock, hasVoted, canReport, hasReported, isAnswered, isAnswerable, answeredBy { ${userAttributes} }, answer { ${commentAttributes} }, tutorialPages, preview(removeMarkdown: true, length: 150)`;
21+
22+
class Leaderboard {
23+
async leaderboardData(after, count) {
24+
if (!after) after = '';
25+
if (!count) count = 10;
26+
27+
let output = [];
28+
29+
async function recurse(after) {
30+
if (after === null) return;
31+
32+
let info = await fetch('https://repl.it/graphql', {
33+
method: 'POST',
34+
headers,
35+
body: JSON.stringify({
36+
query: `
37+
query Leaderboard($after: String!, $count: Int!) {
38+
leaderboard(after: $after, count: $count) {
39+
items { ${userAttributes} }
40+
pageInfo {
41+
nextCursor
42+
}
43+
}
44+
}`,
45+
variables: {
46+
after,
47+
count
48+
}
49+
})
50+
}).then(res => res.json());
51+
52+
if (!info.data.leaderboard) {
53+
throw new Error(
54+
`Cannot fetch leaderboard`
55+
);
56+
} else {
57+
info.data.leaderboard.items.forEach(user => {
58+
output.push(user);
59+
});
60+
if(output.length != count) {
61+
await recurse(info.data.leaderboard.pageInfo.nextCursor);
62+
}
63+
}
64+
}
65+
66+
await recurse(after);
67+
return output;
68+
}
69+
}
70+
71+
module.exports = {
72+
Leaderboard: Leaderboard
73+
};

0 commit comments

Comments
 (0)