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

Commit 76c4ae9

Browse files
author
Rayhan Arayilakath
committed
Actually Refactor the User Class
1 parent 739ab43 commit 76c4ae9

File tree

2 files changed

+127
-11
lines changed

2 files changed

+127
-11
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let _defaultInitVariables = {
88
markdown: {
99
length: undefined,
1010
removeMarkdown: undefined
11-
}
11+
},
1212
previewCount: {
1313
comments: undefined
1414
}

src/classes/User.js

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ class User {
2727
user: user
2828
})
2929
})
30-
})
31-
.then(res => res.json());
30+
}).then(res => res.json());
3231

3332
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
3433

@@ -39,6 +38,66 @@ class User {
3938
}
4039
}
4140

41+
async postDataAbridged(after, count, order) {
42+
if (!after) after = '';
43+
if (!count) count = 50;
44+
if (!order) order = '';
45+
46+
let user = this.username;
47+
let output = [];
48+
49+
async function recurse(after) {
50+
if (after === null) return;
51+
52+
let info = await constants
53+
.fetch(constants.graphql, {
54+
method: 'POST',
55+
headers,
56+
body: JSON.stringify({
57+
query: `
58+
query UserPost($user: String!, $after: String!, $count: Int!, $order: String!) {
59+
userByUsername(username: $user) {
60+
posts(count: $count, after: $after, order: $order) {
61+
items {
62+
id,
63+
title,
64+
preview(length: ${global.initVariables.markdown.length || 150}, removeMarkdown: ${global.initVariables.markdown.removeMarkdown || true})
65+
}
66+
pageInfo {
67+
nextCursor
68+
}
69+
}
70+
}
71+
}`,
72+
variables: JSON.stringify({
73+
user: user,
74+
after: after,
75+
count: count,
76+
order: order
77+
})
78+
})
79+
}).then(res => res.json());
80+
81+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
82+
83+
if (!info.data.userByUsername) {
84+
throw new Error(
85+
`${user} is not a user. Please query users on Replit.`
86+
);
87+
} else {
88+
info.data.userByUsername.posts.items.forEach(post => {
89+
output.push(post);
90+
});
91+
if (output.length != count) {
92+
await recurse(info.data.userByUsername.posts.pageInfo.nextCursor);
93+
}
94+
}
95+
}
96+
97+
await recurse(after);
98+
return output;
99+
}
100+
42101
async postDataFull(after, count, order) {
43102
if (!after) after = '';
44103
if (!count) count = 50;
@@ -50,7 +109,7 @@ class User {
50109
async function recurse(after) {
51110
if (after === null) return;
52111

53-
let info = await variables
112+
let info = await constants
54113
.fetch(constants.graphql, {
55114
method: 'POST',
56115
headers,
@@ -82,8 +141,7 @@ class User {
82141
order: order
83142
})
84143
})
85-
})
86-
.then(res => res.json());
144+
}).then(res => res.json());
87145

88146
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
89147

@@ -105,7 +163,7 @@ class User {
105163
return output;
106164
}
107165

108-
async commentData(after, count, order) {
166+
async commentDataAbridged(after, count, order) {
109167
if (!after) after = '';
110168
if (!count) count = 50;
111169
if (!order) order = '';
@@ -116,8 +174,67 @@ class User {
116174
async function recurse(after) {
117175
if (after === null) return;
118176

119-
let info = await variables
120-
.fetch(variables.graphql, {
177+
let info = await constants
178+
.fetch(constants.graphql, {
179+
method: 'POST',
180+
headers,
181+
body: JSON.stringify({
182+
query: `
183+
query UserComment($user: String!, $after: String!, $count: Int!, $order: String!) {
184+
userByUsername(username: $user) {
185+
comments(count: $count, after: $after, order: $order) {
186+
items {
187+
id,
188+
preview(length: ${global.initVariables.markdown.length || 150}, removeMarkdown: ${global.initVariables.markdown.removeMarkdown || true})
189+
}
190+
pageInfo {
191+
nextCursor
192+
}
193+
}
194+
}
195+
}`,
196+
variables: JSON.stringify({
197+
user: user,
198+
after: after,
199+
count: count,
200+
order: order
201+
})
202+
})
203+
}).then(res => res.json());
204+
205+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
206+
207+
if (!info.data.userByUsername) {
208+
throw new Error(
209+
`${user} is not a user. Please query users on Replit.`
210+
);
211+
} else {
212+
info.data.userByUsername.comments.items.forEach(comment => {
213+
output.push(comment);
214+
});
215+
if (output.length != count) {
216+
await recurse(info.data.userByUsername.comments.pageInfo.nextCursor);
217+
}
218+
}
219+
}
220+
221+
await recurse(after);
222+
return output;
223+
}
224+
225+
async commentDataFull(after, count, order) {
226+
if (!after) after = '';
227+
if (!count) count = 50;
228+
if (!order) order = '';
229+
230+
let user = this.username;
231+
let output = [];
232+
233+
async function recurse(after) {
234+
if (after === null) return;
235+
236+
let info = await constants
237+
.fetch(constants.graphql, {
121238
method: 'POST',
122239
headers,
123240
body: JSON.stringify({
@@ -154,8 +271,7 @@ class User {
154271
order: order
155272
})
156273
})
157-
})
158-
.then(res => res.json());
274+
}).then(res => res.json());
159275

160276
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
161277

0 commit comments

Comments
 (0)