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

Commit 53f2914

Browse files
author
Rayhan Arayilakath
committed
Refactor Post Class
1 parent 4e858f2 commit 53f2914

File tree

1 file changed

+236
-32
lines changed

1 file changed

+236
-32
lines changed

src/classes/Post.js

Lines changed: 236 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,72 @@ class Post {
66
this.id = id;
77
}
88

9-
async postData() {
9+
async postDataAbridged() {
1010
let id = this.id;
1111
if (typeof id != 'number') {
1212
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
1313
}
1414

15-
let info = await variables
16-
.fetch(variables.graphql, {
15+
let info = await constants
16+
.fetch(constants.graphql, {
1717
method: 'POST',
1818
headers,
1919
body: JSON.stringify({
2020
query: `
2121
query Post($id: Int!) {
2222
post(id: $id) {
23-
${variables.postAttributes}
23+
id,
24+
title,
25+
preview(length: ${global.initVariables.markdown.length || 150}, removeMarkdown: ${global.initVariables.markdown.removeMarkdown || true})
2426
}
2527
}`,
2628
variables: JSON.stringify({
2729
id: id
2830
})
2931
})
30-
})
31-
.then(res => res.json());
32+
}).then(res => res.json());
3233

34+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
35+
36+
if (!info.data.post) {
37+
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
38+
} else {
39+
return info.data.post;
40+
}
41+
}
42+
43+
async postDataFull() {
44+
let id = this.id;
45+
if (typeof id != 'number') {
46+
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
47+
}
48+
49+
let info = await constants
50+
.fetch(constants.graphql, {
51+
method: 'POST',
52+
headers,
53+
body: JSON.stringify({
54+
query: `
55+
query Post($id: Int!) {
56+
post(id: $id) {
57+
${constants.postAttributes},
58+
user { ${constants.userAttributes} },
59+
board { ${constants.boardAttributes} },
60+
repl { ${constants.replAttributes} },
61+
comments(count: ${global.initVariables.previewCount.comments || 10}) { items { ${constants.commentAttributes} } },
62+
votes { items { id, user { ${constants.userAttributes} } } },
63+
answeredBy { ${constants.userAttributes} },
64+
answer { ${ constants.commentAttributes} }
65+
}
66+
}`,
67+
variables: JSON.stringify({
68+
id: id
69+
})
70+
})
71+
}).then(res => res.json());
72+
73+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
74+
3375
if (!info.data.post) {
3476
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
3577
} else {
@@ -43,80 +85,242 @@ class Post {
4385
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
4486
}
4587

46-
let info = await variables
47-
.fetch(variables.graphql, {
88+
let info = await constants
89+
.fetch(constants.graphql, {
4890
method: 'POST',
4991
headers,
5092
body: JSON.stringify({
5193
query: `
5294
query Post($id: Int!) {
5395
post(id: $id) {
5496
recentComments {
55-
${variables.commentAttributes}
97+
${constants.commentAttributes},
98+
parentComment { ${constants.commentAttributes} },
99+
comments { ${constants.commentAttributes} },
100+
user { ${constants.userAttributes} },
101+
post {
102+
${constants.postAttributes},
103+
user { ${constants.userAttributes} },
104+
board { ${constants.boardAttributes} },
105+
repl { ${constants.replAttributes} },
106+
comments(count: ${global.initVariables.previewCount.comments || 10}) { items { ${constants.commentAttributes} } },
107+
votes { items { id, user { ${constants.userAttributes} } } },
108+
answeredBy { ${constants.userAttributes} },
109+
answer { ${ constants.commentAttributes} }
110+
}
56111
}
57112
}
58113
}`,
59114
variables: JSON.stringify({
60115
id: id
61116
})
62117
})
63-
})
64-
.then(res => res.json());
118+
}).then(res => res.json());
65119

120+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`)
121+
66122
if (!info.data.post) {
67123
throw new Error(`${id} is not a post. Please query posts on Repl.it.`);
68124
} else {
69125
return info.data.post.recentComments;
70126
}
71127
}
72128

73-
async createComment(message) {
129+
async createPost(title, body, boardId, replId, showHosted) {
130+
if (!global.cookies) {
131+
throw new Error('ReplAPI.it: Not logged in.');
132+
} else {
133+
if (['RayhanADev'].contains(global.initVariables.username)) {
134+
if (typeof title != 'string') {
135+
throw new Error(
136+
`Title must be of type string. Got type ${typeof title}.`
137+
);
138+
}
139+
if (typeof body != 'string') {
140+
throw new Error(
141+
`Body must be of type string. Got type ${typeof body}.`
142+
);
143+
}
144+
if (typeof boardId != 'number') {
145+
throw new Error(
146+
`Board ID must be of type number. Got type ${typeof boardId}.`
147+
);
148+
}
149+
if (typeof replId != 'string') {
150+
throw new Error(
151+
`Repl ID must be of type string. Got type ${typeof replId}.`
152+
);
153+
}
154+
if (typeof showHosted != 'string') {
155+
throw new Error(
156+
`Show Hosted must be of type boolean. Got type ${typeof showHosted}.`
157+
);
158+
}
159+
160+
headers['Set-Cookie'] = global.cookies;
161+
let info = await constants
162+
.fetch(constants.graphql, {
163+
method: 'POST',
164+
headers,
165+
body: JSON.stringify({
166+
query: `
167+
mutation CreatePost($input: CreatePostInput!) {
168+
createPost(input: $input) {
169+
post {
170+
id,
171+
title,
172+
preview(length: ${global.initVariables.markdown.length || 150}, removeMarkdown: ${global.initVariables.markdown.removeMarkdown || true})
173+
}
174+
}
175+
}`,
176+
variables: JSON.stringify({
177+
input: {
178+
title: title,
179+
body: body,
180+
boardId: boardId,
181+
replId: replId,
182+
showHosted: showHosted
183+
}
184+
})
185+
})
186+
})
187+
.then(res => res.json());
188+
189+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`);
190+
else return info.data.createPost.post;
191+
} else {
192+
throw new Error(
193+
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`
194+
);
195+
}
196+
}
197+
}
198+
199+
async updatePost(title, body, isPinned, postType, isLocked, boardId, replId, showHosted) {
74200
if (!global.cookies) {
75-
throw new Error('Not logged in.');
201+
throw new Error('ReplAPI.it: Not logged in.');
76202
} else {
77-
if (this.username == 'RayhanADev') {
78-
let id = this.id;
79-
if (typeof id != 'number') {
203+
if (['RayhanADev'].contains(global.initVariables.username)) {
204+
if (typeof title != 'string' || typeof title != 'undefined') {
205+
throw new Error(
206+
`Title must be of type string. Got type ${typeof title}.`
207+
);
208+
}
209+
if (typeof body != 'string' || typeof body != 'undefined') {
210+
throw new Error(
211+
`Body must be of type string. Got type ${typeof body}.`
212+
);
213+
}
214+
if (typeof isPinned != 'boolean' || typeof isPinned != 'undefined') {
215+
throw new Error(
216+
`isPinned must be of type boolean. Got type ${typeof isPinned}.`
217+
);
218+
}
219+
if (typeof postType != 'string' || typeof postType != 'undefined') {
220+
throw new Error(
221+
`Post Type must be of type string. Got type ${typeof postType}.`
222+
);
223+
}
224+
if (typeof isLocked != 'boolean' || typeof isLocked != 'undefined') {
225+
throw new Error(
226+
`isLocked must be of type boolean. Got type ${typeof isPinned}.`
227+
);
228+
}
229+
if (typeof boardId != 'number' || typeof boardId != 'undefined') {
230+
throw new Error(
231+
`Board ID must be of type number. Got type ${typeof boardId}.`
232+
);
233+
}
234+
if (typeof replId != 'string' || typeof replId != 'undefined') {
80235
throw new Error(
81-
`${id} is not a post. Please query posts on Repl.it.`
236+
`Repl ID must be of type string. Got type ${typeof replId}.`
82237
);
83238
}
84-
if (typeof message != 'string') {
239+
if (typeof showHosted != 'string' || typeof showHosted != 'undefined') {
85240
throw new Error(
86-
`Message must be of type string. Got type ${typeof message}.`
241+
`Show Hosted must be of type boolean. Got type ${typeof showHosted}.`
87242
);
88243
}
89244

90-
headers.Cookie = global.cookies;
91-
let info = await variables
92-
.fetch(variables.graphql, {
245+
headers['Set-Cookie'] = global.cookies;
246+
let info = await constants
247+
.fetch(constants.graphql, {
93248
method: 'POST',
94249
headers,
95250
body: JSON.stringify({
96251
query: `
97-
mutation createComment($id: Int!, $message: String!) {
98-
createComment(input: { body: $message, postId: $id }) {
99-
comment { ${variables.commentAttributes} }
252+
mutation UpdatePost($input: CreatePostInput!) {
253+
updatePost(input: $input) {
254+
post {
255+
id,
256+
title,
257+
preview(length: ${global.initVariables.markdown.length || 150}, removeMarkdown: ${global.initVariables.markdown.removeMarkdown || true})
258+
}
100259
}
101260
}`,
102261
variables: JSON.stringify({
103-
id: id,
104-
message: message
262+
input: {
263+
title: title,
264+
body: body,
265+
isPinned: isPinned,
266+
postType: postType,
267+
isLocked: isLocked,
268+
boardId: boardId,
269+
replId: replId,
270+
showHosted: showHosted
271+
}
105272
})
106273
})
107274
})
108275
.then(res => res.json());
109276

110-
if (!info.data.createComment) {
277+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`);
278+
else return info.data.updatePost.post;
279+
} else {
280+
throw new Error(
281+
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`
282+
);
283+
}
284+
}
285+
}
286+
287+
async deletePost(id) {
288+
if (!global.cookies) {
289+
throw new Error('ReplAPI.it: Not logged in.');
290+
} else {
291+
if (['RayhanADev'].contains(global.initVariables.username)) {
292+
if (typeof id != 'number') {
111293
throw new Error(
112-
`${id} is not a post. Please query posts on Repl.it.`
294+
`Id must be of type number. Got type ${typeof title}.`
113295
);
114-
} else {
115-
return info.data.createComment;
116296
}
297+
298+
headers['Set-Cookie'] = global.cookies;
299+
let info = await constants
300+
.fetch(constants.graphql, {
301+
method: 'POST',
302+
headers,
303+
body: JSON.stringify({
304+
query: `
305+
mutation CreatePost($id: Int!) {
306+
deletePost(id: $id) {
307+
id,
308+
title,
309+
preview(length: ${global.initVariables.markdown.length || 150}, removeMarkdown: ${global.initVariables.markdown.removeMarkdown || true})
310+
}
311+
}`,
312+
variables: JSON.stringify({
313+
id: id
314+
})
315+
})
316+
})
317+
.then(res => res.json());
318+
319+
if(info.errors) throw new Error(`Replit GraphQL Error(s): ${JSON.stringify(info.errors)}`);
320+
else return info.data.post;
117321
} else {
118322
throw new Error(
119-
`${user} is not whitelisted. Please contact @RayhanADev in Repl.it to talk about getting added to the whitelist.`
323+
`${global.initVariables.username} is not whitelisted. Please contact @RayhanADev in ReplTalk to talk about getting added to the whitelist.`
120324
);
121325
}
122326
}

0 commit comments

Comments
 (0)