]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/PushSubscription.php
Merge pull request #13176 from MrPetovan/bug/warnings
[friendica.git] / src / Module / Api / Mastodon / PushSubscription.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\Api\Mastodon;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Factory\Api\Mastodon\Error;
27 use Friendica\Factory\Api\Mastodon\Subscription as SubscriptionFactory;
28 use Friendica\Model\Subscription;
29 use Friendica\Module\Api\ApiResponse;
30 use Friendica\Module\BaseApi;
31 use Friendica\Object\Api\Mastodon\Notification;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
34
35 /**
36  * @see https://docs.joinmastodon.org/methods/notifications/push/
37  */
38 class PushSubscription extends BaseApi
39 {
40         /** @var SubscriptionFactory */
41         protected $subscriptionFac;
42         /** @var Error */
43         protected $errorFac;
44
45         public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, SubscriptionFactory $subscriptionFac, Error $errorFac, array $server, array $parameters = [])
46         {
47                 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48
49                 $this->subscriptionFac = $subscriptionFac;
50                 $this->errorFac        = $errorFac;
51         }
52
53         protected function post(array $request = []): void
54         {
55                 self::checkAllowedScope(self::SCOPE_PUSH);
56                 $uid         = self::getCurrentUserID();
57                 $application = self::getCurrentApplication();
58
59                 $request = $this->getRequest([
60                         'subscription' => [],
61                         'data'         => [],
62                 ], $request);
63
64                 $subscription = [
65                         'application-id'                => $application['id'],
66                         'uid'                           => $uid,
67                         'endpoint'                      => $request['subscription']['endpoint'] ?? '',
68                         'pubkey'                        => $request['subscription']['keys']['p256dh'] ?? '',
69                         'secret'                        => $request['subscription']['keys']['auth'] ?? '',
70                         Notification::TYPE_FOLLOW       => filter_var($request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false, FILTER_VALIDATE_BOOLEAN),
71                         Notification::TYPE_LIKE         => filter_var($request['data']['alerts'][Notification::TYPE_LIKE] ?? false, FILTER_VALIDATE_BOOLEAN),
72                         Notification::TYPE_RESHARE      => filter_var($request['data']['alerts'][Notification::TYPE_RESHARE] ?? false, FILTER_VALIDATE_BOOLEAN),
73                         Notification::TYPE_MENTION      => filter_var($request['data']['alerts'][Notification::TYPE_MENTION] ?? false, FILTER_VALIDATE_BOOLEAN),
74                         Notification::TYPE_POLL         => filter_var($request['data']['alerts'][Notification::TYPE_POLL] ?? false, FILTER_VALIDATE_BOOLEAN),
75                         Notification::TYPE_INTRODUCTION => filter_var($request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false, FILTER_VALIDATE_BOOLEAN),
76                         Notification::TYPE_POST         => filter_var($request['data']['alerts'][Notification::TYPE_POST] ?? false, FILTER_VALIDATE_BOOLEAN),
77                 ];
78
79                 $ret = Subscription::replace($subscription);
80
81                 $this->logger->info('Subscription stored', ['ret' => $ret, 'subscription' => $subscription]);
82
83                 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
84                 $this->response->exitWithJson($subscriptionObj->toArray());
85         }
86
87         public function put(array $request = []): void
88         {
89                 self::checkAllowedScope(self::SCOPE_PUSH);
90                 $uid         = self::getCurrentUserID();
91                 $application = self::getCurrentApplication();
92
93                 $request = $this->getRequest([
94                         'data' => [],
95                 ], $request);
96
97                 $subscription = Subscription::select($application['id'], $uid, ['id']);
98                 if (empty($subscription)) {
99                         $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
100                         $this->errorFac->RecordNotFound();
101                 }
102
103                 $fields = [
104                         Notification::TYPE_FOLLOW       => $request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false,
105                         Notification::TYPE_LIKE         => $request['data']['alerts'][Notification::TYPE_LIKE] ?? false,
106                         Notification::TYPE_RESHARE      => $request['data']['alerts'][Notification::TYPE_RESHARE] ?? false,
107                         Notification::TYPE_MENTION      => $request['data']['alerts'][Notification::TYPE_MENTION] ?? false,
108                         Notification::TYPE_POLL         => $request['data']['alerts'][Notification::TYPE_POLL] ?? false,
109                         Notification::TYPE_INTRODUCTION => $request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false,
110                         Notification::TYPE_POST         => $request['data']['alerts'][Notification::TYPE_POST] ?? false,
111                 ];
112
113                 $ret = Subscription::update($application['id'], $uid, $fields);
114
115                 $this->logger->info('Subscription updated', [
116                         'result'         => $ret,
117                         'application-id' => $application['id'],
118                         'uid'            => $uid,
119                         'fields'         => $fields,
120                 ]);
121
122                 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
123                 $this->response->exitWithJson($subscriptionObj->toArray());
124         }
125
126         protected function delete(array $request = []): void
127         {
128                 self::checkAllowedScope(self::SCOPE_PUSH);
129                 $uid         = self::getCurrentUserID();
130                 $application = self::getCurrentApplication();
131
132                 $ret = Subscription::delete($application['id'], $uid);
133
134                 $this->logger->info('Subscription deleted', [
135                         'result'         => $ret,
136                         'application-id' => $application['id'],
137                         'uid'            => $uid,
138                 ]);
139
140                 $this->response->exitWithJson([]);
141         }
142
143         protected function rawContent(array $request = []): void
144         {
145                 self::checkAllowedScope(self::SCOPE_PUSH);
146                 $uid         = self::getCurrentUserID();
147                 $application = self::getCurrentApplication();
148
149                 if (!Subscription::exists($application['id'], $uid)) {
150                         $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
151                         $this->errorFac->RecordNotFound();
152                 }
153
154                 $this->logger->info('Fetch subscription', ['application-id' => $application['id'], 'uid' => $uid]);
155
156                 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
157                 $this->response->exitWithJson($subscriptionObj->toArray());
158         }
159 }