|
| 1 | +let headers = require('../utils/headers.js'); |
| 2 | +let variables = require('../utils/variables.js'); |
| 3 | + |
| 4 | +class Post { |
| 5 | + constructor(id) { |
| 6 | + this.id = id; |
| 7 | + } |
| 8 | + |
| 9 | + async postData() { |
| 10 | + let id = this.id; |
| 11 | + if (typeof id != 'number') { |
| 12 | + throw new Error(`${id} is not a post. Please query posts on Repl.it.`); |
| 13 | + } |
| 14 | + |
| 15 | + let info = await variables |
| 16 | + .fetch('https://repl.it/graphql', { |
| 17 | + method: 'POST', |
| 18 | + headers, |
| 19 | + body: JSON.stringify({ |
| 20 | + query: ` |
| 21 | + query Post($id: Int!) { |
| 22 | + post(id: $id) { |
| 23 | + ${variables.postAttributes} |
| 24 | + } |
| 25 | + }`, |
| 26 | + variables: { |
| 27 | + id |
| 28 | + } |
| 29 | + }) |
| 30 | + }) |
| 31 | + .then(res => res.json()); |
| 32 | + |
| 33 | + if (!info.data.post) { |
| 34 | + throw new Error(`${id} is not a post. Please query posts on Repl.it.`); |
| 35 | + } else { |
| 36 | + return info.data.post; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + async recentComments() { |
| 41 | + let id = this.id; |
| 42 | + if (typeof id != 'number') { |
| 43 | + throw new Error(`${id} is not a post. Please query posts on Repl.it.`); |
| 44 | + } |
| 45 | + |
| 46 | + let info = await variables |
| 47 | + .fetch('https://repl.it/graphql', { |
| 48 | + method: 'POST', |
| 49 | + headers, |
| 50 | + body: JSON.stringify({ |
| 51 | + query: ` |
| 52 | + query Post($id: Int!) { |
| 53 | + post(id: $id) { |
| 54 | + recentComments { |
| 55 | + ${variables.commentAttributes} |
| 56 | + } |
| 57 | + } |
| 58 | + }`, |
| 59 | + variables: { |
| 60 | + id |
| 61 | + } |
| 62 | + }) |
| 63 | + }) |
| 64 | + .then(res => res.json()); |
| 65 | + |
| 66 | + if (!info.data.post) { |
| 67 | + throw new Error(`${id} is not a post. Please query posts on Repl.it.`); |
| 68 | + } else { |
| 69 | + return info.data.post.recentComments; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + async createComment(message) { |
| 74 | + if (!global.cookies) { |
| 75 | + throw new Error('Not logged in.'); |
| 76 | + } else { |
| 77 | + if (this.username == 'RayhanADev') { |
| 78 | + let id = this.id; |
| 79 | + if (typeof id != 'number') { |
| 80 | + throw new Error( |
| 81 | + `${id} is not a post. Please query posts on Repl.it.` |
| 82 | + ); |
| 83 | + } |
| 84 | + if (typeof message != 'string') { |
| 85 | + throw new Error( |
| 86 | + `Message must be of type string. Got type ${typeof message}.` |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + headers.Cookie = global.cookies; |
| 91 | + let info = await variables |
| 92 | + .fetch('https://repl.it/graphql', { |
| 93 | + method: 'POST', |
| 94 | + headers, |
| 95 | + body: JSON.stringify({ |
| 96 | + query: ` |
| 97 | + mutation createComment($id: Int!, $message: String!) { |
| 98 | + createComment(input: { body: $message, postId: $id }) { |
| 99 | + comment { ${variables.commentAttributes} } |
| 100 | + } |
| 101 | + }`, |
| 102 | + variables: { |
| 103 | + id, |
| 104 | + message |
| 105 | + } |
| 106 | + }) |
| 107 | + }) |
| 108 | + .then(res => res.json()); |
| 109 | + |
| 110 | + if (!info.data.createComment) { |
| 111 | + throw new Error( |
| 112 | + `${id} is not a post. Please query posts on Repl.it.` |
| 113 | + ); |
| 114 | + } else { |
| 115 | + return info.data.createComment; |
| 116 | + } |
| 117 | + } else { |
| 118 | + throw new Error( |
| 119 | + `${user} is not whitelisted. Please contact @RayhanADev in Repl.it to talk about getting added to the whitelist.` |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +module.exports = { |
| 127 | + Post: Post |
| 128 | +}; |
0 commit comments