-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRedditLive.php
425 lines (387 loc) · 14.5 KB
/
RedditLive.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
<?php
namespace CodeWizz\RedditAPI;
class RedditLive
{
/** @var RedditAPI */
private $redditAPI;
private $thread_id;
public function __construct($redditAPI, $thread_id)
{
$this->redditAPI = $redditAPI;
$this->thread_id = $thread_id;
}
/**
* Returns the thread ID of the current thread. Useful for newly created threads.
*
* @return string Thread ID.
*/
public function getThreadId(): string
{
return $this->thread_id;
}
/**
* Accepts a pending invitation to contribute to the live thread.
*
* @return object Response to API call.
*/
public function acceptContributorInvite()
{
$params = [
'api_type' => 'json',
];
return $this->apiCall('/accept_contributor_invite', 'POST', $params);
}
/**
* Permanently closes the live thread.
*
* @return object Response to API call.
*/
public function close()
{
$params = [
'api_type' => 'json',
];
return $this->apiCall('/close_thread', 'POST', $params);
}
/**
* Deletes the specified update.
*
* @param string $update_id ID (rather, name attribute) of update to delete.
*
* @return object Response to API call.
*/
public function deleteUpdate($update_id)
{
$params = [
'api_type' => 'json',
'id' => $update_id,
];
return $this->apiCall('/delete_update', 'POST', $params);
}
/**
* Edit the settings for the live thread.
*
* @param string|null $title The thread's title.
* @param string|null $description The thread's description.
* @param string|null $resources The thread's resources section in the sidebar.
* @param bool|null $nsfw Whether or not the thread is NSFW. Prompts guests to continue when visiting.
*
* @return mixed|null Response to API call OR null if not all settings defined and getting current settings failed.
*/
public function editSettings($title = null, $description = null, $resources = null, $nsfw = null)
{
$current_settings = null;
if ($title === null || $description === null || $resources === null || $nsfw === null) {
$current_settings = $this->about();
if (!isset($current_settings->data)) {
return null;
}
}
$params = [
'api_type' => 'json',
'description' => ($description === null) ? $current_settings->data->description : $description,
'nsfw' => ($nsfw === null) ? $current_settings->data->nsfw : (($nsfw) ? 'true' : 'false'),
'resources' => ($resources === null) ? $current_settings->data->resources : $resources,
'title' => ($title === null) ? $current_settings->data->title : $title,
];
return $this->apiCall('/edit', 'POST', $params);
}
/**
* Invite a user as a contributor to the live thread.
*
* @param string $user Username of user to invite.
* @param bool $perm_all If the user should have full permissions.
* @param bool $perm_close If the user should have the 'close live thread' permission. User must have 'settings' too to close via the web UI.
* @param bool $perm_edit If the user should have the 'edit' permission.
* @param bool $perm_manage If the user should have the 'manage contributors' permission.
* @param bool $perm_settings If the user should have the 'settings' permission.
* @param bool $perm_update If the user should have the 'update' permission.
*
* @return object Response to API call.
*/
public function inviteContributor($user, $perm_all = true, $perm_close = false, $perm_edit = false, $perm_manage = false, $perm_settings = false, $perm_update = false)
{
$permissions = [];
if ($perm_all) {
$permissions[] = '+all';
} else {
if ($perm_close) {
$permissions[] = '+close';
}
if ($perm_edit) {
$permissions[] = '+edit';
}
if ($perm_manage) {
$permissions[] = '+manage';
}
if ($perm_settings) {
$permissions[] = '+settings';
}
if ($perm_update) {
$permissions[] = '+update';
}
}
if (count($permissions) == 0) {
$permissions = ['-all', '-close', '-edit', '-manage', '-settings', '-update'];
}
$params = [
'api_type' => 'json',
'name' => $user,
'permissions' => implode(',', $permissions),
'type' => 'liveupdate_contributor_invite',
];
return $this->apicall('/invite_contributor', 'POST', $params);
}
/**
* Abdicate contributorship of the thread.
*
* @return object Response to API call.
*/
public function leaveContributor()
{
$params = [
'api_type' => 'json',
];
return $this->apiCall('/leave_contributor', 'POST', $params);
}
/**
* Report the thread to the reddit admins for breaking one of the site's rules.
*
* @param string $type One of 'spam', 'vote-manipulation', 'personal-information', 'sexualizing-minors', 'site-breaking'.
*
* @return object Response to API call.
*/
public function report($type)
{
$params = [
'api_type' => 'json',
'type' => $type,
];
return $this->apiCall('/report', 'POST', $params);
}
/**
* Remove a user as a contributor from the thread. To revoke a pending invitation, use uninviteContributor().
*
* @param string $user Username of user to remove from thread's contributor list.
*
* @return object|null Response to API call OR null if the user does not exist.
*/
public function removeContributor($user)
{
$user_object = $this->redditAPI->getUser($user);
if (!isset($user_object->data)) {
return null;
}
$user_id = $user_object->kind . '_' . $user_object->data->id;
$params = [
'api_type' => 'json',
'id' => $user_id,
];
return $this->apiCall('/rm_contributor', 'POST', $params);
}
/**
* Revoke a pending contributor invitation. To remove a current contributor, use removeContributor().
*
* @param string $user Username of user to uninvite.
*
* @return object|null Response to API call OR null if the user does not exit.
*/
public function uninviteContributor($user)
{
$user_object = $this->redditAPI->getUser($user);
if (!isset($user_object->data)) {
return null;
}
$user_id = $user_object->kind . '_' . $user_object->data->id;
$params = [
'api_type' => 'json',
'id' => $user_id,
];
return $this->apiCall('/rm_contributor_invite', 'POST', $params);
}
/**
* Modify a current contributor's permission set. To modify an invited contributor's permissions, use setInvitationPermissions().
*
* @param string $user Username of user for which to edit permissions.
* @param bool $perm_all If the user should have full permissions.
* @param bool $perm_close If the user should have the 'close live thread' permission. User must have 'settings' too to close via the web UI.
* @param bool $perm_edit If the user should have the 'edit' permission.
* @param bool $perm_manage If the user should have the 'manage contributors' permission.
* @param bool $perm_settings If the user should have the 'settings' permission.
* @param bool $perm_update If the user should have the 'update' permission.
*
* @return object Response to API call.
*/
public function setContributorPermissions($user, $perm_all = true, $perm_close = false, $perm_edit = false, $perm_manage = false, $perm_settings = false, $perm_update = false)
{
$permissions = [];
if ($perm_all) {
$permissions[] = '+all';
} else {
if ($perm_close) {
$permissions[] = '+close';
}
if ($perm_edit) {
$permissions[] = '+edit';
}
if ($perm_manage) {
$permissions[] = '+manage';
}
if ($perm_settings) {
$permissions[] = '+settings';
}
if ($perm_update) {
$permissions[] = '+update';
}
}
if (count($permissions) == 0) {
$permissions = ['-all', '-close', '-edit', '-manage', '-settings', '-update'];
}
$params = [
'api_type' => 'json',
'name' => $user,
'permissions' => implode(',', $permissions),
'type' => 'liveupdate_contributor',
];
return $this->apicall('/set_contributor_permissions', 'POST', $params);
}
/**
* Modify an invited contributor's permission set. To modify a current contributor's permissions, use setContributorPermissions().
*
* @param string $user Username of user for which to edit permissions.
* @param bool $perm_all If the user should have full permissions.
* @param bool $perm_close If the user should have the 'close live thread' permission. User must have 'settings' too to close via the web UI.
* @param bool $perm_edit If the user should have the 'edit' permission.
* @param bool $perm_manage If the user should have the 'manage contributors' permission.
* @param bool $perm_settings If the user should have the 'settings' permission.
* @param bool $perm_update If the user should have the 'update' permission.
*
* @return object Response to API call.
*/
public function setInvitationPermissions($user, $perm_all = true, $perm_close = false, $perm_edit = false, $perm_manage = false, $perm_settings = false, $perm_update = false)
{
$permissions = [];
if ($perm_all) {
$permissions[] = '+all';
} else {
if ($perm_close) {
$permissions[] = '+close';
}
if ($perm_edit) {
$permissions[] = '+edit';
}
if ($perm_manage) {
$permissions[] = '+manage';
}
if ($perm_settings) {
$permissions[] = '+settings';
}
if ($perm_update) {
$permissions[] = '+update';
}
}
if (count($permissions) == 0) {
$permissions = ['-all', '-close', '-edit', '-manage', '-settings', '-update'];
}
$params = [
'api_type' => 'json',
'name' => $user,
'permissions' => implode(',', $permissions),
'type' => 'liveupdate_contributor_invite',
];
return $this->apiCall('/set_contributor_permissions', 'POST', $params);
}
/**
* Strikes the specified update, which will show up as crossed out in the live thread.
*
* @param string $update_id ID (rather, name attribute) of update to strike.
*
* @return object Response to API call.
*/
public function strikeUpdate($update_id)
{
$params = [
'api_type' => 'json',
'id' => $update_id,
];
return $this->apiCall('/strike_update', 'POST', $params);
}
/**
* Makes an update on the live thread.
*
* @param string $body Body of update to post.
*
* @return object Response to API call. Unfortunately, no update ID is returned yet. You need to run getUpdates() to find this.
*/
public function update($body)
{
$params = [
'api_type' => 'json',
'body' => $body,
];
return $this->apiCall('/update', 'POST', $params);
}
/**
* Retrieves updates on a thread.
*
* @param int $limit Upper limit of the number of links to retrieve. Maximum is 100.
* @param string|null $after Get items lower on list than this entry. Does not mean chronologically. Should be the *name* of an update: "LiveUpdate_..."
* @param string|null $before Get items higher on list than this entry. Does not mean chronologically. Should be the *name* of an update: "LiveUpdate_..."
*
* @return object Listing of LiveUpdate objects.
*/
public function getUpdates($limit = 25, $after = null, $before = null)
{
$params = [
'after' => $after,
'before' => $before,
'limit' => $limit,
];
return $this->liveCall('.json', 'GET', $params);
}
/**
* Retrieves information about the live thread.
*
* @return object LiveUpdateEvent object.
*/
public function about()
{
return $this->liveCall('/about.json');
}
/**
* Retrieves a list of contributors for the thread. To see invitations, the current user must have the 'manage' permission.
*
* @return object|array UserList object OR array of two UserList objects if there are visible pending invitations.
*/
public function getContributors()
{
return $this->liveCall('/contributors.json');
}
/**
* Retrieves a list of discussions about the current thread.
*
* @param int $limit Upper limit of the number of links 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 object Listing of posts.
*/
public function getDiscussions($limit = 25, $after = null, $before = null)
{
$params = [
'after' => $after,
'before' => $before,
'limit' => $limit,
'show' => 'all',
];
return $this->liveCall('/discussions', 'GET', $params);
}
public function apiCall($path, $method = 'GET', $params = null)
{
return $this->redditAPI->apiCall("/api/live/{$this->thread_id}$path", $method, $params);
}
public function liveCall($path, $method = 'GET', $params = null)
{
return $this->redditAPI->apiCall("/live/{$this->thread_id}$path", $method, $params);
}
}