[RedditBridge]: Add user option (#1943)

This commit is contained in:
dawidsowa 2021-02-23 08:08:43 +01:00 committed by GitHub
parent 687eb728d4
commit 569276f4ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,6 +23,19 @@ class RedditBridge extends BridgeAbstract {
'exampleValue' => 'selfhosted, php',
'title' => 'SubReddit names, separated by commas'
)
),
'user' => array(
'u' => array(
'name' => 'User',
'required' => true,
'title' => 'User name'
),
'comments' => array(
'type' => 'checkbox',
'name' => 'Comments',
'title' => 'Whether to return comments',
'defaultValue' => false
)
)
);
@ -33,12 +46,18 @@ class RedditBridge extends BridgeAbstract {
public function getName() {
if ($this->queriedContext == 'single') {
return 'Reddit r/' . $this->getInput('r');
} elseif ($this->queriedContext == 'user') {
return 'Reddit u/' . $this->getInput('u');
} else {
return self::NAME;
}
}
public function collectData() {
$user = false;
$comments = false;
switch ($this->queriedContext) {
case 'single':
$subreddits[] = $this->getInput('r');
@ -46,33 +65,55 @@ class RedditBridge extends BridgeAbstract {
case 'multi':
$subreddits = explode(',', $this->getInput('rs'));
break;
case 'user':
$subreddits[] = $this->getInput('u');
$user = true;
$comments = $this->getInput('comments');
break;
}
foreach ($subreddits as $subreddit) {
$name = trim($subreddit);
$values = getContents(self::URI . '/r/' . $name . '.json')
$values = getContents(self::URI . ($user ? '/user/' : '/r/') . $name . '.json')
or returnServerError('Unable to fetch posts!');
$decodedValues = json_decode($values);
foreach ($decodedValues->data->children as $post) {
if ($post->kind == 't1' && !$comments) {
continue;
}
$data = $post->data;
$item = array();
$item['author'] = $data->author;
$item['title'] = $data->title;
$item['uid'] = $data->id;
$item['timestamp'] = $data->created_utc;
$item['uri'] = $this->encodePermalink($data->permalink);
$item['categories'] = array();
$item['categories'][] = $data->link_flair_text;
$item['categories'][] = $data->pinned ? 'Pinned' : null;
if ($post->kind == 't1') {
$item['title'] = 'Comment: ' . $data->link_title;
} else {
$item['title'] = $data->title;
$item['categories'][] = $data->link_flair_text;
$item['categories'][] = $data->pinned ? 'Pinned' : null;
$item['categories'][] = $data->spoiler ? 'Spoiler' : null;
}
$item['categories'][] = $data->over_18 ? 'NSFW' : null;
$item['categories'][] = $data->spoiler ? 'Spoiler' : null;
$item['categories'] = array_filter($item['categories']);
if ($data->is_self) {
if ($post->kind == 't1') {
// Comment
$item['content']
= htmlspecialchars_decode($data->body_html);
} elseif ($data->is_self) {
// Text post
$item['content']
@ -112,7 +153,7 @@ class RedditBridge extends BridgeAbstract {
$id = $media->media_id;
$type = $data->media_metadata->$id->m == 'image/gif' ? 'gif' : 'u';
$src = $data->media_metadata->$id->s->$type;
$images[] = '<img src="' . $src . '"/>';
$images[] = '<figure><img src="' . $src . '"/></figure>';
}
$item['content'] = implode('', $images);