feat: media play
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Media;
|
||||
use App\Models\MediaPlay;
|
||||
use App\Models\Category;
|
||||
use App\Models\SubCategory;
|
||||
use App\Models\Tag;
|
||||
@@ -567,6 +568,76 @@ public function saved()
|
||||
return auth()->user()->savedMedia()->with(['image','categories', 'subCategories', 'myNote' , 'tags'])->get();
|
||||
}
|
||||
|
||||
// RECORD a play for the current user (feeds popular + recently played).
|
||||
public function recordPlay($id)
|
||||
{
|
||||
$media = Media::where('id', $id)
|
||||
->where(function ($q) {
|
||||
$q->where('visibility', 'public')->orWhere('user_id', auth()->id());
|
||||
})
|
||||
->firstOrFail();
|
||||
|
||||
$play = MediaPlay::firstOrNew([
|
||||
'user_id' => auth()->id(),
|
||||
'media_id' => $media->id,
|
||||
]);
|
||||
$play->play_count = ($play->play_count ?? 0) + 1;
|
||||
$play->last_played_at = now();
|
||||
$play->save();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Play recorded',
|
||||
'play_count' => $play->play_count,
|
||||
'last_played_at' => $play->last_played_at,
|
||||
]);
|
||||
}
|
||||
|
||||
// POPULAR media (global), ranked by total play count across all users.
|
||||
public function popular(Request $request)
|
||||
{
|
||||
$limit = (int) $request->input('limit', 20);
|
||||
|
||||
$media = Media::query()
|
||||
->where(function ($q) {
|
||||
$q->where('visibility', 'public')->orWhere('user_id', auth()->id());
|
||||
})
|
||||
->withCount('plays as listeners_count') // distinct users who played
|
||||
->withSum('plays as plays_count', 'play_count') // total plays
|
||||
->with(['image', 'categories', 'subCategories', 'tags'])
|
||||
->orderByDesc('plays_count')
|
||||
->orderByDesc('listeners_count')
|
||||
->orderByDesc('created_at')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
return response()->json($media);
|
||||
}
|
||||
|
||||
// RECENTLY PLAYED media for the current user, most recent first.
|
||||
public function recentlyPlayed(Request $request)
|
||||
{
|
||||
$limit = (int) $request->input('limit', 20);
|
||||
|
||||
$plays = MediaPlay::where('user_id', auth()->id())
|
||||
->whereNotNull('last_played_at')
|
||||
->with(['media' => fn ($q) => $q->with(['image', 'categories', 'subCategories', 'tags'])])
|
||||
->orderByDesc('last_played_at')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
$media = $plays->map(function ($play) {
|
||||
$media = $play->media;
|
||||
if (!$media) {
|
||||
return null;
|
||||
}
|
||||
$media->last_played_at = $play->last_played_at;
|
||||
$media->play_count = $play->play_count;
|
||||
return $media;
|
||||
})->filter()->values();
|
||||
|
||||
return response()->json($media);
|
||||
}
|
||||
|
||||
public function storeNote(Request $request, $mediaId)
|
||||
{
|
||||
$media = Media::findOrFail($mediaId);
|
||||
|
||||
@@ -56,6 +56,11 @@ public function tags()
|
||||
{
|
||||
return $this->belongsToMany(Tag::class, 'media_tag');
|
||||
}
|
||||
|
||||
public function plays()
|
||||
{
|
||||
return $this->hasMany(MediaPlay::class);
|
||||
}
|
||||
public function notes()
|
||||
{
|
||||
return $this->morphMany(Note::class, 'noteable');
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class MediaPlay extends Model
|
||||
{
|
||||
protected $fillable = ['user_id', 'media_id', 'play_count', 'last_played_at'];
|
||||
|
||||
protected $casts = [
|
||||
'play_count' => 'integer',
|
||||
'last_played_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function media(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Media::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user