1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-24 16:53:17 +00:00
pixelfed/app/Status.php

215 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace App;
2018-08-28 03:07:36 +00:00
use Auth;
2018-06-14 00:52:42 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
2018-08-28 03:07:36 +00:00
use Storage;
class Status extends Model
{
2018-06-14 00:52:42 +00:00
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
2018-08-28 03:07:36 +00:00
2018-04-17 01:24:18 +00:00
public function profile()
{
2018-08-28 03:07:36 +00:00
return $this->belongsTo(Profile::class);
2018-04-17 01:24:18 +00:00
}
public function media()
{
2018-08-28 03:07:36 +00:00
return $this->hasMany(Media::class);
2018-04-17 01:24:18 +00:00
}
public function firstMedia()
{
2018-08-28 03:07:36 +00:00
return $this->hasMany(Media::class)->orderBy('order', 'asc')->first();
2018-04-17 01:24:18 +00:00
}
2018-08-27 03:31:56 +00:00
public function viewType()
{
2018-08-28 03:07:36 +00:00
$media = $this->firstMedia();
$type = explode('/', $media->mime);
return $type[0];
2018-08-27 03:31:56 +00:00
}
2018-08-28 03:07:36 +00:00
2018-08-27 03:31:56 +00:00
public function thumb($showNsfw = false)
{
2018-08-28 03:07:36 +00:00
$type = $this->viewType();
$is_nsfw = !$showNsfw ? $this->is_nsfw : false;
if ($this->media->count() == 0 || $is_nsfw || $type != 'image') {
return 'data:image/gif;base64,R0lGODlhAQABAIAAAMLCwgAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==';
}
return url(Storage::url($this->firstMedia()->thumbnail_path));
}
2018-04-17 01:24:18 +00:00
public function url()
{
2018-08-28 03:07:36 +00:00
$id = $this->id;
$username = $this->profile->username;
$path = config('app.url')."/p/{$username}/{$id}";
if (!is_null($this->in_reply_to_id)) {
$pid = $this->in_reply_to_id;
$path = config('app.url')."/p/{$username}/{$pid}/c/{$id}";
}
return url($path);
2018-04-17 01:24:18 +00:00
}
2018-08-10 02:57:06 +00:00
public function permalink($suffix = '/activity')
{
2018-08-28 03:07:36 +00:00
$id = $this->id;
$username = $this->profile->username;
$path = config('app.url')."/p/{$username}/{$id}{$suffix}";
return url($path);
2018-08-10 02:57:06 +00:00
}
2018-06-14 00:52:42 +00:00
public function editUrl()
{
2018-08-28 03:07:36 +00:00
return $this->url().'/edit';
2018-06-14 00:52:42 +00:00
}
2018-04-17 01:24:18 +00:00
public function mediaUrl()
{
2018-08-28 03:07:36 +00:00
$media = $this->firstMedia();
$path = $media->media_path;
$hash = is_null($media->processed_at) ? md5('unprocessed') : md5($media->created_at);
$url = Storage::url($path)."?v={$hash}";
return url($url);
2018-04-17 01:24:18 +00:00
}
public function likes()
{
2018-08-28 03:07:36 +00:00
return $this->hasMany(Like::class);
}
2018-07-12 16:41:14 +00:00
public function liked() : bool
{
2018-08-28 03:07:36 +00:00
$profile = Auth::user()->profile;
return Like::whereProfileId($profile->id)->whereStatusId($this->id)->count();
2018-07-12 16:41:14 +00:00
}
2018-04-19 05:57:24 +00:00
public function comments()
{
2018-08-28 03:07:36 +00:00
return $this->hasMany(self::class, 'in_reply_to_id');
}
2018-07-12 16:41:14 +00:00
public function bookmarked()
{
2018-08-28 03:07:36 +00:00
if (!Auth::check()) {
return 0;
}
$profile = Auth::user()->profile;
return Bookmark::whereProfileId($profile->id)->whereStatusId($this->id)->count();
2018-07-12 16:41:14 +00:00
}
public function shares()
{
2018-08-28 03:07:36 +00:00
return $this->hasMany(self::class, 'reblog_of_id');
2018-07-12 16:41:14 +00:00
}
public function shared() : bool
{
2018-08-28 03:07:36 +00:00
$profile = Auth::user()->profile;
return self::whereProfileId($profile->id)->whereReblogOfId($this->id)->count();
2018-07-12 16:41:14 +00:00
}
public function parent()
{
2018-08-28 03:07:36 +00:00
$parent = $this->in_reply_to_id ?? $this->reblog_of_id;
if (!empty($parent)) {
return self::findOrFail($parent);
}
}
public function conversation()
{
2018-08-28 03:07:36 +00:00
return $this->hasOne(Conversation::class);
}
public function hashtags()
{
2018-08-28 03:07:36 +00:00
return $this->hasManyThrough(
Hashtag::class,
StatusHashtag::class,
'status_id',
'id',
'id',
'hashtag_id'
);
}
2018-07-12 16:41:14 +00:00
public function mentions()
{
2018-08-28 03:07:36 +00:00
return $this->hasManyThrough(
2018-07-12 16:41:14 +00:00
Profile::class,
Mention::class,
'status_id',
'id',
'id',
'profile_id'
);
}
public function reportUrl()
{
2018-08-28 03:07:36 +00:00
return route('report.form')."?type=post&id={$this->id}";
2018-07-12 16:41:14 +00:00
}
public function toActivityStream()
{
2018-08-28 03:07:36 +00:00
$media = $this->media;
$mediaCollection = [];
foreach ($media as $image) {
$mediaCollection[] = [
'type' => 'Link',
'href' => $image->url(),
'mediaType' => $image->mime,
];
2018-08-28 03:07:36 +00:00
}
$obj = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Image',
'name' => null,
'url' => $mediaCollection,
];
2018-08-28 03:07:36 +00:00
return $obj;
2018-04-19 05:57:24 +00:00
}
2018-06-04 08:16:33 +00:00
public function replyToText()
{
2018-08-28 03:07:36 +00:00
$actorName = $this->profile->username;
return "{$actorName} ".__('notification.commented');
2018-06-04 08:16:33 +00:00
}
public function replyToHtml()
{
2018-08-28 03:07:36 +00:00
$actorName = $this->profile->username;
$actorUrl = $this->profile->url();
return "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> ".
2018-06-04 08:16:33 +00:00
__('notification.commented');
}
2018-08-10 02:57:06 +00:00
public function recentComments()
{
2018-08-28 03:07:36 +00:00
return $this->comments()->orderBy('created_at', 'desc')->take(3);
2018-08-10 02:57:06 +00:00
}
}