repltalk 2.0.3 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,164 @@
1
+ require "http"
2
+ require "json"
3
+
4
+ module ReplTalk
5
+ class Client
6
+ attr_writer :sid
7
+
8
+ def initialize(sid=nil)
9
+ @sid = sid
10
+ end
11
+
12
+ def graphql(name, query, **variables)
13
+ payload = {
14
+ operationName: name,
15
+ query: query,
16
+ variables: variables.to_json
17
+ }
18
+ r = HTTP
19
+ .cookies(
20
+ "connect.sid": @sid
21
+ )
22
+ .headers(
23
+ referer: "#{$BASE_URL}/@CodingCactus/repltalk",
24
+ "X-Requested-With": "ReplTalk"
25
+ )
26
+ .post(
27
+ "#{$BASE_URL}/graphql",
28
+ form: payload
29
+ )
30
+ begin data = JSON.parse(r)
31
+ rescue
32
+ puts "\e[31mERROR\n#{r}\e[0m"
33
+ return nil
34
+ end
35
+ if data.include?("errors")
36
+ puts "\e[31mERROR\n#{r}\e[0m"
37
+ return nil
38
+ end
39
+ data = data["data"] if data.include?("data")
40
+ data
41
+ end
42
+
43
+ def get_user(name)
44
+ u = graphql(
45
+ "userByUsername",
46
+ GQL::Queries::GET_USER,
47
+ username: name
48
+ )
49
+ return nil if u == nil || u["user"] == nil
50
+ User.new(self, u["user"])
51
+ end
52
+
53
+ def get_user_by_id(id)
54
+ u = graphql(
55
+ "user",
56
+ GQL::Queries::GET_USER_BY_ID,
57
+ user_id: id
58
+ )
59
+ return nil if u == nil || u["user"] == nil
60
+ User.new(self, u["user"])
61
+ end
62
+
63
+ def search_user(username, count: 10)
64
+ u = graphql(
65
+ "usernameSearch",
66
+ GQL::Queries::USER_SEARCH,
67
+ username: username,
68
+ count: count
69
+ )
70
+ return nil if u["usernameSearch"] == nil
71
+ u["usernameSearch"].map { |user| User.new(self, user) }
72
+ end
73
+
74
+ def get_post(id)
75
+ p = graphql(
76
+ "post",
77
+ GQL::Queries::GET_POST,
78
+ id: id
79
+ )
80
+ return nil if p == nil || p["post"] == nil
81
+ Post.new(self, p["post"])
82
+ end
83
+
84
+ def get_comment(id)
85
+ c = graphql(
86
+ "comment",
87
+ GQL::Queries::GET_COMMENT,
88
+ id: id
89
+ )
90
+ return nil if c == nil || c["comment"] == nil
91
+ Comment.new(self, c["comment"])
92
+ end
93
+
94
+ def get_repl(url)
95
+ r = graphql(
96
+ "ReplView",
97
+ GQL::Queries::GET_REPL,
98
+ url: url
99
+ )
100
+ return nil if r == nil || r["repl"] == nil
101
+ Repl.new(self, r["repl"])
102
+ end
103
+
104
+ def get_repl_comment(id)
105
+ c = graphql(
106
+ "ReplViewComment",
107
+ GQL::Queries::GET_REPL_COMMENT,
108
+ id: id
109
+ )
110
+ return nil if c == nil || c["replComment"] == nil
111
+ ReplComment.new(self, c["replComment"])
112
+ end
113
+
114
+ def get_board(name)
115
+ b = graphql(
116
+ "boardBySlug",
117
+ GQL::Queries::GET_BOARD,
118
+ slug: name
119
+ )
120
+ return nil if b == nil || b["board"] == nil
121
+ Board.new(b["board"])
122
+ end
123
+
124
+ def get_leaderboard(count: nil, since: nil, after: nil)
125
+ u = graphql(
126
+ "LeaderboardQuery",
127
+ GQL::Queries::GET_LEADERBOARD,
128
+ count: count,
129
+ since: since,
130
+ after: after
131
+ )
132
+ u["leaderboard"]["items"].map { |user| LeaderboardUser.new(self, user) }
133
+ end
134
+
135
+ def get_posts(board: "all", order: "new", count: nil, after: nil, search: nil, languages: nil)
136
+ p = graphql(
137
+ "PostsFeed",
138
+ GQL::Queries::GET_POSTS,
139
+ boardSlugs: [board],
140
+ order: order,
141
+ count: count,
142
+ after: after,
143
+ searchQuery: search,
144
+ languages: languages
145
+ )
146
+ p["posts"]["items"].map { |post| Post.new(self, post) }
147
+ end
148
+
149
+ def create_post(board_name, title, content, repl_id: nil, show_hosted: false)
150
+ p = graphql(
151
+ "createPost",
152
+ GQL::Mutations::CREATE_POST,
153
+ input: {
154
+ boardId: get_board(board_name).id,
155
+ title: title,
156
+ body: content,
157
+ replId: repl_id,
158
+ showHosted: show_hosted
159
+ }
160
+ )
161
+ Post.new(self, p["createPost"]["post"])
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,462 @@
1
+ module ReplTalk
2
+ module GQL
3
+ module Fields
4
+ ROLES = "
5
+ id
6
+ name
7
+ key
8
+ tagline
9
+ "
10
+
11
+ ORGANIZATION = "
12
+ id
13
+ name
14
+ country
15
+ postalCode
16
+ state
17
+ city
18
+ timeCreated
19
+ "
20
+
21
+ LANGUAGE = "
22
+ id
23
+ key
24
+ displayName
25
+ tagline
26
+ icon
27
+ category
28
+ "
29
+
30
+ BOARD = "
31
+ id
32
+ name
33
+ color
34
+ description
35
+ "
36
+
37
+ USER = "
38
+ id
39
+ fullName
40
+ username
41
+ image
42
+ bio
43
+ karma
44
+ isHacker
45
+ timeCreated
46
+ roles {
47
+ #{ROLES}
48
+ }
49
+ organization {
50
+ #{ORGANIZATION}
51
+ }
52
+ languages {
53
+ #{LANGUAGE}
54
+ }
55
+ "
56
+
57
+ REPL = "
58
+ id
59
+ url
60
+ title
61
+ description
62
+ timeCreated
63
+ size
64
+ imageUrl
65
+ isPrivate
66
+ isAlwaysOn
67
+ lang {
68
+ #{LANGUAGE}
69
+ }
70
+ user {
71
+ #{USER}
72
+ }
73
+ origin {
74
+ url
75
+ }
76
+ "
77
+
78
+ COMMENT = "
79
+ id
80
+ body
81
+ timeCreated
82
+ url
83
+ isAnswer
84
+ voteCount
85
+ canVote
86
+ hasVoted
87
+ user {
88
+ #{USER}
89
+ }
90
+ post {
91
+ id
92
+ }
93
+ "
94
+
95
+ REPL_COMMENT = "
96
+ id
97
+ body
98
+ timeCreated
99
+ user {
100
+ #{USER}
101
+ }
102
+ repl {
103
+ #{REPL}
104
+ }
105
+ "
106
+
107
+ POST = "
108
+ id
109
+ title
110
+ body
111
+ preview(removeMarkdown: true, length: 150)
112
+ url
113
+ commentCount
114
+ isHidden
115
+ isPinned
116
+ isLocked
117
+ isAnnouncement
118
+ timeCreated
119
+ isAnswered
120
+ isAnswerable
121
+ voteCount
122
+ canVote
123
+ hasVoted
124
+ user {
125
+ #{USER}
126
+ }
127
+ repl {
128
+ #{REPL}
129
+ }
130
+ board {
131
+ #{BOARD}
132
+ }
133
+ answer {
134
+ #{COMMENT}
135
+ }
136
+ "
137
+ end
138
+
139
+
140
+ module Queries
141
+ GET_USER = "
142
+ query userByUsername($username: String!) {
143
+ user: userByUsername(username: $username) {
144
+ #{Fields::USER}
145
+ }
146
+ }
147
+ "
148
+
149
+ GET_USER_BY_ID = "
150
+ query user($user_id: Int!) {
151
+ user: user(id: $user_id) {
152
+ #{Fields::USER}
153
+ }
154
+ }
155
+ "
156
+
157
+ USER_SEARCH = "
158
+ query usernameSearch($username: String!, $count: Int) {
159
+ usernameSearch(query: $username, limit: $count) {
160
+ #{Fields::USER}
161
+ }
162
+ }
163
+ "
164
+
165
+ GET_USER_POSTS = "
166
+ query user($username: String!, $after: String, $order: String, $count: Int) {
167
+ user: userByUsername(username: $username) {
168
+ posts(after: $after, order: $order, count: $count) {
169
+ items {
170
+ #{Fields::POST}
171
+ }
172
+ }
173
+ }
174
+ }
175
+ "
176
+
177
+ GET_USER_COMMENTS = "
178
+ query user($username: String!, $after: String, $order: String, $count: Int) {
179
+ user: userByUsername(username: $username) {
180
+ comments(after: $after, order: $order, count: $count) {
181
+ items {
182
+ #{Fields::COMMENT}
183
+ }
184
+ }
185
+ }
186
+ }
187
+ "
188
+
189
+ GET_USER_REPLS = "
190
+ query user($username: String!, $count: Int, $order: String, $direction: String, $before: String, $after: String, $pinnedReplsFirst: Boolean, $showUnnamed: Boolean) {
191
+ user: userByUsername(username: $username) {
192
+ publicRepls(count: $count, order: $order, direction: $direction, before: $before, after: $after, pinnedReplsFirst: $pinnedReplsFirst, showUnnamed: $showUnnamed) {
193
+ items {
194
+ #{Fields::REPL}
195
+ }
196
+ }
197
+ }
198
+ }
199
+ "
200
+
201
+ GET_POST = "
202
+ query post($id: Int!) {
203
+ post(id: $id) {
204
+ #{Fields::POST}
205
+ }
206
+ }
207
+ "
208
+
209
+ GET_POSTS_COMMENTS = "
210
+ query post($postId: Int!, $order: String, $count: Int, $after: String) {
211
+ post(id: $postId) {
212
+ comments(order: $order, count: $count, after: $after) {
213
+ items {
214
+ #{Fields::COMMENT}
215
+ }
216
+ }
217
+ }
218
+ }
219
+ "
220
+
221
+ GET_POSTS_UPVOTERS = "
222
+ query post($id: Int!, $count: Int) {
223
+ post(id: $id) {
224
+ votes(count: $count) {
225
+ items {
226
+ user {
227
+ #{Fields::POST}
228
+ }
229
+ }
230
+ }
231
+ }
232
+ }
233
+ "
234
+
235
+ GET_COMMENT = "
236
+ query comment ($id: Int!) {
237
+ comment(id: $id) {
238
+ #{Fields::COMMENT}
239
+ }
240
+ }
241
+ "
242
+
243
+ GET_COMMENTS_COMMENTS = "
244
+ query comment ($id: Int!) {
245
+ comment(id: $id) {
246
+ comments {
247
+ #{Fields::COMMENT}
248
+ }
249
+ }
250
+ }
251
+ "
252
+
253
+ GET_PARENT_COMMENT = "
254
+ query comment ($id: Int!) {
255
+ comment(id: $id) {
256
+ parentComment {
257
+ #{Fields::COMMENT}
258
+ }
259
+ }
260
+ }
261
+ "
262
+
263
+ GET_REPL = "
264
+ query ReplView($url: String!) {
265
+ repl(url: $url) {
266
+ ... on Repl {
267
+ #{Fields::REPL}
268
+ }
269
+ }
270
+ }
271
+ "
272
+
273
+ GET_REPL_FORKS = "
274
+ query ReplViewForks($url: String!, $count: Int!, $after: String) {
275
+ repl(url: $url) {
276
+ ... on Repl {
277
+ publicForks(count: $count, after: $after) {
278
+ items {
279
+ #{Fields::REPL}
280
+ }
281
+ }
282
+ }
283
+ }
284
+ }
285
+ "
286
+
287
+ GET_REPL_COMMENTS = "
288
+ query ReplViewComments($url: String!, $count: Int, $after: String) {
289
+ repl(url: $url) {
290
+ ... on Repl {
291
+ comments(count: $count, after: $after) {
292
+ items {
293
+ #{Fields::REPL_COMMENT}
294
+ replies {
295
+ #{Fields::REPL_COMMENT}
296
+ }
297
+ }
298
+ }
299
+ }
300
+ }
301
+ }
302
+ "
303
+
304
+ GET_REPL_COMMENT = "
305
+ query ReplViewComment($id: Int!) {
306
+ replComment(id: $id) {
307
+ ... on ReplComment {
308
+ #{Fields::REPL_COMMENT}
309
+ }
310
+ }
311
+ }
312
+ "
313
+
314
+
315
+ GET_BOARD = "
316
+ query boardBySlug($slug: String!) {
317
+ board: boardBySlug(slug: $slug) {
318
+ #{Fields::BOARD}
319
+ }
320
+ }
321
+ "
322
+
323
+ GET_POSTS = "
324
+ query PostsFeed($order: String, $after: String, $searchQuery: String, $languages: [String!], $count: Int, $boardSlugs: [String!]) {
325
+ posts(order: $order, after: $after, searchQuery: $searchQuery, languages: $languages, count: $count, boardSlugs: $boardSlugs) {
326
+ items {
327
+ #{Fields::POST}
328
+ }
329
+ }
330
+ }
331
+ "
332
+
333
+ GET_LEADERBOARD = "
334
+ query LeaderboardQuery($count: Int, $after: String, $since: KarmaSince) {
335
+ leaderboard(count: $count, after: $after, since: $since) {
336
+ items {
337
+ #{Fields::USER}
338
+ karmaSince: karma(since: $since)
339
+ }
340
+ }
341
+ }
342
+ "
343
+ end
344
+
345
+
346
+ module Mutations
347
+ include Fields
348
+
349
+ CREATE_POST = "
350
+ mutation createPost($input: CreatePostInput!) {
351
+ createPost(input: $input) {
352
+ post {
353
+ #{Fields::POST}
354
+ }
355
+ }
356
+ }
357
+ "
358
+
359
+ EDIT_POST = "
360
+ mutation updatePost($input: UpdatePostInput!) {
361
+ updatePost(input: $input) {
362
+ post {
363
+ #{Fields::POST}
364
+ }
365
+ }
366
+ }
367
+ "
368
+
369
+ DELETE_POST = "
370
+ mutation deletePost($id: Int!) {
371
+ deletePost(id: $id) {
372
+ id
373
+ }
374
+ }
375
+ "
376
+
377
+ CREATE_COMMENT = "
378
+ mutation createComment($input: CreateCommentInput!) {
379
+ createComment(input: $input) {
380
+ comment {
381
+ #{Fields::COMMENT}
382
+ }
383
+ }
384
+ }
385
+ "
386
+
387
+ EDIT_COMMENT = "
388
+ mutation updateComment($input: UpdateCommentInput!) {
389
+ updateComment(input: $input) {
390
+ comment {
391
+ #{Fields::COMMENT}
392
+ }
393
+ }
394
+ }
395
+ "
396
+
397
+ DELETE_COMMENT = "
398
+ mutation deleteComment($id: Int!) {
399
+ deleteComment(id: $id) {
400
+ id
401
+ }
402
+ }
403
+ "
404
+
405
+ CREATE_REPL_COMMENT = "
406
+ mutation ReplViewCreateReplComment($input: CreateReplCommentInput!) {
407
+ createReplComment(input: $input) {
408
+ ... on ReplComment {
409
+ #{Fields::REPL_COMMENT}
410
+ }
411
+ }
412
+ }
413
+ "
414
+
415
+ REPLY_REPL_COMMENT = "
416
+ mutation ReplViewCreateReplCommentReply($input: CreateReplCommentReplyInput!) {
417
+ createReplCommentReply(input: $input) {
418
+ ... on ReplComment {
419
+ #{Fields::REPL_COMMENT}
420
+ }
421
+ }
422
+ }
423
+ "
424
+
425
+ EDIT_REPL_COMMENT = "
426
+ mutation ReplViewCommentsUpdateReplComment($input: UpdateReplCommentInput!) {
427
+ updateReplComment(input: $input) {
428
+ ... on ReplComment {
429
+ #{Fields::REPL_COMMENT}
430
+ }
431
+ }
432
+ }
433
+ "
434
+
435
+ DELETE_REPL_COMMENT = "
436
+ mutation ReplViewCommentsDeleteReplComment($id: Int!) {
437
+ deleteReplComment(id: $id) {
438
+ ... on ReplComment {
439
+ id
440
+ }
441
+ }
442
+ }
443
+ "
444
+
445
+ REPORT_POST = "
446
+ mutation createBoardReport($id: Int!, $reason: String!) {
447
+ createBoardReport(postId: $id, reason: $reason) {
448
+ id
449
+ }
450
+ }
451
+ "
452
+
453
+ REPORT_COMMENT = "
454
+ mutation createBoardReport($id: Int!, $reason: String!) {
455
+ createBoardReport(commentId: $id, reason: $reason) {
456
+ id
457
+ }
458
+ }
459
+ "
460
+ end
461
+ end
462
+ end