]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Status.php
API: Speed improvements when fetching posts
[friendica.git] / src / Factory / Api / Mastodon / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\BaseFactory;
25 use Friendica\Content\ContactSelector;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Database\Database;
28 use Friendica\Database\DBA;
29 use Friendica\Model\Post;
30 use Friendica\Model\Tag as TagModel;
31 use Friendica\Model\Verb;
32 use Friendica\Network\HTTPException;
33 use Friendica\Protocol\Activity;
34 use Friendica\Protocol\ActivityPub;
35 use ImagickException;
36 use Psr\Log\LoggerInterface;
37
38 class Status extends BaseFactory
39 {
40         /** @var Database */
41         private $dba;
42         /** @var Account */
43         private $mstdnAccountFactory;
44         /** @var Mention */
45         private $mstdnMentionFactory;
46         /** @var Tag */
47         private $mstdnTagFactory;
48         /** @var Card */
49         private $mstdnCardFactory;
50         /** @var Attachment */
51         private $mstdnAttachementFactory;
52         /** @var Error */
53         private $mstdnErrorFactory;
54         /** @var Poll */
55         private $mstdnPollFactory;
56
57         public function __construct(LoggerInterface $logger, Database $dba,
58                 Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
59                 Tag $mstdnTagFactory, Card $mstdnCardFactory,
60                 Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory, Poll $mstdnPollFactory)
61         {
62                 parent::__construct($logger);
63                 $this->dba                     = $dba;
64                 $this->mstdnAccountFactory     = $mstdnAccountFactory;
65                 $this->mstdnMentionFactory     = $mstdnMentionFactory;
66                 $this->mstdnTagFactory         = $mstdnTagFactory;
67                 $this->mstdnCardFactory        = $mstdnCardFactory;
68                 $this->mstdnAttachementFactory = $mstdnAttachementFactory;
69                 $this->mstdnErrorFactory       = $mstdnErrorFactory;
70                 $this->mstdnPollFactory        = $mstdnPollFactory;
71         }
72
73         /**
74          * @param int $uriId Uri-ID of the item
75          * @param int $uid   Item user
76          *
77          * @return \Friendica\Object\Api\Mastodon\Status
78          * @throws HTTPException\InternalServerErrorException
79          * @throws ImagickException|HTTPException\NotFoundException
80          */
81         public function createFromUriId(int $uriId, $uid = 0, $item = []): \Friendica\Object\Api\Mastodon\Status
82         {
83                 $fields = ['uri-id', 'uid', 'author-id', 'author-uri-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id',
84                         'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media'];
85                 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
86                 if (!$item) {
87                         $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
88                         if ($mail) {
89                                 return $this->createFromMailId($mail['id']);
90                         }
91                         throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
92                 }
93                 $account = $this->mstdnAccountFactory->createFromUriId($item['author-uri-id'], $uid);
94
95                 $count_announce = Post::countPosts([
96                         'thr-parent-id' => $uriId,
97                         'gravity'       => GRAVITY_ACTIVITY,
98                         'vid'           => Verb::getID(Activity::ANNOUNCE),
99                         'deleted'       => false
100                 ], []);
101                 $count_like = Post::countPosts([
102                         'thr-parent-id' => $uriId,
103                         'gravity'       => GRAVITY_ACTIVITY,
104                         'vid'           => Verb::getID(Activity::LIKE),
105                         'deleted'       => false
106                 ], []);
107
108                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
109                         Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT, 'deleted' => false], []),
110                         $count_announce,
111                         $count_like
112                 );
113
114                 $origin_like = ($count_like == 0) ? false : Post::exists([
115                         'thr-parent-id' => $uriId,
116                         'uid'           => $uid,
117                         'origin'        => true,
118                         'gravity'       => GRAVITY_ACTIVITY,
119                         'vid'           => Verb::getID(Activity::LIKE),
120                         'deleted'     => false
121                 ]);
122                 $origin_announce = ($count_announce == 0) ? false : Post::exists([
123                         'thr-parent-id' => $uriId,
124                         'uid'           => $uid,
125                         'origin'        => true,
126                         'gravity'       => GRAVITY_ACTIVITY,
127                         'vid'           => Verb::getID(Activity::ANNOUNCE),
128                         'deleted'       => false
129                 ]);
130                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
131                         $origin_like,
132                         $origin_announce,
133                         Post\ThreadUser::getIgnored($uriId, $uid),
134                         (bool)($item['starred'] && ($item['gravity'] == GRAVITY_PARENT)),
135                         $item['featured']
136                 );
137
138                 $sensitive   = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw', 'type' => TagModel::HASHTAG]);
139                 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
140
141                 $mentions    = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
142                 $tags        = $this->mstdnTagFactory->createFromUriId($uriId);
143                 if ($item['has-media']) {
144                         $card        = $this->mstdnCardFactory->createFromUriId($uriId);
145                         $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
146                 } else {
147                         $card        = new \Friendica\Object\Api\Mastodon\Card([]);
148                         $attachments = [];
149                 }
150
151                 if (!empty($item['question-id'])) {
152                         $poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
153                 } else {
154                         $poll = null;
155                 }
156
157                 $shared = BBCode::fetchShareAttributes($item['body']);
158                 if (!empty($shared['guid'])) {
159                         $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
160
161                         $shared_uri_id = $shared_item['uri-id'] ?? 0;
162
163                         $mentions    = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
164                         $tags        = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
165                         $attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id));
166
167                         if (empty($card->toArray())) {
168                                 $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
169                         }
170                 }
171
172                 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
173                         $reshare       = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
174                         $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
175                         $item['title'] = $reshared_item['title'] ?? $item['title'];
176                         $item['body']  = $reshared_item['body'] ?? $item['body'];
177                 } else {
178                         $reshare = [];
179                 }
180
181                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare, $poll);
182         }
183
184         /**
185          * @param int $uriId id of the mail
186          *
187          * @return \Friendica\Object\Api\Mastodon\Status
188          * @throws HTTPException\InternalServerErrorException
189          * @throws ImagickException|HTTPException\NotFoundException
190          */
191         public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
192         {
193                 $item = ActivityPub\Transmitter::ItemArrayFromMail($id, true);
194                 if (empty($item)) {
195                         $this->mstdnErrorFactory->RecordNotFound();
196                 }
197
198                 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
199
200                 $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
201
202                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
203
204                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
205
206                 $sensitive   = false;
207                 $application = new \Friendica\Object\Api\Mastodon\Application('');
208                 $mentions    = [];
209                 $tags        = [];
210                 $card        = new \Friendica\Object\Api\Mastodon\Card([]);
211                 $attachments = [];
212                 $reshare     = [];
213
214                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
215         }
216 }