Add follow system, activity feed, notifications, sitemap, caching, Scout search

This commit is contained in:
Mark
2026-05-30 11:23:24 +03:00
parent 5c6ba86e15
commit 3083071a6c
24 changed files with 1657 additions and 49 deletions
+15 -1
View File
@@ -5,12 +5,14 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cache;
use Laravel\Scout\Searchable;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
class Game extends Model
{
use HasSlug;
use HasSlug, Searchable;
protected $fillable = [
'igdb_id', 'slug', 'title', 'summary', 'cover_url', 'cover_thumb_url',
@@ -60,6 +62,15 @@ class Game extends Model
return $this->hasMany(Review::class)->latest();
}
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'title' => $this->title,
'summary' => $this->summary,
];
}
public function scopePublished($query)
{
return $query->where('status', 'published');
@@ -72,5 +83,8 @@ class Game extends Model
'rating_avg' => $stats->avg ? round($stats->avg, 2) : null,
'rating_count' => $stats->cnt ?? 0,
]);
Cache::forget('home.top_rated');
Cache::forget('games.top_rated.1');
}
}
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Notification extends Model
{
public $incrementing = false;
protected $keyType = 'string';
public $timestamps = false;
protected $fillable = ['id', 'user_id', 'type', 'data', 'read_at', 'created_at'];
protected $casts = [
'data' => 'array',
'read_at' => 'datetime',
'created_at' => 'datetime',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
+5
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Services\ActivityService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -23,6 +24,10 @@ class Review extends Model
static::deleted(function (Review $review) {
$review->game->recalculateRating();
});
static::created(function (Review $review) {
ActivityService::log($review->user, 'reviewed_game', $review->game, ['rating' => $review->rating]);
});
}
public function user(): BelongsTo { return $this->belongsTo(User::class); }
+1
View File
@@ -20,6 +20,7 @@ class User extends Authenticatable
public function reviews(): HasMany { return $this->hasMany(Review::class); }
public function lists(): HasMany { return $this->hasMany(UserList::class); }
public function activities(): HasMany { return $this->hasMany(Activity::class)->latest(); }
public function notifications(): HasMany { return $this->hasMany(Notification::class)->latest('created_at'); }
public function following(): BelongsToMany
{
+16
View File
@@ -1,5 +1,6 @@
<?php
namespace App\Models;
use App\Services\ActivityService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -10,6 +11,21 @@ class UserGame extends Model
const STATUSES = ['playing','played','want_to_play','on_hold','dropped'];
protected static function boot(): void
{
parent::boot();
static::created(function (UserGame $ug) {
ActivityService::log($ug->user, 'added_to_library', $ug->game, ['status' => $ug->status]);
});
static::updated(function (UserGame $ug) {
if ($ug->isDirty('rating') && $ug->rating) {
ActivityService::log($ug->user, 'rated_game', $ug->game, ['rating' => $ug->rating]);
}
});
}
public function user(): BelongsTo { return $this->belongsTo(User::class); }
public function game(): BelongsTo { return $this->belongsTo(Game::class); }
}