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