API: We can now display polls
This commit is contained in:
parent
ee05bd91a3
commit
9b646dad97
10 changed files with 245 additions and 7 deletions
73
src/Factory/Api/Mastodon/Poll.php
Normal file
73
src/Factory/Api/Mastodon/Poll.php
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue