168 lines
3.7 KiB
PHP
168 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Video extends Model
|
|
{
|
|
protected $fillable = [
|
|
'user_id',
|
|
'title',
|
|
'description',
|
|
'filename',
|
|
'path',
|
|
'thumbnail',
|
|
'duration',
|
|
'size',
|
|
'mime_type',
|
|
'orientation',
|
|
'width',
|
|
'height',
|
|
'status',
|
|
'visibility',
|
|
'type',
|
|
];
|
|
|
|
protected $casts = [
|
|
'duration' => 'integer',
|
|
'size' => 'integer',
|
|
'width' => 'integer',
|
|
'height' => 'integer',
|
|
];
|
|
|
|
// Relationships
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function likes()
|
|
{
|
|
return $this->belongsToMany(User::class, 'video_likes')->withTimestamps();
|
|
}
|
|
|
|
public function viewers()
|
|
{
|
|
return $this->belongsToMany(User::class, 'video_views')->withTimestamps();
|
|
}
|
|
|
|
// Accessors
|
|
public function getUrlAttribute()
|
|
{
|
|
return asset('storage/videos/' . $this->filename);
|
|
}
|
|
|
|
public function getThumbnailUrlAttribute()
|
|
{
|
|
if ($this->thumbnail) {
|
|
return asset('storage/thumbnails/' . $this->thumbnail);
|
|
}
|
|
return asset('images/video-placeholder.jpg');
|
|
}
|
|
|
|
// Check if video is liked by user
|
|
public function isLikedBy($user)
|
|
{
|
|
if (!$user) return false;
|
|
return $this->likes()->where('user_id', $user->id)->exists();
|
|
}
|
|
|
|
// Get like count
|
|
public function getLikeCountAttribute()
|
|
{
|
|
return $this->likes()->count();
|
|
}
|
|
|
|
// Get view count
|
|
public function getViewCountAttribute()
|
|
{
|
|
return $this->viewers()->count();
|
|
}
|
|
|
|
// Get shareable URL for the video
|
|
public function getShareUrlAttribute()
|
|
{
|
|
return route('videos.show', $this->id);
|
|
}
|
|
|
|
// Visibility helpers
|
|
public function isPublic()
|
|
{
|
|
return $this->visibility === 'public';
|
|
}
|
|
|
|
public function isUnlisted()
|
|
{
|
|
return $this->visibility === 'unlisted';
|
|
}
|
|
|
|
public function isPrivate()
|
|
{
|
|
return $this->visibility === 'private';
|
|
}
|
|
|
|
// Check if user can view this video
|
|
public function canView($user = null)
|
|
{
|
|
// Owner can always view
|
|
if ($user && $this->user_id === $user->id) {
|
|
return true;
|
|
}
|
|
|
|
// Public and unlisted videos can be viewed by anyone
|
|
return in_array($this->visibility, ['public', 'unlisted']);
|
|
}
|
|
|
|
// Check if video is shareable
|
|
public function isShareable()
|
|
{
|
|
// Only public and unlisted videos are shareable
|
|
return in_array($this->visibility, ['public', 'unlisted']);
|
|
}
|
|
|
|
// Scope for public videos (home page, search)
|
|
public function scopePublic($query)
|
|
{
|
|
return $query->where('visibility', 'public');
|
|
}
|
|
|
|
// Scope for videos visible to a specific user
|
|
public function scopeVisibleTo($query, $user = null)
|
|
{
|
|
if ($user) {
|
|
return $query->where(function ($q) use ($user) {
|
|
$q->where('visibility', '!=', 'private')
|
|
->orWhere('user_id', $user->id);
|
|
});
|
|
}
|
|
return $query->where('visibility', '!=', 'private');
|
|
}
|
|
|
|
// Video type helpers
|
|
public function getTypeIconAttribute()
|
|
{
|
|
return match($this->type) {
|
|
'music' => 'bi-music-note',
|
|
'match' => 'bi-trophy',
|
|
default => 'bi-film',
|
|
};
|
|
}
|
|
|
|
public function isGeneric()
|
|
{
|
|
return $this->type === 'generic';
|
|
}
|
|
|
|
public function isMusic()
|
|
{
|
|
return $this->type === 'music';
|
|
}
|
|
|
|
public function isMatch()
|
|
{
|
|
return $this->type === 'match';
|
|
}
|
|
}
|
|
|