friendica/src/Model/Subscription.php

160 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/**
2022-01-02 08:27:47 +01:00
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @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;
use Friendica\Core\Logger;
2021-08-15 22:52:46 +02:00
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
2022-06-23 16:03:55 +02:00
use Friendica\Factory\Api\Mastodon\Notification as NotificationFactory;
2021-09-18 06:03:32 +02:00
use Friendica\Navigation\Notifications\Entity;
use Friendica\Object\Api\Mastodon\Notification;
2021-08-15 23:54:24 +02:00
use Minishlink\WebPush\VAPID;
class Subscription
{
/**
2021-08-15 09:28:26 +02:00
* Select a subscription record exists
*
* @param int $applicationid
* @param int $uid
* @param array $fields
2022-06-23 10:56:37 +02:00
* @return array|bool Array on success, false on failure
2021-08-15 09:28:26 +02: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 10:56:37 +02:00
public static function exists(int $applicationid, int $uid): bool
2021-08-15 09:28:26 +02: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 10:56:37 +02:00
public static function update(int $applicationid, int $uid, array $fields): bool
2021-08-15 09:28:26 +02:00
{
return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
}
/**
* Insert or replace a subscription record
*
* @param array $fields subscription fields
* @return bool result of replace
*/
2022-06-23 10:56:37 +02:00
public static function replace(array $fields): bool
{
return DBA::replace('subscription', $fields);
}
/**
* Delete a subscription record
2021-08-15 09:28:26 +02:00
*
2021-08-15 02:43:07 +02:00
* @param int $applicationid
* @param int $uid
* @return bool
*/
2022-06-23 10:56:37 +02:00
public static function delete(int $applicationid, int $uid): bool
{
return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
}
/**
2021-08-15 23:24:23 +02:00
* Fetch a VAPID keypair
2021-08-15 09:28:26 +02:00
*
2021-08-15 23:24:23 +02:00
* @return array
*/
2021-08-15 23:24:23 +02:00
private static function getKeyPair(): array
{
$keypair = DI::config()->get('system', 'ec_keypair');
2021-08-15 23:54:24 +02:00
if (empty($keypair['publicKey']) || empty($keypair['privateKey'])) {
2021-08-16 00:09:32 +02:00
$keypair = VAPID::createVapidKeys();
DI::config()->set('system', 'ec_keypair', $keypair);
}
2021-08-15 23:24:23 +02:00
return $keypair;
}
/**
* Fetch the public VAPID key
*
* @return string
*/
public static function getPublicVapidKey(): string
{
$keypair = self::getKeyPair();
2021-08-15 23:54:24 +02:00
return $keypair['publicKey'];
}
2021-08-15 23:24:23 +02:00
/**
* Fetch the public VAPID key
*
* @return string
*/
public static function getPrivateVapidKey(): string
{
$keypair = self::getKeyPair();
2021-08-15 23:54:24 +02:00
return $keypair['privateKey'];
2021-08-15 23:24:23 +02:00
}
/**
* Prepare push notification
*
2022-06-23 16:03:55 +02:00
* @param Notification $Notification
2021-08-15 18:22:23 +02:00
* @return void
*/
2022-06-23 10:56:37 +02:00
public static function pushByNotification(Entity\Notification $notification)
{
2022-06-23 16:03:55 +02:00
$type = NotificationFactory::getType($notification);
2021-08-15 18:24:12 +02:00
2022-06-23 10:56:37 +02:00
if (DI::notify()->NotifyOnDesktop($notification, $type)) {
DI::notify()->createFromNotification($notification);
}
if (empty($type)) {
return;
}
2022-06-23 10:56:37 +02:00
$subscriptions = DBA::select('subscription', [], ['uid' => $notification->uid, $type => true]);
while ($subscription = DBA::fetch($subscriptions)) {
Logger::info('Push notification', ['id' => $subscription['id'], 'uid' => $subscription['uid'], 'type' => $type]);
2022-06-23 10:56:37 +02:00
Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $notification->id);
}
DBA::close($subscriptions);
}
}