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/>.
22 namespace Friendica\Worker;
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Content\Text\Plaintext;
26 use Friendica\Core\Logger;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Notification;
31 use Friendica\Model\Post;
32 use Friendica\Model\Subscription as ModelSubscription;
33 use Friendica\Model\User;
34 use Minishlink\WebPush\WebPush;
35 use Minishlink\WebPush\Subscription;
37 class PushSubscription
39 public static function execute(int $sid, int $nid)
41 Logger::info('Start', ['subscription' => $sid, 'notification' => $nid]);
43 $subscription = DBA::selectFirst('subscription', [], ['id' => $sid]);
44 if (empty($subscription)) {
45 Logger::info('Subscription not found', ['subscription' => $sid]);
49 $notification = DBA::selectFirst('notification', [], ['id' => $nid]);
50 if (empty($notification)) {
51 Logger::info('Notification not found', ['notification' => $nid]);
55 $application_token = DBA::selectFirst('application-token', [], ['application-id' => $subscription['application-id'], 'uid' => $subscription['uid']]);
56 if (empty($application_token)) {
57 Logger::info('Application token not found', ['application' => $subscription['application-id']]);
61 $user = User::getById($notification['uid']);
63 Logger::info('User not found', ['application' => $subscription['uid']]);
67 $l10n = DI::l10n()->withLang($user['language']);
69 $type = Notification::getType($notification);
71 if (!empty($notification['actor-id'])) {
72 $actor = Contact::getById($notification['actor-id']);
77 if (!empty($notification['target-uri-id'])) {
78 $post = Post::selectFirst([], ['uri-id' => $notification['target-uri-id'], 'uid' => [0, $notification['uid']]]);
79 if (!empty($post['body'])) {
80 $body = BBCode::toPlaintext($post['body'], false);
81 $body = Plaintext::shorten($body, 160, $notification['uid']);
85 $push = Subscription::create([
86 'contentEncoding' => 'aesgcm',
87 'endpoint' => $subscription['endpoint'],
89 'p256dh' => $subscription['pubkey'],
90 'auth' => $subscription['secret']
95 'access_token' => $application_token['access_token'],
96 'preferred_locale' => $user['language'],
97 'notification_id' => $nid,
98 'notification_type' => $type,
99 'icon' => $actor['thumb'] ?? '',
100 'title' => $l10n->t('Notification from Friendica'), // @todo Replace it with a speaking title
101 'body' => $body ?: $l10n->t('Empty Post'),
104 Logger::info('Payload', ['payload' => $payload]);
108 'subject' => DI::baseUrl()->getHostname(),
109 'publicKey' => ModelSubscription::getPublicVapidKey(),
110 'privateKey' => ModelSubscription::getPrivateVapidKey(),
114 $webPush = new WebPush($auth, [], DI::config()->get('system', 'xrd_timeout'));
116 $report = $webPush->sendOneNotification($push, json_encode($payload), ['urgency' => 'normal']);
118 $endpoint = $report->getRequest()->getUri()->__toString();
120 if ($report->isSuccess()) {
121 Logger::info('Message sent successfully for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint]);
123 Logger::info('Message failed to sent for subscription', ['subscription' => $sid, 'notification' => $nid, 'endpoint' => $endpoint, 'reason' => $report->getReason()]);