pixelfed/app/Story.php

110 wiersze
2.2 KiB
PHP

2018-11-04 03:45:44 +00:00
<?php
namespace App;
2025-08-17 08:22:38 +00:00
use App\Util\Lexer\Bearcap;
2019-01-12 20:14:22 +00:00
use Auth;
2018-11-04 03:45:44 +00:00
use Illuminate\Database\Eloquent\Model;
2025-08-17 08:22:38 +00:00
use Storage;
2018-11-04 03:45:44 +00:00
class Story extends Model
{
2019-11-11 00:25:28 +00:00
use HasSnowflakePrimary;
2020-01-29 04:32:32 +00:00
public const MAX_PER_DAY = 20;
2019-11-11 00:25:28 +00:00
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
2021-08-31 06:41:43 +00:00
protected $casts = [
2025-08-30 04:45:34 +00:00
'story' => 'json',
2025-08-17 08:22:38 +00:00
'expires_at' => 'datetime',
2025-08-30 04:45:34 +00:00
'view_count' => 'integer',
2021-08-31 06:41:43 +00:00
];
2019-11-11 00:25:28 +00:00
2021-08-31 06:41:43 +00:00
protected $fillable = ['profile_id', 'view_count'];
2019-12-31 09:33:54 +00:00
2025-08-17 08:22:38 +00:00
protected $visible = ['id'];
protected $hidden = ['json'];
public function profile()
{
return $this->belongsTo(Profile::class);
}
public function views()
{
return $this->hasMany(StoryView::class);
}
public function seen($pid = false)
{
return StoryView::whereStoryId($this->id)
->whereProfileId(Auth::user()->profile->id)
->exists();
}
public function permalink()
{
$username = $this->profile->username;
return url("/stories/{$username}/{$this->id}/activity");
}
public function url()
{
$username = $this->profile->username;
return url("/stories/{$username}/{$this->id}");
}
public function mediaUrl()
{
return url(Storage::url($this->path));
}
public function bearcapUrl()
{
return Bearcap::encode($this->url(), $this->bearcap_token);
}
public function scopeToAudience($scope)
{
$res = [];
switch ($scope) {
case 'to':
$res = [
$this->profile->permalink('/followers'),
];
break;
default:
$res = [];
break;
}
return $res;
}
public function toAdminEntity()
{
return [
'id' => $this->id,
'profile_id' => $this->profile_id,
'media_src' => $this->mediaUrl(),
'url' => $this->url(),
'type' => $this->type,
'duration' => $this->duration,
'mime' => $this->mime,
'size' => $this->size,
'local' => $this->local,
];
}
2018-11-04 03:45:44 +00:00
}