updated poll parsing

This commit is contained in:
Max Nuding 2025-05-20 18:57:01 +02:00
parent 7f0f8ac95b
commit 4ceeba7f4b
Signed by: phlaym
SSH Key Fingerprint: SHA256:mionmF+5trOUI1AxqzAU1ZK3tv6IiDcdKGXcMWwa1nQ
2 changed files with 306 additions and 279 deletions

View File

@ -524,8 +524,23 @@ class APnutI
foreach ($response as $post) {
if (!empty($post['raw'])) {
foreach ($post['raw'] as $raw) {
if (Poll::$notice_type === $raw['type']) {
$polls[] = $this->getPoll($raw['value']['poll_id']);
if (!empty($raw[Poll::$notice_type])) {
try {
$this->logger->debug('Parsing poll from raw');
$polls[] = new Poll($raw, $this);
} catch (NotSupportedPollException) {
$this->logger->warning('Parsing poll from raw failed. Loading from poll id');
$polls[] = $this->getPoll($raw[Poll::$notice_type]['poll_id']);
}
// if (empty($raw[Poll::$notice_type]['prompt'])) {
// $polls[] = $this->getPoll($raw[Poll::$notice_type]['poll_id']);
// } else {
// /*$poll_parsing_data = [
// 'type' => Poll::$notice_type,
// 'value' => $raw[Poll::$notice_type]
// ];*/
// $polls[] = new Poll($raw, $this);
// }
}
}
}
@ -542,10 +557,10 @@ class APnutI
$this->logger->error('Poll not supported: ' . json_encode($res));
throw $e;
} catch (HttpPnutForbiddenException $fe) {
$this->logger->error('Poll token required and not provided!');
$this->logger->error('Poll token required and not provided!', ['ex' => $fe]);
throw new PollAccessRestrictedException();
} catch (NotAuthorizedException $nauth) {
$this->logger->error('Not authorized when fetching poll');
$this->logger->error('Not authorized when fetching poll', ['ex' => $nauth]);
throw new PollAccessRestrictedException();
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace APnutI\Entities;
use APnutI\Entities\PollOption;
@ -17,7 +18,7 @@ class Poll
public bool $is_public = false;
public array $options = [];
public ?string $token = null;
public string $prompt = "";
public string $prompt = '';
public ?User $user = null;
public ?Source $source = null;
public string $type;
@ -30,7 +31,7 @@ class Poll
'net.unsweets.beta',
'io.pnut.core.poll',
'io.broadsword.poll',
'nl.chimpnut.quizbot.attachment.poll'
'nl.chimpnut.quizbot.attachment.poll',
];
public function __construct(array $data, APnutI $api)
@ -38,18 +39,24 @@ class Poll
$this->api = $api;
$this->options = [];
$type = '';
if (array_key_exists('type', $data) && $data['type'] === Poll::$notice_type) {
if (array_key_exists('type', $data) &&
$data['type'] === Poll::$notice_type
) {
$val = $data['value'];
$this->closed_at = new \DateTime($val['closed_at']);
foreach ($val['options'] as $option) {
$this->options[] = new PollOption($option);
}
$this->id = (int)$val['poll_id'];
$this->id = (int) $val['poll_id'];
$this->token = $val['poll_token'];
$this->prompt = $val['prompt'];
} elseif (array_key_exists('type', $data) &&in_array($data['type'], Poll::$poll_types)) {
} elseif (array_key_exists('type', $data) &&
in_array($data['type'], Poll::$poll_types)
) {
$this->parsePoll($data);
} elseif (array_key_exists('type', $data) &&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 {
$this->parsePoll($data);
@ -61,10 +68,12 @@ class Poll
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
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
if (!empty($data['user'])) {
#User is attached to post, not to poll raw
$poll_data['user'] = $data['user'];
}
$type = Poll::$notice_type;
@ -79,10 +88,16 @@ class Poll
{
$this->created_at = new \DateTime($data['created_at']);
$this->closed_at = new \DateTime($data['closed_at']);
$this->id = (int)$data['id'];
$this->is_anonymous = array_key_exists('is_anonymous', $data) ? (bool)$data['is_anonymous'] : false;
$this->max_options = array_key_exists('max_options', $data) ? (int)$data['max_options'] : 1;
$this->is_public = array_key_exists('is_public', $data) ? (bool)$data['is_public'] : false;
$this->id = (int) $data['id'];
$this->is_anonymous = array_key_exists('is_anonymous', $data)
? (bool) $data['is_anonymous']
: false;
$this->max_options = array_key_exists('max_options', $data)
? (int) $data['max_options']
: 1;
$this->is_public = array_key_exists('is_public', $data)
? (bool) $data['is_public']
: false;
foreach ($data['options'] as $option) {
$this->options[] = new PollOption($option);
}
@ -171,12 +186,9 @@ class Poll
bool $is_public
): Poll {
$options = array_filter($options);
$options = array_map(
function ($v) {
$options = array_map(function ($v) {
return ['text' => $v];
},
$options
);
}, $options);
$params = [
'duration' => $duration_minutes,
'options' => array_filter($options), #filters empty options
@ -184,7 +196,7 @@ class Poll
'type' => 'io.pnut.core.poll',
'is_anonymous' => $is_anonymous,
'is_public' => $is_public,
'max_options' => $max_options
'max_options' => $max_options,
];
$api->logger->debug('Creating poll');
$api->logger->debug(json_encode($params));
@ -200,10 +212,10 @@ class Poll
[
'+io.pnut.core.poll' => [
'poll_id' => $poll_id,
'poll_token' => $poll_token
]
]
]
'poll_token' => $poll_token,
],
],
],
];
}
@ -215,10 +227,10 @@ class Poll
} else {
$str = 'Unknown user';
}
return $str
. " asked: '"
. $this->prompt
. "', closed at "
. $this->closed_at->format('Y-m-d H:i:s T');
return $str .
" asked: '" .
$this->prompt .
"', closed at " .
$this->closed_at->format('Y-m-d H:i:s T');
}
}