]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Status.php
Merge pull request #9476 from annando/media-attach
[friendica.git] / src / Factory / Api / Mastodon / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Factory\Api\Mastodon;
23
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Item;
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;
35
36 class Status extends BaseFactory
37 {
38         /** @var BaseURL */
39         protected $baseUrl;
40         /** @var ProfileField */
41         protected $profileField;
42         /** @var Field */
43         protected $mstdnField;
44
45         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
46         {
47                 parent::__construct($logger);
48
49                 $this->baseUrl = $baseURL;
50                 $this->profileField = $profileField;
51                 $this->mstdnField = $mstdnField;
52         }
53
54         /**
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
60          */
61         public function createFromUriId(int $uriId, $uid = 0)
62         {
63                 $item = Item::selectFirst([], ['uri-id' => $uriId, 'uid' => $uid]);
64                 $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
65
66                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
67                         DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]),
68                         DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]),
69                         DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)])
70                 );
71
72                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
73                         DBA::exists('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]),
74                         DBA::exists('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]),
75                         DBA::exists('thread', ['iid' => $item['id'], 'uid' => $item['uid'], 'ignored' => true]),
76                         (bool)$item['starred'],
77                         DBA::exists('user-item', ['iid' => $item['id'], 'uid' => $item['uid'], 'pinned' => true])
78                 );
79
80                 $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
81                 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?? '');
82                 $mentions = DI::mstdnMention()->createFromUriId($uriId);
83                 $tags = DI::mstdnTag()->createFromUriId($uriId);
84
85                 $data = BBCode::getAttachmentData($item['body']);
86                 $card = new \Friendica\Object\Api\Mastodon\Card($data);
87
88                 $attachments = DI::mstdnAttachment()->createFromUriId($uriId);
89
90                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments);
91         }
92 }