This commit is contained in:
2026-03-20 21:17:26 +01:00
parent 1a62fcb81d
commit 29c3ff8572
229 changed files with 13147 additions and 2577 deletions

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\ArtworkComment;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
final class ReceivedCommentsInboxService
{
public function queryForUser(User $user): Builder
{
return ArtworkComment::query()
->whereHas('artwork', function ($query) use ($user): void {
$query->where('user_id', $user->id)
->where('is_approved', true)
->whereNull('deleted_at');
})
->where('user_id', '!=', $user->id)
->where('is_approved', true)
->whereNull('deleted_at');
}
public function unreadCountForUser(User $user): int
{
return (int) $this->unreadQueryForUser($user)->count();
}
public function markInboxRead(User $user): void
{
$readAt = Carbon::now();
$this->unreadQueryForUser($user)
->select('artwork_comments.id')
->orderBy('artwork_comments.id')
->chunkById(200, function ($comments) use ($user, $readAt): void {
$rows = collect($comments)->map(function ($comment) use ($user, $readAt): array {
return [
'user_id' => $user->id,
'artwork_comment_id' => (int) $comment->id,
'read_at' => $readAt,
'created_at' => $readAt,
'updated_at' => $readAt,
];
})->all();
if ($rows !== []) {
DB::table('user_received_comment_reads')->insertOrIgnore($rows);
}
}, 'artwork_comments.id', 'id');
}
private function unreadQueryForUser(User $user): Builder
{
return $this->queryForUser($user)
->whereNotExists(function ($query) use ($user): void {
$query->selectRaw('1')
->from('user_received_comment_reads as ucr')
->whereColumn('ucr.artwork_comment_id', 'artwork_comments.id')
->where('ucr.user_id', $user->id);
});
}
}