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