updated poll parsing
This commit is contained in:
parent
7f0f8ac95b
commit
4ceeba7f4b
@ -524,8 +524,23 @@ class APnutI
|
|||||||
foreach ($response as $post) {
|
foreach ($response as $post) {
|
||||||
if (!empty($post['raw'])) {
|
if (!empty($post['raw'])) {
|
||||||
foreach ($post['raw'] as $raw) {
|
foreach ($post['raw'] as $raw) {
|
||||||
if (Poll::$notice_type === $raw['type']) {
|
if (!empty($raw[Poll::$notice_type])) {
|
||||||
$polls[] = $this->getPoll($raw['value']['poll_id']);
|
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));
|
$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!', ['ex' => $fe]);
|
||||||
throw new PollAccessRestrictedException();
|
throw new PollAccessRestrictedException();
|
||||||
} catch (NotAuthorizedException $nauth) {
|
} catch (NotAuthorizedException $nauth) {
|
||||||
$this->logger->error('Not authorized when fetching poll');
|
$this->logger->error('Not authorized when fetching poll', ['ex' => $nauth]);
|
||||||
throw new PollAccessRestrictedException();
|
throw new PollAccessRestrictedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace APnutI\Entities;
|
namespace APnutI\Entities;
|
||||||
|
|
||||||
use APnutI\Entities\PollOption;
|
use APnutI\Entities\PollOption;
|
||||||
@ -17,7 +18,7 @@ class Poll
|
|||||||
public bool $is_public = false;
|
public bool $is_public = false;
|
||||||
public array $options = [];
|
public array $options = [];
|
||||||
public ?string $token = null;
|
public ?string $token = null;
|
||||||
public string $prompt = "";
|
public string $prompt = '';
|
||||||
public ?User $user = null;
|
public ?User $user = null;
|
||||||
public ?Source $source = null;
|
public ?Source $source = null;
|
||||||
public string $type;
|
public string $type;
|
||||||
@ -30,7 +31,7 @@ class Poll
|
|||||||
'net.unsweets.beta',
|
'net.unsweets.beta',
|
||||||
'io.pnut.core.poll',
|
'io.pnut.core.poll',
|
||||||
'io.broadsword.poll',
|
'io.broadsword.poll',
|
||||||
'nl.chimpnut.quizbot.attachment.poll'
|
'nl.chimpnut.quizbot.attachment.poll',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(array $data, APnutI $api)
|
public function __construct(array $data, APnutI $api)
|
||||||
@ -38,7 +39,9 @@ class Poll
|
|||||||
$this->api = $api;
|
$this->api = $api;
|
||||||
$this->options = [];
|
$this->options = [];
|
||||||
$type = '';
|
$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'];
|
$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) {
|
||||||
@ -47,9 +50,13 @@ 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 (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);
|
$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 parsing unknown types if they *might* be a poll
|
||||||
try {
|
try {
|
||||||
$this->parsePoll($data);
|
$this->parsePoll($data);
|
||||||
@ -61,10 +68,12 @@ class Poll
|
|||||||
count($data['raw'][Poll::$notice_type]) > 0
|
count($data['raw'][Poll::$notice_type]) > 0
|
||||||
) {
|
) {
|
||||||
$poll_data = $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'];
|
$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'];
|
$poll_data['user'] = $data['user'];
|
||||||
}
|
}
|
||||||
$type = Poll::$notice_type;
|
$type = Poll::$notice_type;
|
||||||
@ -80,9 +89,15 @@ 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 = array_key_exists('is_anonymous', $data) ? (bool)$data['is_anonymous'] : false;
|
$this->is_anonymous = array_key_exists('is_anonymous', $data)
|
||||||
$this->max_options = array_key_exists('max_options', $data) ? (int)$data['max_options'] : 1;
|
? (bool) $data['is_anonymous']
|
||||||
$this->is_public = array_key_exists('is_public', $data) ? (bool)$data['is_public'] : false;
|
: 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) {
|
foreach ($data['options'] as $option) {
|
||||||
$this->options[] = new PollOption($option);
|
$this->options[] = new PollOption($option);
|
||||||
}
|
}
|
||||||
@ -171,12 +186,9 @@ class Poll
|
|||||||
bool $is_public
|
bool $is_public
|
||||||
): Poll {
|
): Poll {
|
||||||
$options = array_filter($options);
|
$options = array_filter($options);
|
||||||
$options = array_map(
|
$options = array_map(function ($v) {
|
||||||
function ($v) {
|
|
||||||
return ['text' => $v];
|
return ['text' => $v];
|
||||||
},
|
}, $options);
|
||||||
$options
|
|
||||||
);
|
|
||||||
$params = [
|
$params = [
|
||||||
'duration' => $duration_minutes,
|
'duration' => $duration_minutes,
|
||||||
'options' => array_filter($options), #filters empty options
|
'options' => array_filter($options), #filters empty options
|
||||||
@ -184,7 +196,7 @@ class Poll
|
|||||||
'type' => 'io.pnut.core.poll',
|
'type' => 'io.pnut.core.poll',
|
||||||
'is_anonymous' => $is_anonymous,
|
'is_anonymous' => $is_anonymous,
|
||||||
'is_public' => $is_public,
|
'is_public' => $is_public,
|
||||||
'max_options' => $max_options
|
'max_options' => $max_options,
|
||||||
];
|
];
|
||||||
$api->logger->debug('Creating poll');
|
$api->logger->debug('Creating poll');
|
||||||
$api->logger->debug(json_encode($params));
|
$api->logger->debug(json_encode($params));
|
||||||
@ -200,10 +212,10 @@ class Poll
|
|||||||
[
|
[
|
||||||
'+io.pnut.core.poll' => [
|
'+io.pnut.core.poll' => [
|
||||||
'poll_id' => $poll_id,
|
'poll_id' => $poll_id,
|
||||||
'poll_token' => $poll_token
|
'poll_token' => $poll_token,
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,10 +227,10 @@ class Poll
|
|||||||
} else {
|
} else {
|
||||||
$str = 'Unknown user';
|
$str = 'Unknown user';
|
||||||
}
|
}
|
||||||
return $str
|
return $str .
|
||||||
. " asked: '"
|
" asked: '" .
|
||||||
. $this->prompt
|
$this->prompt .
|
||||||
. "', closed at "
|
"', closed at " .
|
||||||
. $this->closed_at->format('Y-m-d H:i:s T');
|
$this->closed_at->format('Y-m-d H:i:s T');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user