Added poll voting

This commit is contained in:
aymm 2021-03-28 21:18:33 +02:00
parent ddeb77da95
commit a0b03d439f
Signed by: phlaym
GPG Key ID: A06651BAB6777237
2 changed files with 28 additions and 4 deletions

View File

@ -200,7 +200,7 @@ class APnutI
// if they passed an array, build a list of parameters from it // if they passed an array, build a list of parameters from it
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
if (is_array($parameters) && $method !== 'POST-RAW') { if (is_array($parameters) && $method !== 'POST-RAW') {
$parameters = http_build_query($parameters); $parameters = $content_type === 'application/json' ? json_encode($parameters) : http_build_query($parameters);
} }
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
$headers[] = "Content-Type: ".$content_type; $headers[] = "Content-Type: ".$content_type;
@ -217,8 +217,7 @@ class APnutI
$this->logger->info("Using server token for auth"); $this->logger->info("Using server token for auth");
$headers[] = 'Authorization: Bearer '.$this->server_token; $headers[] = 'Authorization: Bearer '.$this->server_token;
} }
#$this->logger->info("Access token: ".$this->access_token);
#$this->logger->info("Headers: ".json_encode($headers));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true);
@ -529,6 +528,25 @@ class APnutI
} }
} }
public function voteInPoll(int $poll_id, array $options, ?string $poll_token): Poll
{
$params = [
'positions' => $options
];
if (!empty($poll_token)) {
$params['poll_token'] = $poll_token;
}
$this->logger->debug(json_encode($params));
$resp = $this->makeRequest(
'PUT',
"/polls/$poll_id/response",
$params,
'application/json'
);
#TODO: Use getPollFromEndpoint
return new Poll($resp, $this);
}
public function getAuthorizedUser(): User public function getAuthorizedUser(): User
{ {
$this->current_user ??= new User($this->get('/users/me'), $this); $this->current_user ??= new User($this->get('/users/me'), $this);

View File

@ -119,7 +119,8 @@ class Poll
return $optns; return $optns;
} }
public function getMyVotes(): array { public function getMyVotes(): array
{
$optns = []; $optns = [];
foreach ($this->options as $option) { foreach ($this->options as $option) {
if ($option->is_your_response) { if ($option->is_your_response) {
@ -145,6 +146,11 @@ class Poll
return $this->closed_at <= new \DateTime(); return $this->closed_at <= new \DateTime();
} }
public function vote(array $options): Poll
{
return $this-api->voteInPoll($this->id, $options, $this->token);
}
public function __toString(): string public function __toString(): string
{ {
if (!empty($this->user)) { if (!empty($this->user)) {