3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon;
25 use Friendica\Core\L10n;
26 use Friendica\Factory\Api\Mastodon\Error;
27 use Friendica\Model\Subscription;
28 use Friendica\Module\Api\ApiResponse;
29 use Friendica\Module\BaseApi;
30 use Friendica\Object\Api\Mastodon\Notification;
31 use Friendica\Util\Profiler;
32 use Psr\Log\LoggerInterface;
35 * @see https://docs.joinmastodon.org/methods/notifications/push/
37 class PushSubscription extends BaseApi
39 /** @var \Friendica\Factory\Api\Mastodon\Subscription */
40 protected $subscriptionFac;
44 public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, \Friendica\Factory\Api\Mastodon\Subscription $subscriptionFac, Error $errorFac, array $server, array $parameters = [])
46 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48 $this->subscriptionFac = $subscriptionFac;
49 $this->errorFac = $errorFac;
52 protected function post(array $request = []): void
54 self::checkAllowedScope(self::SCOPE_PUSH);
55 $uid = self::getCurrentUserID();
56 $application = self::getCurrentApplication();
58 $request = $this->getRequest([
64 'application-id' => $application['id'],
66 'endpoint' => $request['subscription']['endpoint'] ?? '',
67 'pubkey' => $request['subscription']['keys']['p256dh'] ?? '',
68 'secret' => $request['subscription']['keys']['auth'] ?? '',
69 Notification::TYPE_FOLLOW => filter_var($request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false, FILTER_VALIDATE_BOOLEAN),
70 Notification::TYPE_LIKE => filter_var($request['data']['alerts'][Notification::TYPE_LIKE] ?? false, FILTER_VALIDATE_BOOLEAN),
71 Notification::TYPE_RESHARE => filter_var($request['data']['alerts'][Notification::TYPE_RESHARE] ?? false, FILTER_VALIDATE_BOOLEAN),
72 Notification::TYPE_MENTION => filter_var($request['data']['alerts'][Notification::TYPE_MENTION] ?? false, FILTER_VALIDATE_BOOLEAN),
73 Notification::TYPE_POLL => filter_var($request['data']['alerts'][Notification::TYPE_POLL] ?? false, FILTER_VALIDATE_BOOLEAN),
74 Notification::TYPE_INTRODUCTION => filter_var($request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false, FILTER_VALIDATE_BOOLEAN),
75 Notification::TYPE_POST => filter_var($request['data']['alerts'][Notification::TYPE_POST] ?? false, FILTER_VALIDATE_BOOLEAN),
78 $ret = Subscription::replace($subscription);
80 $this->logger->info('Subscription stored', ['ret' => $ret, 'subscription' => $subscription]);
82 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
83 $this->response->exitWithJson($subscriptionObj->toArray());
86 public function put(array $request = []): void
88 self::checkAllowedScope(self::SCOPE_PUSH);
89 $uid = self::getCurrentUserID();
90 $application = self::getCurrentApplication();
92 $request = $this->getRequest([
96 $subscription = Subscription::select($application['id'], $uid, ['id']);
97 if (empty($subscription)) {
98 $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
99 $this->errorFac->RecordNotFound();
103 Notification::TYPE_FOLLOW => $request['data']['alerts'][Notification::TYPE_FOLLOW] ?? false,
104 Notification::TYPE_LIKE => $request['data']['alerts'][Notification::TYPE_LIKE] ?? false,
105 Notification::TYPE_RESHARE => $request['data']['alerts'][Notification::TYPE_RESHARE] ?? false,
106 Notification::TYPE_MENTION => $request['data']['alerts'][Notification::TYPE_MENTION] ?? false,
107 Notification::TYPE_POLL => $request['data']['alerts'][Notification::TYPE_POLL] ?? false,
108 Notification::TYPE_INTRODUCTION => $request['data']['alerts'][Notification::TYPE_INTRODUCTION] ?? false,
109 Notification::TYPE_POST => $request['data']['alerts'][Notification::TYPE_POST] ?? false,
112 $ret = Subscription::update($application['id'], $uid, $fields);
114 $this->logger->info('Subscription updated', ['result' => $ret, 'application-id' => $application['id'],
115 'uid' => $uid, 'fields' => $fields]);
117 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
118 $this->response->exitWithJson($subscriptionObj->toArray());
121 protected function delete(array $request = []): void
123 self::checkAllowedScope(self::SCOPE_PUSH);
124 $uid = self::getCurrentUserID();
125 $application = self::getCurrentApplication();
127 $ret = Subscription::delete($application['id'], $uid);
129 $this->logger->info('Subscription deleted', ['result' => $ret, 'application-id' => $application['id'],
132 $this->response->exitWithJson([]);
135 protected function rawContent(array $request = []): void
137 self::checkAllowedScope(self::SCOPE_PUSH);
138 $uid = self::getCurrentUserID();
139 $application = self::getCurrentApplication();
141 if (!Subscription::exists($application['id'], $uid)) {
142 $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
143 $this->errorFac->RecordNotFound();
146 $this->logger->info('Fetch subscription', ['application-id' => $application['id'], 'uid' => $uid]);
148 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
149 $this->response->exitWithJson($subscriptionObj->toArray());