|
| 1 | +let headers = require('../utils/headers.js'); |
| 2 | +let variables = require('../utils/variables.js'); |
| 3 | + |
| 4 | +class Leaderboard { |
| 5 | + async leaderboardData(after, count, since) { |
| 6 | + if (!after) after = ''; |
| 7 | + if (!count) count = 10; |
| 8 | + if (!since) { |
| 9 | + let output = []; |
| 10 | + |
| 11 | + async function recurse(after) { |
| 12 | + if (after === null) return; |
| 13 | + |
| 14 | + let info = await variables |
| 15 | + .fetch('https://repl.it/graphql', { |
| 16 | + method: 'POST', |
| 17 | + headers, |
| 18 | + body: JSON.stringify({ |
| 19 | + query: ` |
| 20 | + query Leaderboard($after: String!, $count: Int!) { |
| 21 | + leaderboard(after: $after, count: $count) { |
| 22 | + items { ${variables.userAttributes} } |
| 23 | + pageInfo { |
| 24 | + nextCursor |
| 25 | + } |
| 26 | + } |
| 27 | + }`, |
| 28 | + variables: { |
| 29 | + after, |
| 30 | + count |
| 31 | + } |
| 32 | + }) |
| 33 | + }) |
| 34 | + .then(res => res.json()); |
| 35 | + |
| 36 | + if (!info.data.leaderboard) { |
| 37 | + throw new Error(`Cannot fetch leaderboard`); |
| 38 | + } else { |
| 39 | + info.data.leaderboard.items.forEach(user => { |
| 40 | + output.push(user); |
| 41 | + }); |
| 42 | + if (output.length != count) { |
| 43 | + await recurse(info.data.leaderboard.pageInfo.nextCursor); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + await recurse(after); |
| 49 | + return output; |
| 50 | + } else { |
| 51 | + let output = []; |
| 52 | + |
| 53 | + async function recurse(after) { |
| 54 | + if (after === null) return; |
| 55 | + |
| 56 | + let info = await variables |
| 57 | + .fetch('https://repl.it/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: { |
| 71 | + after, |
| 72 | + count, |
| 73 | + 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 | +} |
| 96 | + |
| 97 | +module.exports = { |
| 98 | + Leaderboard: Leaderboard |
| 99 | +}; |
0 commit comments