repltalk 2.0.5 → 4.0.0

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