API: We can now display polls

This commit is contained in:
Michael 2022-04-22 19:24:22 +00:00
parent ee05bd91a3
commit 9b646dad97
10 changed files with 245 additions and 7 deletions

View File

@ -92,6 +92,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
- [`GET /api/v1/notifications/:id`](https://docs.joinmastodon.org/methods/notifications/)
- [`POST /api/v1/notifications/clear`](https://docs.joinmastodon.org/methods/notifications/)
- [`POST /api/v1/notifications/:id/dismiss`](https://docs.joinmastodon.org/methods/notifications/)
- [`GET /api/v1/polls/:id`](https://docs.joinmastodon.org/methods/statuses/polls/)
- [`GET /api/v1/preferences`](https://docs.joinmastodon.org/methods/accounts/preferences/)
- [`DELETE /api/v1/push/subscription`](https://docs.joinmastodon.org/methods/notifications/push/)
- [`GET /api/v1/push/subscription`](https://docs.joinmastodon.org/methods/notifications/push/)
@ -182,7 +183,6 @@ They refer to features or data that don't exist in Friendica yet.
- [`DELETE /api/v1/filters/:id`](https://docs.joinmastodon.org/methods/accounts/filters/)
- [`GET /api/v1/instance/activity`](https://docs.joinmastodon.org/methods/instance#weekly-activity)
- [`POST /api/v1/markers`](https://docs.joinmastodon.org/methods/timelines/markers/)
- [`GET /api/v1/polls/:id`](https://docs.joinmastodon.org/methods/statuses/polls/)
- [`POST /api/v1/polls/:id/votes`](https://docs.joinmastodon.org/methods/statuses/polls/)
- [`POST /api/v1/reports`](https://docs.joinmastodon.org/methods/accounts/reports/)
- [`PUT /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/)

View File

@ -326,6 +326,14 @@ abstract class DI
return self::$dice->create(Factory\Api\Mastodon\FollowRequest::class);
}
/**
* @return Factory\Api\Mastodon\Poll
*/
public static function mstdnPoll()
{
return self::$dice->create(Factory\Api\Mastodon\Poll::class);
}
/**
* @return Factory\Api\Mastodon\Relationship
*/

View File

@ -0,0 +1,73 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Mastodon;
use Friendica\BaseFactory;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Friendica\Util\DateTimeFormat;
class Poll extends BaseFactory
{
/**
* @param int $id Id the question
* @param int $uid Item user
*/
public function createFromId(int $id, $uid = 0): \Friendica\Object\Api\Mastodon\Poll
{
$question = Post\Question::getById($id);
if (empty($question)) {
throw new HTTPException\NotFoundException('Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
}
if (!Post::exists(['uri-id' => $question['uri-id'], 'uid' => [0, $uid]])) {
throw new HTTPException\NotFoundException('Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
}
$question_options = Post\QuestionOption::getByURIId($question['uri-id']);
if (empty($question_options)) {
throw new HTTPException\NotFoundException('No options found for Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
}
$expired = false;
if (!empty($question['end-time'])) {
$expired = DateTimeFormat::utcNow() > DateTimeFormat::utc($question['end-time']);
}
$votes = 0;
$options = [];
foreach ($question_options as $option) {
$options[$option['id']] = ['title' => $option['name'], 'votes_count' => $option['replies']];
$votes += $option['replies'];
}
if (empty($uid)) {
$ownvotes = null;
} else {
$ownvotes = [];
}
return new \Friendica\Object\Api\Mastodon\Poll($question, $options, $expired, $votes, $ownvotes);
}
}

View File

@ -51,11 +51,13 @@ class Status extends BaseFactory
private $mstdnAttachementFactory;
/** @var Error */
private $mstdnErrorFactory;
/** @var Poll */
private $mstdnPollFactory;
public function __construct(LoggerInterface $logger, Database $dba,
Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
Tag $mstdnTagFactory, Card $mstdnCardFactory,
Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory)
Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory, Poll $mstdnPollFactory)
{
parent::__construct($logger);
$this->dba = $dba;
@ -65,6 +67,7 @@ class Status extends BaseFactory
$this->mstdnCardFactory = $mstdnCardFactory;
$this->mstdnAttachementFactory = $mstdnAttachementFactory;
$this->mstdnErrorFactory = $mstdnErrorFactory;
$this->mstdnPollFactory = $mstdnPollFactory;
}
/**
@ -77,7 +80,7 @@ class Status extends BaseFactory
*/
public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Mastodon\Status
{
$fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning',
$fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id',
'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured'];
$item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
if (!$item) {
@ -136,6 +139,12 @@ class Status extends BaseFactory
$card = $this->mstdnCardFactory->createFromUriId($uriId);
$attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
if (!empty($item['question-id'])) {
$poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
} else {
$poll = null;
}
$shared = BBCode::fetchShareAttributes($item['body']);
if (!empty($shared['guid'])) {
$shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
@ -161,7 +170,7 @@ class Status extends BaseFactory
$reshare = [];
}
return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare, $poll);
}
/**

View File

@ -53,4 +53,14 @@ class Question
return DBA::update('post-question', $fields, ['uri-id' => $uri_id], $insert_if_missing ? true : []);
}
/**
* @param integer $id Question ID
* @param array $fields Array of selected fields, empty for all
* @return array|boolean Question record if it exists, false otherwise
*/
public static function getById($id, $fields = [])
{
return DBA::selectFirst('post-question', $fields, ['id' => $id]);
}
}

View File

@ -55,4 +55,18 @@ class QuestionOption
return DBA::update('post-question-option', $fields, ['uri-id' => $uri_id, 'id' => $id], $insert_if_missing ? true : []);
}
/**
* Retrieves the question options associated with the provided item ID.
*
* @param int $uri_id
* @return array
* @throws \Exception
*/
public static function getByURIId(int $uri_id)
{
$condition = ['uri-id' => $uri_id];
return DBA::selectToArray('post-question-option', [], $condition, ['order' => ['id']]);
}
}

View File

@ -0,0 +1,47 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
/**
* @see https://docs.joinmastodon.org/methods/statuses/polls/
*/
class Polls extends BaseApi
{
/**
* @throws HTTPException\InternalServerErrorException
*/
protected function rawContent(array $request = [])
{
$uid = self::getCurrentUserID();
if (empty($this->parameters['id'])) {
DI::mstdnError()->UnprocessableEntity();
}
System::jsonExit(DI::mstdnPoll()->createFromId($this->parameters['id'], $uid));
}
}

View File

@ -0,0 +1,77 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Object\Api\Mastodon;
use Friendica\BaseDataTransferObject;
use Friendica\Util\DateTimeFormat;
/**
* Class Poll
*
* @see https://docs.joinmastodon.org/entities/poll/
*/
class Poll extends BaseDataTransferObject
{
/** @var string */
protected $id;
/** @var string|null (Datetime) */
protected $expires_at;
/** @var bool */
protected $expired = false;
/** @var bool */
protected $multiple = false;
/** @var int */
protected $votes_count = 0;
/** @var int|null */
protected $voters_count = 0;
/** @var bool|null */
protected $voted = false;
/** @var array|null */
protected $own_votes = false;
/** @var array */
protected $options = [];
/** @var Emoji[] */
protected $emojis = [];
/**
* Creates a poll record.
*
* @param array $question Array with the question
* @param array $options Array of question options
* @param bool $expired "true" if the question is expired
* @param int $votes Number of total votes
* @param array $ownvotes Own vote
*/
public function __construct(array $question, array $options, bool $expired, int $votes, array $ownvotes = null)
{
$this->id = (string)$question['id'];
$this->expires_at = !empty($question['end-time']) ? DateTimeFormat::utc($question['end-time'], DateTimeFormat::JSON) : null;
$this->expired = $expired;
$this->multiple = (bool)$question['multiple'];
$this->votes_count = $votes;
$this->voters_count = $this->multiple ? $question['voters'] : null;
$this->voted = null;
$this->own_votes = $ownvotes;
$this->options = $options;
$this->emojis = [];
}
}

View File

@ -97,7 +97,7 @@ class Status extends BaseDataTransferObject
* @param array $item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $reblog)
public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $reblog, array $poll = null)
{
$this->id = (string)$item['uri-id'];
$this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::JSON);
@ -140,7 +140,7 @@ class Status extends BaseDataTransferObject
$this->tags = $tags;
$this->emojis = [];
$this->card = $card->toArray() ?: null;
$this->poll = null;
$this->poll = $poll;
}
/**

View File

@ -244,7 +244,7 @@ return [
'/notifications/{id:\d+}' => [Module\Api\Mastodon\Notifications::class, [R::GET ]],
'/notifications/clear' => [Module\Api\Mastodon\Notifications\Clear::class, [ R::POST]],
'/notifications/{id:\d+}/dismiss' => [Module\Api\Mastodon\Notifications\Dismiss::class, [ R::POST]],
'/polls/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not supported
'/polls/{id:\d+}' => [Module\Api\Mastodon\Polls::class, [R::GET ]], // not supported
'/polls/{id:\d+}/votes' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], // not supported
'/preferences' => [Module\Api\Mastodon\Preferences::class, [R::GET ]],
'/push/subscription' => [Module\Api\Mastodon\PushSubscription::class, [R::GET, R::POST, R::PUT, R::DELETE]],