Skip to content

A self-hosted media server and file management system

feat: playback progress table

aminnausin756832f02ee5
diff --git a/app/Models/Metadata.php b/app/Models/Metadata.php
index c46ffabd..e1bf67fa 100644
--- a/app/Models/Metadata.php
+++ b/app/Models/Metadata.php
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
class Metadata extends Model {
use HasEditableFields, HasFactory;
@@ -135,6 +136,10 @@ class Metadata extends Model {
return $this->hasMany(Playback::class);
}
+ public function playbackProgress(): HasOne {
+ return $this->hasOne(PlaybackProgress::class);
+ }
+
public function videoTags(): HasMany {
return $this->hasMany(VideoTag::class);
}
diff --git a/app/Models/PlaybackProgress.php b/app/Models/PlaybackProgress.php
new file mode 100644
index 00000000..0ea3faac
--- /dev/null
+++ b/app/Models/PlaybackProgress.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+
+class PlaybackProgress extends Model {
+ /**
+ * id -> int8 (pk) (index)
+ * user_id -> int8 (fk) (index) (unique with metadata_id)
+ * metadata_id -> int8 (fk) (index) (unique with user_id)
+ * record_id -> int8 (fk) (nullable)
+ *
+ * progress_offset -> int4 (in seconds)
+ * progress_percentage -> int1 (0-100) (default=0)
+ *
+ * created_at -> timestampTz (nullable)
+ * updated_at -> timestampTz (nullable)
+ */
+ protected $fillable = [
+ 'user_id',
+ 'metadata_id',
+ 'record_id',
+ 'progress_offset',
+ 'progress_percentage',
+ ];
+
+ public function user(): BelongsTo {
+ return $this->belongsTo(User::class);
+ }
+
+ public function metadata(): BelongsTo {
+ return $this->belongsTo(Metadata::class);
+ }
+
+ public function record(): BelongsTo {
+ return $this->belongsTo(Record::class);
+ }
+}
diff --git a/database/migrations/2026_04_07_025520_create_playback_progress_table.php b/database/migrations/2026_04_07_025520_create_playback_progress_table.php
new file mode 100644
index 00000000..707023fc
--- /dev/null
+++ b/database/migrations/2026_04_07_025520_create_playback_progress_table.php
@@ -0,0 +1,30 @@
+<?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::create('playback_progress', function (Blueprint $table) {
+ $table->id();
+ $table->foreignId('user_id')->constrained()->cascadeOnDelete();
+ $table->foreignId('metadata_id')->constrained()->cascadeOnDelete();
+ $table->foreignId('record_id')->nullable()->constrained()->nullOnDelete();
+ $table->unsignedInteger('progress_offset');
+ $table->timestampsTz();
+
+ $table->unique(['metadata_id', 'user_id']);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::dropIfExists('playback_progress');
+ }
+};
diff --git a/database/migrations/2026_04_09_005438_add_progress_percentage_to_playback_progress.php b/database/migrations/2026_04_09_005438_add_progress_percentage_to_playback_progress.php
new file mode 100644
index 00000000..08ead549
--- /dev/null
+++ b/database/migrations/2026_04_09_005438_add_progress_percentage_to_playback_progress.php
@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\DB;
+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->unsignedTinyInteger('progress_percentage')->default(0);
+ });
+
+ DB::statement('
+ UPDATE playback_progress pp
+ SET progress_percentage = COALESCE(
+ LEAST(100, GREATEST(0, ROUND(pp.progress_offset * 100.0 / NULLIF(m.duration, 0), 0))),
+ 0
+ )
+ FROM metadata m
+ WHERE m.id = pp.metadata_id
+ ');
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::table('playback_progress', function (Blueprint $table) {
+ $table->dropColumn('progress_percentage');
+ });
+ }
+};