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