Only track was watched if more than 10s is played #6
This commit is contained in:
@@ -4,7 +4,11 @@ namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Episode;
|
||||
use App\Models\Watched;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class StreamApiController extends Controller
|
||||
{
|
||||
@@ -34,4 +38,36 @@ class StreamApiController extends Controller
|
||||
'extra_subtitles' => $subtitles,
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Track that the authenticated user has watched the episode.
|
||||
* Called client-side after 10 seconds of playback.
|
||||
*/
|
||||
public function trackWatched(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'episode_id' => 'required|integer|exists:episodes,id',
|
||||
]);
|
||||
|
||||
$user = Auth::user();
|
||||
if (! $user) {
|
||||
return response()->json(['watched' => false], 401);
|
||||
}
|
||||
|
||||
$episodeId = $request->input('episode_id');
|
||||
|
||||
// 1-hour cooldown to prevent duplicate entries
|
||||
$time = Carbon::now()->subHour(1);
|
||||
$alreadyWatched = Watched::where('user_id', $user->id)
|
||||
->where('episode_id', $episodeId)
|
||||
->where('created_at', '>=', $time)
|
||||
->exists();
|
||||
|
||||
if (! $alreadyWatched) {
|
||||
Watched::create(['user_id' => $user->id, 'episode_id' => $episodeId]);
|
||||
cache()->forget('user'.$user->id.'watched'.$episodeId);
|
||||
}
|
||||
|
||||
return response()->json(['watched' => true], 200);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user