2021-11-27 13:12:44 +09:00
|
|
|
<?php
|
|
|
|
|
2021-12-23 22:18:50 +09:00
|
|
|
declare(strict_types = 1);
|
2021-11-27 13:12:44 +09:00
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Component\Notification;
|
|
|
|
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2021-11-27 13:12:44 +09:00
|
|
|
use App\Core\Event;
|
2021-12-23 22:18:50 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-11-27 13:12:44 +09:00
|
|
|
use App\Core\Log;
|
|
|
|
use App\Core\Modules\Component;
|
2022-03-05 23:23:08 +09:00
|
|
|
use App\Core\Queue\Queue;
|
2021-12-23 22:18:50 +09:00
|
|
|
use App\Core\Router\RouteLoader;
|
|
|
|
use App\Core\Router\Router;
|
2021-11-27 13:12:44 +09:00
|
|
|
use App\Entity\Activity;
|
|
|
|
use App\Entity\Actor;
|
2021-12-23 22:18:50 +09:00
|
|
|
use App\Entity\LocalUser;
|
2022-02-26 23:45:38 +09:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-11-27 13:12:44 +09:00
|
|
|
use Component\FreeNetwork\FreeNetwork;
|
2021-12-23 22:18:50 +09:00
|
|
|
use Component\Notification\Controller\Feed;
|
2022-02-26 23:45:38 +09:00
|
|
|
use Exception;
|
|
|
|
use Throwable;
|
2021-11-27 13:12:44 +09:00
|
|
|
|
|
|
|
class Notification extends Component
|
|
|
|
{
|
2021-12-23 22:18:50 +09:00
|
|
|
public function onAddRoute(RouteLoader $m): bool
|
|
|
|
{
|
|
|
|
$m->connect('feed_notifications', '/feed/notifications', [Feed::class, 'notifications']);
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-02-11 20:39:25 +09:00
|
|
|
/**
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
|
|
|
public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering): bool
|
2021-12-23 22:18:50 +09:00
|
|
|
{
|
|
|
|
DB::persist(\App\Entity\Feed::create([
|
|
|
|
'actor_id' => $actor_id,
|
2021-12-29 22:27:04 +09:00
|
|
|
'url' => Router::url($route = 'feed_notifications'),
|
2021-12-23 22:18:50 +09:00
|
|
|
'route' => $route,
|
|
|
|
'title' => _m('Notifications'),
|
|
|
|
'ordering' => $ordering++,
|
|
|
|
]));
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-11-27 13:12:44 +09:00
|
|
|
/**
|
2022-02-11 20:39:25 +09:00
|
|
|
* Enqueues a notification for an Actor (such as person or group) which means
|
2021-11-27 13:12:44 +09:00
|
|
|
* it shows up in their home feed and such.
|
2022-03-14 03:23:19 +09:00
|
|
|
* WARNING: It's highly advisable to have flushed any relevant objects before triggering this event.
|
|
|
|
*
|
|
|
|
* $targets should be of the shape:
|
2022-03-29 04:52:12 +09:00
|
|
|
* (int|Actor)[] // Prefer Actor whenever possible
|
2022-03-14 03:23:19 +09:00
|
|
|
* Example of $targets:
|
2022-03-29 04:52:12 +09:00
|
|
|
* [42, $actor_alice, $actor_bob] // Avoid repeating actors or ids
|
2022-03-14 03:23:19 +09:00
|
|
|
*
|
|
|
|
* @param Actor $sender The one responsible for this activity, take care not to include it in targets
|
|
|
|
* @param Activity $activity The activity responsible for the object being given to known to targets
|
2022-03-29 04:52:12 +09:00
|
|
|
* @param array $targets Attentions, Mentions, any other source. Should never be empty, you usually want to register an attention to every $sender->getSubscribers()
|
2022-03-14 03:23:19 +09:00
|
|
|
* @param null|string $reason An optional reason explaining why this notification exists
|
2021-11-27 13:12:44 +09:00
|
|
|
*/
|
2022-03-29 04:52:12 +09:00
|
|
|
public function onNewNotification(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool
|
2021-11-27 13:12:44 +09:00
|
|
|
{
|
2022-03-14 03:23:19 +09:00
|
|
|
// Ensure targets are all actor objects and unique
|
|
|
|
$effective_targets = [];
|
|
|
|
foreach ($targets as $target) {
|
|
|
|
if (\is_int($target)) {
|
|
|
|
$target_id = $target;
|
|
|
|
$target_object = null;
|
|
|
|
} else {
|
|
|
|
$target_id = $target->getId();
|
|
|
|
$target_object = $target;
|
|
|
|
}
|
|
|
|
if (!\array_key_exists(key: $target_id, array: $effective_targets)) {
|
|
|
|
$target_object ??= Actor::getById($target_id);
|
|
|
|
$effective_targets[$target_id] = $target_object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($targets);
|
|
|
|
|
|
|
|
if (Event::handle('NewNotificationStart', [$sender, $activity, $effective_targets, $reason]) === Event::next) {
|
|
|
|
self::notify($sender, $activity, $effective_targets, $reason);
|
2022-02-11 20:39:25 +09:00
|
|
|
}
|
2021-11-27 13:12:44 +09:00
|
|
|
|
2022-03-14 03:23:19 +09:00
|
|
|
Event::handle('NewNotificationEnd', [$sender, $activity, $effective_targets, $reason]);
|
2021-11-27 13:12:44 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2022-03-05 23:23:08 +09:00
|
|
|
public function onQueueNotificationLocal(Actor $sender, Activity $activity, Actor $target, ?string $reason, array &$retry_args): bool
|
|
|
|
{
|
|
|
|
// TODO: use https://symfony.com/doc/current/notifier.html
|
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onQueueNotificationRemote(Actor $sender, Activity $activity, array $targets, ?string $reason, array &$retry_args): bool
|
|
|
|
{
|
|
|
|
if (FreeNetwork::notify($sender, $activity, $targets, $reason)) {
|
|
|
|
return Event::stop;
|
|
|
|
} else {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 04:52:14 +09:00
|
|
|
/**
|
2022-03-14 03:23:19 +09:00
|
|
|
* Bring given Activity to Targets' knowledge.
|
|
|
|
* This will flush a Notification to DB.
|
2022-03-05 23:23:08 +09:00
|
|
|
*
|
|
|
|
* @return true if successful, false otherwise
|
2021-12-05 04:52:14 +09:00
|
|
|
*/
|
2022-02-11 20:39:25 +09:00
|
|
|
public static function notify(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool
|
2021-11-27 13:12:44 +09:00
|
|
|
{
|
2021-11-30 08:58:27 +09:00
|
|
|
$remote_targets = [];
|
|
|
|
foreach ($targets as $target) {
|
|
|
|
if ($target->getIsLocal()) {
|
2022-03-14 03:23:19 +09:00
|
|
|
if ($target->hasBlocked($author = $activity->getActor())) {
|
|
|
|
Log::info("Not saving notification to actor {$target->getId()} from sender {$sender->getId()} because receiver blocked author {$author->getId()}.");
|
2022-02-10 13:31:06 +09:00
|
|
|
continue;
|
2021-11-30 08:58:27 +09:00
|
|
|
}
|
2021-12-29 03:45:18 +09:00
|
|
|
if (Event::handle('NewNotificationShould', [$activity, $target]) === Event::next) {
|
2022-02-11 22:44:32 +09:00
|
|
|
if ($sender->getId() === $target->getId()
|
|
|
|
|| $activity->getActorId() === $target->getId()) {
|
|
|
|
// The target already knows about this, no need to bother with a notification
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-29 03:45:18 +09:00
|
|
|
}
|
2022-03-05 23:23:08 +09:00
|
|
|
Queue::enqueue(
|
|
|
|
payload: [$sender, $activity, $target, $reason],
|
2022-03-14 03:23:19 +09:00
|
|
|
queue: 'NotificationLocal',
|
2022-03-05 23:23:08 +09:00
|
|
|
priority: true,
|
|
|
|
);
|
2021-11-30 08:58:27 +09:00
|
|
|
} else {
|
2021-12-26 02:46:45 +09:00
|
|
|
// We have no authority nor responsibility of notifying remote actors of a remote actor's doing
|
|
|
|
if ($sender->getIsLocal()) {
|
|
|
|
$remote_targets[] = $target;
|
|
|
|
}
|
2021-11-27 13:12:44 +09:00
|
|
|
}
|
2022-02-14 07:51:42 +09:00
|
|
|
// XXX: Unideal as in failures the rollback will leave behind a false notification,
|
|
|
|
// but most notifications (all) require flushing the objects first
|
2022-03-14 03:23:19 +09:00
|
|
|
// Should be okay as long as implementations bear this in mind
|
2022-02-19 13:47:08 +09:00
|
|
|
try {
|
|
|
|
DB::wrapInTransaction(fn () => DB::persist(Entity\Notification::create([
|
|
|
|
'activity_id' => $activity->getId(),
|
|
|
|
'target_id' => $target->getId(),
|
|
|
|
'reason' => $reason,
|
|
|
|
])));
|
2022-02-26 23:45:38 +09:00
|
|
|
} catch (Exception|Throwable $e) {
|
2022-03-14 03:23:19 +09:00
|
|
|
// We do our best not to record duplicate notifications, but it's not insane that can happen
|
2022-02-19 13:47:08 +09:00
|
|
|
Log::error('It was attempted to record an invalid notification!', [$e]);
|
|
|
|
}
|
2021-11-27 13:12:44 +09:00
|
|
|
}
|
|
|
|
|
2022-03-05 23:23:08 +09:00
|
|
|
if ($remote_targets !== []) {
|
|
|
|
Queue::enqueue(
|
|
|
|
payload: [$sender, $activity, $remote_targets, $reason],
|
2022-03-14 03:23:19 +09:00
|
|
|
queue: 'NotificationRemote',
|
2022-03-05 23:23:08 +09:00
|
|
|
priority: false,
|
|
|
|
);
|
|
|
|
}
|
2021-11-30 08:58:27 +09:00
|
|
|
|
2022-03-05 23:23:08 +09:00
|
|
|
return true;
|
2021-11-27 13:12:44 +09:00
|
|
|
}
|
|
|
|
}
|