2021-08-14 20:30:41 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2021-08-14 20:30:41 -04:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2021-08-15 12:18:25 -04:00
|
|
|
use Friendica\Core\Logger;
|
2021-08-15 16:52:46 -04:00
|
|
|
use Friendica\Core\Worker;
|
2021-08-14 20:30:41 -04:00
|
|
|
use Friendica\Database\DBA;
|
|
|
|
use Friendica\DI;
|
2022-06-23 10:03:55 -04:00
|
|
|
use Friendica\Factory\Api\Mastodon\Notification as NotificationFactory;
|
2021-09-18 00:03:32 -04:00
|
|
|
use Friendica\Navigation\Notifications\Entity;
|
2021-09-17 23:15:23 -04:00
|
|
|
use Friendica\Object\Api\Mastodon\Notification;
|
2021-08-15 17:54:24 -04:00
|
|
|
use Minishlink\WebPush\VAPID;
|
2021-08-14 20:30:41 -04:00
|
|
|
|
|
|
|
class Subscription
|
|
|
|
{
|
|
|
|
/**
|
2021-08-15 03:28:26 -04:00
|
|
|
* Select a subscription record exists
|
|
|
|
*
|
|
|
|
* @param int $applicationid
|
|
|
|
* @param int $uid
|
|
|
|
* @param array $fields
|
2022-06-23 04:56:37 -04:00
|
|
|
* @return array|bool Array on success, false on failure
|
2021-08-15 03:28:26 -04:00
|
|
|
*/
|
|
|
|
public static function select(int $applicationid, int $uid, array $fields = [])
|
|
|
|
{
|
|
|
|
return DBA::selectFirst('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a subscription record exists
|
|
|
|
*
|
|
|
|
* @param int $applicationid
|
|
|
|
* @param int $uid
|
|
|
|
*
|
|
|
|
* @return bool Does it exist?
|
|
|
|
*/
|
2022-06-23 04:56:37 -04:00
|
|
|
public static function exists(int $applicationid, int $uid): bool
|
2021-08-15 03:28:26 -04:00
|
|
|
{
|
|
|
|
return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a subscription record
|
|
|
|
*
|
|
|
|
* @param int $applicationid
|
|
|
|
* @param int $uid
|
|
|
|
* @param array $fields subscription fields
|
|
|
|
* @return bool result of update
|
|
|
|
*/
|
2022-06-23 04:56:37 -04:00
|
|
|
public static function update(int $applicationid, int $uid, array $fields): bool
|
2021-08-15 03:28:26 -04:00
|
|
|
{
|
|
|
|
return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert or replace a subscription record
|
2021-08-14 20:30:41 -04:00
|
|
|
*
|
|
|
|
* @param array $fields subscription fields
|
|
|
|
* @return bool result of replace
|
|
|
|
*/
|
2022-06-23 04:56:37 -04:00
|
|
|
public static function replace(array $fields): bool
|
2021-08-14 20:30:41 -04:00
|
|
|
{
|
|
|
|
return DBA::replace('subscription', $fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a subscription record
|
2021-08-15 03:28:26 -04:00
|
|
|
*
|
2021-08-14 20:43:07 -04:00
|
|
|
* @param int $applicationid
|
|
|
|
* @param int $uid
|
|
|
|
* @return bool
|
2021-08-14 20:30:41 -04:00
|
|
|
*/
|
2022-06-23 04:56:37 -04:00
|
|
|
public static function delete(int $applicationid, int $uid): bool
|
2021-08-14 20:30:41 -04:00
|
|
|
{
|
|
|
|
return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-15 17:24:23 -04:00
|
|
|
* Fetch a VAPID keypair
|
2021-08-15 03:28:26 -04:00
|
|
|
*
|
2021-08-15 17:24:23 -04:00
|
|
|
* @return array
|
2021-08-14 20:30:41 -04:00
|
|
|
*/
|
2021-08-15 17:24:23 -04:00
|
|
|
private static function getKeyPair(): array
|
2021-08-14 20:30:41 -04:00
|
|
|
{
|
|
|
|
$keypair = DI::config()->get('system', 'ec_keypair');
|
2021-08-15 17:54:24 -04:00
|
|
|
if (empty($keypair['publicKey']) || empty($keypair['privateKey'])) {
|
2021-08-15 18:09:32 -04:00
|
|
|
$keypair = VAPID::createVapidKeys();
|
2021-08-14 20:30:41 -04:00
|
|
|
DI::config()->set('system', 'ec_keypair', $keypair);
|
|
|
|
}
|
2021-08-15 17:24:23 -04:00
|
|
|
return $keypair;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the public VAPID key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getPublicVapidKey(): string
|
|
|
|
{
|
|
|
|
$keypair = self::getKeyPair();
|
2021-08-15 17:54:24 -04:00
|
|
|
return $keypair['publicKey'];
|
2021-08-14 20:30:41 -04:00
|
|
|
}
|
2021-08-15 12:18:25 -04:00
|
|
|
|
2021-08-15 17:24:23 -04:00
|
|
|
/**
|
|
|
|
* Fetch the public VAPID key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getPrivateVapidKey(): string
|
|
|
|
{
|
|
|
|
$keypair = self::getKeyPair();
|
2021-08-15 17:54:24 -04:00
|
|
|
return $keypair['privateKey'];
|
2021-08-15 17:24:23 -04:00
|
|
|
}
|
|
|
|
|
2021-08-15 12:18:25 -04:00
|
|
|
/**
|
|
|
|
* Prepare push notification
|
|
|
|
*
|
2022-06-23 10:03:55 -04:00
|
|
|
* @param Notification $Notification
|
2021-08-15 12:22:23 -04:00
|
|
|
* @return void
|
2021-08-15 12:18:25 -04:00
|
|
|
*/
|
2022-06-23 04:56:37 -04:00
|
|
|
public static function pushByNotification(Entity\Notification $notification)
|
2021-08-15 12:18:25 -04:00
|
|
|
{
|
2022-06-23 10:03:55 -04:00
|
|
|
$type = NotificationFactory::getType($notification);
|
2021-08-15 12:24:12 -04:00
|
|
|
|
2022-11-02 20:28:53 -04:00
|
|
|
if (DI::notify()->shouldShowOnDesktop($notification, $type)) {
|
2022-06-23 04:56:37 -04:00
|
|
|
DI::notify()->createFromNotification($notification);
|
2021-08-21 16:35:04 -04:00
|
|
|
}
|
|
|
|
|
2021-08-15 12:18:25 -04:00
|
|
|
if (empty($type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-23 04:56:37 -04:00
|
|
|
$subscriptions = DBA::select('subscription', [], ['uid' => $notification->uid, $type => true]);
|
2021-08-15 12:18:25 -04:00
|
|
|
while ($subscription = DBA::fetch($subscriptions)) {
|
|
|
|
Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
|
2022-10-17 01:49:55 -04:00
|
|
|
Worker::add(Worker::PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $notification->id);
|
2021-08-15 12:18:25 -04:00
|
|
|
}
|
|
|
|
DBA::close($subscriptions);
|
|
|
|
}
|
2021-08-14 20:30:41 -04:00
|
|
|
}
|