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

Commit ee3d1e0

Browse files
authored
Create User.js
1 parent 87f26ab commit ee3d1e0

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

src/classes/User.js

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

0 commit comments

Comments
 (0)