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,583 @@
1
+ require "http"
2
+ require "json"
3
+
4
+ module ReplTalk
5
+ class Role
6
+ attr_reader :name, :key, :tagline
7
+
8
+ def initialize(role)
9
+ @name = role["name"]
10
+ @key = role["key"]
11
+ @tagline = role["tagline"]
12
+ end
13
+
14
+ def to_s
15
+ @name
16
+ end
17
+ end
18
+
19
+
20
+
21
+ class Organization
22
+ attr_reader :id, :name, :country, :postal_code, :state, :city, :timestamp
23
+
24
+ def initialize(organization)
25
+ @id = organization["id"]
26
+ @name = organization["name"]
27
+ @country = organization["country"]
28
+ @postal_code = organization["postalCode"]
29
+ @state = organization["state"]
30
+ @city = organization["city"]
31
+ @timestamp = organization["timeCreated"]
32
+ end
33
+
34
+ def to_s
35
+ @name
36
+ end
37
+ end
38
+
39
+
40
+
41
+ class Language
42
+ attr_reader :id, :key, :name, :tagline, :icon, :category
43
+
44
+ def initialize(lang)
45
+ @id = lang["id"]
46
+ @key = lang["key"]
47
+ @name = lang["displayName"]
48
+ @tagline = lang["tagline"]
49
+ @icon = lang["icon"]
50
+ @category = lang["category"]
51
+ end
52
+
53
+ def to_s
54
+ @id
55
+ end
56
+ end
57
+
58
+
59
+
60
+ class Board
61
+ attr_reader :id, :name, :color, :description
62
+
63
+ def initialize(board)
64
+ @id = board["id"]
65
+ @name = board["name"]
66
+ @color = board["color"]
67
+ @description = board["description"]
68
+ end
69
+
70
+ def to_s
71
+ @name
72
+ end
73
+ end
74
+
75
+
76
+
77
+ class Tag
78
+ attr_reader :id, :repl_count, :creator_count, :is_trending, :repls_tagged_today_count
79
+
80
+ def initialize(client, tag)
81
+ @client = client
82
+
83
+ @id = tag["id"]
84
+ @repl_count = tag["replCount"]
85
+ @is_trending = tag["isTrending"]
86
+ @creator_count = tag["creatorCount"]
87
+ @repls_tagged_today_count = tag["replsTaggedTodayCount"]
88
+ end
89
+
90
+ def get_repls(count: nil, after: nil)
91
+ r = @client.graphql(
92
+ "ExploreTrendingRepls",
93
+ GQL::Queries::GET_TAGS_REPLS,
94
+ tag: @id,
95
+ count: count,
96
+ after: after
97
+ )
98
+ r["tag"]["repls"]["items"].map { |repl| Repl.new(@client, repl) }
99
+ end
100
+
101
+ def to_s
102
+ @id
103
+ end
104
+ end
105
+
106
+
107
+
108
+
109
+ class Reaction
110
+ attr_reader :id, :type, :count
111
+
112
+ def initialize(reaction)
113
+ @id = reaction["id"]
114
+ @type = reaction["type"]
115
+ @count = reaction["count"]
116
+ end
117
+
118
+ def to_s
119
+ @type
120
+ end
121
+ end
122
+
123
+
124
+
125
+
126
+ class ReplComment
127
+ attr_reader :id, :content, :author, :repl, :replies
128
+
129
+ def initialize(client, comment)
130
+ @client = client
131
+
132
+ @id = comment["id"]
133
+ @content = comment["body"]
134
+ @author = comment["user"] == nil ? nil : User.new(@client, comment["user"])
135
+ @repl = comment["repl"] == nil ? nil : Repl.new(@client, comment["repl"])
136
+ @replies = comment["replies"] == nil ? nil : comment["replies"].map { |c| ReplComment.new(@client, c) }
137
+ end
138
+
139
+ def create_comment(content)
140
+ c = @client.graphql(
141
+ "ReplViewCreateReplCommentReply",
142
+ GQL::Mutations::REPLY_REPL_COMMENT,
143
+ input: {
144
+ replCommentId: @id,
145
+ body: content
146
+ }
147
+ )
148
+ ReplComment.new(@client, c["createReplCommentReply"])
149
+ end
150
+
151
+ def edit(content)
152
+ c = @client.graphql(
153
+ "ReplViewCommentsUpdateReplComment",
154
+ GQL::Mutations::EDIT_REPL_COMMENT,
155
+ input: {
156
+ id: @id,
157
+ body: content
158
+ }
159
+ )
160
+ ReplComment.new(@client, c["updateReplComment"])
161
+ end
162
+
163
+ def delete
164
+ @client.graphql(
165
+ "ReplViewCommentsDeleteReplComment",
166
+ GQL::Mutations::DELETE_REPL_COMMENT,
167
+ id: @id
168
+ )
169
+ nil
170
+ end
171
+
172
+ def to_s
173
+ @content
174
+ end
175
+ end
176
+
177
+
178
+
179
+ class Repl
180
+ attr_reader :id, :url, :title, :author, :description, :timestamp, :size, :run_count, :fork_count, :language, :img_url, :origin_url, :is_private, :is_always_on, :tags, :reactions
181
+
182
+ def initialize(client, repl)
183
+ @client = client
184
+
185
+ @id = repl["id"]
186
+ @url = $BASE_URL + repl["url"]
187
+ @title = repl["title"]
188
+ @author = User.new(@client, repl["user"])
189
+ @description = repl["description"]
190
+ @timestamp = repl["timeCreated"]
191
+ @size = repl["size"]
192
+ @run_count = repl["runCount"]
193
+ @fork_count = repl["publicForkCount"]
194
+ @language = Language.new(repl["lang"])
195
+ @image_url = repl["imageUrl"]
196
+ @origin_url = repl["origin"] == nil ? nil : $BASE_URL + repl["origin"]["url"]
197
+
198
+ @is_private = repl["isPrivate"]
199
+ @is_always_on = repl["isAlwaysOn"]
200
+
201
+ @tags = repl["tags"].map { |tag| Tag.new(@client, tag) }
202
+ @reactions = repl["reactions"].map { |reaction| Reaction.new(reaction) }
203
+ end
204
+
205
+ def get_forks(count: 100, after: nil)
206
+ f = @client.graphql(
207
+ "ReplViewForks",
208
+ GQL::Queries::GET_REPL_FORKS,
209
+ url: @url,
210
+ count: count,
211
+ after: after
212
+ )
213
+ f["repl"]["publicForks"]["items"].map { |repl| Repl.new(@client, repl) }
214
+ end
215
+
216
+ def get_comments(count: nil, after: nil)
217
+ c = @client.graphql(
218
+ "ReplViewComments",
219
+ GQL::Queries::GET_REPL_COMMENTS,
220
+ url: @url,
221
+ count: count,
222
+ after: after
223
+ )
224
+ c["repl"]["comments"]["items"].map { |comment| ReplComment.new(@client, comment) }
225
+ end
226
+
227
+ def create_comment(content)
228
+ c = @client.graphql(
229
+ "ReplViewCreateReplComment",
230
+ GQL::Mutations::CREATE_REPL_COMMENT,
231
+ input: {
232
+ replId: @id,
233
+ body: content
234
+ }
235
+ )
236
+ ReplComment.new(@client, c["createReplComment"])
237
+ end
238
+
239
+ def add_reaction(type)
240
+ r = @client.graphql(
241
+ "ReplViewReactionsToggleReactions",
242
+ GQL::Mutations::TOGGLE_REACTION,
243
+ input: {
244
+ replId: @id,
245
+ react: true,
246
+ reactionType: type
247
+ }
248
+ )
249
+ if r["setReplReaction"]["reactions"] == nil
250
+ @reactions
251
+ else
252
+ @reactions = r["setReplReaction"]["reactions"].map { |reaction| Reaction.new(reaction) }
253
+ end
254
+ end
255
+
256
+ def remove_reaction(type)
257
+ r = @client.graphql(
258
+ "ReplViewReactionsToggleReactions",
259
+ GQL::Mutations::TOGGLE_REACTION,
260
+ input: {
261
+ replId: @id,
262
+ react: false,
263
+ reactionType: type
264
+ }
265
+ )
266
+ @reactions = r["setReplReaction"]["reactions"].map { |reaction| Reaction.new(reaction) }
267
+ end
268
+
269
+ def publish(description, image_url, tags, enable_comments: true)
270
+ r = @client.graphql(
271
+ "PublishRepl",
272
+ GQL::Mutations::PUBLISH_REPL,
273
+ input: {
274
+ replId: @id,
275
+ replTitle: @title,
276
+ description: description,
277
+ imageUrl: image_url,
278
+ tags: tags,
279
+ enableComments: enable_comments,
280
+ }
281
+ )
282
+ Repl.new(@client, r["publishRepl"])
283
+ end
284
+
285
+ def unpublish
286
+ r = @client.graphql(
287
+ "ReplViewHeaderActionsUnpublishRepl",
288
+ GQL::Mutations::UNPUBLISH_REPL,
289
+ input: {
290
+ replId: @id
291
+ }
292
+ )
293
+ Repl.new(@client, r["unpublishRepl"])
294
+ end
295
+
296
+ def to_s
297
+ @title
298
+ end
299
+ end
300
+
301
+
302
+
303
+ class Comment
304
+ attr_reader :id, :url, :author, :content, :post_id, :is_answer, :vote_count, :timestamp, :can_vote, :has_voted
305
+
306
+ def initialize(client, comment)
307
+ @client = client
308
+
309
+ @id = comment["id"]
310
+ @url = $BASE_URL + comment["url"]
311
+ @author = comment["user"] == nil ? "[deleted user]" : User.new(@client, comment["user"])
312
+ @content = comment["body"]
313
+ @post_id = comment["post"]["id"]
314
+ @is_answer = comment["isAnswer"]
315
+ @vote_count = comment["voteCount"]
316
+ @timestamp = comment["timeCreated"]
317
+
318
+ @can_vote = comment["canVote"]
319
+ @has_voted = comment["hasVoted"]
320
+ end
321
+
322
+ def get_post
323
+ p = @client.graphql(
324
+ "post",
325
+ GQL::Queries::GET_POST,
326
+ id: @post_id
327
+ )
328
+ Post.new(@client, p["post"])
329
+ end
330
+
331
+ def get_comments
332
+ c = @client.graphql(
333
+ "comment",
334
+ GQL::Queries::GET_COMMENTS_COMMENTS,
335
+ id: @id
336
+ )
337
+ c["comment"]["comments"].map { |comment| Comment.new(@client, comment) }
338
+ end
339
+
340
+ def get_parent
341
+ c = @client.graphql(
342
+ "comment",
343
+ GQL::Queries::GET_PARENT_COMMENT,
344
+ id: @id
345
+ )
346
+ c["comment"]["parentComment"] == nil ? nil : Comment.new(@client, c["comment"]["parentComment"])
347
+ end
348
+
349
+ def create_comment(content)
350
+ c = @client.graphql(
351
+ "createComment",
352
+ GQL::Mutations::CREATE_COMMENT,
353
+ input: {
354
+ postId: @post_id,
355
+ commentId: @id,
356
+ body: content
357
+ }
358
+ )
359
+ Comment.new(@client, c["createComment"]["comment"])
360
+ end
361
+
362
+ def edit(content)
363
+ c = @client.graphql(
364
+ "updateComment",
365
+ GQL::Mutations::EDIT_COMMENT,
366
+ input: {
367
+ id: @id,
368
+ body: content
369
+ }
370
+ )
371
+ Comment.new(@client, c["updateComment"]["comment"])
372
+ end
373
+
374
+ def delete
375
+ @client.graphql(
376
+ "deleteComment",
377
+ GQL::Mutations::DELETE_COMMENT,
378
+ id: @id
379
+ )
380
+ nil
381
+ end
382
+
383
+ def report(reason)
384
+ @client.graphql(
385
+ "createBoardReport",
386
+ GQL::Mutations::REPORT_COMMENT,
387
+ id: @id,
388
+ reason: reason
389
+ )
390
+ nil
391
+ end
392
+
393
+ def to_s
394
+ @content
395
+ end
396
+ end
397
+
398
+
399
+
400
+ class Post
401
+ attr_reader :id, :url, :repl, :board, :title, :author, :answer, :content, :preview, :timestamp, :vote_count, :comment_count, :can_vote, :has_voted, :is_answered, :is_answerable, :is_hidden, :is_pinned, :is_locked, :is_announcement
402
+
403
+ def initialize(client, post)
404
+ @client = client
405
+
406
+ @id = post["id"]
407
+ @url = $BASE_URL + post["url"]
408
+ @title = post["title"]
409
+ @content = post["body"]
410
+ @preview = post["preview"]
411
+ @timestamp = post["timeCreated"]
412
+
413
+ @board = Board.new(post["board"])
414
+ @repl = post["repl"] == nil ? nil : Repl.new(@client, post["repl"])
415
+ @author = post["user"] == nil ? nil : User.new(@client, post["user"])
416
+ @answer = post["answer"] == nil ? nil : Comment.new(@client, post["answer"])
417
+
418
+ @vote_count = post["voteCount"]
419
+ @comment_count = post["commentCount"]
420
+
421
+ @can_vote = post["canVote"]
422
+ @has_voted = post["hasVoted"]
423
+
424
+ @is_answered = post["isAnswered"]
425
+ @is_answerable = post["isAnswerable"]
426
+
427
+ @is_hidden = post["isHidden"]
428
+ @is_pinned = post["isPinned"]
429
+ @is_locked = post["isLocked"]
430
+ @is_announcement = post["isAnnouncement"]
431
+ end
432
+
433
+ def get_comments(order: "new", count: nil, after: nil)
434
+ c = @client.graphql(
435
+ "post",
436
+ GQL::Queries::GET_POSTS_COMMENTS,
437
+ postId: @id,
438
+ order: order,
439
+ count: count,
440
+ after: after
441
+ )
442
+ c["post"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
443
+ end
444
+
445
+ def get_upvotes(count: nil)
446
+ u = @client.graphql(
447
+ "post",
448
+ GQL::Queries::GET_POSTS_UPVOTERS,
449
+ id: @id,
450
+ count: count
451
+ )
452
+ u["post"]["votes"]["items"].map { |vote| User.new(@client, vote["user"]) }
453
+ end
454
+
455
+ def create_comment(content)
456
+ c = @client.graphql(
457
+ "createComment",
458
+ GQL::Mutations::CREATE_COMMENT,
459
+ input: {
460
+ postId: @id,
461
+ body: content
462
+ }
463
+ )
464
+ Comment.new(@client, c["createComment"]["comment"])
465
+ end
466
+
467
+ def edit(title: @title, content: @content, repl_id: @repl.id, show_hosted: false)
468
+ p = @client.graphql(
469
+ "updatePost",
470
+ GQL::Mutations::EDIT_POST,
471
+ input: {
472
+ id: @id,
473
+ title: title,
474
+ body: content,
475
+ replId: repl_id,
476
+ showHosted: show_hosted
477
+ }
478
+ )
479
+ Post.new(@client, p["updatePost"]["post"])
480
+ end
481
+
482
+ def delete
483
+ @client.graphql(
484
+ "deletePost",
485
+ GQL::Mutations::DELETE_POST,
486
+ id: @id
487
+ )
488
+ nil
489
+ end
490
+
491
+ def report(reason)
492
+ @client.graphql(
493
+ "createBoardReport",
494
+ GQL::Mutations::REPORT_POST,
495
+ id: @id,
496
+ reason: reason
497
+ )
498
+ nil
499
+ end
500
+
501
+ def to_s
502
+ @title
503
+ end
504
+ end
505
+
506
+
507
+
508
+ class User
509
+ attr_reader :id, :username, :name, :pfp, :bio, :cycles, :is_hacker, :timestamp, :subscription, :roles, :organization, :languages
510
+
511
+ def initialize(client, user)
512
+ return nil if user == nil
513
+ @client = client
514
+
515
+ @id = user["id"]
516
+ @username = user["username"]
517
+ @name = user["fullName"]
518
+ @pfp = user["image"]
519
+ @bio = user["bio"]
520
+ @cycles = user["karma"]
521
+ @is_hacker = user["isHacker"]
522
+ @timestamp = user["timeCreated"]
523
+ @roles = user["roles"].map { |role| Role.new(role) }
524
+ @organization = user["organization"] == nil ? nil : Organization.new(user["organization"])
525
+ @languages = user["languages"].map { |lang| Language.new(lang) }
526
+ end
527
+
528
+ def get_posts(order: "new", count: nil, after: nil)
529
+ p = @client.graphql(
530
+ "user",
531
+ GQL::Queries::GET_USER_POSTS,
532
+ username: @username,
533
+ order: order,
534
+ count: count,
535
+ after: after
536
+ )
537
+ p["user"]["posts"]["items"].map { |post| Post.new(@client, post) }
538
+ end
539
+
540
+ def get_comments(order: "new", count: nil, after: nil)
541
+ c = @client.graphql(
542
+ "user",
543
+ GQL::Queries::GET_USER_COMMENTS,
544
+ username: @username,
545
+ order: order,
546
+ count: count,
547
+ after: after
548
+ )
549
+ c["user"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
550
+ end
551
+
552
+ def get_repls(count: nil, order: nil, direction: nil, before: nil, after: nil, pinnedReplsFirst: nil, showUnnamed: nil)
553
+ r = @client.graphql(
554
+ "user",
555
+ GQL::Queries::GET_USER_REPLS,
556
+ username: @username,
557
+ order: order,
558
+ count: count,
559
+ direction: direction,
560
+ before: before,
561
+ after: after,
562
+ pinnedReplsFirst: pinnedReplsFirst,
563
+ showUnnamed: showUnnamed
564
+ )
565
+ r["user"]["publicRepls"]["items"].map { |repl| Repl.new(@client, repl) }
566
+ end
567
+
568
+ def to_s
569
+ @username
570
+ end
571
+ end
572
+
573
+
574
+
575
+ class LeaderboardUser < User
576
+ attr_reader :cycles_since
577
+
578
+ def initialize(client, user)
579
+ super(client, user)
580
+ @cycles_since = user["karmaSince"]
581
+ end
582
+ end
583
+ end