]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/PushSubscription.php
Merge pull request #10600 from nupplaphil/bug/update_autoinstall_doc
[friendica.git] / src / Module / Api / Mastodon / PushSubscription.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\Module\Api\Mastodon;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\DI;
27 use Friendica\Model\Subscription;
28 use Friendica\Module\BaseApi;
29
30 /**
31  * @see https://docs.joinmastodon.org/methods/notifications/push/
32  */
33 class PushSubscription extends BaseApi
34 {
35         public static function post(array $parameters = [])
36         {
37                 self::checkAllowedScope(self::SCOPE_PUSH);
38                 $uid         = self::getCurrentUserID();
39                 $application = self::getCurrentApplication();
40
41                 $request = self::getRequest([
42                         'subscription' => [],
43                         'data'         => [],
44                 ]);
45
46                 $subscription = [
47                         'application-id' => $application['id'],
48                         'uid'            => $uid,
49                         'endpoint'       => $request['subscription']['endpoint'] ?? '',
50                         'pubkey'         => $request['subscription']['keys']['p256dh'] ?? '',
51                         'secret'         => $request['subscription']['keys']['auth'] ?? '',
52                         'follow'         => $request['data']['alerts']['follow'] ?? false,
53                         'favourite'      => $request['data']['alerts']['favourite'] ?? false,
54                         'reblog'         => $request['data']['alerts']['reblog'] ?? false,
55                         'mention'        => $request['data']['alerts']['mention'] ?? false,
56                         'poll'           => $request['data']['alerts']['poll'] ?? false,
57                         'follow_request' => $request['data']['alerts']['follow_request'] ?? false,
58                         'status'         => $request['data']['alerts']['status'] ?? false,
59                 ];
60
61                 $ret = Subscription::replace($subscription);
62
63                 Logger::info('Subscription stored', ['ret' => $ret, 'subscription' => $subscription]);
64
65                 return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray();
66         }
67
68         public static function put(array $parameters = [])
69         {
70                 self::checkAllowedScope(self::SCOPE_PUSH);
71                 $uid         = self::getCurrentUserID();
72                 $application = self::getCurrentApplication();
73
74                 $request = self::getRequest([
75                         'data' => [],
76                 ]);
77
78                 $subscription = Subscription::select($application['id'], $uid, ['id']);
79                 if (empty($subscription)) {
80                         Logger::info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
81                         DI::mstdnError()->RecordNotFound();
82                 }
83
84                 $fields = [
85                         'follow'         => $request['data']['alerts']['follow'] ?? false,
86                         'favourite'      => $request['data']['alerts']['favourite'] ?? false,
87                         'reblog'         => $request['data']['alerts']['reblog'] ?? false,
88                         'mention'        => $request['data']['alerts']['mention'] ?? false,
89                         'poll'           => $request['data']['alerts']['poll'] ?? false,
90                         'follow_request' => $request['data']['alerts']['follow_request'] ?? false,
91                         'status'         => $request['data']['alerts']['status'] ?? false,
92                 ];
93
94                 $ret = Subscription::update($application['id'], $uid, $fields);
95
96                 Logger::info('Subscription updated', ['result' => $ret, 'application-id' => $application['id'], 'uid' => $uid, 'fields' => $fields]);
97
98                 return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray();
99         }
100
101         public static function delete(array $parameters = [])
102         {
103                 self::checkAllowedScope(self::SCOPE_PUSH);
104                 $uid         = self::getCurrentUserID();
105                 $application = self::getCurrentApplication();
106
107                 $ret = Subscription::delete($application['id'], $uid);
108
109                 Logger::info('Subscription deleted', ['result' => $ret, 'application-id' => $application['id'], 'uid' => $uid]);
110
111                 System::jsonExit([]);
112         }
113
114         public static function rawContent(array $parameters = [])
115         {
116                 self::checkAllowedScope(self::SCOPE_PUSH);
117                 $uid         = self::getCurrentUserID();
118                 $application = self::getCurrentApplication();
119
120                 if (!Subscription::exists($application['id'], $uid)) {
121                         Logger::info('Subscription not found', ['application-id' => $application['id'], 'uid' => $uid]);
122                         DI::mstdnError()->RecordNotFound();
123                 }
124
125                 Logger::info('Fetch subscription', ['application-id' => $application['id'], 'uid' => $uid]);
126
127                 return DI::mstdnSubscription()->createForApplicationIdAndUserId($application['id'], $uid)->toArray();
128         }
129 }