pixelfed/app/Status.php

123 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
2018-04-17 01:24:18 +00:00
use Storage;
use Vinkla\Hashids\Facades\Hashids;
class Status extends Model
{
2018-04-17 01:24:18 +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 thumb()
{
2018-06-09 23:32:07 +00:00
if($this->media->count() == 0) {
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()
{
$id = $this->id;
2018-04-17 01:24:18 +00:00
$username = $this->profile->username;
2018-06-04 06:07:40 +00:00
$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
}
public function mediaUrl()
{
$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}";
2018-04-17 01:24:18 +00:00
return url($url);
}
public function likes()
{
return $this->hasMany(Like::class);
}
2018-04-19 05:57:24 +00:00
public function comments()
{
return $this->hasMany(Status::class, 'in_reply_to_id');
}
public function parent()
{
if(!empty($this->in_reply_to_id)) {
return Status::findOrFail($this->in_reply_to_id);
}
}
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 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;
2018-04-19 05:57:24 +00:00
}
2018-06-04 08:16:33 +00:00
public function replyToText()
{
$actorName = $this->profile->username;
return "{$actorName} " . __('notification.commented');
}
public function replyToHtml()
{
$actorName = $this->profile->username;
$actorUrl = $this->profile->url();
return "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> " .
__('notification.commented');
}
}