]> git.mxchange.org Git - friendica.git/blob - src/Model/Subscription.php
Merge pull request #10969 from MrPetovan/task/remove-private-contacts
[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 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          *
41          * @return bool Does it exist?
42          */
43         public static function select(int $applicationid, int $uid, array $fields = [])
44         {
45                 return DBA::selectFirst('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
46         }
47
48         /**
49          * Check if a subscription record exists
50          *
51          * @param int   $applicationid
52          * @param int   $uid
53          *
54          * @return bool Does it exist?
55          */
56         public static function exists(int $applicationid, int $uid)
57         {
58                 return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
59         }
60
61         /**
62          * Update a subscription record
63          *
64          * @param int   $applicationid
65          * @param int   $uid
66          * @param array $fields subscription fields
67          *
68          * @return bool result of update
69          */
70         public static function update(int $applicationid, int $uid, array $fields)
71         {
72                 return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
73         }
74
75         /**
76          * Insert or replace a subscription record
77          *
78          * @param array $fields subscription fields
79          *
80          * @return bool result of replace
81          */
82         public static function replace(array $fields)
83         {
84                 return DBA::replace('subscription', $fields);
85         }
86
87         /**
88          * Delete a subscription record
89          *
90          * @param int $applicationid
91          * @param int $uid
92          * @return bool
93          */
94         public static function delete(int $applicationid, int $uid)
95         {
96                 return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
97         }
98
99         /**
100          * Fetch a VAPID keypair
101          *
102          * @return array
103          */
104         private static function getKeyPair(): array
105         {
106                 $keypair = DI::config()->get('system', 'ec_keypair');
107                 if (empty($keypair['publicKey']) || empty($keypair['privateKey'])) {
108                         $keypair = VAPID::createVapidKeys();
109                         DI::config()->set('system', 'ec_keypair', $keypair);
110                 }
111                 return $keypair;
112         }
113
114         /**
115          * Fetch the public VAPID key
116          *
117          * @return string
118          */
119         public static function getPublicVapidKey(): string
120         {
121                 $keypair = self::getKeyPair();
122                 return $keypair['publicKey'];
123         }
124
125         /**
126          * Fetch the public VAPID key
127          *
128          * @return string
129          */
130         public static function getPrivateVapidKey(): string
131         {
132                 $keypair = self::getKeyPair();
133                 return $keypair['privateKey'];
134         }
135
136         /**
137          * Prepare push notification
138          *
139          * @param int $nid
140          * @return void
141          */
142         public static function pushByNotification(Entity\Notification $Notification)
143         {
144                 $type = \Friendica\Factory\Api\Mastodon\Notification::getType($Notification);
145
146                 $desktop_notification = !in_array($type, [Notification::TYPE_RESHARE, Notification::TYPE_LIKE]);
147
148                 if (DI::pConfig()->get($Notification->uid, 'system', 'notify_like') && ($type == Notification::TYPE_LIKE)) {
149                         $desktop_notification = true;
150                 }
151
152                 if (DI::pConfig()->get($Notification->uid, 'system', 'notify_announce') && ($type == Notification::TYPE_RESHARE)) {
153                         $desktop_notification = true;
154                 }
155
156                 if ($desktop_notification) {
157                         DI::notify()->createFromNotification($Notification);
158                 }
159
160                 if (empty($type)) {
161                         return;
162                 }
163
164                 $subscriptions = DBA::select('subscription', [], ['uid' => $Notification->uid, $type => true]);
165                 while ($subscription = DBA::fetch($subscriptions)) {
166                         Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
167                         Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $Notification->id);
168                 }
169                 DBA::close($subscriptions);
170         }
171 }