<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\DI;
+use Friendica\Factory\Api\Mastodon\Notification as NotificationFactory;
use Friendica\Model\Contact;
-use Friendica\Model\Notification;
use Friendica\Model\Post;
use Friendica\Model\Subscription as ModelSubscription;
use Friendica\Model\User;
+use Friendica\Navigation\Notifications;
+use Friendica\Network\HTTPException\NotFoundException;
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;
class PushSubscription
{
+ /**
+ * Creates push subscription by subscription and notification ids
+ *
+ * @param int $sid Subscription id
+ * @param int $nid Notification id
+ * @return void
+ */
public static function execute(int $sid, int $nid)
{
Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
return;
}
- $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
- if (empty($notification)) {
+ try {
+ $notification = DI::notification()->selectOneById($nid);
+ } catch (NotFoundException $e) {
Logger::info('Notification not found', ['notification' => $nid]);
return;
}
return;
}
- $user = User::getById($notification['uid']);
+ $user = User::getById($notification->uid);
if (empty($user)) {
Logger::info('User not found', ['application' => $subscription['uid']]);
return;
$l10n = DI::l10n()->withLang($user['language']);
- $type = Notification::getType($notification);
-
- if (!empty($notification['actor-id'])) {
- $actor = Contact::getById($notification['actor-id']);
+ if ($notification->actorId) {
+ $actor = Contact::getById($notification->actorId);
}
$body = '';
- if (!empty($notification['target-uri-id'])) {
- $post = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]]);
+ if ($notification->targetUriId) {
+ $post = Post::selectFirst([], ['uri-id' => $notification->targetUriId, 'uid' => [0, $notification->uid]]);
if (!empty($post['body'])) {
$body = BBCode::toPlaintext($post['body'], false);
- $body = Plaintext::shorten($body, 160, $notification['uid']);
+ $body = Plaintext::shorten($body, 160, $notification->uid);
}
}
- // @todo Add a meaningful title here, see the functionality in enotify.php
- $title = '';
+ $message = DI::notificationFactory()->getMessageFromNotification($notification);
+ $title = $message['plain'] ?: '';
$push = Subscription::create([
'contentEncoding' => 'aesgcm',
'access_token' => $application_token['access_token'],
'preferred_locale' => $user['language'],
'notification_id' => $nid,
- 'notification_type' => $type,
+ 'notification_type' => NotificationFactory::getType($notification),
'icon' => $actor['thumb'] ?? '',
'title' => $title ?: $l10n->t('Notification from Friendica'),
'body' => $body ?: $l10n->t('Empty Post'),