1
0
mirror of https://github.com/flarum/core.git synced 2025-05-21 06:40:00 +02:00
php-flarum/src/Notification/NotificationRepository.php
Franz Liedke 5dc9451c21
Fix notification query with DB prefix
This was fixed in https://github.com/laravel/framework/pull/28400.
See commit 7f1048352d09738ba6f4811ff84e6c4c2c9e4582.
2020-05-09 14:45:57 +02:00

56 lines
1.5 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Notification;
use Carbon\Carbon;
use Flarum\User\User;
class NotificationRepository
{
/**
* Find a user's notifications.
*
* @param User $user
* @param int|null $limit
* @param int $offset
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByUser(User $user, $limit = null, $offset = 0)
{
$primaries = Notification::selectRaw('MAX(id) AS id')
->selectRaw('SUM(read_at IS NULL) AS unread_count')
->where('user_id', $user->id)
->whereIn('type', $user->getAlertableNotificationTypes())
->where('is_deleted', false)
->whereSubjectVisibleTo($user)
->groupBy('type', 'subject_id')
->orderByRaw('MAX(created_at) DESC')
->skip($offset)
->take($limit);
return Notification::select('notifications.*', 'p.unread_count')
->joinSub($primaries, 'p', 'notifications.id', '=', 'p.id')
->latest()
->get();
}
/**
* Mark all of a user's notifications as read.
*
* @param User $user
*
* @return void
*/
public function markAllAsRead(User $user)
{
Notification::where('user_id', $user->id)->update(['read_at' => Carbon::now()]);
}
}