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