]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Status.php
Merge pull request #10396 from annando/public-timeline
[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\ContactSelector;
27 use Friendica\Content\Text\BBCode;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Post;
31 use Friendica\Model\Verb;
32 use Friendica\Network\HTTPException;
33 use Friendica\Protocol\Activity;
34 use Friendica\Protocol\ActivityPub;
35 use Friendica\Repository\ProfileField;
36 use Psr\Log\LoggerInterface;
37
38 class Status extends BaseFactory
39 {
40         /** @var BaseURL */
41         protected $baseUrl;
42         /** @var ProfileField */
43         protected $profileField;
44         /** @var Field */
45         protected $mstdnField;
46
47         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
48         {
49                 parent::__construct($logger);
50
51                 $this->baseUrl = $baseURL;
52                 $this->profileField = $profileField;
53                 $this->mstdnField = $mstdnField;
54         }
55
56         /**
57          * @param int $uriId Uri-ID of the item
58          * @param int $uid   Item user
59          * @return \Friendica\Object\Api\Mastodon\Status
60          * @throws HTTPException\InternalServerErrorException
61          * @throws \ImagickException
62          */
63         public function createFromUriId(int $uriId, $uid = 0)
64         {
65                 $fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
66                         'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
67                 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
68                 if (!$item) {
69                         throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . 'not found' . ($uid ? ' for user ' . $uid : '.'));
70                 }
71
72                 $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
73
74                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
75                         Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT, 'deleted' => false], []),
76                         Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE), 'deleted' => false], []),
77                         Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE), 'deleted' => false], [])
78                 );
79
80                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
81                         Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE), 'deleted' => false]),
82                         Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE), 'deleted' => false]),
83                         Post\ThreadUser::getIgnored($uriId, $uid),
84                         (bool)($item['starred'] && ($item['gravity'] == GRAVITY_PARENT)),
85                         Post\ThreadUser::getPinned($uriId, $uid)
86                 );
87
88                 $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
89                 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
90
91                 $mentions    = DI::mstdnMention()->createFromUriId($uriId);
92                 $tags        = DI::mstdnTag()->createFromUriId($uriId);
93                 $card        = DI::mstdnCard()->createFromUriId($uriId);
94                 $attachments = DI::mstdnAttachment()->createFromUriId($uriId);
95
96                 $shared = BBCode::fetchShareAttributes($item['body']);
97                 if (!empty($shared['guid'])) {
98                         $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
99
100                         $shared_uri_id = $shared_item['uri-id'] ?? 0;
101
102                         $mentions    = array_merge($mentions, DI::mstdnMention()->createFromUriId($shared_uri_id));
103                         $tags        = array_merge($tags, DI::mstdnTag()->createFromUriId($shared_uri_id));
104                         $attachments = array_merge($attachments, DI::mstdnAttachment()->createFromUriId($shared_uri_id));
105
106                         if (empty($card->toArray())) {
107                                 $card = DI::mstdnCard()->createFromUriId($shared_uri_id);
108                         }
109                 }
110
111
112                 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
113                         $reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
114                         $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]);
115                         $item['title'] = $reshared_item['title'] ?? $item['title'];
116                         $item['body'] = $reshared_item['body'] ?? $item['body'];
117                 } else {
118                         $reshare = [];
119                 }
120
121                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
122         }
123
124         /**
125          * @param int $uriId id of the mail
126          * @return \Friendica\Object\Api\Mastodon\Status
127          * @throws HTTPException\InternalServerErrorException
128          * @throws \ImagickException
129          */
130         public function createFromMailId(int $id)
131         {
132                 $item = ActivityPub\Transmitter::ItemArrayFromMail($id, true);
133                 if (empty($item)) {
134                         DI::mstdnError()->RecordNotFound();
135                 }
136
137                 $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
138
139                 $replies = DBA::count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
140
141                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
142
143                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
144
145                 $sensitive   = false;
146                 $application = new \Friendica\Object\Api\Mastodon\Application('');
147                 $mentions    = [];
148                 $tags        = [];
149                 $card        = new \Friendica\Object\Api\Mastodon\Card([]);
150                 $attachments = [];
151                 $reshare     = [];
152
153                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
154         }
155 }