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

Commit 0ab05a5

Browse files
authored
Merge pull request #3 from RayhanADev/RayhanADev-Comment
Create Comment Functions
2 parents 209902f + a55b80f commit 0ab05a5

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
const User = require('./src/User.js');
1+
const user = require('./src/User.js');
2+
const post = require('./src/Post.js');
3+
const comment = require('./src/Comment.js');
4+
const leaderboard = require('./src/Leaderboard.js');
25

36
module.exports = {
4-
User: User.User
7+
User: user.User,
8+
Post: post.Post,
9+
Comment: comment.Comment,
10+
Leaderboard: leaderboard.Leaderboard
511
}

src/Comment.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 Comment {
23+
constructor(id, filter) {
24+
this.id = id;
25+
}
26+
27+
async commentData() {
28+
let id = this.id;
29+
if (typeof id != 'number') {
30+
throw new Error(`${id} is not a comment. Please query comments on Repl.it.`);
31+
}
32+
33+
let info = await fetch('https://repl.it/graphql', {
34+
method: 'POST',
35+
headers,
36+
body: JSON.stringify({
37+
query: `
38+
query Comment($id: Int!) {
39+
comment(id: $id) {
40+
${commentAttributes}
41+
}
42+
}`,
43+
variables: {
44+
id
45+
}
46+
})
47+
}).then(res => res.json());
48+
49+
if (!info.data.comment) {
50+
throw new Error(`${id} is not a comment. Please query comments on Repl.it.`);
51+
} else {
52+
return info.data.comment;
53+
}
54+
}
55+
}
56+
57+
module.exports = {
58+
Comment: Comment
59+
};

0 commit comments

Comments
 (0)