]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Poll.php
Adding and removing of pictures via API is now possible
[friendica.git] / src / Factory / Api / Mastodon / Poll.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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          * @return \Friendica\Object\Api\Mastodon\Poll
36          * @throws HTTPException\NotFoundException
37          */
38         public function createFromId(int $id, int $uid = 0): \Friendica\Object\Api\Mastodon\Poll
39         {
40                 $question = Post\Question::getById($id);
41                 if (empty($question)) {
42                         throw new HTTPException\NotFoundException('Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
43                 }
44
45                 if (!Post::exists(['uri-id' => $question['uri-id'], 'uid' => [0, $uid]])) {
46                         throw new HTTPException\NotFoundException('Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
47                 }
48
49                 $question_options = Post\QuestionOption::getByURIId($question['uri-id']);
50                 if (empty($question_options)) {
51                         throw new HTTPException\NotFoundException('No options found for Poll with id ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
52                 }
53
54                 $expired = false;
55
56                 if (!empty($question['end-time'])) {
57                         $expired = DateTimeFormat::utcNow() > DateTimeFormat::utc($question['end-time']);
58                 }
59
60                 $votes   = 0;
61                 $options = [];
62
63                 foreach ($question_options as $option) {
64                         $options[$option['id']] = ['title' => $option['name'], 'votes_count' => $option['replies']];
65                         $votes += $option['replies'];
66                 }
67
68                 if (empty($uid)) {
69                         $ownvotes = null;
70                 } else {
71                         $ownvotes = [];
72                 }
73
74                 return new \Friendica\Object\Api\Mastodon\Poll($question, $options, $expired, $votes, $ownvotes);
75         }
76 }