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