*
*/
- /**
- * @see https://github.com/web-push-libs/web-push-php
- * Possibly we should simply use this.
- */
-
namespace Friendica\Model;
use Friendica\Core\Logger;
+use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
-use Friendica\Util\Crypto;
+use Minishlink\WebPush\VAPID;
class Subscription
{
}
/**
- * Fetch a VAPID key
+ * Fetch a VAPID keypair
*
- * @return string
+ * @return array
*/
- public static function getVapidKey(): string
+ private static function getKeyPair(): array
{
$keypair = DI::config()->get('system', 'ec_keypair');
- if (empty($keypair)) {
- $keypair = Crypto::newECKeypair();
+ if (empty($keypair['publicKey']) || empty($keypair['privateKey'])) {
+ $keypair = VAPID::createVapidKeys();
DI::config()->set('system', 'ec_keypair', $keypair);
}
- return $keypair['vapid-public'];
+ return $keypair;
+ }
+
+ /**
+ * Fetch the public VAPID key
+ *
+ * @return string
+ */
+ public static function getPublicVapidKey(): string
+ {
+ $keypair = self::getKeyPair();
+ return $keypair['publicKey'];
+ }
+
+ /**
+ * Fetch the public VAPID key
+ *
+ * @return string
+ */
+ public static function getPrivateVapidKey(): string
+ {
+ $keypair = self::getKeyPair();
+ return $keypair['privateKey'];
}
/**
* Prepare push notification
*
- * @param int $nid
- * @return void
+ * @param int $nid
+ * @return void
*/
public static function pushByNotificationId(int $nid)
{
$notification = DBA::selectFirst('notification', [], ['id' => $nid]);
+
$type = Notification::getType($notification);
+
+ $desktop_notification = !in_array($type, ['reblog', 'favourite']);
+
+ if (DI::pConfig()->get($notification['uid'], 'system', 'notify_like') && ($type == 'favourite')) {
+ $desktop_notification = true;
+ }
+
+ if (DI::pConfig()->get($notification['uid'], 'system', 'notify_announce') && ($type == 'reblog')) {
+ $desktop_notification = true;
+ }
+
+ if ($desktop_notification) {
+ notification_from_array($notification);
+ }
+
if (empty($type)) {
return;
}
$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]);
+ Worker::add(PRIORITY_HIGH, 'PushSubscription', $subscription['id'], $nid);
}
DBA::close($subscriptions);
}