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\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;
36 * @see https://docs.joinmastodon.org/methods/notifications/push/
38 class PushSubscription extends BaseApi
40 /** @var SubscriptionFactory */
41 protected $subscriptionFac;
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 = [])
47 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
49 $this->subscriptionFac = $subscriptionFac;
50 $this->errorFac = $errorFac;
53 protected function post(array $request = []): void
55 self::checkAllowedScope(self::SCOPE_PUSH);
56 $uid = self::getCurrentUserID();
57 $application = self::getCurrentApplication();
59 $request = $this->getRequest([
65 'application-id' => $application['id'],
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),
79 $ret = Subscription::replace($subscription);
81 $this->logger->info('Subscription stored', ['ret' => $ret, 'subscription' => $subscription]);
83 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
84 $this->response->exitWithJson($subscriptionObj->toArray());
87 public function put(array $request = []): void
89 self::checkAllowedScope(self::SCOPE_PUSH);
90 $uid = self::getCurrentUserID();
91 $application = self::getCurrentApplication();
93 $request = $this->getRequest([
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();
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,
113 $ret = Subscription::update($application['id'], $uid, $fields);
115 $this->logger->info('Subscription updated', [
117 'application-id' => $application['id'],
122 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
123 $this->response->exitWithJson($subscriptionObj->toArray());
126 protected function delete(array $request = []): void
128 self::checkAllowedScope(self::SCOPE_PUSH);
129 $uid = self::getCurrentUserID();
130 $application = self::getCurrentApplication();
132 $ret = Subscription::delete($application['id'], $uid);
134 $this->logger->info('Subscription deleted', [
136 'application-id' => $application['id'],
140 $this->response->exitWithJson([]);
143 protected function rawContent(array $request = []): void
145 self::checkAllowedScope(self::SCOPE_PUSH);
146 $uid = self::getCurrentUserID();
147 $application = self::getCurrentApplication();
149 if (!Subscription::exists($application['id'], $uid)) {
150 $this->logger->info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
151 $this->errorFac->RecordNotFound();
154 $this->logger->info('Fetch subscription', ['application-id' => $application['id'], 'uid' => $uid]);
156 $subscriptionObj = $this->subscriptionFac->createForApplicationIdAndUserId($application['id'], $uid);
157 $this->response->exitWithJson($subscriptionObj->toArray());