mirror of
https://github.com/ad-aures/castopod.git
synced 2026-04-12 19:17:45 +02:00
fix(notifications): notify actors after activities insert / update using model callback methods
--> Remove sql triggers because most shared hosting plans prevent using them
This commit is contained in:
parent
460f52f70e
commit
e08555a4e9
10 changed files with 98 additions and 126 deletions
|
|
@ -32,6 +32,16 @@ class ActivityModel extends BaseUuidModel
|
|||
*/
|
||||
protected $uuidFields = ['id', 'post_id'];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $afterInsert = ['notify'];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $afterUpdate = ['notify'];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
|
|
@ -116,4 +126,71 @@ class ActivityModel extends BaseUuidModel
|
|||
->orderBy('scheduled_at', 'ASC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array<string|int, mixed>> $data
|
||||
* @return array<string, array<string|int, mixed>>
|
||||
*/
|
||||
protected function notify(array $data): array
|
||||
{
|
||||
$activity = (new self())->getActivityById(is_array($data['id']) ? $data['id'][0] : $data['id']);
|
||||
|
||||
if (! $activity instanceof Activity) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($activity->target_actor_id === $activity->actor_id) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
// notify only if incoming activity (with status set to NULL) is created
|
||||
if ($activity->status !== null) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($activity->type === 'Follow') {
|
||||
(new NotificationModel())->insert([
|
||||
'actor_id' => $activity->actor_id,
|
||||
'target_actor_id' => $activity->target_actor_id,
|
||||
'activity_id' => $activity->id,
|
||||
'type' => 'follow',
|
||||
'created_at' => $activity->created_at,
|
||||
]);
|
||||
} elseif ($activity->type === 'Undo_Follow') {
|
||||
(new NotificationModel())->builder()
|
||||
->delete([
|
||||
'actor_id' => $activity->actor_id,
|
||||
'target_actor_id' => $activity->target_actor_id,
|
||||
'type' => 'follow',
|
||||
]);
|
||||
} elseif (in_array($activity->type, ['Create', 'Like', 'Announce'], true) && $activity->post_id !== null) {
|
||||
(new NotificationModel())->insert([
|
||||
'actor_id' => $activity->actor_id,
|
||||
'target_actor_id' => $activity->target_actor_id,
|
||||
'post_id' => $activity->post_id,
|
||||
'activity_id' => $activity->id,
|
||||
'type' => match ($activity->type) {
|
||||
'Create' => 'reply',
|
||||
'Like' => 'like',
|
||||
'Announce' => 'share',
|
||||
},
|
||||
'created_at' => $activity->created_at,
|
||||
]);
|
||||
} elseif (in_array($activity->type, ['Undo_Like', 'Undo_Announce'], true) && $activity->post_id !== null) {
|
||||
(new NotificationModel())->builder()
|
||||
->delete([
|
||||
'actor_id' => $activity->actor_id,
|
||||
'target_actor_id' => $activity->target_actor_id,
|
||||
'post_id' => service('uuid')
|
||||
->fromString($activity->post_id)
|
||||
->getBytes(),
|
||||
'type' => match ($activity->type) {
|
||||
'Undo_Like' => 'like',
|
||||
'Undo_Announce' => 'share',
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue