]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Status.php
Fix notices
[friendica.git] / src / Factory / Api / Mastodon / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\Post;
31 use Friendica\Model\Verb;
32 use Friendica\Network\HTTPException;
33 use Friendica\Protocol\Activity;
34 use Friendica\Repository\ProfileField;
35 use Psr\Log\LoggerInterface;
36
37 class Status extends BaseFactory
38 {
39         /** @var BaseURL */
40         protected $baseUrl;
41         /** @var ProfileField */
42         protected $profileField;
43         /** @var Field */
44         protected $mstdnField;
45
46         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
47         {
48                 parent::__construct($logger);
49
50                 $this->baseUrl = $baseURL;
51                 $this->profileField = $profileField;
52                 $this->mstdnField = $mstdnField;
53         }
54
55         /**
56          * @param int $uriId Uri-ID of the item
57          * @param int $uid   Item user
58          * @return \Friendica\Object\Api\Mastodon\Status
59          * @throws HTTPException\InternalServerErrorException
60          * @throws \ImagickException
61          */
62         public function createFromUriId(int $uriId, $uid = 0)
63         {
64                 $fields = ['uri-id', 'uid', 'author-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created',
65                         'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
66                 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => $uid]);
67                 if (!$item) {
68                         throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . 'not found' . ($uid ? ' for user ' . $uid : '.'));
69                 }
70
71                 $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
72
73                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
74                         Post::count(['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]),
75                         Post::count(['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]),
76                         Post::count(['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)])
77                 );
78
79                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
80                         Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]),
81                         Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]),
82                         Post\ThreadUser::getIgnored($uriId, $item['uid']),
83                         (bool)$item['starred'],
84                         Post\ThreadUser::getPinned($uriId, $item['uid'])
85                 );
86
87                 $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
88                 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?? '');
89                 $mentions = DI::mstdnMention()->createFromUriId($uriId);
90                 $tags = DI::mstdnTag()->createFromUriId($uriId);
91
92                 $data = BBCode::getAttachmentData($item['body']);
93                 $card = new \Friendica\Object\Api\Mastodon\Card($data);
94
95                 $attachments = DI::mstdnAttachment()->createFromUriId($uriId);
96
97                 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
98                         $reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
99                         $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'], 'uid' => $uid]);
100                         $item['title'] = $reshared_item['title'] ?? $item['title'];
101                         $item['body'] = $reshared_item['body'] ?? $item['body'];
102                 } else {
103                         $reshare = [];
104                 }
105
106                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
107         }
108 }