Skip to content

A self-hosted media server and file management system

feat: completion count logic to playback progress

aminnausin3cb8db654d67
diff --git a/app/Http/Controllers/Api/V1/PlaybackProgressController.php b/app/Http/Controllers/Api/V1/PlaybackProgressController.php
index 2767a399..49d1bd04 100644
--- a/app/Http/Controllers/Api/V1/PlaybackProgressController.php
+++ b/app/Http/Controllers/Api/V1/PlaybackProgressController.php
@@ -7,7 +7,10 @@ use App\Http\Requests\PlaybackProgressStoreRequest;
use App\Models\Metadata;
use App\Models\PlaybackProgress;
use App\Traits\HttpResponses;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class PlaybackProgressController extends Controller {
@@ -22,33 +25,38 @@ class PlaybackProgressController extends Controller {
/**
* Upserts a playback progress tracker associated with a user and a specific metadata
*/
- public function upsert(PlaybackProgressStoreRequest $request, Metadata $metadata) {
+ public function upsert(PlaybackProgressStoreRequest $request, Metadata $metadata): Response|JsonResponse {
try {
$user_id = Auth::id();
$validated = $request->validated();
- $duration = $metadata->duration ?? 0;
- $progress_offset = min($validated['progress_offset'], $duration);
+ DB::transaction(function () use ($user_id, $validated, $metadata) {
+ $progress = PlaybackProgress::firstOrNew([
+ 'user_id' => $user_id,
+ 'metadata_id' => $metadata->id,
+ ]);
- $progress_pct = $duration ? round($progress_offset * 100 / $duration) : 0;
+ $duration = $metadata->duration ?? 0;
+ $progress_offset = min($validated['progress_offset'], $duration);
+ $progress_pct = $duration ? round($progress_offset * 100 / $duration) : 0;
+ $threshold = config('playback.completion_percentage_threshold', 95);
- if ($progress_pct >= config('playback.completion_percentage_threshold')) {
- $progress_offset = $metadata->duration;
- $progress_pct = 100;
- }
+ $is_newly_completed = ($progress_pct >= $threshold && $progress->progress_percentage < $threshold);
+ $is_completed = $progress_pct >= $threshold;
- PlaybackProgress::upsert(
- [
- 'user_id' => $user_id,
- 'metadata_id' => $metadata->id,
- 'progress_offset' => $progress_offset,
- 'progress_percentage' => $progress_pct,
- 'record_id' => $validated['record_id'] ?? null,
- 'updated_at' => now(),
- ],
- ['user_id', 'metadata_id'],
- ['progress_offset', 'progress_percentage', 'record_id', 'updated_at']
- );
+ $progress->progress_offset = $is_completed ? $metadata->duration : $progress_offset;
+ $progress->progress_percentage = $is_completed ? 100 : $progress_pct;
+
+ if ($is_newly_completed) {
+ $progress->last_completed_at = now();
+ $progress->completion_count++;
+ }
+
+ if (isset($validated['record_id'])) {
+ $progress->record_id = $validated['record_id'];
+ }
+ $progress->save();
+ });
return response()->noContent();
} catch (\Throwable $th) {
diff --git a/app/Models/PlaybackProgress.php b/app/Models/PlaybackProgress.php
index 0ea3faac..f44332af 100644
--- a/app/Models/PlaybackProgress.php
+++ b/app/Models/PlaybackProgress.php
@@ -15,6 +15,9 @@ class PlaybackProgress extends Model {
* progress_offset -> int4 (in seconds)
* progress_percentage -> int1 (0-100) (default=0)
*
+ * completion_count -> int4 (default=0)
+ * last_completed_at -> timestampTz (nullable)
+ *
* created_at -> timestampTz (nullable)
* updated_at -> timestampTz (nullable)
*/
@@ -26,6 +29,11 @@ class PlaybackProgress extends Model {
'progress_percentage',
];
+ protected $attributes = [
+ 'progress_percentage' => 0,
+ 'completion_count' => 0,
+ ];
+
public function user(): BelongsTo {
return $this->belongsTo(User::class);
}
diff --git a/database/migrations/2026_04_10_155746_add_completion_counter_to_playback_progress.php b/database/migrations/2026_04_10_155746_add_completion_counter_to_playback_progress.php
new file mode 100644
index 00000000..5b42db62
--- /dev/null
+++ b/database/migrations/2026_04_10_155746_add_completion_counter_to_playback_progress.php
@@ -0,0 +1,26 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration {
+ /**
+ * Run the migrations.
+ */
+ public function up(): void {
+ Schema::table('playback_progress', function (Blueprint $table) {
+ $table->unsignedInteger('completion_count')->default(0);
+ $table->timestamptz('last_completed_at')->nullable()->after('completion_count');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::table('playback_progress', function (Blueprint $table) {
+ $table->dropColumn(['completion_count', 'last_completed_at']);
+ });
+ }
+};