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

Commit 831a2a2

Browse files
authored
Create User.js
1 parent 377db22 commit 831a2a2

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

src/User.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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 User {
23+
constructor(username, filter) {
24+
this.username = username;
25+
this.filter = filter;
26+
}
27+
28+
async profileData() {
29+
let user = this.username;
30+
let info = await fetch('https://repl.it/graphql', {
31+
method: 'POST',
32+
headers,
33+
body: JSON.stringify({
34+
query: `
35+
query User($user: String!) {
36+
userByUsername(username: $user) {
37+
${userAttributes}
38+
}
39+
}`,
40+
variables: {
41+
user
42+
}
43+
})
44+
}).then(res => res.json());
45+
46+
if (info.data.userByUsername == null) {
47+
throw new Error(
48+
`${username} is not a user. Please query users on Repl.it.`
49+
);
50+
} else {
51+
return info.data.userByUsername;
52+
}
53+
}
54+
55+
async postData() {
56+
let user = this.username;
57+
let output = [];
58+
59+
async function recurse(next = '') {
60+
if (next === null) return;
61+
62+
let info = await fetch('https://repl.it/graphql', {
63+
method: 'POST',
64+
headers,
65+
body: JSON.stringify({
66+
query: `
67+
query UserPost($user: String!, $next: String) {
68+
userByUsername(username: $user) {
69+
posts(count: 50, after: $next) {
70+
items { ${postAttributes} }
71+
pageInfo {
72+
nextCursor
73+
}
74+
}
75+
}
76+
}`,
77+
variables: {
78+
user,
79+
next
80+
}
81+
})
82+
}).then(res => res.json());
83+
84+
if (info.data.userByUsername == null) {
85+
throw new Error(
86+
`${username} is not a user. Please query users on Repl.it.`
87+
);
88+
} else {
89+
info.data.userByUsername.posts.items.forEach(post => {
90+
output.push(post);
91+
});
92+
await recurse(info.data.userByUsername.posts.pageInfo.nextCursor);
93+
}
94+
}
95+
96+
await recurse();
97+
return output;
98+
}
99+
100+
async commentData(username) {
101+
let user = this.username;
102+
let output = [];
103+
104+
async function recurse(next = '') {
105+
if (next === null) return;
106+
107+
let info = await fetch('https://repl.it/graphql', {
108+
method: 'POST',
109+
headers,
110+
body: JSON.stringify({
111+
query: `
112+
query UserComment($user: String!, $next: String) {
113+
userByUsername(username: $user) {
114+
comments(count: 50, after: $next) {
115+
items { ${commentAttributes} }
116+
pageInfo {
117+
nextCursor
118+
}
119+
}
120+
}
121+
}`,
122+
variables: {
123+
user,
124+
next
125+
}
126+
})
127+
}).then(res => res.json());
128+
129+
if (info.data.userByUsername == null) {
130+
throw new Error(
131+
`${username} is not a user. Please query users on Repl.it.`
132+
);
133+
} else {
134+
info.data.userByUsername.comments.items.forEach(comment => {
135+
output.push(comment);
136+
});
137+
await recurse(info.data.userByUsername.comments.pageInfo.nextCursor);
138+
}
139+
}
140+
141+
await recurse();
142+
return output;
143+
}
144+
}
145+
146+
module.exports = {
147+
User: User
148+
};

0 commit comments

Comments
 (0)