2018-04-16 01:18:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2019-02-25 18:56:24 +00:00
|
|
|
use Auth, Cache, Hashids, Storage;
|
2018-06-14 00:52:42 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-09-02 04:46:57 +00:00
|
|
|
use App\HasSnowflakePrimary;
|
2019-03-12 05:38:21 +00:00
|
|
|
use App\Http\Controllers\StatusController;
|
2018-06-14 00:52:42 +00:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2021-08-31 06:40:41 +00:00
|
|
|
use App\Models\Poll;
|
2021-11-04 05:29:12 +00:00
|
|
|
use App\Services\AccountService;
|
2023-09-25 07:57:43 +00:00
|
|
|
use App\Services\StatusService;
|
2023-05-25 06:33:44 +00:00
|
|
|
use App\Models\StatusEdit;
|
2023-09-25 07:57:43 +00:00
|
|
|
use Illuminate\Support\Str;
|
2018-04-16 01:18:59 +00:00
|
|
|
|
|
|
|
class Status extends Model
|
|
|
|
{
|
2021-07-27 01:02:11 +00:00
|
|
|
use HasSnowflakePrimary, SoftDeletes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if the IDs are auto-incrementing.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-04-20 04:30:37 +00:00
|
|
|
protected $casts = [
|
2023-05-25 06:33:44 +00:00
|
|
|
'deleted_at' => 'datetime',
|
|
|
|
'edited_at' => 'datetime'
|
2023-04-20 04:30:37 +00:00
|
|
|
];
|
2021-07-27 01:02:11 +00:00
|
|
|
|
2022-09-01 07:21:06 +00:00
|
|
|
protected $guarded = [];
|
2021-07-27 01:02:11 +00:00
|
|
|
|
|
|
|
const STATUS_TYPES = [
|
|
|
|
'text',
|
|
|
|
'photo',
|
|
|
|
'photo:album',
|
|
|
|
'video',
|
|
|
|
'video:album',
|
|
|
|
'photo:video:album',
|
|
|
|
'share',
|
|
|
|
'reply',
|
|
|
|
'story',
|
|
|
|
'story:reply',
|
|
|
|
'story:reaction',
|
|
|
|
'story:live',
|
|
|
|
'loop'
|
|
|
|
];
|
|
|
|
|
2023-05-27 13:23:20 +00:00
|
|
|
const MAX_MENTIONS = 20;
|
2021-07-27 01:02:11 +00:00
|
|
|
|
2023-05-27 13:23:20 +00:00
|
|
|
const MAX_HASHTAGS = 60;
|
2021-07-27 01:02:11 +00:00
|
|
|
|
2023-05-27 13:23:20 +00:00
|
|
|
const MAX_LINKS = 5;
|
2021-07-27 01:02:11 +00:00
|
|
|
|
|
|
|
public function profile()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Profile::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function media()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Media::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function firstMedia()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Media::class)->orderBy('order', 'asc')->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function viewType()
|
|
|
|
{
|
|
|
|
if($this->type) {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
return $this->setType();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setType()
|
|
|
|
{
|
|
|
|
if(in_array($this->type, self::STATUS_TYPES)) {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
$mimes = $this->media->pluck('mime')->toArray();
|
|
|
|
$type = StatusController::mimeTypeCheck($mimes);
|
|
|
|
if($type) {
|
|
|
|
$this->type = $type;
|
|
|
|
$this->save();
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function thumb($showNsfw = false)
|
|
|
|
{
|
2023-09-25 08:10:19 +00:00
|
|
|
$entity = StatusService::get($this->id, false);
|
2023-09-25 07:57:43 +00:00
|
|
|
|
|
|
|
if(!$entity || !isset($entity['media_attachments']) || empty($entity['media_attachments'])) {
|
|
|
|
return url(Storage::url('public/no-preview.png'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if((!isset($entity['sensitive']) || $entity['sensitive']) && !$showNsfw) {
|
|
|
|
return url(Storage::url('public/no-preview.png'));
|
|
|
|
}
|
|
|
|
|
2023-09-25 08:10:19 +00:00
|
|
|
if(!isset($entity['visibility']) || !in_array($entity['visibility'], ['public', 'unlisted'])) {
|
|
|
|
return url(Storage::url('public/no-preview.png'));
|
|
|
|
}
|
|
|
|
|
2023-09-25 07:57:43 +00:00
|
|
|
return collect($entity['media_attachments'])
|
|
|
|
->filter(fn($media) => $media['type'] == 'image' && in_array($media['mime'], ['image/jpeg', 'image/png']))
|
|
|
|
->map(function($media) {
|
|
|
|
if(!Str::endsWith($media['preview_url'], ['no-preview.png', 'no-preview.jpg'])) {
|
|
|
|
return $media['preview_url'];
|
|
|
|
}
|
2021-07-27 01:02:11 +00:00
|
|
|
|
2023-09-25 07:57:43 +00:00
|
|
|
return $media['url'];
|
|
|
|
})
|
|
|
|
->first() ?? url(Storage::url('public/no-preview.png'));
|
2021-07-27 01:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function url($forceLocal = false)
|
|
|
|
{
|
|
|
|
if($this->uri) {
|
|
|
|
return $forceLocal ? "/i/web/post/_/{$this->profile_id}/{$this->id}" : $this->uri;
|
|
|
|
} else {
|
|
|
|
$id = $this->id;
|
2021-11-04 05:29:12 +00:00
|
|
|
$account = AccountService::get($this->profile_id, true);
|
|
|
|
if(!$account || !isset($account['username'])) {
|
|
|
|
return '/404';
|
|
|
|
}
|
|
|
|
$path = url(config('app.url')."/p/{$account['username']}/{$id}");
|
2021-07-27 01:02:11 +00:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function permalink($suffix = '/activity')
|
|
|
|
{
|
|
|
|
$id = $this->id;
|
|
|
|
$username = $this->profile->username;
|
|
|
|
$path = config('app.url')."/p/{$username}/{$id}{$suffix}";
|
|
|
|
|
|
|
|
return url($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function editUrl()
|
|
|
|
{
|
|
|
|
return $this->url().'/edit';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mediaUrl()
|
|
|
|
{
|
|
|
|
$media = $this->firstMedia();
|
|
|
|
$path = $media->media_path;
|
|
|
|
$hash = is_null($media->processed_at) ? md5('unprocessed') : md5($media->created_at);
|
|
|
|
$url = $media->cdn_url ? $media->cdn_url . "?v={$hash}" : url(Storage::url($path)."?v={$hash}");
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function likes()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Like::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function liked() : bool
|
|
|
|
{
|
|
|
|
if(!Auth::check()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pid = Auth::user()->profile_id;
|
|
|
|
|
|
|
|
return Like::select('status_id', 'profile_id')
|
|
|
|
->whereStatusId($this->id)
|
|
|
|
->whereProfileId($pid)
|
|
|
|
->exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function likedBy()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(
|
|
|
|
Profile::class,
|
|
|
|
Like::class,
|
|
|
|
'status_id',
|
|
|
|
'id',
|
|
|
|
'id',
|
|
|
|
'profile_id'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function comments()
|
|
|
|
{
|
|
|
|
return $this->hasMany(self::class, 'in_reply_to_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bookmarked()
|
|
|
|
{
|
|
|
|
if (!Auth::check()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$profile = Auth::user()->profile;
|
|
|
|
|
|
|
|
return Bookmark::whereProfileId($profile->id)->whereStatusId($this->id)->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shares()
|
|
|
|
{
|
|
|
|
return $this->hasMany(self::class, 'reblog_of_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shared() : bool
|
|
|
|
{
|
|
|
|
if(!Auth::check()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$pid = Auth::user()->profile_id;
|
|
|
|
|
|
|
|
return $this->select('profile_id', 'reblog_of_id')
|
|
|
|
->whereProfileId($pid)
|
|
|
|
->whereReblogOfId($this->id)
|
|
|
|
->exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sharedBy()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(
|
|
|
|
Profile::class,
|
|
|
|
Status::class,
|
|
|
|
'reblog_of_id',
|
|
|
|
'id',
|
|
|
|
'id',
|
|
|
|
'profile_id'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parent()
|
|
|
|
{
|
|
|
|
$parent = $this->in_reply_to_id ?? $this->reblog_of_id;
|
|
|
|
if (!empty($parent)) {
|
|
|
|
return $this->findOrFail($parent);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function conversation()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Conversation::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hashtags()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(
|
|
|
|
Hashtag::class,
|
|
|
|
StatusHashtag::class,
|
|
|
|
'status_id',
|
|
|
|
'id',
|
|
|
|
'id',
|
|
|
|
'hashtag_id'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mentions()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(
|
|
|
|
Profile::class,
|
|
|
|
Mention::class,
|
|
|
|
'status_id',
|
|
|
|
'id',
|
|
|
|
'id',
|
|
|
|
'profile_id'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reportUrl()
|
|
|
|
{
|
|
|
|
return route('report.form')."?type=post&id={$this->id}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toActivityStream()
|
|
|
|
{
|
|
|
|
$media = $this->media;
|
|
|
|
$mediaCollection = [];
|
|
|
|
foreach ($media as $image) {
|
|
|
|
$mediaCollection[] = [
|
|
|
|
'type' => 'Link',
|
|
|
|
'href' => $image->url(),
|
|
|
|
'mediaType' => $image->mime,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$obj = [
|
|
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type' => 'Image',
|
|
|
|
'name' => null,
|
|
|
|
'url' => $mediaCollection,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function recentComments()
|
|
|
|
{
|
|
|
|
return $this->comments()->orderBy('created_at', 'desc')->take(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeToAudience($audience)
|
|
|
|
{
|
|
|
|
if(!in_array($audience, ['to', 'cc']) || $this->local == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$res = [];
|
|
|
|
$res['to'] = [];
|
|
|
|
$res['cc'] = [];
|
|
|
|
$scope = $this->scope;
|
|
|
|
$mentions = $this->mentions->map(function ($mention) {
|
|
|
|
return $mention->permalink();
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
if($this->in_reply_to_id != null) {
|
|
|
|
$parent = $this->parent();
|
|
|
|
if($parent) {
|
|
|
|
$mentions = array_merge([$parent->profile->permalink()], $mentions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($scope) {
|
|
|
|
case 'public':
|
|
|
|
$res['to'] = [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
];
|
|
|
|
$res['cc'] = array_merge([$this->profile->permalink('/followers')], $mentions);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'unlisted':
|
|
|
|
$res['to'] = array_merge([$this->profile->permalink('/followers')], $mentions);
|
|
|
|
$res['cc'] = [
|
|
|
|
"https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'private':
|
|
|
|
$res['to'] = array_merge([$this->profile->permalink('/followers')], $mentions);
|
|
|
|
$res['cc'] = [];
|
|
|
|
break;
|
|
|
|
|
|
|
|
// TODO: Update scope when DMs are supported
|
|
|
|
case 'direct':
|
|
|
|
$res['to'] = [];
|
|
|
|
$res['cc'] = [];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $res[$audience];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function place()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Place::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function directMessage()
|
|
|
|
{
|
|
|
|
return $this->hasOne(DirectMessage::class);
|
|
|
|
}
|
2020-11-19 02:19:07 +00:00
|
|
|
|
2021-08-31 06:40:41 +00:00
|
|
|
public function poll()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Poll::class);
|
|
|
|
}
|
2023-05-25 06:33:44 +00:00
|
|
|
|
|
|
|
public function edits()
|
|
|
|
{
|
|
|
|
return $this->hasMany(StatusEdit::class);
|
|
|
|
}
|
2018-04-16 01:18:59 +00:00
|
|
|
}
|