]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Status.php
Merge branch 'develop' into new_image_presentation
[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', 'causer-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                 if (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($item['vid'] == Verb::getID(Activity::ANNOUNCE))) {
101                         $is_reshare = true;
102                         $account    = $this->mstdnAccountFactory->createFromUriId($item['author-uri-id'], $uid);
103                         $uriId      = $item['thr-parent-id'];
104                         $item       = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
105                         if (!$item) {
106                                 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
107                         }
108                 } else {
109                         $is_reshare = $reblog && !is_null($item['causer-uri-id']) && ($item['causer-id'] != $item['author-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT);
110                         $account    = $this->mstdnAccountFactory->createFromUriId($is_reshare ? $item['causer-uri-id'] : $item['author-uri-id'], $uid);
111                 }
112
113                 $count_announce = Post::countPosts([
114                         'thr-parent-id' => $uriId,
115                         'gravity'       => Item::GRAVITY_ACTIVITY,
116                         'vid'           => Verb::getID(Activity::ANNOUNCE),
117                         'deleted'       => false
118                 ], []);
119                 $count_like = Post::countPosts([
120                         'thr-parent-id' => $uriId,
121                         'gravity'       => Item::GRAVITY_ACTIVITY,
122                         'vid'           => Verb::getID(Activity::LIKE),
123                         'deleted'       => false
124                 ], []);
125
126                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
127                         Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_COMMENT, 'deleted' => false], []),
128                         $count_announce,
129                         $count_like
130                 );
131
132                 $origin_like = ($count_like == 0) ? false : Post::exists([
133                         'thr-parent-id' => $uriId,
134                         'uid'           => $uid,
135                         'origin'        => true,
136                         'gravity'       => Item::GRAVITY_ACTIVITY,
137                         'vid'           => Verb::getID(Activity::LIKE),
138                         'deleted'     => false
139                 ]);
140                 $origin_announce = ($count_announce == 0) ? false : Post::exists([
141                         'thr-parent-id' => $uriId,
142                         'uid'           => $uid,
143                         'origin'        => true,
144                         'gravity'       => Item::GRAVITY_ACTIVITY,
145                         'vid'           => Verb::getID(Activity::ANNOUNCE),
146                         'deleted'       => false
147                 ]);
148                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
149                         $origin_like,
150                         $origin_announce,
151                         Post\ThreadUser::getIgnored($uriId, $uid),
152                         (bool)($item['starred'] && ($item['gravity'] == Item::GRAVITY_PARENT)),
153                         $item['featured']
154                 );
155
156                 $sensitive   = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw', 'type' => TagModel::HASHTAG]);
157                 $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
158
159                 $mentions    = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
160                 $tags        = $this->mstdnTagFactory->createFromUriId($uriId);
161                 if ($item['has-media']) {
162                         $card        = $this->mstdnCardFactory->createFromUriId($uriId);
163                         $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
164                 } else {
165                         $card        = new \Friendica\Object\Api\Mastodon\Card([]);
166                         $attachments = [];
167                 }
168
169                 if (!empty($item['question-id'])) {
170                         $poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
171                 } else {
172                         $poll = null;
173                 }
174
175                 $shared = $this->contentItem->getSharedPost($item, ['uri-id']);
176                 if (!empty($shared)) {
177                         $shared_uri_id = $shared['post']['uri-id'];
178
179                         foreach ($this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy() as $mention) {
180                                 if (!in_array($mention, $mentions)) {
181                                         $mentions[] = $mention;
182                                 }
183                         }
184
185                         foreach ($this->mstdnTagFactory->createFromUriId($shared_uri_id) as $tag) {
186                                 if (!in_array($tag, $tags)) {
187                                         $tags[] = $tag;
188                                 }
189                         }
190
191                         foreach ($this->mstdnAttachementFactory->createFromUriId($shared_uri_id) as $attachment) {
192                                 if (!in_array($attachment, $attachments)) {
193                                         $attachments[] = $attachment;
194                                 }
195                         }
196
197                         if (empty($card->toArray())) {
198                                 $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
199                         }
200                 }
201
202                 $item['body']     = $this->contentItem->addSharedPost($item);
203                 $item['raw-body'] = $this->contentItem->addSharedPost($item, $item['raw-body']);
204
205                 if ($is_reshare) {
206                         $reshare = $this->createFromUriId($uriId, $uid, false)->toArray();
207                 } else {
208                         $reshare = [];
209                 }
210
211                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare, $poll);
212         }
213
214         /**
215          * @param int $uriId id of the mail
216          *
217          * @return \Friendica\Object\Api\Mastodon\Status
218          * @throws HTTPException\InternalServerErrorException
219          * @throws ImagickException|HTTPException\NotFoundException
220          */
221         public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
222         {
223                 $item = ActivityPub\Transmitter::getItemArrayFromMail($id, true);
224                 if (empty($item)) {
225                         $this->mstdnErrorFactory->RecordNotFound();
226                 }
227
228                 $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
229
230                 $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
231
232                 $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
233
234                 $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
235
236                 $sensitive   = false;
237                 $application = new \Friendica\Object\Api\Mastodon\Application('');
238                 $mentions    = [];
239                 $tags        = [];
240                 $card        = new \Friendica\Object\Api\Mastodon\Card([]);
241                 $attachments = [];
242                 $reshare     = [];
243
244                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
245         }
246 }