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\Content\ContactSelector;
26 use Friendica\Content\Item as ContentItem;
27 use Friendica\Content\Text\BBCode;
28 use Friendica\Core\Logger;
29 use Friendica\Database\Database;
30 use Friendica\Database\DBA;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Model\Tag as TagModel;
34 use Friendica\Model\Verb;
35 use Friendica\Network\HTTPException;
36 use Friendica\Protocol\Activity;
37 use Friendica\Protocol\ActivityPub;
39 use Psr\Log\LoggerInterface;
41 class Status extends BaseFactory
46 private $mstdnAccountFactory;
48 private $mstdnMentionFactory;
50 private $mstdnTagFactory;
52 private $mstdnCardFactory;
53 /** @var Attachment */
54 private $mstdnAttachementFactory;
56 private $mstdnErrorFactory;
58 private $mstdnPollFactory;
59 /** @var ContentItem */
62 public function __construct(LoggerInterface $logger, Database $dba,
63 Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
64 Tag $mstdnTagFactory, Card $mstdnCardFactory,
65 Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory,
66 Poll $mstdnPollFactory, ContentItem $contentItem)
68 parent::__construct($logger);
70 $this->mstdnAccountFactory = $mstdnAccountFactory;
71 $this->mstdnMentionFactory = $mstdnMentionFactory;
72 $this->mstdnTagFactory = $mstdnTagFactory;
73 $this->mstdnCardFactory = $mstdnCardFactory;
74 $this->mstdnAttachementFactory = $mstdnAttachementFactory;
75 $this->mstdnErrorFactory = $mstdnErrorFactory;
76 $this->mstdnPollFactory = $mstdnPollFactory;
77 $this->contentItem = $contentItem;
81 * @param int $uriId Uri-ID of the item
82 * @param int $uid Item user
83 * @param bool $display_quote Display quoted posts
84 * @param bool $reblog Check for reblogged post
85 * @param bool $in_reply_status Add an "in_reply_status" element
87 * @return \Friendica\Object\Api\Mastodon\Status
88 * @throws HTTPException\InternalServerErrorException
89 * @throws ImagickException|HTTPException\NotFoundException
91 public function createFromUriId(int $uriId, int $uid = 0, bool $display_quote = false, bool $reblog = true, bool $in_reply_status = true): \Friendica\Object\Api\Mastodon\Status
93 $fields = ['uri-id', 'uid', 'author-id', 'causer-id', 'author-uri-id', 'author-link', 'causer-uri-id', 'post-reason', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id',
94 'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id'];
95 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
97 $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
99 return $this->createFromMailId($mail['id']);
101 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
104 $activity_fields = ['uri-id', 'thr-parent-id', 'uri', 'author-id', 'author-uri-id', 'author-link', 'app', 'created', 'network', 'parent-author-id', 'private'];
106 if (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($item['vid'] == Verb::getID(Activity::ANNOUNCE))) {
108 $account = $this->mstdnAccountFactory->createFromUriId($item['author-uri-id'], $uid);
109 $uriId = $item['thr-parent-id'];
111 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
113 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
115 foreach ($activity_fields as $field) {
116 $item[$field] = $activity[$field];
119 $is_reshare = $reblog && !is_null($item['causer-uri-id']) && ($item['causer-id'] != $item['author-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT);
120 $account = $this->mstdnAccountFactory->createFromUriId($is_reshare ? $item['causer-uri-id'] : $item['author-uri-id'], $uid);
122 $activity = Post::selectFirstPost($activity_fields, ['thr-parent-id' => $item['uri-id'], 'author-id' => $item['causer-id'], 'verb' => Activity::ANNOUNCE]);
124 $item = array_merge($item, $activity);
129 $count_announce = Post::countPosts([
130 'thr-parent-id' => $uriId,
131 'gravity' => Item::GRAVITY_ACTIVITY,
132 'vid' => Verb::getID(Activity::ANNOUNCE),
134 ]) + Post::countPosts([
135 'quote-uri-id' => $uriId,
140 $count_like = Post::countPosts([
141 'thr-parent-id' => $uriId,
142 'gravity' => Item::GRAVITY_ACTIVITY,
143 'vid' => Verb::getID(Activity::LIKE),
147 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
148 Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_COMMENT, 'deleted' => false], []),
153 $origin_like = ($count_like == 0) ? false : Post::exists([
154 'thr-parent-id' => $uriId,
157 'gravity' => Item::GRAVITY_ACTIVITY,
158 'vid' => Verb::getID(Activity::LIKE),
161 $origin_announce = ($count_announce == 0) ? false : Post::exists([
162 'thr-parent-id' => $uriId,
165 'gravity' => Item::GRAVITY_ACTIVITY,
166 'vid' => Verb::getID(Activity::ANNOUNCE),
169 'quote-uri-id' => $uriId,
175 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
178 Post\ThreadUser::getIgnored($uriId, $uid),
179 (bool)($item['starred'] && ($item['gravity'] == Item::GRAVITY_PARENT)),
183 $sensitive = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw', 'type' => TagModel::HASHTAG]);
184 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
186 $mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
187 $tags = $this->mstdnTagFactory->createFromUriId($uriId);
188 if ($item['has-media']) {
189 $card = $this->mstdnCardFactory->createFromUriId($uriId);
190 $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
192 $card = new \Friendica\Object\Api\Mastodon\Card([]);
196 if (!empty($item['question-id'])) {
197 $poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
202 if ($display_quote) {
203 $quote = self::createQuote($item, $uid);
205 $item['body'] = BBCode::removeSharedData($item['body']);
207 if (!is_null($item['raw-body'])) {
208 $item['raw-body'] = BBCode::removeSharedData($item['raw-body']);
211 // We can always safely add attached activities. Real quotes are added to the body via "addSharedPost".
212 if (empty($item['quote-uri-id'])) {
213 $quote = self::createQuote($item, $uid);
218 $shared = $this->contentItem->getSharedPost($item, ['uri-id']);
219 if (!empty($shared)) {
220 $shared_uri_id = $shared['post']['uri-id'];
222 foreach ($this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy() as $mention) {
223 if (!in_array($mention, $mentions)) {
224 $mentions[] = $mention;
228 foreach ($this->mstdnTagFactory->createFromUriId($shared_uri_id) as $tag) {
229 if (!in_array($tag, $tags)) {
234 foreach ($this->mstdnAttachementFactory->createFromUriId($shared_uri_id) as $attachment) {
235 if (!in_array($attachment, $attachments)) {
236 $attachments[] = $attachment;
240 if (empty($card->toArray())) {
241 $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
245 $item['body'] = $this->contentItem->addSharedPost($item);
247 if (!is_null($item['raw-body'])) {
248 $item['raw-body'] = $this->contentItem->addSharedPost($item, $item['raw-body']);
254 $reshare = $this->createFromUriId($uriId, $uid, $display_quote, false, false)->toArray();
255 } catch (\Throwable $th) {
256 Logger::info('Reshare not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
263 if ($in_reply_status && ($item['gravity'] == Item::GRAVITY_COMMENT)) {
265 $in_reply = $this->createFromUriId($item['thr-parent-id'], $uid, $display_quote, false, false)->toArray();
266 } catch (\Throwable $th) {
267 Logger::info('Reply post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
274 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $quote, $poll);
278 * Create a quote status object
281 * @param integer $uid
284 private function createQuote(array $item, int $uid): array
286 if (empty($item['quote-uri-id'])) {
287 $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::ACTIVITY]);
288 if (!empty($media) && $shared_item = Post::selectFirst(['uri-id'], ['plink' => $media[0]['url'], 'uid' => [$uid, 0]])) {
289 $quote_id = $shared_item['uri-id'];
292 $quote_id = $item['quote-uri-id'];
295 if (!empty($quote_id)) {
297 $quote = $this->createFromUriId($quote_id, $uid, false, false, false)->toArray();
298 } catch (\Throwable $th) {
299 Logger::info('Quote not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
309 * @param int $uriId id of the mail
311 * @return \Friendica\Object\Api\Mastodon\Status
312 * @throws HTTPException\InternalServerErrorException
313 * @throws ImagickException|HTTPException\NotFoundException
315 public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
317 $item = ActivityPub\Transmitter::getItemArrayFromMail($id, true);
319 $this->mstdnErrorFactory->RecordNotFound();
322 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
324 $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
326 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
328 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
331 $application = new \Friendica\Object\Api\Mastodon\Application('');
334 $card = new \Friendica\Object\Api\Mastodon\Card([]);
339 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare);