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\Verb;
31 use Friendica\Network\HTTPException;
32 use Friendica\Protocol\Activity;
33 use Friendica\Protocol\ActivityPub;
35 use Psr\Log\LoggerInterface;
37 class Status extends BaseFactory
42 private $mstdnAccountFactory;
44 private $mstdnMentionFactory;
46 private $mstdnTagFactory;
48 private $mstdnCardFactory;
49 /** @var Attachment */
50 private $mstdnAttachementFactory;
52 private $mstdnErrorFactory;
54 public function __construct(LoggerInterface $logger, Database $dba,
55 Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
56 Tag $mstdnTagFactory, Card $mstdnCardFactory,
57 Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory)
59 parent::__construct($logger);
61 $this->mstdnAccountFactory = $mstdnAccountFactory;
62 $this->mstdnMentionFactory = $mstdnMentionFactory;
63 $this->mstdnTagFactory = $mstdnTagFactory;
64 $this->mstdnCardFactory = $mstdnCardFactory;
65 $this->mstdnAttachementFactory = $mstdnAttachementFactory;
66 $this->mstdnErrorFactory = $mstdnErrorFactory;
70 * @param int $uriId Uri-ID of the item
71 * @param int $uid Item user
73 * @return \Friendica\Object\Api\Mastodon\Status
74 * @throws HTTPException\InternalServerErrorException
75 * @throws ImagickException|HTTPException\NotFoundException
77 public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Mastodon\Status
79 $fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
80 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
81 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
83 $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
85 return $this->createFromMailId($mail['id']);
87 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
90 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
92 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
93 Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT, 'deleted' => false], []),
95 'thr-parent-id' => $uriId,
96 'gravity' => GRAVITY_ACTIVITY,
97 'vid' => Verb::getID(Activity::ANNOUNCE),
101 'thr-parent-id' => $uriId,
102 'gravity' => GRAVITY_ACTIVITY,
103 'vid' => Verb::getID(Activity::LIKE),
108 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
110 'thr-parent-id' => $uriId,
113 'gravity' => GRAVITY_ACTIVITY,
114 'vid' => Verb::getID(Activity::LIKE)
118 'thr-parent-id' => $uriId,
121 'gravity' => GRAVITY_ACTIVITY,
122 'vid' => Verb::getID(Activity::ANNOUNCE),
125 Post\ThreadUser::getIgnored($uriId, $uid),
126 (bool)($item['starred'] && ($item['gravity'] == GRAVITY_PARENT)),
127 Post\ThreadUser::getPinned($uriId, $uid)
130 $sensitive = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
131 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
133 $mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
134 $tags = $this->mstdnTagFactory->createFromUriId($uriId);
135 $card = $this->mstdnCardFactory->createFromUriId($uriId);
136 $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
138 $shared = BBCode::fetchShareAttributes($item['body']);
139 if (!empty($shared['guid'])) {
140 $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
142 $shared_uri_id = $shared_item['uri-id'] ?? 0;
144 $mentions = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
145 $tags = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
146 $attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id));
148 if (empty($card->toArray())) {
149 $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
154 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
155 $reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
156 $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
157 $item['title'] = $reshared_item['title'] ?? $item['title'];
158 $item['body'] = $reshared_item['body'] ?? $item['body'];
163 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
167 * @param int $uriId id of the mail
169 * @return \Friendica\Object\Api\Mastodon\Status
170 * @throws HTTPException\InternalServerErrorException
171 * @throws ImagickException|HTTPException\NotFoundException
173 public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
175 $item = ActivityPub\Transmitter::ItemArrayFromMail($id, true);
177 $this->mstdnErrorFactory->RecordNotFound();
180 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
182 $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
184 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
186 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
189 $application = new \Friendica\Object\Api\Mastodon\Application('');
192 $card = new \Friendica\Object\Api\Mastodon\Card([]);
196 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);