diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index 74b6fabc5f..f6f18ce967 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -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/) diff --git a/src/DI.php b/src/DI.php index 835f1ffed7..e34df7b669 100644 --- a/src/DI.php +++ b/src/DI.php @@ -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 */ diff --git a/src/Factory/Api/Mastodon/Poll.php b/src/Factory/Api/Mastodon/Poll.php new file mode 100644 index 0000000000..f1a9397230 --- /dev/null +++ b/src/Factory/Api/Mastodon/Poll.php @@ -0,0 +1,73 @@ +. + * + */ + +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); + } +} diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 9148140335..cac9dfd06a 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -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); } /** diff --git a/src/Model/Post/Question.php b/src/Model/Post/Question.php index 75de2925d3..db0d755b7f 100644 --- a/src/Model/Post/Question.php +++ b/src/Model/Post/Question.php @@ -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]); + } } diff --git a/src/Model/Post/QuestionOption.php b/src/Model/Post/QuestionOption.php index 9ca4ba3b69..641c8f2ccf 100644 --- a/src/Model/Post/QuestionOption.php +++ b/src/Model/Post/QuestionOption.php @@ -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']]); + } } diff --git a/src/Module/Api/Mastodon/Polls.php b/src/Module/Api/Mastodon/Polls.php new file mode 100644 index 0000000000..2391a0d60f --- /dev/null +++ b/src/Module/Api/Mastodon/Polls.php @@ -0,0 +1,47 @@ +. + * + */ + +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)); + } +} diff --git a/src/Object/Api/Mastodon/Poll.php b/src/Object/Api/Mastodon/Poll.php new file mode 100644 index 0000000000..fb52b540a4 --- /dev/null +++ b/src/Object/Api/Mastodon/Poll.php @@ -0,0 +1,77 @@ +. + * + */ + +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 = []; + } +} diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index 12fef7ac8f..33ae98eb26 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -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; } /** diff --git a/static/routes.config.php b/static/routes.config.php index d7c1ee6e59..385fcef9bb 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -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]],