]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Poll.php
API: Speed improvements when fetching posts
[friendica.git] / src / Factory / Api / Mastodon / Poll.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Factory\Api\Mastodon;
23
24 use Friendica\BaseFactory;
25 use Friendica\Model\Post;
26 use Friendica\Network\HTTPException;
27 use Friendica\Util\DateTimeFormat;
28
29 class Poll extends BaseFactory
30 {
31         /**
32          * @param int $id  Id the question
33          * @param int $uid Item user
34          */
35         public function createFromId(int $id, $uid = 0): \Friendica\Object\Api\Mastodon\Poll
36         {
37                 $question = Post\Question::getById($id);
38                 if (empty($question)) {
39                         throw new HTTPException\NotFoundException('Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
40                 }
41
42                 if (!Post::exists(['uri-id' => $question['uri-id'], 'uid' => [0, $uid]])) {
43                         throw new HTTPException\NotFoundException('Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
44                 }
45
46                 $question_options = Post\QuestionOption::getByURIId($question['uri-id']);
47                 if (empty($question_options)) {
48                         throw new HTTPException\NotFoundException('No options found for Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
49                 }
50
51                 $expired = false;
52
53                 if (!empty($question['end-time'])) {
54                         $expired = DateTimeFormat::utcNow() > DateTimeFormat::utc($question['end-time']);
55                 }
56
57                 $votes   = 0;
58                 $options = [];
59
60                 foreach ($question_options as $option) {
61                         $options[$option['id']] = ['title' => $option['name'], 'votes_count' => $option['replies']];
62                         $votes += $option['replies'];
63                 }
64
65                 if (empty($uid)) {
66                         $ownvotes = null;
67                 } else {
68                         $ownvotes = [];
69                 }
70
71                 return new \Friendica\Object\Api\Mastodon\Poll($question, $options, $expired, $votes, $ownvotes);
72         }
73 }