3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
23 * @see https://github.com/web-push-libs/web-push-php
24 * Possibly we should simply use this.
27 namespace Friendica\Model;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Worker;
31 use Friendica\Database\DBA;
33 use Friendica\Util\Crypto;
38 * Select a subscription record exists
40 * @param int $applicationid
42 * @param array $fields
44 * @return bool Does it exist?
46 public static function select(int $applicationid, int $uid, array $fields = [])
48 return DBA::selectFirst('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
52 * Check if a subscription record exists
54 * @param int $applicationid
57 * @return bool Does it exist?
59 public static function exists(int $applicationid, int $uid)
61 return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
65 * Update a subscription record
67 * @param int $applicationid
69 * @param array $fields subscription fields
71 * @return bool result of update
73 public static function update(int $applicationid, int $uid, array $fields)
75 return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
79 * Insert or replace a subscription record
81 * @param array $fields subscription fields
83 * @return bool result of replace
85 public static function replace(array $fields)
87 return DBA::replace('subscription', $fields);
91 * Delete a subscription record
93 * @param int $applicationid
97 public static function delete(int $applicationid, int $uid)
99 return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
103 * Fetch a VAPID keypair
107 private static function getKeyPair(): array
109 $keypair = DI::config()->get('system', 'ec_keypair');
110 if (empty($keypair)) {
111 $keypair = Crypto::newECKeypair();
112 DI::config()->set('system', 'ec_keypair', $keypair);
118 * Fetch the public VAPID key
122 public static function getPublicVapidKey(): string
124 $keypair = self::getKeyPair();
125 return $keypair['vapid-public'];
129 * Fetch the public VAPID key
133 public static function getPrivateVapidKey(): string
135 $keypair = self::getKeyPair();
136 return $keypair['vapid-private'];
140 * Prepare push notification
145 public static function pushByNotificationId(int $nid)
147 $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
149 $type = Notification::getType($notification);
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']);
159 DBA::close($subscriptions);