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\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Content\ContactSelector;
27 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\Repository\ProfileField;
34 use Psr\Log\LoggerInterface;
36 class Status extends BaseFactory
40 /** @var ProfileField */
41 protected $profileField;
43 protected $mstdnField;
45 public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
47 parent::__construct($logger);
49 $this->baseUrl = $baseURL;
50 $this->profileField = $profileField;
51 $this->mstdnField = $mstdnField;
55 * @param int $uriId Uri-ID of the item
56 * @param int $uid Item user
57 * @return \Friendica\Object\Api\Mastodon\Status
58 * @throws HTTPException\InternalServerErrorException
59 * @throws \ImagickException
61 public function createFromUriId(int $uriId, $uid = 0)
63 $fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
64 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
65 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
67 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . 'not found' . ($uid ? ' for user ' . $uid : '.'));
70 $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
72 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
73 Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT], [], false),
74 Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)], [], false),
75 Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)], [], false)
78 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
79 Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]),
80 Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]),
81 Post\ThreadUser::getIgnored($uriId, $uid),
82 (bool)$item['starred'],
83 Post\ThreadUser::getPinned($uriId, $uid)
86 $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
87 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
89 $mentions = DI::mstdnMention()->createFromUriId($uriId);
90 $tags = DI::mstdnTag()->createFromUriId($uriId);
91 $card = DI::mstdnCard()->createFromUriId($uriId);
92 $attachments = DI::mstdnAttachment()->createFromUriId($uriId);
94 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
95 $reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
96 $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]);
97 $item['title'] = $reshared_item['title'] ?? $item['title'];
98 $item['body'] = $reshared_item['body'] ?? $item['body'];
103 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);