]> git.mxchange.org Git - friendica.git/blob - src/Model/Subscription.php
e5305378e35945f8148efd160ae659b81d49ec44
[friendica.git] / src / Model / Subscription.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\Model;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Navigation\Notifications\Entity;
29 use Friendica\Object\Api\Mastodon\Notification;
30 use Minishlink\WebPush\VAPID;
31
32 class Subscription
33 {
34         /**
35          * Select a subscription record exists
36          *
37          * @param int   $applicationid
38          * @param int   $uid
39          * @param array $fields
40          * @return array|bool Array on success, false on failure
41          */
42         public static function select(int $applicationid, int $uid, array $fields = [])
43         {
44                 return DBA::selectFirst('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
45         }
46
47         /**
48          * Check if a subscription record exists
49          *
50          * @param int   $applicationid
51          * @param int   $uid
52          *
53          * @return bool Does it exist?
54          */
55         public static function exists(int $applicationid, int $uid): bool
56         {
57                 return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
58         }
59
60         /**
61          * Update a subscription record
62          *
63          * @param int   $applicationid
64          * @param int   $uid
65          * @param array $fields subscription fields
66          * @return bool result of update
67          */
68         public static function update(int $applicationid, int $uid, array $fields): bool
69         {
70                 return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
71         }
72
73         /**
74          * Insert or replace a subscription record
75          *
76          * @param array $fields subscription fields
77          * @return bool result of replace
78          */
79         public static function replace(array $fields): bool
80         {
81                 return DBA::replace('subscription', $fields);
82         }
83
84         /**
85          * Delete a subscription record
86          *
87          * @param int $applicationid
88          * @param int $uid
89          * @return bool
90          */
91         public static function delete(int $applicationid, int $uid): bool
92         {
93                 return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
94         }
95
96         /**
97          * Fetch a VAPID keypair
98          *
99          * @return array
100          */
101         private static function getKeyPair(): array
102         {
103                 $keypair = DI::config()->get('system', 'ec_keypair');
104                 if (empty($keypair['publicKey']) || empty($keypair['privateKey'])) {
105                         $keypair = VAPID::createVapidKeys();
106                         DI::config()->set('system', 'ec_keypair', $keypair);
107                 }
108                 return $keypair;
109         }
110
111         /**
112          * Fetch the public VAPID key
113          *
114          * @return string
115          */
116         public static function getPublicVapidKey(): string
117         {
118                 $keypair = self::getKeyPair();
119                 return $keypair['publicKey'];
120         }
121
122         /**
123          * Fetch the public VAPID key
124          *
125          * @return string
126          */
127         public static function getPrivateVapidKey(): string
128         {
129                 $keypair = self::getKeyPair();
130                 return $keypair['privateKey'];
131         }
132
133         /**
134          * Prepare push notification
135          *
136          * @param Notification $Notification Notification instance
137          * @return void
138          */
139         public static function pushByNotification(Entity\Notification $notification)
140         {
141                 $type = \Friendica\Factory\Api\Mastodon\Notification::getType($notification);
142
143                 if (DI::notify()->NotifyOnDesktop($notification, $type)) {
144                         DI::notify()->createFromNotification($notification);
145                 }
146
147                 if (empty($type)) {
148                         return;
149                 }
150
151                 $subscriptions = DBA::select('subscription', [], ['uid' => $notification->uid, $type => true]);
152                 while ($subscription = DBA::fetch($subscriptions)) {
153                         Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
154                         Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $notification->id);
155                 }
156                 DBA::close($subscriptions);
157         }
158 }