From 7e3b868a90ff6380bfee03656969df740e22afac Mon Sep 17 00:00:00 2001 From: Ghostie Date: Sat, 11 Jan 2025 15:29:26 -0500 Subject: [PATCH] added a queue for posting activities --- README.md | 17 ++++++++++++++++- app/Http/Controllers/PostController.php | 3 +++ app/Http/Controllers/ProfileController.php | 7 ++++--- app/Listeners/UserUnfollowedListener.php | 4 ++-- app/Notifications/UserNotification.php | 3 ++- app/Types/TypeActivity.php | 2 +- resources/views/posts/show.blade.php | 1 + resources/views/users/notifications.blade.php | 3 +-- 8 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 355c2e7..f86069b 100644 --- a/README.md +++ b/README.md @@ -202,13 +202,28 @@ php artisan storage:link php artisan install:broadcasting ``` -Now, we need to create two services to handle the jobs that OurSpace needs to run and another one to run Laravel Reverb. So run something `emacs /lib/systemd/system/ourspace-queue.service` and `emacs /lib/systemd/system/ourspace-ws.service` and add the following content: +Now, we need to create three services to handle the jobs that OurSpace needs to run, another to handle the notifications' queue and another one to run Laravel Reverb. So run something `emacs /lib/systemd/system/ourspace-queue.service`, `emacs /lib/systemd/system/ourspace-notifications.service`, `emacs /lib/systemd/system/ourspace-ws.service` and add the following content: ```ini # /lib/systemd/system/ourspace-queue.service [Unit] Description=OurSpace queue worker +[Service] +User=www-data +Group=www-data +Restart=on-failure +ExecStart=/usr/bin/php /var/www/html/ourspace/artisan queue:work --daemon --env=production --queue=ap + +[Install] +WantedBy=multi-user.target +``` + +```ini +# /lib/systemd/system/ourspace-notifications.service +[Unit] +Description=OurSpace notifications worker + [Service] User=www-data Group=www-data diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index 96d4513..5750149 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -18,6 +18,9 @@ class PostController extends Controller { $actor = $note->get_actor ()->first (); + if (!$actor) + return redirect ()->back (); + return view ("posts.show", compact ("note", "actor")); } diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 698402c..baba677 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -170,8 +170,9 @@ class ProfileController extends Controller return redirect ()->route ("login"); $user = auth ()->user (); - $unread_notifications = $user->notifications; - $notifications = PaginationHelper::paginate (collect ($unread_notifications), 20); + $user_notifications = $user->notifications; + $notifications = PaginationHelper::paginate (collect ($user_notifications), 20); + $unread_notifications = $user->unreadNotifications->count (); $processed_notifications = []; foreach ($notifications as $notification) @@ -212,6 +213,6 @@ class ProfileController extends Controller $notification->markAsRead (); } - return view ("users.notifications", compact ("user", "notifications", "processed_notifications")); + return view ("users.notifications", compact ("user", "notifications", "processed_notifications", "unread_notifications")); } } diff --git a/app/Listeners/UserUnfollowedListener.php b/app/Listeners/UserUnfollowedListener.php index 0abdb9e..a0b9c77 100644 --- a/app/Listeners/UserUnfollowedListener.php +++ b/app/Listeners/UserUnfollowedListener.php @@ -27,7 +27,7 @@ class UserUnfollowedListener */ public function handle(UserUnfollowedEvent $event): void { - $follow_exists = Follow::where("actor_id", $event->actor->id) + $follow_exists = Follow::where ("actor_id", $event->actor->id) ->where("object_id", $event->object->id) ->first(); @@ -38,7 +38,7 @@ class UserUnfollowedListener if (!$user) return; - $user->notify(new UserNotification( + $user->notify (new UserNotification ( "Unfollow", $event->actor->id, $event->object->id, diff --git a/app/Notifications/UserNotification.php b/app/Notifications/UserNotification.php index 6e38602..984aca2 100644 --- a/app/Notifications/UserNotification.php +++ b/app/Notifications/UserNotification.php @@ -4,10 +4,11 @@ namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Notifications\Messages\BroadcastMessage; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; -class UserNotification extends Notification +class UserNotification extends Notification implements ShouldQueue { use Queueable; diff --git a/app/Types/TypeActivity.php b/app/Types/TypeActivity.php index 7c08d6a..1f72467 100644 --- a/app/Types/TypeActivity.php +++ b/app/Types/TypeActivity.php @@ -237,7 +237,7 @@ class TypeActivity { public static function post_activity (Activity $activity, Actor $source, $target, $should_sign = false) { - PostActivityJob::dispatch ($activity, $source, $target, $should_sign); + PostActivityJob::dispatch ($activity, $source, $target, $should_sign)->onQueue ("ap"); return [ "success" => "activity posted" diff --git a/resources/views/posts/show.blade.php b/resources/views/posts/show.blade.php index b03d18a..e708bfc 100644 --- a/resources/views/posts/show.blade.php +++ b/resources/views/posts/show.blade.php @@ -3,6 +3,7 @@ @section ("title", "View Post") @section ("content") +
diff --git a/resources/views/users/notifications.blade.php b/resources/views/users/notifications.blade.php index 67ece07..1fcd338 100644 --- a/resources/views/users/notifications.blade.php +++ b/resources/views/users/notifications.blade.php @@ -5,8 +5,7 @@ @section ("content")

Notifications

-

You have {{ count ($user->unreadNotifications) }} unread notifications

-
+

You have {{ $unread_notifications }} unread notifications