Added fetching multiple polls

This commit is contained in:
aymm 2021-04-18 18:52:49 +02:00
parent f9fbbbd225
commit 9edfc37a68
Signed by: phlaym
GPG Key ID: A06651BAB6777237
3 changed files with 50 additions and 2 deletions

View File

@ -1,5 +1,8 @@
{
"identifier" : "8326F7AE-6E68-4B42-ABE1-63DB3DF1C0D4",
"ignoredFilePatterns" : [
"logs"
],
"remotePath" : "\/var\/www\/html\/Dragonpolls\/vendor\/hutattedonmyarm\/apnuti",
"server" : "Phlaym",
"usesPublishing" : true

View File

@ -327,7 +327,9 @@ class APnutI
bool $follow_redirect = true
): array {
if (!empty($parameters)) {
$end_point .= '?'.http_build_query($parameters);
$parsed = parse_url($end_point);
$separator = empty($parsed['query']) ? '?' : '&';
$end_point .= $separator . http_build_query($parameters);
$parameters = [];
}
return $this->makeRequest('get', $end_point, $parameters, $content_type, $follow_redirect);
@ -540,6 +542,46 @@ class APnutI
}
}
public function getPolls(array $ids = []): array
{
$this->logger->debug('getPolls');
$endpoint = '';
$params = [];
if (!empty($ids)) {
$endpoint = '/polls?ids=' . implode(',', $ids);
} else {
$poll_types = Poll::$poll_types;
$poll_types[] = Poll::$notice_type;
$poll_types_param = implode(',', $poll_types);
$this->logger->info('No list of polls provided, using post search for poll types: '.$poll_types_param);
$endpoint = '/posts/search?raw_types='.$poll_types_param;
$params = [
'include_raw' => true,
'include_counts' => false,
'include_html' => false,
'include_post_raw' => true
];
}
try {
$res = $this->get($endpoint, $params);
$this->logger->debug(json_encode($res));
$polls = [];
foreach ($res as $poll_resp) {
$polls[] = new Poll($poll_resp, $this);
}
return $polls;
} catch (NotSupportedPollException $e) {
$this->logger->error('Poll not supported: '.json_encode($res));
throw $e;
} catch (HttpPnutForbiddenException $fe) {
$this->logger->error('Poll token required and not provided!');
throw new PollAccessRestrictedException();
} catch (NotAuthorizedException $nauth) {
$this->logger->error('Not authorized when fetching poll');
throw new PollAccessRestrictedException();
}
}
public function voteInPoll(int $poll_id, array $options, ?string $poll_token): Poll
{
$params = [

View File

@ -25,7 +25,7 @@ class Poll
private APnutI $api;
public static string $notice_type = 'io.pnut.core.poll-notice';
protected static array $poll_types = [
public static array $poll_types = [
'general.poll',
'net.unsweets.beta',
'io.pnut.core.poll',
@ -64,6 +64,9 @@ class Poll
if (!empty($data['source'])) { #Source is attached to post, not to poll raw
$poll_data['source'] = $data['source'];
}
if (!empty($data['user'])) { #User is attached to post, not to poll raw
$poll_data['user'] = $data['user'];
}
$type = Poll::$notice_type;
$this->parsePoll($poll_data);
} else {