diff --git a/modules/Fediverse/Commands/Broadcast.php b/modules/Fediverse/Commands/Broadcast.php index ed718f0b68a695f34a0480d73d6eee535e21b8ef..4f2b3e5f6eee3e461cc454a926a3a0966a29ce05 100644 --- a/modules/Fediverse/Commands/Broadcast.php +++ b/modules/Fediverse/Commands/Broadcast.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Modules\Fediverse\Commands; use CodeIgniter\CLI\BaseCommand; +use Exception; use Override; class Broadcast extends BaseCommand @@ -22,32 +23,49 @@ class Broadcast extends BaseCommand // retrieve scheduled activities from database $scheduledActivities = model('ActivityModel', false) - ->getScheduledActivities(); + ->getScheduledActivities(10); + + foreach ($scheduledActivities as $scheduledActivity) { + // set activity post to processing + model('ActivityModel', false) + ->update($scheduledActivity->id, [ + 'status' => 'processing', + ]); + } // Send activity to all followers foreach ($scheduledActivities as $scheduledActivity) { - if ($scheduledActivity->target_actor_id !== null) { - if ($scheduledActivity->actor_id !== $scheduledActivity->target_actor_id) { - // send activity to targeted actor - send_activity_to_actor( + try { + if ($scheduledActivity->target_actor_id !== null) { + if ($scheduledActivity->actor_id !== $scheduledActivity->target_actor_id) { + // send activity to targeted actor + send_activity_to_actor( + $scheduledActivity->actor, + $scheduledActivity->targetActor, + json_encode($scheduledActivity->payload, JSON_THROW_ON_ERROR) + ); + } + } else { + // send activity to all actor followers + send_activity_to_followers( $scheduledActivity->actor, - $scheduledActivity->targetActor, - json_encode($scheduledActivity->payload, JSON_THROW_ON_ERROR) + json_encode($scheduledActivity->payload, JSON_THROW_ON_ERROR), ); } - } else { - // send activity to all actor followers - send_activity_to_followers( - $scheduledActivity->actor, - json_encode($scheduledActivity->payload, JSON_THROW_ON_ERROR), - ); - } - // set activity post to delivered - model('ActivityModel', false) - ->update($scheduledActivity->id, [ - 'status' => 'delivered', - ]); + // set activity post to delivered + model('ActivityModel', false) + ->update($scheduledActivity->id, [ + 'status' => 'delivered', + ]); + + } catch (Exception) { + // set activity post to delivered + model('ActivityModel', false) + ->update($scheduledActivity->id, [ + 'status' => 'failed', + ]); + } } } } diff --git a/modules/Fediverse/Database/Migrations/2018-01-02-120000_update_activities_status.php b/modules/Fediverse/Database/Migrations/2018-01-02-120000_update_activities_status.php new file mode 100644 index 0000000000000000000000000000000000000000..c8ccf0e92eb941e63ceebef389439f9b82ef75d4 --- /dev/null +++ b/modules/Fediverse/Database/Migrations/2018-01-02-120000_update_activities_status.php @@ -0,0 +1,31 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2024 Ad Aures + * @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3 + * @link https://castopod.org/ + */ + +namespace Modules\Fediverse\Migrations; + +use App\Database\Migrations\BaseMigration; +use Override; + +class UpdateActivitiesStatus extends BaseMigration +{ + #[Override] + public function up(): void + { + $fields = [ + 'status' => [ + 'type' => 'ENUM', + 'constraint' => ['queued', 'processing', 'delivered', 'failed'], + 'null' => true, + ], + ]; + + $this->forge->modifyColumn('fediverse_activities', $fields); + } +} diff --git a/modules/Fediverse/Helpers/fediverse_helper.php b/modules/Fediverse/Helpers/fediverse_helper.php index 0b8d53ed391f604ce6be39bfc2f89d07602cc903..09cdb7d8ca89bef632835f9a5a828a37a8d9f1c6 100644 --- a/modules/Fediverse/Helpers/fediverse_helper.php +++ b/modules/Fediverse/Helpers/fediverse_helper.php @@ -131,6 +131,7 @@ if (! function_exists('send_activity_to_followers')) { */ function send_activity_to_followers(Actor $actor, string $activityPayload): void { + // TODO: send activities in parallel with https://www.php.net/manual/en/function.curl-multi-init.php foreach ($actor->followers as $follower) { send_activity_to_actor($actor, $follower, $activityPayload); } diff --git a/modules/Fediverse/Models/ActivityModel.php b/modules/Fediverse/Models/ActivityModel.php index 47341b93d02e3bbc492c54a8a5c0979ae8126896..548ee4a48e4adc9a373bfe1508de310328ac3d3e 100644 --- a/modules/Fediverse/Models/ActivityModel.php +++ b/modules/Fediverse/Models/ActivityModel.php @@ -123,11 +123,12 @@ class ActivityModel extends UuidModel /** * @return Activity[] */ - public function getScheduledActivities(): array + public function getScheduledActivities(int $limit = 10): array { return $this->where('`scheduled_at` <= UTC_TIMESTAMP()', null, false) ->where('status', 'queued') ->orderBy('scheduled_at', 'ASC') + ->limit($limit) ->findAll(); }