|
| 1 | +let headers = require('../utils/headers.js'); |
| 2 | +let variables = require('../utils/variables.js'); |
| 3 | + |
| 4 | +class Comment { |
| 5 | + constructor(id) { |
| 6 | + this.id = id; |
| 7 | + } |
| 8 | + |
| 9 | + async commentData() { |
| 10 | + let id = this.id; |
| 11 | + if (typeof id != 'number') { |
| 12 | + throw new Error( |
| 13 | + `${id} is not a comment. Please query comments on Repl.it.` |
| 14 | + ); |
| 15 | + } |
| 16 | + |
| 17 | + let info = await variables |
| 18 | + .fetch('https://repl.it/graphql', { |
| 19 | + method: 'POST', |
| 20 | + headers, |
| 21 | + body: JSON.stringify({ |
| 22 | + query: ` |
| 23 | + query Comment($id: Int!) { |
| 24 | + comment(id: $id) { |
| 25 | + ${variables.commentAttributes} |
| 26 | + } |
| 27 | + }`, |
| 28 | + variables: { |
| 29 | + id |
| 30 | + } |
| 31 | + }) |
| 32 | + }) |
| 33 | + .then(res => res.json()); |
| 34 | + |
| 35 | + if (!info.data.comment) { |
| 36 | + throw new Error( |
| 37 | + `${id} is not a comment. Please query comments on Repl.it.` |
| 38 | + ); |
| 39 | + } else { |
| 40 | + return info.data.comment; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + async createComment(message) { |
| 45 | + if (!global.cookies) { |
| 46 | + throw new Error('Not logged in.'); |
| 47 | + } else { |
| 48 | + if (this.username == 'RayhanADev') { |
| 49 | + let id = this.id; |
| 50 | + if (typeof id != 'number') { |
| 51 | + throw new Error( |
| 52 | + `${id} is not a post. Please query posts on Repl.it.` |
| 53 | + ); |
| 54 | + } |
| 55 | + if (typeof message != 'string') { |
| 56 | + throw new Error( |
| 57 | + `Message must be of type string. Got type ${typeof message}.` |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + headers.Cookie = global.cookies; |
| 62 | + let info = await variables |
| 63 | + .fetch('https://repl.it/graphql', { |
| 64 | + method: 'POST', |
| 65 | + headers, |
| 66 | + body: JSON.stringify({ |
| 67 | + query: ` |
| 68 | + mutation createComment($id: Int!, $message: String!) { |
| 69 | + createComment(input: { body: $message, commentId: $id }) { |
| 70 | + comment { ${variables.commentAttributes} } |
| 71 | + } |
| 72 | + }`, |
| 73 | + variables: { |
| 74 | + id, |
| 75 | + message |
| 76 | + } |
| 77 | + }) |
| 78 | + }) |
| 79 | + .then(res => res.json()); |
| 80 | + |
| 81 | + if (!info.data.createComment) { |
| 82 | + throw new Error( |
| 83 | + `${id} is not a post. Please query posts on Repl.it.` |
| 84 | + ); |
| 85 | + } else { |
| 86 | + return info.data.createComment; |
| 87 | + } |
| 88 | + } else { |
| 89 | + throw new Error( |
| 90 | + `${user} is not whitelisted. Please contact @RayhanADev in Repl.it to talk about getting added to the whitelist.` |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + async deleteComment() { |
| 97 | + if (!global.cookies) { |
| 98 | + throw new Error('Not logged in.'); |
| 99 | + } else { |
| 100 | + if (this.username == 'RayhanADev') { |
| 101 | + let id = this.id; |
| 102 | + if (typeof id != 'number') { |
| 103 | + throw new Error( |
| 104 | + `${id} is not a post. Please query posts on Repl.it.` |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + headers.Cookie = global.cookies; |
| 109 | + let info = await variables |
| 110 | + .fetch('https://repl.it/graphql', { |
| 111 | + method: 'POST', |
| 112 | + headers, |
| 113 | + body: JSON.stringify({ |
| 114 | + query: ` |
| 115 | + mutation deleteComment($id: Int!) { |
| 116 | + deleteComment(id: $id ) { |
| 117 | + ${variables.commentAttributes} |
| 118 | + } |
| 119 | + }`, |
| 120 | + variables: { |
| 121 | + id |
| 122 | + } |
| 123 | + }) |
| 124 | + }) |
| 125 | + .then(res => res.json()); |
| 126 | + |
| 127 | + if (!info.data.deleteComment) { |
| 128 | + throw new Error( |
| 129 | + `${id} is not a post. Please query posts on Repl.it.` |
| 130 | + ); |
| 131 | + } else { |
| 132 | + return info.data.deleteComment; |
| 133 | + } |
| 134 | + } else { |
| 135 | + throw new Error( |
| 136 | + `${user} is not whitelisted. Please contact @RayhanADev in Repl.it to talk about getting added to the whitelist.` |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + async updateComment(message) { |
| 143 | + if (!global.cookies) { |
| 144 | + throw new Error('Not logged in.'); |
| 145 | + } else { |
| 146 | + if (this.username == 'RayhanADev') { |
| 147 | + let id = this.id; |
| 148 | + if (typeof id != 'number') { |
| 149 | + throw new Error( |
| 150 | + `${id} is not a post. Please query posts on Repl.it.` |
| 151 | + ); |
| 152 | + } |
| 153 | + if (typeof message != 'string') { |
| 154 | + throw new Error( |
| 155 | + `Message must be of type string. Got type ${typeof message}.` |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + headers.Cookie = global.cookies; |
| 160 | + let info = await variables |
| 161 | + .fetch('https://repl.it/graphql', { |
| 162 | + method: 'POST', |
| 163 | + headers, |
| 164 | + body: JSON.stringify({ |
| 165 | + query: ` |
| 166 | + mutation updateComment($id: Int!, $message: String!) { |
| 167 | + updateComment(input: { body: $message, commentId: $id }) { |
| 168 | + comment { ${variables.commentAttributes} } |
| 169 | + } |
| 170 | + }`, |
| 171 | + variables: { |
| 172 | + id, |
| 173 | + message |
| 174 | + } |
| 175 | + }) |
| 176 | + }) |
| 177 | + .then(res => res.json()); |
| 178 | + |
| 179 | + if (!info.data.updateComment) { |
| 180 | + throw new Error( |
| 181 | + `${id} is not a post. Please query posts on Repl.it.` |
| 182 | + ); |
| 183 | + } else { |
| 184 | + return info.data.updateComment; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +module.exports = { |
| 192 | + Comment: Comment |
| 193 | +}; |
0 commit comments