]> git.mxchange.org Git - friendica.git/blob - src/Model/Subscription.php
dd81ee2d636fe03dbf77ff60dacaf338bfe20cee
[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 key
104          *
105          * @return string
106          */
107         public static function getVapidKey(): string
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['vapid-public'];
115         }
116
117         /**
118          * Prepare push notification
119          *
120          * @param int $nid
121          * @return void
122          */
123         public static function pushByNotificationId(int $nid)
124         {
125                 $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
126
127                 $type = Notification::getType($notification);
128                 if (empty($type)) {
129                         return;
130                 }
131
132                 $subscriptions = DBA::select('subscription', [], ['uid' => $notification['uid'], $type => true]);
133                 while ($subscription = DBA::fetch($subscriptions)) {
134                         Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
135                         Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id']);
136                 }
137                 DBA::close($subscriptions);
138         }
139 }