friendica/src/Factory/Api/Mastodon/Status.php

244 lines
8.9 KiB
PHP
Raw Normal View History

2020-09-03 20:57:18 +02:00
<?php
/**
2022-01-02 08:27:47 +01:00
* @copyright Copyright (C) 2010-2022, the Friendica project
2020-09-03 20:57:18 +02:00
*
* @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\Content\ContactSelector;
2022-10-27 07:44:44 +02:00
use Friendica\Content\Item as ContentItem;
2021-06-05 22:36:45 +02:00
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Model\Tag as TagModel;
use Friendica\Model\Verb;
2020-09-03 20:57:18 +02:00
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
2021-05-20 06:39:45 +02:00
use Friendica\Protocol\ActivityPub;
2021-06-05 22:36:45 +02:00
use ImagickException;
2020-09-03 20:57:18 +02:00
use Psr\Log\LoggerInterface;
class Status extends BaseFactory
{
2021-06-05 22:36:45 +02:00
/** @var Database */
private $dba;
/** @var Account */
private $mstdnAccountFactory;
/** @var Mention */
private $mstdnMentionFactory;
/** @var Tag */
private $mstdnTagFactory;
/** @var Card */
private $mstdnCardFactory;
/** @var Attachment */
private $mstdnAttachementFactory;
/** @var Error */
private $mstdnErrorFactory;
2022-04-22 21:24:22 +02:00
/** @var Poll */
private $mstdnPollFactory;
2022-10-27 07:44:44 +02:00
/** @var ContentItem */
private $contentItem;
2021-06-05 22:36:45 +02:00
public function __construct(LoggerInterface $logger, Database $dba,
Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
Tag $mstdnTagFactory, Card $mstdnCardFactory,
2022-10-27 07:44:44 +02:00
Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory,
Poll $mstdnPollFactory, ContentItem $contentItem)
2020-09-03 20:57:18 +02:00
{
parent::__construct($logger);
2021-06-05 22:36:45 +02:00
$this->dba = $dba;
$this->mstdnAccountFactory = $mstdnAccountFactory;
$this->mstdnMentionFactory = $mstdnMentionFactory;
$this->mstdnTagFactory = $mstdnTagFactory;
$this->mstdnCardFactory = $mstdnCardFactory;
$this->mstdnAttachementFactory = $mstdnAttachementFactory;
$this->mstdnErrorFactory = $mstdnErrorFactory;
2022-04-22 21:24:22 +02:00
$this->mstdnPollFactory = $mstdnPollFactory;
2022-10-27 07:44:44 +02:00
$this->contentItem = $contentItem;
2020-09-03 20:57:18 +02:00
}
/**
* @param int $uriId Uri-ID of the item
* @param int $uid Item user
* @param bool $reblog Check for reblogged post
2021-06-05 22:36:45 +02:00
*
2020-09-03 20:57:18 +02:00
* @return \Friendica\Object\Api\Mastodon\Status
* @throws HTTPException\InternalServerErrorException
2021-06-05 22:36:45 +02:00
* @throws ImagickException|HTTPException\NotFoundException
2020-09-03 20:57:18 +02:00
*/
public function createFromUriId(int $uriId, int $uid = 0, bool $reblog = true): \Friendica\Object\Api\Mastodon\Status
2020-09-03 20:57:18 +02:00
{
$fields = ['uri-id', 'uid', 'author-id', 'author-uri-id', 'author-link', 'causer-uri-id', 'post-reason', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id',
2022-10-30 02:20:05 +01:00
'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id'];
$item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
if (!$item) {
$mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
if ($mail) {
return $this->createFromMailId($mail['id']);
}
2021-07-20 23:45:42 +02:00
throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
}
$is_reshare = $reblog && !is_null($item['causer-uri-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT);
$account = $this->mstdnAccountFactory->createFromUriId($is_reshare ? $item['causer-uri-id']:$item['author-uri-id'], $uid);
$count_announce = Post::countPosts([
'thr-parent-id' => $uriId,
'gravity' => Item::GRAVITY_ACTIVITY,
'vid' => Verb::getID(Activity::ANNOUNCE),
'deleted' => false
], []);
$count_like = Post::countPosts([
'thr-parent-id' => $uriId,
'gravity' => Item::GRAVITY_ACTIVITY,
'vid' => Verb::getID(Activity::LIKE),
'deleted' => false
], []);
2020-09-03 20:57:18 +02:00
$counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_COMMENT, 'deleted' => false], []),
$count_announce,
$count_like
);
$origin_like = ($count_like == 0) ? false : Post::exists([
'thr-parent-id' => $uriId,
'uid' => $uid,
'origin' => true,
'gravity' => Item::GRAVITY_ACTIVITY,
'vid' => Verb::getID(Activity::LIKE),
'deleted' => false
]);
$origin_announce = ($count_announce == 0) ? false : Post::exists([
'thr-parent-id' => $uriId,
'uid' => $uid,
'origin' => true,
'gravity' => Item::GRAVITY_ACTIVITY,
'vid' => Verb::getID(Activity::ANNOUNCE),
'deleted' => false
]);
$userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
$origin_like,
$origin_announce,
Post\ThreadUser::getIgnored($uriId, $uid),
(bool)($item['starred'] && ($item['gravity'] == Item::GRAVITY_PARENT)),
2022-04-07 23:52:25 +02:00
$item['featured']
);
$sensitive = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw', 'type' => TagModel::HASHTAG]);
$application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
2021-06-05 22:36:45 +02:00
$mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
$tags = $this->mstdnTagFactory->createFromUriId($uriId);
if ($item['has-media']) {
$card = $this->mstdnCardFactory->createFromUriId($uriId);
$attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
} else {
$card = new \Friendica\Object\Api\Mastodon\Card([]);
$attachments = [];
}
2022-04-22 21:24:22 +02:00
if (!empty($item['question-id'])) {
$poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
} else {
$poll = null;
}
2022-10-27 07:44:44 +02:00
$shared = $this->contentItem->getSharedPost($item, ['uri-id']);
if (!empty($shared)) {
$shared_uri_id = $shared['post']['uri-id'];
2021-05-12 01:10:59 +02:00
2022-11-30 06:59:27 +01:00
foreach ($this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy() as $mention) {
if (!in_array($mention, $mentions)) {
2022-11-30 14:35:57 +01:00
$mentions[] = $mention;
2022-11-30 06:59:27 +01:00
}
}
foreach ($this->mstdnTagFactory->createFromUriId($shared_uri_id) as $tag) {
if (!in_array($tag, $tags)) {
2022-11-30 14:35:57 +01:00
$tags[] = $tag;
2022-11-30 06:59:27 +01:00
}
}
foreach ($this->mstdnAttachementFactory->createFromUriId($shared_uri_id) as $attachment) {
if (!in_array($attachment, $attachments)) {
$attachments[] = $attachment;
}
}
2021-05-12 01:10:59 +02:00
if (empty($card->toArray())) {
2021-06-05 22:36:45 +02:00
$card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
2021-05-12 01:10:59 +02:00
}
}
2020-11-01 12:01:57 +01:00
if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
2021-06-05 22:36:45 +02:00
$reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
$reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
2020-11-01 12:01:57 +01:00
$item['title'] = $reshared_item['title'] ?? $item['title'];
2021-06-05 22:36:45 +02:00
$item['body'] = $reshared_item['body'] ?? $item['body'];
2020-11-01 12:01:57 +01:00
} else {
2022-10-30 02:20:05 +01:00
$item['body'] = $this->contentItem->addSharedPost($item);
$item['raw-body'] = $this->contentItem->addSharedPost($item, $item['raw-body']);
2020-11-01 12:01:57 +01:00
$reshare = [];
}
if ($is_reshare) {
$reshare = $this->createFromUriId($uriId, $uid, false)->toArray();
}
2022-11-30 15:33:55 +01:00
2022-04-22 21:24:22 +02:00
return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare, $poll);
2020-09-03 20:57:18 +02:00
}
2021-05-19 23:56:50 +02:00
/**
* @param int $uriId id of the mail
2021-06-05 22:36:45 +02:00
*
2021-05-19 23:56:50 +02:00
* @return \Friendica\Object\Api\Mastodon\Status
* @throws HTTPException\InternalServerErrorException
2021-06-09 00:09:32 +02:00
* @throws ImagickException|HTTPException\NotFoundException
2021-05-19 23:56:50 +02:00
*/
2021-06-09 00:09:32 +02:00
public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
2021-05-19 23:56:50 +02:00
{
$item = ActivityPub\Transmitter::getItemArrayFromMail($id, true);
2021-05-20 06:39:45 +02:00
if (empty($item)) {
2021-06-05 22:36:45 +02:00
$this->mstdnErrorFactory->RecordNotFound();
2021-05-19 23:56:50 +02:00
}
2021-06-05 22:36:45 +02:00
$account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
2021-05-19 23:56:50 +02:00
2021-06-05 22:36:45 +02:00
$replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
2021-05-22 15:37:04 +02:00
$counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
2021-05-19 23:56:50 +02:00
$userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
$sensitive = false;
$application = new \Friendica\Object\Api\Mastodon\Application('');
$mentions = [];
$tags = [];
$card = new \Friendica\Object\Api\Mastodon\Card([]);
$attachments = [];
$reshare = [];
return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
}
2020-09-03 20:57:18 +02:00
}