3 * @copyright Copyright (C) 2010-2021, 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\Model\Post;
29 use Friendica\Model\Verb;
30 use Friendica\Network\HTTPException;
31 use Friendica\Protocol\Activity;
32 use Friendica\Protocol\ActivityPub;
34 use Psr\Log\LoggerInterface;
36 class Status extends BaseFactory
41 private $mstdnAccountFactory;
43 private $mstdnMentionFactory;
45 private $mstdnTagFactory;
47 private $mstdnCardFactory;
48 /** @var Attachment */
49 private $mstdnAttachementFactory;
51 private $mstdnErrorFactory;
53 public function __construct(LoggerInterface $logger, Database $dba,
54 Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
55 Tag $mstdnTagFactory, Card $mstdnCardFactory,
56 Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory)
58 parent::__construct($logger);
60 $this->mstdnAccountFactory = $mstdnAccountFactory;
61 $this->mstdnMentionFactory = $mstdnMentionFactory;
62 $this->mstdnTagFactory = $mstdnTagFactory;
63 $this->mstdnCardFactory = $mstdnCardFactory;
64 $this->mstdnAttachementFactory = $mstdnAttachementFactory;
65 $this->mstdnErrorFactory = $mstdnErrorFactory;
69 * @param int $uriId Uri-ID of the item
70 * @param int $uid Item user
72 * @return \Friendica\Object\Api\Mastodon\Status
73 * @throws HTTPException\InternalServerErrorException
74 * @throws ImagickException|HTTPException\NotFoundException
76 public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Mastodon\Status
78 $fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
79 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
80 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
82 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . 'not found' . ($uid ? ' for user ' . $uid : '.'));
85 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
87 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
88 Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT, 'deleted' => false], []),
90 'thr-parent-id' => $uriId,
91 'gravity' => GRAVITY_ACTIVITY,
92 'vid' => Verb::getID(Activity::ANNOUNCE),
96 'thr-parent-id' => $uriId,
97 'gravity' => GRAVITY_ACTIVITY,
98 'vid' => Verb::getID(Activity::LIKE),
103 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
105 'thr-parent-id' => $uriId,
108 'gravity' => GRAVITY_ACTIVITY,
109 'vid' => Verb::getID(Activity::LIKE)
113 'thr-parent-id' => $uriId,
116 'gravity' => GRAVITY_ACTIVITY,
117 'vid' => Verb::getID(Activity::ANNOUNCE),
120 Post\ThreadUser::getIgnored($uriId, $uid),
121 (bool)($item['starred'] && ($item['gravity'] == GRAVITY_PARENT)),
122 Post\ThreadUser::getPinned($uriId, $uid)
125 $sensitive = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
126 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
128 $mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
129 $tags = $this->mstdnTagFactory->createFromUriId($uriId);
130 $card = $this->mstdnCardFactory->createFromUriId($uriId);
131 $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
133 $shared = BBCode::fetchShareAttributes($item['body']);
134 if (!empty($shared['guid'])) {
135 $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
137 $shared_uri_id = $shared_item['uri-id'] ?? 0;
139 $mentions = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
140 $tags = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
141 $attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id));
143 if (empty($card->toArray())) {
144 $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
149 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
150 $reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
151 $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
152 $item['title'] = $reshared_item['title'] ?? $item['title'];
153 $item['body'] = $reshared_item['body'] ?? $item['body'];
158 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
162 * @param int $uriId id of the mail
164 * @return \Friendica\Object\Api\Mastodon\Status
165 * @throws HTTPException\InternalServerErrorException
166 * @throws ImagickException|HTTPException\NotFoundException
168 public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
170 $item = ActivityPub\Transmitter::ItemArrayFromMail($id, true);
172 $this->mstdnErrorFactory->RecordNotFound();
175 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
177 $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
179 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
181 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
184 $application = new \Friendica\Object\Api\Mastodon\Application('');
187 $card = new \Friendica\Object\Api\Mastodon\Card([]);
191 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);