repltalk 2.1.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,190 @@
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 get_explore_featured_repls
150
+ r = graphql(
151
+ "ExploreFeaturedRepls",
152
+ GQL::Queries::GET_EXPLORE_FEATURED_REPLS
153
+ )
154
+ r["featuredRepls"].map { |repl| Repl.new(self, repl) }
155
+ end
156
+
157
+ def get_trending_tags(count: nil)
158
+ t = graphql(
159
+ "ExploreFeed",
160
+ GQL::Queries::GET_TRENDING_TAGS,
161
+ count: count
162
+ )
163
+ t["trendingTagsFeed"]["initialTags"].map { |tag| Tag.new(self, tag) }
164
+ end
165
+
166
+ def get_tag(tag)
167
+ t = graphql(
168
+ "ExploreTrendingRepls",
169
+ GQL::Queries::GET_TAG,
170
+ tag: tag
171
+ )
172
+ Tag.new(self, t["tag"])
173
+ end
174
+
175
+ def create_post(board_name, title, content, repl_id: nil, show_hosted: false)
176
+ p = graphql(
177
+ "createPost",
178
+ GQL::Mutations::CREATE_POST,
179
+ input: {
180
+ boardId: get_board(board_name).id,
181
+ title: title,
182
+ body: content,
183
+ replId: repl_id,
184
+ showHosted: show_hosted
185
+ }
186
+ )
187
+ Post.new(self, p["createPost"]["post"])
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,554 @@
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
+ TAG = "
58
+ id
59
+ replCount
60
+ replsTaggedTodayCount
61
+ creatorCount
62
+ isTrending
63
+ "
64
+
65
+ REACTIONS = "
66
+ id
67
+ type
68
+ count
69
+ "
70
+
71
+ REPL = "
72
+ id
73
+ url
74
+ title
75
+ description
76
+ timeCreated
77
+ size
78
+ runCount
79
+ publicForkCount
80
+ imageUrl
81
+ isPrivate
82
+ isAlwaysOn
83
+ tags {
84
+ #{TAG}
85
+ }
86
+ reactions {
87
+ #{REACTIONS}
88
+ }
89
+ lang {
90
+ #{LANGUAGE}
91
+ }
92
+ user {
93
+ #{USER}
94
+ }
95
+ origin {
96
+ url
97
+ }
98
+ "
99
+
100
+ COMMENT = "
101
+ id
102
+ body
103
+ timeCreated
104
+ url
105
+ isAnswer
106
+ voteCount
107
+ canVote
108
+ hasVoted
109
+ user {
110
+ #{USER}
111
+ }
112
+ post {
113
+ id
114
+ }
115
+ "
116
+
117
+ REPL_COMMENT = "
118
+ id
119
+ body
120
+ timeCreated
121
+ user {
122
+ #{USER}
123
+ }
124
+ repl {
125
+ #{REPL}
126
+ }
127
+ "
128
+
129
+ POST = "
130
+ id
131
+ title
132
+ body
133
+ preview(removeMarkdown: true, length: 150)
134
+ url
135
+ commentCount
136
+ isHidden
137
+ isPinned
138
+ isLocked
139
+ isAnnouncement
140
+ timeCreated
141
+ isAnswered
142
+ isAnswerable
143
+ voteCount
144
+ canVote
145
+ hasVoted
146
+ user {
147
+ #{USER}
148
+ }
149
+ repl {
150
+ #{REPL}
151
+ }
152
+ board {
153
+ #{BOARD}
154
+ }
155
+ answer {
156
+ #{COMMENT}
157
+ }
158
+ "
159
+ end
160
+
161
+
162
+ module Queries
163
+ GET_USER = "
164
+ query userByUsername($username: String!) {
165
+ user: userByUsername(username: $username) {
166
+ #{Fields::USER}
167
+ }
168
+ }
169
+ "
170
+
171
+ GET_USER_BY_ID = "
172
+ query user($user_id: Int!) {
173
+ user: user(id: $user_id) {
174
+ #{Fields::USER}
175
+ }
176
+ }
177
+ "
178
+
179
+ USER_SEARCH = "
180
+ query usernameSearch($username: String!, $count: Int) {
181
+ usernameSearch(query: $username, limit: $count) {
182
+ #{Fields::USER}
183
+ }
184
+ }
185
+ "
186
+
187
+ GET_USER_POSTS = "
188
+ query user($username: String!, $after: String, $order: String, $count: Int) {
189
+ user: userByUsername(username: $username) {
190
+ posts(after: $after, order: $order, count: $count) {
191
+ items {
192
+ #{Fields::POST}
193
+ }
194
+ }
195
+ }
196
+ }
197
+ "
198
+
199
+ GET_USER_COMMENTS = "
200
+ query user($username: String!, $after: String, $order: String, $count: Int) {
201
+ user: userByUsername(username: $username) {
202
+ comments(after: $after, order: $order, count: $count) {
203
+ items {
204
+ #{Fields::COMMENT}
205
+ }
206
+ }
207
+ }
208
+ }
209
+ "
210
+
211
+ GET_USER_REPLS = "
212
+ query user($username: String!, $count: Int, $order: String, $direction: String, $before: String, $after: String, $pinnedReplsFirst: Boolean, $showUnnamed: Boolean) {
213
+ user: userByUsername(username: $username) {
214
+ publicRepls(count: $count, order: $order, direction: $direction, before: $before, after: $after, pinnedReplsFirst: $pinnedReplsFirst, showUnnamed: $showUnnamed) {
215
+ items {
216
+ #{Fields::REPL}
217
+ }
218
+ }
219
+ }
220
+ }
221
+ "
222
+
223
+ GET_POST = "
224
+ query post($id: Int!) {
225
+ post(id: $id) {
226
+ #{Fields::POST}
227
+ }
228
+ }
229
+ "
230
+
231
+ GET_POSTS_COMMENTS = "
232
+ query post($postId: Int!, $order: String, $count: Int, $after: String) {
233
+ post(id: $postId) {
234
+ comments(order: $order, count: $count, after: $after) {
235
+ items {
236
+ #{Fields::COMMENT}
237
+ }
238
+ }
239
+ }
240
+ }
241
+ "
242
+
243
+ GET_POSTS_UPVOTERS = "
244
+ query post($id: Int!, $count: Int) {
245
+ post(id: $id) {
246
+ votes(count: $count) {
247
+ items {
248
+ user {
249
+ #{Fields::POST}
250
+ }
251
+ }
252
+ }
253
+ }
254
+ }
255
+ "
256
+
257
+ GET_COMMENT = "
258
+ query comment ($id: Int!) {
259
+ comment(id: $id) {
260
+ #{Fields::COMMENT}
261
+ }
262
+ }
263
+ "
264
+
265
+ GET_COMMENTS_COMMENTS = "
266
+ query comment ($id: Int!) {
267
+ comment(id: $id) {
268
+ comments {
269
+ #{Fields::COMMENT}
270
+ }
271
+ }
272
+ }
273
+ "
274
+
275
+ GET_PARENT_COMMENT = "
276
+ query comment ($id: Int!) {
277
+ comment(id: $id) {
278
+ parentComment {
279
+ #{Fields::COMMENT}
280
+ }
281
+ }
282
+ }
283
+ "
284
+
285
+ GET_REPL = "
286
+ query ReplView($url: String!) {
287
+ repl(url: $url) {
288
+ ... on Repl {
289
+ #{Fields::REPL}
290
+ }
291
+ }
292
+ }
293
+ "
294
+
295
+ GET_REPL_FORKS = "
296
+ query ReplViewForks($url: String!, $count: Int!, $after: String) {
297
+ repl(url: $url) {
298
+ ... on Repl {
299
+ publicForks(count: $count, after: $after) {
300
+ items {
301
+ #{Fields::REPL}
302
+ }
303
+ }
304
+ }
305
+ }
306
+ }
307
+ "
308
+
309
+ GET_REPL_COMMENTS = "
310
+ query ReplViewComments($url: String!, $count: Int, $after: String) {
311
+ repl(url: $url) {
312
+ ... on Repl {
313
+ comments(count: $count, after: $after) {
314
+ items {
315
+ #{Fields::REPL_COMMENT}
316
+ replies {
317
+ #{Fields::REPL_COMMENT}
318
+ }
319
+ }
320
+ }
321
+ }
322
+ }
323
+ }
324
+ "
325
+
326
+ GET_REPL_COMMENT = "
327
+ query ReplViewComment($id: Int!) {
328
+ replComment(id: $id) {
329
+ ... on ReplComment {
330
+ #{Fields::REPL_COMMENT}
331
+ }
332
+ }
333
+ }
334
+ "
335
+
336
+
337
+ GET_BOARD = "
338
+ query boardBySlug($slug: String!) {
339
+ board: boardBySlug(slug: $slug) {
340
+ #{Fields::BOARD}
341
+ }
342
+ }
343
+ "
344
+
345
+ GET_POSTS = "
346
+ query PostsFeed($order: String, $after: String, $searchQuery: String, $languages: [String!], $count: Int, $boardSlugs: [String!]) {
347
+ posts(order: $order, after: $after, searchQuery: $searchQuery, languages: $languages, count: $count, boardSlugs: $boardSlugs) {
348
+ items {
349
+ #{Fields::POST}
350
+ }
351
+ }
352
+ }
353
+ "
354
+
355
+ GET_LEADERBOARD = "
356
+ query LeaderboardQuery($count: Int, $after: String, $since: KarmaSince) {
357
+ leaderboard(count: $count, after: $after, since: $since) {
358
+ items {
359
+ #{Fields::USER}
360
+ karmaSince: karma(since: $since)
361
+ }
362
+ }
363
+ }
364
+ "
365
+
366
+ GET_EXPLORE_FEATURED_REPLS = "
367
+ query ExploreFeaturedRepls {
368
+ featuredRepls {
369
+ #{Fields::REPL}
370
+ }
371
+ }
372
+ "
373
+
374
+ GET_TAG = "
375
+ query ExploreTrendingRepls($tag: String!) {
376
+ tag(id: $tag) {
377
+ #{Fields::TAG}
378
+ }
379
+ }
380
+ "
381
+
382
+ GET_TRENDING_TAGS = "
383
+ query ExploreFeed($count: Int) {
384
+ trendingTagsFeed(initialTagsCount: $count) {
385
+ initialTags {
386
+ #{Fields::TAG}
387
+ }
388
+ }
389
+ }
390
+ "
391
+
392
+ GET_TAGS_REPLS = "
393
+ query ExploreTrendingRepls($tag: String!, $count: Int, $after: String) {
394
+ tag(id: $tag) {
395
+ repls(limit: $count, after: $after) {
396
+ items {
397
+ #{Fields::REPL}
398
+ }
399
+ }
400
+ }
401
+ }
402
+ "
403
+ end
404
+
405
+
406
+ module Mutations
407
+ include Fields
408
+
409
+ CREATE_POST = "
410
+ mutation createPost($input: CreatePostInput!) {
411
+ createPost(input: $input) {
412
+ post {
413
+ #{Fields::POST}
414
+ }
415
+ }
416
+ }
417
+ "
418
+
419
+ EDIT_POST = "
420
+ mutation updatePost($input: UpdatePostInput!) {
421
+ updatePost(input: $input) {
422
+ post {
423
+ #{Fields::POST}
424
+ }
425
+ }
426
+ }
427
+ "
428
+
429
+ DELETE_POST = "
430
+ mutation deletePost($id: Int!) {
431
+ deletePost(id: $id) {
432
+ id
433
+ }
434
+ }
435
+ "
436
+
437
+ CREATE_COMMENT = "
438
+ mutation createComment($input: CreateCommentInput!) {
439
+ createComment(input: $input) {
440
+ comment {
441
+ #{Fields::COMMENT}
442
+ }
443
+ }
444
+ }
445
+ "
446
+
447
+ EDIT_COMMENT = "
448
+ mutation updateComment($input: UpdateCommentInput!) {
449
+ updateComment(input: $input) {
450
+ comment {
451
+ #{Fields::COMMENT}
452
+ }
453
+ }
454
+ }
455
+ "
456
+
457
+ DELETE_COMMENT = "
458
+ mutation deleteComment($id: Int!) {
459
+ deleteComment(id: $id) {
460
+ id
461
+ }
462
+ }
463
+ "
464
+
465
+ CREATE_REPL_COMMENT = "
466
+ mutation ReplViewCreateReplComment($input: CreateReplCommentInput!) {
467
+ createReplComment(input: $input) {
468
+ ... on ReplComment {
469
+ #{Fields::REPL_COMMENT}
470
+ }
471
+ }
472
+ }
473
+ "
474
+
475
+ REPLY_REPL_COMMENT = "
476
+ mutation ReplViewCreateReplCommentReply($input: CreateReplCommentReplyInput!) {
477
+ createReplCommentReply(input: $input) {
478
+ ... on ReplComment {
479
+ #{Fields::REPL_COMMENT}
480
+ }
481
+ }
482
+ }
483
+ "
484
+
485
+ EDIT_REPL_COMMENT = "
486
+ mutation ReplViewCommentsUpdateReplComment($input: UpdateReplCommentInput!) {
487
+ updateReplComment(input: $input) {
488
+ ... on ReplComment {
489
+ #{Fields::REPL_COMMENT}
490
+ }
491
+ }
492
+ }
493
+ "
494
+
495
+ DELETE_REPL_COMMENT = "
496
+ mutation ReplViewCommentsDeleteReplComment($id: Int!) {
497
+ deleteReplComment(id: $id) {
498
+ ... on ReplComment {
499
+ id
500
+ }
501
+ }
502
+ }
503
+ "
504
+
505
+ PUBLISH_REPL = "
506
+ mutation PublishRepl($input: PublishReplInput!) {
507
+ publishRepl(input: $input) {
508
+ ... on Repl {
509
+ #{Fields::REPL}
510
+ }
511
+ }
512
+ }
513
+ "
514
+
515
+ UNPUBLISH_REPL = "
516
+ mutation ReplViewHeaderActionsUnpublishRepl($input: UnpublishReplInput!) {
517
+ unpublishRepl(input: $input) {
518
+ ... on Repl {
519
+ #{Fields::REPL}
520
+ }
521
+ }
522
+ }
523
+ "
524
+
525
+ TOGGLE_REACTION = "
526
+ mutation ReplViewReactionsToggleReactions($input: SetReplReactionInput!) {
527
+ setReplReaction(input: $input) {
528
+ ... on Repl {
529
+ reactions {
530
+ #{Fields::REACTIONS}
531
+ }
532
+ }
533
+ }
534
+ }
535
+ "
536
+
537
+ REPORT_POST = "
538
+ mutation createBoardReport($id: Int!, $reason: String!) {
539
+ createBoardReport(postId: $id, reason: $reason) {
540
+ id
541
+ }
542
+ }
543
+ "
544
+
545
+ REPORT_COMMENT = "
546
+ mutation createBoardReport($id: Int!, $reason: String!) {
547
+ createBoardReport(commentId: $id, reason: $reason) {
548
+ id
549
+ }
550
+ }
551
+ "
552
+ end
553
+ end
554
+ end