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