From 9edfc37a68d468d12e329cda3d7ca42d2c05eb13 Mon Sep 17 00:00:00 2001 From: aymm Date: Sun, 18 Apr 2021 18:52:49 +0200 Subject: [PATCH] Added fetching multiple polls --- .nova/Publishing/Phlaym.json | 3 +++ src/APnutI.php | 44 +++++++++++++++++++++++++++++++++++- src/Entities/Poll.php | 5 +++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/.nova/Publishing/Phlaym.json b/.nova/Publishing/Phlaym.json index e8b440c..0f5cfff 100644 --- a/.nova/Publishing/Phlaym.json +++ b/.nova/Publishing/Phlaym.json @@ -1,5 +1,8 @@ { "identifier" : "8326F7AE-6E68-4B42-ABE1-63DB3DF1C0D4", + "ignoredFilePatterns" : [ + "logs" + ], "remotePath" : "\/var\/www\/html\/Dragonpolls\/vendor\/hutattedonmyarm\/apnuti", "server" : "Phlaym", "usesPublishing" : true diff --git a/src/APnutI.php b/src/APnutI.php index 53fe9f1..5a7fbd9 100644 --- a/src/APnutI.php +++ b/src/APnutI.php @@ -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 = [ diff --git a/src/Entities/Poll.php b/src/Entities/Poll.php index e3a1802..70b9a95 100644 --- a/src/Entities/Poll.php +++ b/src/Entities/Poll.php @@ -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 {