117 lines
2.5 KiB
PHP
117 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
//use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Laravelista\Comments\Commenter;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasFactory, Notifiable, Commenter;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'locale',
|
|
'is_banned',
|
|
// Discord
|
|
'discord_id',
|
|
'discord_avatar',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
// Laravel defaults
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
// Other
|
|
'name' => 'string',
|
|
'email' => 'string',
|
|
'locale' => 'string',
|
|
'roles' => 'json',
|
|
'tag_blacklist' => 'array',
|
|
// Discord
|
|
'discord_id' => 'integer',
|
|
'discord_avatar' => 'string',
|
|
];
|
|
|
|
|
|
/**
|
|
* Has Many Playlists.
|
|
*/
|
|
public function playlists(): HasMany
|
|
{
|
|
return $this->hasMany(Playlist::class);
|
|
}
|
|
|
|
/**
|
|
* Has Many Watched Episodes.
|
|
*/
|
|
public function watched(): HasMany
|
|
{
|
|
return $this->hasMany(Watched::class);
|
|
}
|
|
|
|
/**
|
|
* Has Many Watched Episodes.
|
|
*/
|
|
public function likes(): int
|
|
{
|
|
return DB::table('markable_likes')->where('user_id', $this->id)->count();
|
|
}
|
|
|
|
/**
|
|
* Has Many Comments.
|
|
*/
|
|
public function commentCount(): int
|
|
{
|
|
return DB::table('comments')->where('commenter_id', $this->id)->count();
|
|
}
|
|
|
|
/**
|
|
* Returns the user avatar image url.
|
|
*/
|
|
public function getAvatar(): string
|
|
{
|
|
if ($this->discord_id && $this->discord_avatar && !$this->avatar)
|
|
{
|
|
return "https://external-content.duckduckgo.com/iu/?u={$this->discord_avatar}";
|
|
}
|
|
|
|
if ($this->avatar)
|
|
{
|
|
return Storage::url($this->avatar);
|
|
}
|
|
|
|
return asset('images/default-avatar.webp');
|
|
}
|
|
}
|