3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Factory\Api\Mastodon;
24 use Friendica\BaseFactory;
25 use Friendica\Model\Post;
26 use Friendica\Network\HTTPException;
27 use Friendica\Util\DateTimeFormat;
29 class Poll extends BaseFactory
32 * @param int $id Id the question
33 * @param int $uid Item user
35 * @return \Friendica\Object\Api\Mastodon\Poll
36 * @throws HTTPException\NotFoundException
38 public function createFromId(int $id, int $uid = 0): \Friendica\Object\Api\Mastodon\Poll
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 : '.'));
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 : '.'));
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 : '.'));
56 if (!empty($question['end-time'])) {
57 $expired = DateTimeFormat::utcNow() > DateTimeFormat::utc($question['end-time']);
63 foreach ($question_options as $option) {
64 $options[$option['id']] = ['title' => $option['name'], 'votes_count' => $option['replies']];
65 $votes += $option['replies'];
74 return new \Friendica\Object\Api\Mastodon\Poll($question, $options, $expired, $votes, $ownvotes);