Update Report model

This commit is contained in:
Daniel Supernault 2025-06-30 02:30:59 -06:00
parent d9a30fbfc4
commit 6d93bf1596
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

View File

@ -2,8 +2,11 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Report extends Model
{
@ -11,6 +14,86 @@ class Report extends Model
public $guarded = [];
#[Scope]
protected function search(Builder $query, ?string $search): Builder
{
if (empty($search)) {
return $query;
}
if (str_starts_with($search, 'video_id:')) {
$videoId = trim(substr($search, 9));
return $query->join('videos', 'reports.reported_video_id', '=', 'videos.id')
->where('videos.id', $videoId)
->select('reports.*');
}
if (str_starts_with($search, 'reported_by:')) {
$reporterId = trim(substr($search, 12));
return $query->where('reporter_profile_id', $reporterId);
}
if (str_starts_with($search, 'reported_profile_id:')) {
$profileId = trim(substr($search, 20));
return $query->where('reports.reported_profile_id', $profileId)
->orWhereExists(function ($subQuery) use ($profileId) {
$subQuery->select(DB::raw(1))
->from('videos')
->whereColumn('videos.id', 'reports.reported_video_id')
->where('videos.profile_id', $profileId);
});
}
return $query->join('profiles', 'reports.reported_profile_id', '=', 'profiles.id')
->where('profiles.username', 'like', '%'.$search.'%')
->select('reports.*');
}
#[Scope]
protected function filterByStatus(Builder $query, ?string $sort): Builder
{
if ($sort === 'all') {
return $query;
}
$adminSeen = $sort === 'closed';
return $query->whereAdminSeen($adminSeen);
}
#[Scope]
protected function paginated(Builder $query, int $perPage = 10): Builder
{
return $query->orderByDesc('id');
}
#[Scope]
protected function againstProfile(Builder $query, int|string $profileId): Builder
{
return $query->where('reported_profile_id', $profileId)
->orWhereExists(function ($subQuery) use ($profileId) {
$subQuery->select(DB::raw(1))
->from('videos')
->whereColumn('videos.id', 'reports.reported_video_id')
->where('videos.profile_id', $profileId);
});
}
public static function totalReportsAgainstProfile(int|string $profileId): int
{
return self::where('reported_profile_id', $profileId)
->orWhereExists(function ($query) use ($profileId) {
$query->select(DB::raw(1))
->from('videos')
->whereColumn('videos.id', 'reports.reported_video_id')
->where('videos.profile_id', $profileId);
})
->count();
}
public function reportEntityType()
{
if ($this->reported_profile_id) {