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