feat: notifications on comments

This commit is contained in:
2026-05-23 10:50:34 +02:00
parent cd38d88fe7
commit 49419c1e7b
5 changed files with 164 additions and 1 deletions
+24 -1
View File
@@ -6,6 +6,7 @@ use App\Http\Requests\PostReviewsRequest;
use App\Http\Resources\PostReviewsResource;
use App\Models\MealPosts;
use App\Models\PostReviews;
use App\Notifications\MealPostCommentedNotification;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
@@ -49,7 +50,10 @@ class PostReviewsController extends Controller
'user_id' => $userId,
]);
return (new PostReviewsResource($postReview->load('user:id,name,avatar_url')))
$postReview->load('user:id,name,avatar_url');
$this->notifyMealOwner($mealPost, $postReview, $userId);
return (new PostReviewsResource($postReview))
->response()
->setStatusCode(201);
}
@@ -97,4 +101,23 @@ class PostReviewsController extends Controller
404,
);
}
private function notifyMealOwner(MealPosts $mealPost, PostReviews $postReview, string $reviewerId): void
{
if ($mealPost->user_id === $reviewerId || blank($postReview->comment)) {
return;
}
$mealPost->loadMissing('user');
$owner = $mealPost->user;
if (! $owner) {
return;
}
$owner->notify(
(new MealPostCommentedNotification($postReview))
->locale($owner->preferredLocale())
);
}
}
@@ -0,0 +1,49 @@
<?php
namespace App\Notifications;
use App\Models\PostReviews;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;
class MealPostCommentedNotification extends Notification
{
use Queueable;
public function __construct(private readonly PostReviews $postReview) {}
/**
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['expo'];
}
public function toExpoPush(object $notifiable): array
{
$this->postReview->loadMissing(
'mealPost:id,title,user_id',
'user:id,name',
);
$mealPost = $this->postReview->mealPost;
$mealPostId = $mealPost?->getKey() ?? $this->postReview->meal_post_id;
$mealTitle = Str::limit((string) ($mealPost?->title ?: __('api.notifications.your_meal')), 60);
$commenterName = $this->postReview->user?->name ?: __('api.notifications.someone');
return [
'title' => __('api.notifications.meal_post_commented_title'),
'body' => __('api.notifications.meal_post_commented_body', [
'meal' => $mealTitle,
'name' => $commenterName,
]),
'sound' => 'default',
'channelId' => 'default',
'data' => [
'url' => "bowly://meals/{$mealPostId}",
],
];
}
}