[GROUP] Notifity group subscribers of new activity concerning the group

This commit is contained in:
Diogo Peralta Cordeiro 2022-02-11 11:39:25 +00:00
parent 66323c5a73
commit 3ae8f8213f
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 58 additions and 5 deletions

View File

@ -22,6 +22,8 @@ declare(strict_types = 1);
namespace Component\Group; namespace Component\Group;
use App\Core\Event; use App\Core\Event;
use App\Entity\Activity;
use Component\Notification\Notification;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Modules\Component; use App\Core\Modules\Component;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
@ -46,6 +48,27 @@ class Group extends Component
return Event::next; return Event::next;
} }
/**
* Enqueues a notification for an Actor (such as person or group) which means
* it shows up in their home feed and such.
* @param Actor $sender
* @param Activity $activity
* @param array $targets
* @param string|null $reason
* @return bool
*/
public function onNewNotificationWithTargets(Actor $sender, Activity $activity, array $targets = [], ?string $reason = null): bool
{
foreach($targets as $target) {
if ($target->isGroup()) {
// The Group announces to its subscribers
Notification::notify($target, $activity, $target->getSubscribers(), $reason);
}
}
return Event::next;
}
/** /**
* Add an <a href=group_settings> to the profile card for groups, if the current actor can access them * Add an <a href=group_settings> to the profile card for groups, if the current actor can access them
*/ */

View File

@ -23,6 +23,7 @@ namespace Component\Notification;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Util\Exception\ServerException;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Core\Modules\Component; use App\Core\Modules\Component;
@ -36,13 +37,24 @@ use Component\Notification\Controller\Feed;
class Notification extends Component class Notification extends Component
{ {
/**
* @param RouteLoader $m
* @return bool
*/
public function onAddRoute(RouteLoader $m): bool public function onAddRoute(RouteLoader $m): bool
{ {
$m->connect('feed_notifications', '/feed/notifications', [Feed::class, 'notifications']); $m->connect('feed_notifications', '/feed/notifications', [Feed::class, 'notifications']);
return Event::next; return Event::next;
} }
public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering) /**
* @param int $actor_id
* @param LocalUser $user
* @param int $ordering
* @return bool
* @throws ServerException
*/
public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering): bool
{ {
DB::persist(\App\Entity\Feed::create([ DB::persist(\App\Entity\Feed::create([
'actor_id' => $actor_id, 'actor_id' => $actor_id,
@ -55,21 +67,33 @@ class Notification extends Component
} }
/** /**
* Enqueues a notification for an Actor (user or group) which means * Enqueues a notification for an Actor (such as person or group) which means
* it shows up in their home feed and such. * it shows up in their home feed and such.
* @param Actor $sender
* @param Activity $activity
* @param array $ids_already_known
* @param string|null $reason
* @return bool
*/ */
public function onNewNotification(Actor $sender, Activity $activity, array $ids_already_known = [], ?string $reason = null): bool public function onNewNotification(Actor $sender, Activity $activity, array $ids_already_known = [], ?string $reason = null): bool
{ {
$targets = $activity->getNotificationTargets(ids_already_known: $ids_already_known, sender_id: $sender->getId()); $targets = $activity->getNotificationTargets(ids_already_known: $ids_already_known, sender_id: $sender->getId());
$this->notify($sender, $activity, $targets, $reason); if (Event::handle('NewNotificationWithTargets', [$sender, $activity, $targets, $reason]) === Event::next) {
self::notify($sender, $activity, $targets, $reason);
}
return Event::next; return Event::next;
} }
/** /**
* Bring given Activity to Targets's attention * Bring given Activity to Targets's attention
* @param Actor $sender
* @param Activity $activity
* @param array $targets
* @param string|null $reason
* @return bool
*/ */
public function notify(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool public static function notify(Actor $sender, Activity $activity, array $targets, ?string $reason = null): bool
{ {
$remote_targets = []; $remote_targets = [];
foreach ($targets as $target) { foreach ($targets as $target) {

View File

@ -32,6 +32,7 @@ declare(strict_types = 1);
namespace Plugin\ActivityPub; namespace Plugin\ActivityPub;
use ActivityPhp\Type;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\HTTPClient; use App\Core\HTTPClient;
@ -269,7 +270,12 @@ class ActivityPub extends Plugin
//$to_failed = []; //$to_failed = [];
foreach ($to_addr as $inbox => $dummy) { foreach ($to_addr as $inbox => $dummy) {
try { try {
$res = self::postman($sender, Model::toJson($activity), $inbox); $data = Model::toJson($activity);
if ($sender->isGroup()) {
// When the sender is a group, we have to wrap it in an Announce activity
$data = Type::create('Announce', ['object' => $data])->toJson();
}
$res = self::postman($sender, $data, $inbox);
// accumulate errors for later use, if needed // accumulate errors for later use, if needed
$status_code = $res->getStatusCode(); $status_code = $res->getStatusCode();