]> git.mxchange.org Git - friendica.git/commitdiff
API: We can now display polls
authorMichael <heluecht@pirati.ca>
Fri, 22 Apr 2022 19:24:22 +0000 (19:24 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 22 Apr 2022 19:24:22 +0000 (19:24 +0000)
doc/API-Mastodon.md
src/DI.php
src/Factory/Api/Mastodon/Poll.php [new file with mode: 0644]
src/Factory/Api/Mastodon/Status.php
src/Model/Post/Question.php
src/Model/Post/QuestionOption.php
src/Module/Api/Mastodon/Polls.php [new file with mode: 0644]
src/Object/Api/Mastodon/Poll.php [new file with mode: 0644]
src/Object/Api/Mastodon/Status.php
static/routes.config.php

index 74b6fabc5f24b19694f8e5561500649cb47c944f..f6f18ce967cc735b55d5dba957e8774915de2e91 100644 (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/)
index 835f1ffed72619e8412a5a32be08c94527c56475..e34df7b669382deebb3804f8228da7ecc8b42af9 100644 (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
         */
diff --git a/src/Factory/Api/Mastodon/Poll.php b/src/Factory/Api/Mastodon/Poll.php
new file mode 100644 (file)
index 0000000..f1a9397
--- /dev/null
@@ -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);
+       }
+}
index 914814033595b0e00519f8c59437d866bb8bdce5..cac9dfd06aa9513c405407b9cbfb34270a530a12 100644 (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);
        }
 
        /**
index 75de2925d33ff06f03fa0b11ae504f053472966e..db0d755b7fe47b1402a3b38a27ab3224b8b26331 100644 (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]);
+       }
 }
index 9ca4ba3b69dc2225b59794ccc27b734d5b5e29fe..641c8f2ccf014adbfef7032a36ae0a9bcb470830 100644 (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']]);
+       }
 }
diff --git a/src/Module/Api/Mastodon/Polls.php b/src/Module/Api/Mastodon/Polls.php
new file mode 100644 (file)
index 0000000..2391a0d
--- /dev/null
@@ -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));
+       }
+}
diff --git a/src/Object/Api/Mastodon/Poll.php b/src/Object/Api/Mastodon/Poll.php
new file mode 100644 (file)
index 0000000..fb52b54
--- /dev/null
@@ -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       = [];
+       }
+}
index 12fef7ac8f0676a7e3fb37b6bde1458e61a751b4..33ae98eb2678f55b31418e98338335223d93bd6a 100644 (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;
        }
 
        /**
index d7c1ee6e59519ff3dfea065c03d3d2986079348c..385fcef9bb7eff26c41431263d440cd2397dc2fa 100644 (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]],