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\Database\Database;
28 use Friendica\Database\DBA;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Friendica\Model\Tag as TagModel;
32 use Friendica\Model\Verb;
33 use Friendica\Network\HTTPException;
34 use Friendica\Protocol\Activity;
35 use Friendica\Protocol\ActivityPub;
37 use Psr\Log\LoggerInterface;
39 class Status extends BaseFactory
44 private $mstdnAccountFactory;
46 private $mstdnMentionFactory;
48 private $mstdnTagFactory;
50 private $mstdnCardFactory;
51 /** @var Attachment */
52 private $mstdnAttachementFactory;
54 private $mstdnErrorFactory;
56 private $mstdnPollFactory;
57 /** @var ContentItem */
60 public function __construct(LoggerInterface $logger, Database $dba,
61 Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
62 Tag $mstdnTagFactory, Card $mstdnCardFactory,
63 Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory,
64 Poll $mstdnPollFactory, ContentItem $contentItem)
66 parent::__construct($logger);
68 $this->mstdnAccountFactory = $mstdnAccountFactory;
69 $this->mstdnMentionFactory = $mstdnMentionFactory;
70 $this->mstdnTagFactory = $mstdnTagFactory;
71 $this->mstdnCardFactory = $mstdnCardFactory;
72 $this->mstdnAttachementFactory = $mstdnAttachementFactory;
73 $this->mstdnErrorFactory = $mstdnErrorFactory;
74 $this->mstdnPollFactory = $mstdnPollFactory;
75 $this->contentItem = $contentItem;
79 * @param int $uriId Uri-ID of the item
80 * @param int $uid Item user
81 * @param bool $reblog Check for reblogged post
83 * @return \Friendica\Object\Api\Mastodon\Status
84 * @throws HTTPException\InternalServerErrorException
85 * @throws ImagickException|HTTPException\NotFoundException
87 public function createFromUriId(int $uriId, int $uid = 0, bool $reblog = true): \Friendica\Object\Api\Mastodon\Status
89 $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',
90 'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id'];
91 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
93 $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
95 return $this->createFromMailId($mail['id']);
97 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
100 if (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($item['vid'] == Verb::getID(Activity::ANNOUNCE))) {
102 $account = $this->mstdnAccountFactory->createFromUriId($item['author-uri-id'], $uid);
103 $uriId = $item['thr-parent-id'];
104 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
106 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
109 $is_reshare = $reblog && !is_null($item['causer-uri-id']) && ($item['causer-id'] != $item['author-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT);
110 $account = $this->mstdnAccountFactory->createFromUriId($is_reshare ? $item['causer-uri-id'] : $item['author-uri-id'], $uid);
113 $count_announce = Post::countPosts([
114 'thr-parent-id' => $uriId,
115 'gravity' => Item::GRAVITY_ACTIVITY,
116 'vid' => Verb::getID(Activity::ANNOUNCE),
118 ]) + Post::countPosts([
119 'quote-uri-id' => $uriId,
124 $count_like = Post::countPosts([
125 'thr-parent-id' => $uriId,
126 'gravity' => Item::GRAVITY_ACTIVITY,
127 'vid' => Verb::getID(Activity::LIKE),
131 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
132 Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_COMMENT, 'deleted' => false], []),
137 $origin_like = ($count_like == 0) ? false : Post::exists([
138 'thr-parent-id' => $uriId,
141 'gravity' => Item::GRAVITY_ACTIVITY,
142 'vid' => Verb::getID(Activity::LIKE),
145 $origin_announce = ($count_announce == 0) ? false : Post::exists([
146 'thr-parent-id' => $uriId,
149 'gravity' => Item::GRAVITY_ACTIVITY,
150 'vid' => Verb::getID(Activity::ANNOUNCE),
153 'quote-uri-id' => $uriId,
159 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
162 Post\ThreadUser::getIgnored($uriId, $uid),
163 (bool)($item['starred'] && ($item['gravity'] == Item::GRAVITY_PARENT)),
167 $sensitive = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw', 'type' => TagModel::HASHTAG]);
168 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
170 $mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
171 $tags = $this->mstdnTagFactory->createFromUriId($uriId);
172 if ($item['has-media']) {
173 $card = $this->mstdnCardFactory->createFromUriId($uriId);
174 $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
176 $card = new \Friendica\Object\Api\Mastodon\Card([]);
180 if (!empty($item['question-id'])) {
181 $poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
186 $shared = $this->contentItem->getSharedPost($item, ['uri-id']);
187 if (!empty($shared)) {
188 $shared_uri_id = $shared['post']['uri-id'];
190 foreach ($this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy() as $mention) {
191 if (!in_array($mention, $mentions)) {
192 $mentions[] = $mention;
196 foreach ($this->mstdnTagFactory->createFromUriId($shared_uri_id) as $tag) {
197 if (!in_array($tag, $tags)) {
202 foreach ($this->mstdnAttachementFactory->createFromUriId($shared_uri_id) as $attachment) {
203 if (!in_array($attachment, $attachments)) {
204 $attachments[] = $attachment;
208 if (empty($card->toArray())) {
209 $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
213 $item['body'] = $this->contentItem->addSharedPost($item);
215 if (!is_null($item['raw-body'])) {
216 $item['raw-body'] = $this->contentItem->addSharedPost($item, $item['raw-body']);
220 $reshare = $this->createFromUriId($uriId, $uid, false)->toArray();
225 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare, $poll);
229 * @param int $uriId id of the mail
231 * @return \Friendica\Object\Api\Mastodon\Status
232 * @throws HTTPException\InternalServerErrorException
233 * @throws ImagickException|HTTPException\NotFoundException
235 public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
237 $item = ActivityPub\Transmitter::getItemArrayFromMail($id, true);
239 $this->mstdnErrorFactory->RecordNotFound();
242 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
244 $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
246 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
248 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
251 $application = new \Friendica\Object\Api\Mastodon\Application('');
254 $card = new \Friendica\Object\Api\Mastodon\Card([]);
258 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);