Added support for poll tokens
This commit is contained in:
parent
01b6b0bf8d
commit
e639234a95
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
index.php
|
index.php
|
||||||
vendor/
|
vendor/
|
||||||
log-*
|
log-*
|
||||||
|
logs
|
||||||
|
config.php
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"com.thorlaksson.phpcs.runOnChange" : "onSave",
|
"com.thorlaksson.phpcs.runOnChange" : "onSave",
|
||||||
"com.thorlaksson.phpcs.standard" : "\/Volumes\/Alyx\/System\/Volumes\/Update\/mnt1\/Users\/max\/Dev\/Pnut\/APnutI\/phpcs.xml",
|
"com.thorlaksson.phpcs.standard" : "phpcs.xml",
|
||||||
"editor.default_syntax" : "php",
|
"editor.default_syntax" : "php",
|
||||||
"php.validate" : "onSave",
|
"php.validate" : "onSave",
|
||||||
"workspace.color" : 1,
|
"workspace.color" : 1,
|
||||||
|
@ -12,6 +12,7 @@ use APnutI\Exceptions\HttpPnutException;
|
|||||||
use APnutI\Exceptions\HttpPnutRedirectException;
|
use APnutI\Exceptions\HttpPnutRedirectException;
|
||||||
use APnutI\Exceptions\NotSupportedPollException;
|
use APnutI\Exceptions\NotSupportedPollException;
|
||||||
use APnutI\Exceptions\HttpPnutForbiddenException;
|
use APnutI\Exceptions\HttpPnutForbiddenException;
|
||||||
|
use APnutI\Exceptions\PollAccessRestrictedException;
|
||||||
use APnutI\Meta;
|
use APnutI\Meta;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\RotatingFileHandler;
|
||||||
@ -471,17 +472,52 @@ class APnutI
|
|||||||
return $polls;
|
return $polls;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPoll(int $poll_id): Poll
|
private function getPollFromResponse(array $res): Poll
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$res = $this->get('/polls/' . $poll_id);
|
|
||||||
return new Poll($res, $this);
|
return new Poll($res, $this);
|
||||||
} catch (NotSupportedPollException $e) {
|
} catch (NotSupportedPollException $e) {
|
||||||
$this->logger->error('Poll not supported: '.json_encode($res));
|
$this->logger->error('Poll not supported: '.json_encode($res));
|
||||||
throw $e;
|
throw $e;
|
||||||
} catch (HttpPnutForbiddenException $fe) {
|
} catch (HttpPnutForbiddenException $fe) {
|
||||||
$this->logger->error('Poll token required and not provided!');
|
$this->logger->error('Poll token required and not provided!');
|
||||||
throw $fe;
|
throw new PollAccessRestrictedException();
|
||||||
|
} catch (NotAuthorizedException $nauth) {
|
||||||
|
$this->logger->error('Not authorized when fetching poll');
|
||||||
|
throw new PollAccessRestrictedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPollFromToken(int $poll_id, ?string $poll_token = null): Poll
|
||||||
|
{
|
||||||
|
$poll_token_query = empty($poll_token) ? '' : '?poll_token=' . $poll_token;
|
||||||
|
$res = $this->get('/polls/' . $poll_id . $poll_token_query);
|
||||||
|
return $this->getPollFromResponse($res);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPoll(int $poll_id, ?string $poll_token = null): Poll
|
||||||
|
{
|
||||||
|
if (empty($poll_token)) {
|
||||||
|
return $this->getPollFromToken($poll_id, $poll_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->logger->debug('Poll token provided');
|
||||||
|
$re = '/((http(s)?:\/\/)?((posts)|(beta))\.pnut\.io\/(@.*\/)?)?(?(1)|^)(?<postid>\d+)/';
|
||||||
|
preg_match($re, $poll_token, $matches);
|
||||||
|
if (!empty($matches['postid'])) {
|
||||||
|
$this->logger->debug('Poll token is post ' . $matches['postid']);
|
||||||
|
$post_id = (int)$matches['postid'];
|
||||||
|
$args = [
|
||||||
|
'include_raw' => true,
|
||||||
|
'include_counts' => false,
|
||||||
|
'include_html' => false,
|
||||||
|
'include_post_raw' => true
|
||||||
|
];
|
||||||
|
$res = $this->get('/posts/' . $post_id, $args);
|
||||||
|
return $this->getPollFromResponse($res);
|
||||||
|
} else {
|
||||||
|
$this->logger->debug('Poll token seems to be an actual poll token');
|
||||||
|
return $this->getPollFromToken($poll_id, $poll_token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +36,8 @@ class Poll
|
|||||||
{
|
{
|
||||||
$this->api = $api;
|
$this->api = $api;
|
||||||
$this->options = [];
|
$this->options = [];
|
||||||
$this->type = $data['type'];
|
$type = '';
|
||||||
if ($data['type'] === Poll::$notice_type) {
|
if (array_key_exists('type', $data) && $data['type'] === Poll::$notice_type) {
|
||||||
$val = $data['value'];
|
$val = $data['value'];
|
||||||
$this->closed_at = new \DateTime($val['closed_at']);
|
$this->closed_at = new \DateTime($val['closed_at']);
|
||||||
foreach ($val['options'] as $option) {
|
foreach ($val['options'] as $option) {
|
||||||
@ -46,18 +46,29 @@ class Poll
|
|||||||
$this->id = (int)$val['poll_id'];
|
$this->id = (int)$val['poll_id'];
|
||||||
$this->token = $val['poll_token'];
|
$this->token = $val['poll_token'];
|
||||||
$this->prompt = $val['prompt'];
|
$this->prompt = $val['prompt'];
|
||||||
} elseif (in_array($data['type'], Poll::$poll_types)) {
|
} elseif (array_key_exists('type', $data) &&in_array($data['type'], Poll::$poll_types)) {
|
||||||
$this->parsePoll($data);
|
$this->parsePoll($data);
|
||||||
} elseif (strpos($data['type'], '.poll') !== false) {
|
} elseif (array_key_exists('type', $data) &&strpos($data['type'], '.poll') !== false) {
|
||||||
// Try parsing unknown types if they *might* be a poll
|
// Try parsing unknown types if they *might* be a poll
|
||||||
try {
|
try {
|
||||||
$this->parsePoll($data);
|
$this->parsePoll($data);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new NotSupportedPollException($data['type']);
|
throw new NotSupportedPollException($data['type']);
|
||||||
}
|
}
|
||||||
|
} elseif (array_key_exists('raw', $data) && #Polls included in posts
|
||||||
|
array_key_exists(Poll::$notice_type, $data['raw']) &&
|
||||||
|
count($data['raw'][Poll::$notice_type]) > 0
|
||||||
|
) {
|
||||||
|
$poll_data = $data['raw'][Poll::$notice_type][0];
|
||||||
|
if (!empty($data['source'])) { #Source is attached to post, not to poll raw
|
||||||
|
$poll_data['source'] = $data['source'];
|
||||||
|
}
|
||||||
|
$type = Poll::$notice_type;
|
||||||
|
$this->parsePoll($poll_data);
|
||||||
} else {
|
} else {
|
||||||
throw new NotSupportedPollException($data['type']);
|
throw new NotSupportedPollException($data['type']);
|
||||||
}
|
}
|
||||||
|
$this->type = empty($type) ? $data['type'] : $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function parsePoll(array $data)
|
private function parsePoll(array $data)
|
||||||
@ -65,8 +76,8 @@ class Poll
|
|||||||
$this->created_at = new \DateTime($data['created_at']);
|
$this->created_at = new \DateTime($data['created_at']);
|
||||||
$this->closed_at = new \DateTime($data['closed_at']);
|
$this->closed_at = new \DateTime($data['closed_at']);
|
||||||
$this->id = (int)$data['id'];
|
$this->id = (int)$data['id'];
|
||||||
$this->is_anonymous = (bool)$data['is_anonymous'];
|
$this->is_anonymous = array_key_exists('is_anonymous', $data) ? (bool)$data['is_anonymous'] : false;
|
||||||
$this->is_public = (bool)$data['is_public'];
|
$this->is_public = array_key_exists('is_public', $data) ? (bool)$data['is_public'] : false;
|
||||||
foreach ($data['options'] as $option) {
|
foreach ($data['options'] as $option) {
|
||||||
$this->options[] = new PollOption($option);
|
$this->options[] = new PollOption($option);
|
||||||
}
|
}
|
||||||
|
7
src/Exceptions/PollAccessRestrictedException.php
Normal file
7
src/Exceptions/PollAccessRestrictedException.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
namespace APnutI\Exceptions;
|
||||||
|
|
||||||
|
class PollAccessRestrictedException extends \Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user