3 * @copyright Copyright (C) 2010-2022, 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\Text\BBCode;
27 use Friendica\Database\Database;
28 use Friendica\Database\DBA;
29 use Friendica\Model\Post;
30 use Friendica\Model\Tag as TagModel;
31 use Friendica\Model\Verb;
32 use Friendica\Network\HTTPException;
33 use Friendica\Protocol\Activity;
34 use Friendica\Protocol\ActivityPub;
36 use Psr\Log\LoggerInterface;
38 class Status extends BaseFactory
43 private $mstdnAccountFactory;
45 private $mstdnMentionFactory;
47 private $mstdnTagFactory;
49 private $mstdnCardFactory;
50 /** @var Attachment */
51 private $mstdnAttachementFactory;
53 private $mstdnErrorFactory;
55 public function __construct(LoggerInterface $logger, Database $dba,
56 Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
57 Tag $mstdnTagFactory, Card $mstdnCardFactory,
58 Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory)
60 parent::__construct($logger);
62 $this->mstdnAccountFactory = $mstdnAccountFactory;
63 $this->mstdnMentionFactory = $mstdnMentionFactory;
64 $this->mstdnTagFactory = $mstdnTagFactory;
65 $this->mstdnCardFactory = $mstdnCardFactory;
66 $this->mstdnAttachementFactory = $mstdnAttachementFactory;
67 $this->mstdnErrorFactory = $mstdnErrorFactory;
71 * @param int $uriId Uri-ID of the item
72 * @param int $uid Item user
74 * @return \Friendica\Object\Api\Mastodon\Status
75 * @throws HTTPException\InternalServerErrorException
76 * @throws ImagickException|HTTPException\NotFoundException
78 public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Mastodon\Status
80 $fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning',
81 'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
82 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
84 $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
86 return $this->createFromMailId($mail['id']);
88 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
91 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
93 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
94 Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT, 'deleted' => false], []),
96 'thr-parent-id' => $uriId,
97 'gravity' => GRAVITY_ACTIVITY,
98 'vid' => Verb::getID(Activity::ANNOUNCE),
102 'thr-parent-id' => $uriId,
103 'gravity' => GRAVITY_ACTIVITY,
104 'vid' => Verb::getID(Activity::LIKE),
109 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
111 'thr-parent-id' => $uriId,
114 'gravity' => GRAVITY_ACTIVITY,
115 'vid' => Verb::getID(Activity::LIKE)
119 'thr-parent-id' => $uriId,
122 'gravity' => GRAVITY_ACTIVITY,
123 'vid' => Verb::getID(Activity::ANNOUNCE),
126 Post\ThreadUser::getIgnored($uriId, $uid),
127 (bool)($item['starred'] && ($item['gravity'] == GRAVITY_PARENT)),
128 Post\ThreadUser::getPinned($uriId, $uid)
131 $sensitive = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw', 'type' => TagModel::HASHTAG]);
132 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
134 $mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
135 $tags = $this->mstdnTagFactory->createFromUriId($uriId);
136 $card = $this->mstdnCardFactory->createFromUriId($uriId);
137 $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
139 $shared = BBCode::fetchShareAttributes($item['body']);
140 if (!empty($shared['guid'])) {
141 $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
143 $shared_uri_id = $shared_item['uri-id'] ?? 0;
145 $mentions = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
146 $tags = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
147 $attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id));
149 if (empty($card->toArray())) {
150 $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
155 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
156 $reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
157 $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
158 $item['title'] = $reshared_item['title'] ?? $item['title'];
159 $item['body'] = $reshared_item['body'] ?? $item['body'];
164 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
168 * @param int $uriId id of the mail
170 * @return \Friendica\Object\Api\Mastodon\Status
171 * @throws HTTPException\InternalServerErrorException
172 * @throws ImagickException|HTTPException\NotFoundException
174 public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
176 $item = ActivityPub\Transmitter::ItemArrayFromMail($id, true);
178 $this->mstdnErrorFactory->RecordNotFound();
181 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
183 $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
185 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
187 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
190 $application = new \Friendica\Object\Api\Mastodon\Application('');
193 $card = new \Friendica\Object\Api\Mastodon\Card([]);
197 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);