-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRedditAPI.php
3936 lines (3583 loc) · 148 KB
/
RedditAPI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace CodeWizz\RedditAPI;
use CodeWizz\RedditAPI\Interfaces\RedditApiInterface;
class RedditAPI implements RedditApiInterface
{
/** @var RedditOAuth2 */
private $oauth2;
/** @var RedditRateLimiter */
public $ratelimiter;
private $user_id;
private $user_agent;
private $basic_endpoint;
private $oauth_endpoint;
private $response_format;
private $debug;
public function __construct($username, $password, $appID, $appSecret, $endpointStandard, $endpointOAuth, $responseFormat)
{
$this->oauth2 = new RedditOAuth2($username, $password, $appID, $appSecret, '(CodeWizz 0.1)', $endpointStandard);
$this->ratelimiter = new RedditRateLimiter(true, 1);
$this->user_agent = '(CodeWizz 0.1)';
$this->basic_endpoint = $endpointStandard;
$this->oauth_endpoint = $endpointOAuth;
$this->response_format = $responseFormat;
$this->debug = false;
}
public function setDebug($debug): void
{
$this->debug = $debug;
}
//-----------------------------------------
// Account
//-----------------------------------------
/**
* Gets information about the current user's account.
*
* @return object An object representing the current user.
*/
public function getMe()
{
$response = $this->apiCall('/api/v1/me');
//might need the current user ID later, so may as well record it now
if (isset($response->id)) {
$this->user_id = 't2_' . $response->id;
}
return $response;
}
/**
* Gets karma breakdown of current user.
*
* @return object Listing subreddits and corresponding karma values.
*/
public function getMyKarmaBreakdown()
{
return $this->apiCall('/api/v1/me/karma');
}
/**
* Gets current user's site preferences.
*
* @return object Object representing user's preferences.
*/
public function getMyPrefs()
{
return $this->apiCall('/api/v1/me/prefs');
}
/**
* Update the current user's preferences.
*
* @param array $settings An array of key-value pairs to update. Use getMyPrefs() to see possible values.
*
* @return object Object representing user's new preferences.
*/
public function updateMyPrefs($settings)
{
$prefs = $this->getMyPrefs();
$params = get_object_vars($prefs);
foreach ($settings as $key => $value) {
$params[$key] = $value;
}
return $this->apiCall('/api/v1/me/prefs', 'PATCH', json_encode($params), true);
}
/**
* Gets current user's trophies.
*
* @return object Listing of current user's trophies.
*/
public function getMyTrophies()
{
return $this->apiCall('/api/v1/me/trophies');
}
/**
* Gets a list of the current user's friends.
*
* @return mixed|null Listing of users that are the current user's friends.
*/
public function getMyFriends()
{
return $this->apiCall('/api/v1/me/friends');
}
/**
* Gets a list of the current user's blocked users.
*
* @return object Listing of current user's blocked users.
*/
public function getMyBlockedUsers()
{
return $this->apiCall('/prefs/blocked');
}
//-----------------------------------------
// Flair
//-----------------------------------------
/**
* Retrieves a list of all assigned user flair in the specified subreddit. Must be a mod of that subreddit.
*
* @param string $subreddit Name of subreddit from which to retrieve flair list.
* @param int $limit Upper limit of number of items to retrieve. Maximum is 1000.
* @param string|null $after Use 'next' attribute of previous call to retrieve next page.
* @param string|null $before Retrieve only flairs that are higher than this user ID on the list.
*
* @return object Listing of users that are assigned flair in the specified subreddit.
*/
public function getUserFlairList($subreddit, $limit = 25, $after = null, $before = null)
{
$params = [
'after' => $after,
'before' => $before,
'limit' => $limit,
'show' => 'all',
];
return $this->apiCall("/r/$subreddit/api/flairlist.json", 'GET', $params);
}
/**
* Adds or modifies a flair template in a subreddit.
*
* @param string $subreddit Name of subreddit to add flair template.
* @param string $type Specifies user or link flair template. One of 'link' or 'user'.
* @param string|null $text Flair text.
* @param string|null $css_class Flair CSS class.
* @param bool $editable Whether or not to allow users to edit the flair's text when assigning it.
* @param string|null $template_id The template ID of an existing flair to modify. If null, will add a new one.
*
* @return object Response to API call.
*/
public function addFlairTemplate($subreddit, $type, $text = null, $css_class = null, $editable = false, $template_id = null)
{
$params = [
'api_type' => 'json',
'css_class' => $css_class,
'flair_template_id' => $template_id,
'flair_type' => ($type == 'link') ? 'LINK_FLAIR' : 'USER_FLAIR',
'text' => $text,
'text_editable' => ($editable) ? 'true' : 'false',
];
return $this->apiCall("/r/$subreddit/api/flairtemplate", 'POST', $params);
}
/**
* Deletes all flair templates of the selected type from the selected subreddit.
*
* @param string $subreddit Subreddit of flairs to clear.
* @param string $type One of 'user' or 'link'.
*
* @return object|null Response to API call. Null if incorrect type.
*/
public function clearFlairTemplates($subreddit, $type)
{
if ($type !== 'user' && $type !== 'link') {
return null;
}
$params = [
'api_type' => 'json',
'flair_type' => $type,
];
return $this->apiCall("/r/$subreddit/api/clearflairtemplates", 'POST', $params);
}
/**
* Deletes the selected flair template from the specified subreddit.
* $template_id can be obtained with getUserFlairSelector and getLinkFlairSelector.
*
* @param string $subreddit Subreddit from which to delete flair template.
* @param string $template_id ID of template to delete.
*
* @return object Response to API call.
*/
public function deleteFlairTemplate($subreddit, $template_id)
{
$params = [
'api_type' => 'json',
'flair_template_id' => $template_id,
];
return $this->apiCall("/r/$subreddit/api/deleteflairtemplate", 'POST', $params);
}
/**
* Deletes a user's flair from the specified subreddit.
*
* @param string $subreddit Subreddit in which to delete user flair.
* @param string $user Username of user whose flair to delete.
*
* @return object Response to API call.
*/
public function deleteUserFlair($subreddit, $user)
{
$params = [
'api_type' => 'json',
'name' => $user,
];
return $this->apiCall("/r/$subreddit/api/deleteflair", 'POST', $params);
}
/**
* Gets current flair and a list of possible flairs for the specified user in the specified subreddit.
* Also useful for obtaining flair ID's.
*
* @param string $subreddit Subreddit in which to view flair options.
* @param string|null $user Username for whom to view selection. Defaults to current user.
*
* @return object Response to API call.
*/
public function getUserFlairSelector($subreddit, $user = null)
{
$params = [
'name' => $user,
];
return $this->apiCall("/r/$subreddit/api/flairselector", 'POST', $params);
}
/**
* Gets current flair and a list of possible flairs for the specified link.
*
* @param string $thing_id Thing ID of object to view flairs.
*
* @return object Response to API call.
*/
public function getLinkFlairSelector($thing_id)
{
$params = [
'link' => $thing_id,
];
return $this->apiCall('/api/flairselector', 'POST', $params);
}
/**
* Selects a user flair to use from the flair selection list.
*
* @param string $subreddit Subreddit in which to select flair.
* @param string $user Username of user to whom to apply flair. Mandatory, don't ask me why.
* @param string|null $template_id Template ID of template to select. Null will remove the user's flair.
* @param string|null $text Modified flair text, if allowed.
*
* @return object Response to API call.
*/
public function selectUserFlair($subreddit, $user, $template_id = null, $text = null)
{
$params = [
'api_type' => 'json',
'flair_template_id' => $template_id,
'name' => $user,
'text' => $text,
];
return $this->apiCall("/r/$subreddit/api/selectflair", 'POST', $params);
}
/**
* Applies a link flair template from the selection list to a link.
*
* @param string $thing_id Thing ID of link to apply flair.
* @param string|null $template_id Template ID of template to apply to link. Null will remove the link's flair.
* @param string|null $text Modified flair text, if allowed.
*
* @return object Response to API call.
*/
public function selectLinkFlair($thing_id, $template_id = null, $text = null)
{
$params = [
'api_type' => 'json',
'flair_template_id' => $template_id,
'link' => $thing_id,
'text' => $text,
];
return $this->apiCall('/api/selectflair', 'POST', $params);
}
/**
* Assigns the selected user custom flair text and CSS class in the specified subreddit. Mods only.
*
* @param string $subreddit Subreddit in which to assign flair.
* @param string $user Username of user to assign flair.
* @param string|null $text Custom flair text.
* @param string|null $css_class Custom flair CSS class. If both fields are null, deletes flair.
*
* @return object Response to API call.
*/
public function assignUserFlair($subreddit, $user, $text = null, $css_class = null)
{
$params = [
'api_type' => 'json',
'css_class' => $css_class,
'name' => $user,
'text' => $text,
];
return $this->apiCall("/r/$subreddit/api/flair", 'POST', $params);
}
/**
* Assigns the selected link custom flair text and CSS class in the specified subreddit. Mods only.
*
* @param string $subreddit Subreddit in which to assign flair. Mandatory, don't ask me why.
* @param string $thing_id Thing ID of link to assign flair.
* @param string|null $text Custom flair text.
* @param string|null $css_class Custom flair CSS class. If both fields are null, deletes flair.
*
* @return object Response to API call.
*/
public function assignLinkFlair($subreddit, $thing_id, $text = null, $css_class = null)
{
$params = [
'api_type' => 'json',
'css_class' => $css_class,
'link' => $thing_id,
'text' => $text,
];
return $this->apiCall("/r/$subreddit/api/flair", 'POST', $params);
}
/**
* Selects whether or not to show the current user's flair in the selected subreddit.
*
* @param string $subreddit Subreddit in which to enable or disable flair.
* @param bool $show True to show flair. False to hide flair.
*
* @return object Response to API call.
*/
public function showMyFlair($subreddit, $show = true)
{
$params = [
'api_type' => 'json',
'flair_enabled' => ($show) ? 'true' : 'false',
];
return $this->apiCall("/r/$subreddit/api/setflairenabled", 'POST', $params);
}
/**
* Updates all options in a subreddit's flair configuration.
*
* @param string $subreddit Subreddit in which to configure flair.
* @param bool $user_enabled Whether or not user flair is displayed.
* @param string $user_position On which side to display user flair. One of 'left' or 'right'.
* @param bool $user_self_assign Whether or not users can select their own user flair.
* @param string $link_position On which side to display links' flair. One of 'left', 'right', or 'none'.
* @param bool $link_self_assign Whether or not users can select their own links' flair.
*
* @return object|null Response to API call. Null if invalid arguments.
*/
public function configureSubredditFlair($subreddit, $user_enabled, $user_position, $user_self_assign, $link_position, $link_self_assign)
{
if (!($user_position == 'left' || $user_position == 'right') || !($link_position === null || $link_position == 'none' || $link_position == 'left' || $link_position == 'right')) {
return null;
}
if ($link_position == 'none') {
$link_position = null;
}
$params = [
'api_type' => 'json',
'flair_enabled' => ($user_enabled) ? 'true' : 'false',
'flair_position' => $user_position,
'flair_self_assign_enabled' => ($user_self_assign) ? 'true' : 'false',
'link_flair_position' => $link_position,
'link_flair_self_assign_enabled' => ($link_self_assign) ? 'true' : 'false',
];
return $this->apiCall("/r/$subreddit/api/flairconfig", 'POST', $params);
}
//-----------------------------------------
// reddit gold (DONE, UNTESTED)
//-----------------------------------------
/**
* UNTESTED
* Gild a link or comment, which gives the author reddit gold. Must have sufficient gold creddits.
* Reddit's documentation is odd, indicating that the thing ID is required both in the URL and the POST parameters.
*
* @param string $thing_id Thing ID of link or comment to gild.
*
* @return object Response to API call.
*/
public function gild($thing_id)
{
$params = [
'fullname' => $thing_id,
];
return $this->apiCall("/api/v1/gold/gild/$thing_id", 'POST', $params);
}
/**
* UNTESTED
* Give the specified user the specified months of reddit gold. Must have sufficient gold creddits.
* Reddit's documentation is odd, indicating that the username is required both in the URL and the POST parameters.
*
* @param string $user Username of user to whom to give gold.
* @param int $months Number of months to give reddit gold.
*
* @return object Response to API call.
*/
public function giveGold($user, $months = 1)
{
$params = [
'months' => (string)$months,
'username' => $user,
];
return $this->apiCall("/api/v1/gold/give/$user", 'POST', $params);
}
//-----------------------------------------
// Links & comments
//-----------------------------------------
/**
* Submits a new link post.
*
* @param string $subreddit Subreddit in which to post link.
* @param string $title Title of post.
* @param string $url Link to post.
* @param bool $send_replies Send comment replies to the current user's inbox. True to enable, false to disable.
* @param bool $distinguish Whether or not it should be mod distinguished (for modded subreddits only).
*
* @return object Response to API call.
*/
public function submitLinkPost($subreddit, $title, $url, $send_replies = true, $distinguish = false)
{
$params = [
'api_type' => 'json',
'extension' => 'json',
'kind' => 'link',
'resubmit' => 'true',
'sendreplies' => ($send_replies) ? 'true' : 'false',
'sr' => $subreddit,
'title' => $title,
'url' => $url,
];
$response = $this->apiCall('/api/submit', 'POST', $params);
if ($distinguish && isset($response->json->data->name)) {
$this->distinguish($response->json->data->name, true);
}
return $response;
}
/**
* Submits a new text post.
*
* @param string $subreddit Subreddit in which to post.
* @param string $title Title of post.
* @param string|null $text Text of post.
* @param bool $send_replies Send comment replies to the current user's inbox. True to enable, false to disable.
* @param bool $distinguish Whether or not it should be mod distinguished (for modded subreddits only).
*
* @return object Response to API call.
*/
public function submitTextPost($subreddit, $title, $text = null, $send_replies = true, $distinguish = false)
{
$params = [
'api_type' => 'json',
'extension' => 'json',
'kind' => 'self',
'resubmit' => 'true',
'sendreplies' => ($send_replies) ? 'true' : 'false',
'sr' => $subreddit,
'text' => $text,
'title' => $title,
];
$response = $this->apiCall('/api/submit', 'POST', $params);
if ($distinguish && isset($response->json->data->name)) {
$this->distinguish($response->json->data->name, true);
}
return $response;
}
/**
* Comments on an object.
*
* @param string $parent Thing ID of parent object on which to comment. Could be link, text post, or comment.
* @param string $text Comment text.
* @param bool $distinguish Whether or not it should be mod distinguished (for modded subreddits only).
*
* @return object Response to API call.
*/
public function comment($parent, $text, $distinguish = false)
{
$params = [
'api_type' => 'json',
'text' => $text,
'thing_id' => $parent,
];
$response = $this->apiCall('/api/comment', 'POST', $params);
if ($distinguish && isset($response->json->data->things[0]->data->name)) {
$dist_response = $this->distinguish($response->json->data->things[0]->data->name, true);
if (isset($dist_response->json->errors) && count($dist_response->json->errors) == 0) {
$response = $dist_response;
}
}
return $response;
}
/**
* Deletes a post or comment.
*
* @param string $thing_id Thing ID of object to delete. Could be link, text post, or comment.
*
* @return object Response to API call, probably empty.
*/
public function delete($thing_id)
{
$params = [
'id' => $thing_id,
];
return $this->apiCall('/api/del', 'POST', $params);
}
/**
* Edits the text of a comment or text post.
*
* @param string $thing_id Thing ID of text object to edit. Could be text post or comment.
* @param string $text New text to replace the old.
*
* @return object Response to API call, probably object of thing that was just edited.
*/
public function editText($thing_id, $text)
{
$params = [
'api_type' => 'json',
'text' => $text,
'thing_id' => $thing_id,
];
return $this->apiCall('/api/editusertext', 'POST', $params);
}
/**
* Hides a post from user's listings.
*
* @param string|array $thing_ids String or array of thing ID's of links to hide.
*
* @return bool|null Response to API call.
*/
public function hide($thing_ids): ?bool
{
if (is_array($thing_ids)) {
$thing_ids = implode(',', $thing_ids);
}
$params = [
'id' => $thing_ids,
];
return $this->apiCall('/api/hide', 'POST', $params);
}
/**
* Unhides a post from user's hidden posts.
*
* @param string|array $thing_ids String or array of thing ID's of links to unhide.
*
* @return bool|null Returns true if success. Null if failed.
*/
public function unhide($thing_ids): ?bool
{
if (is_array($thing_ids)) {
$thing_ids = implode(',', $thing_ids);
}
$params = [
'id' => $thing_ids,
];
return $this->apiCall('/api/unhide', 'POST', $params);
}
/**
* Gives a listing of information on objects.
*
* @param string|array $thing_ids String or array of single or multiple thing ID's.
*
* @return object Listing objects requested.
*/
public function getInfo($thing_ids)
{
if (is_array($thing_ids)) {
$thing_ids = implode(',', $thing_ids);
}
$params = [
'id' => $thing_ids,
];
return $this->apiCall('/api/info', 'GET', $params);
}
/**
* Marks a post as NSFW.
*
* @param string $thing_id Thing ID of post to mark as NSFW.
*
* @return object Response to API call, probably empty.
*/
public function markNSFW($thing_id)
{
$params = [
'id' => $thing_id,
];
return $this->apiCall('/api/marknsfw', 'POST', $params);
}
/**
* Unmarks a post as NSFW.
*
* @param string $thing_id Thing ID of post to unmark as NSFW.
*
* @return object Response to API call, probably empty.
*/
public function unmarkNSFW($thing_id)
{
$params = [
'id' => $thing_id,
];
return $this->apiCall('/api/unmarknsfw', 'POST', $params);
}
/**
* Get comments in a tree that are hidden by "load more comments".
* NOTE: Only make one request for this at a time. Higher concurrency will result in an error.
*
* @param string $link_id Fullname (thing ID) of link/post of the comment tree.
* @param string|array $comment_ids ID36 or fullname of one or more parent comments for which to retrieve children.
*
* @return object Complex object containing comment's children.
*/
public function getCommentChildren($link_id, $comment_ids)
{
if (!is_array($comment_ids)) {
$comment_ids = explode(',', $comment_ids);
}
for ($i = 0, $iMax = count($comment_ids); $i < $iMax; $i++) {
if (strpos($comment_ids[$i], 't1_') === 0) {
$comment_ids[$i] = substr($comment_ids[$i], 3);
}
}
$comment_ids = implode(',', $comment_ids);
$params = [
'api_type' => 'json',
'children' => $comment_ids,
'link_id' => $link_id,
];
return $this->apiCall('/api/morechildren', 'GET', $params);
}
/**
* Reports a post, comment, or message.
*
* @param string $thing_id Thing ID of object to report.
* @param null $reason The reason for the report. Must be <100 characters.
*
* @return object Response to API call.
*/
public function report($thing_id, $reason = null)
{
$params = [
'api_type' => 'json',
'reason' => $reason,
'thing_id' => $thing_id,
];
return $this->apiCall('/api/report', 'POST', $params);
}
/**
* Saves a post or comment in the selected category.
*
* @param string $thing_id Thing ID of object to save. Can be post or comment.
* @param null $category Category in which to save object. Defaults to none.
*
* @return object Response to API call, probably empty.
*/
public function save($thing_id, $category = null)
{
$params = [
'category' => $category,
'id' => $thing_id,
];
return $this->apiCall('/api/save', 'POST', $params);
}
/**
* Unsaves a post or comment from the current user's saved posts.
*
* @param string $thing_id Thing ID of object to unsave. Can be post or comment.
*
* @return object Response to API call, probably empty.
*/
public function unsave($thing_id)
{
$params = [
'id' => $thing_id,
];
return $this->apiCall('/api/unsave', 'POST', $params);
}
/**
* Gets the current user's save categories.
*
* @return object Contains an array of categories.
*/
public function getSavedCategories()
{
return $this->apiCall('/api/saved_categories', 'GET');
}
/**
* Toggles whether or not the current user should receive replies to a specific post or comment to their inbox.
*
* @param string $thing_id Thing ID of object to toggle.
* @param bool $state State of inbox replies. True to receive, false for not.
*
* @return object Response to API call, probably empty.
*/
public function sendInboxReplies($thing_id, $state = true)
{
$params = [
'id' => $thing_id,
'state' => ($state) ? 'true' : 'false',
];
return $this->apiCall('/api/sendreplies', 'POST', $params);
}
/**
* Store that the current user has visited a certain link.
*
* @param string|array $thing_ids String or array of thing ID's of links to store as visited.
*
* @return object Response to API call, probably empty.
*/
public function storeVisits($thing_ids)
{
if (is_array($thing_ids)) {
$thing_ids = implode(',', $thing_ids);
}
$params = [
'links' => $thing_ids,
];
return $this->apiCall('/api/store_visits', 'POST', $params);
}
/**
* VOTES MUST BE CAST BY A HUMAN!!
* Proxying a person's single vote is okay, but bots should not use vote functions on their own.
*
* Upvotes a post or comment.
*
* @param string $thing_id Thing ID of object to upvote.
*
* @return object Response to API call, probably empty.
*/
public function upvote($thing_id)
{
$params = [
'dir' => '1',
'id' => $thing_id,
];
return $this->apiCall('/api/vote', 'POST', $params);
}
/**
* Downvotes a post or comment.
*
* @param string $thing_id Thing ID of object to downvote.
*
* @return object Response to API call, probably empty.
*/
public function downvote($thing_id)
{
$params = [
'dir' => '-1',
'id' => $thing_id,
];
return $this->apiCall('/api/vote', 'POST', $params);
}
/**
* Resets the current user's vote on a post or comment.
*
* @param string $thing_id Thing ID of object to reset vote.
*
* @return object Response to API call, probably empty.
*/
public function unvote($thing_id)
{
$params = [
'dir' => '0',
'id' => $thing_id,
];
return $this->apiCall('/api/vote', 'POST', $params);
}
//-----------------------------------------
// Listings
//-----------------------------------------
/**
* Private function to unify process of retrieving several subreddit listings.
*
* @param string $listing Listing type. Can be hot, new, controversial, top, gilded, ads.
* @param string $subreddit
* @param string $limit
* @param string $after
* @param string $before
* @param string|null $time
*
* @return mixed|null
*/
private function getSubredditListing($listing, $subreddit, $limit, $after, $before, $time = null)
{
$params = [
't' => $time,
'after' => $after,
'before' => $before,
'limit' => $limit,
'show' => 'all',
];
$api_sr = ($subreddit) ? "/r/$subreddit" : '';
$response = $this->apiCall("$api_sr/$listing.json", 'GET', $params);
if (isset($response->error)) {
return null;
}
return $response;
}
/**
* Retrieves a listing of links by their specified thing ID.
*
* @param string $thing_ids Thing ID's of links to retrieve.
*
* @return object A listing of links.
*/
public function getLinksById($thing_ids)
{
if (is_array($thing_ids)) {
$thing_ids = implode(',', $thing_ids);
}
return $this->apiCall("/by_id/$thing_ids");
}
/**
* Retrieves a listing of comments and children for a link and optionally a specific comment.
*
* @param string $link_id ID36 or fullname of link for comments to fetch.
* @param string|null $comment_id Optional, ID36 or fullname of a single comment to fetch with children, much like permalink.
* @param int|null $context Number of levels up of parent comments to retrieve. Only applicable to child comments.
* @param int|null $depth Depth of child comments to retrieve.
* @param int|null $limit Limit of comments to retrieve.
* @param string|null $sort How to sort the comments, one of 'confidence', 'top', 'new', 'hot', 'controversial', 'old', 'random', 'qa'
* @param bool $show_edits Show edited comments, perhaps? Not well documented by reddit.
* @param bool $show_more Include links to show more comments, maybe? Not well documented by reddit.
*
* @return object Listing of link and specified comment(s).
*/
public function getComments($link_id, $comment_id = null, $context = null, $depth = null, $limit = null, $sort = null, $show_edits = false, $show_more = false)
{
if (strpos($link_id, 't3_') === 0) {
$link_id = substr($link_id, 3);
}
if (strpos($comment_id, 't1_') === 0) {
$comment_id = substr($comment_id, 3);
}
$params = [
'article' => $link_id,
'comment' => $comment_id,
'context' => (string)$context,
'depth' => (string)$depth,
'limit' => (string)$limit,
'showedits' => ($show_edits) ? 'true' : 'false',
'showmore' => ($show_more) ? 'true' : 'false',
'sort' => $sort,
];
return $this->apiCall("/comments/$link_id", 'GET', $params);
}
/**
* Retrieves the specified link and a listing of other links that are to duplicate destinations.
*
* @param string $thing_id ID36 or fullname of link to check for duplicates.
* @param int $limit Limit of duplicate links to retrieve.
* @param string|null $after Get items lower on list than this entry. Does not mean chronologically.
* @param string|null $before Get items higher on list than this entry. Does not mean chronologically.
*
* @return object Listing of original link and listing of duplicate links.
*/
public function getDuplicateLinks($thing_id, $limit = 25, $after = null, $before = null)
{
if (strpos($thing_id, 't3_') === 0) {
$thing_id = substr($thing_id, 3);
}
$params = [
'after' => $after,
'article' => $thing_id,
'before' => $before,
'limit' => $limit,
'show' => 'all',
];
return $this->apiCall("/duplicates/$thing_id", 'GET', $params);
}
/**
* Retrieves the hot listing for the optionally specified subreddit.
*
* @param string|null $subreddit Subreddit of listing to retrieve. If none, defaults to front page.
* @param int $limit Upper limit of number of items to retrieve. Maximum is 100.
* @param string|null $after Get items lower on list than this entry. Does not mean chronologically.
* @param string|null $before Get items higher on list than this entry. Does not mean chronologically.
*
* @return mixed|null Returns listing object on success. Null if failed.
*/
public function getHot($subreddit = null, $limit = 25, $after = null, $before = null)
{
return $this->getSubredditListing('hot', $subreddit, $limit, $after, $before);
}
/**
* Retrieves the new listing for the optionally specified subreddit.
*
* @param string|null $subreddit Subreddit of listing to retrieve. If none, defaults to front page.
* @param int $limit Upper limit of number of items to retrieve. Maximum is 100.
* @param string|null $after Get items lower on list than this entry. Does not mean chronologically.
* @param string|null $before Get items higher on list than this entry. Does not mean chronologically.
*
* @return mixed|null Returns listing object on success. Null if failed.
*/
public function getNew($subreddit = null, $limit = 25, $after = null, $before = null)
{
return $this->getSubredditListing('new', $subreddit, $limit, $after, $before);
}
/**
* Retrieves the controversial listing for the optionally specified subreddit.
*
* @param string|null $subreddit Subreddit of listing to retrieve. If none, defaults to front page.
* @param string $time Time constraint for age of items on list. One of hour, day, week, month, year, all.
* @param int $limit Upper limit of number of items to retrieve. Maximum is 100.
* @param string|null $after Get items lower on list than this entry. Does not mean chronologically.
* @param string|null $before Get items higher on list than this entry. Does not mean chronologically.
*
* @return mixed|null Returns listing object on success. Null if failed.