79 lines
1.7 KiB
PHP
79 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Storage;
|
|
|
|
class ServiceChatMessage extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public const PATH_PREFIX = 'public/chats';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'service_chat_id',
|
|
'message',
|
|
'from_user_id',
|
|
'to_user_id',
|
|
'to_admin',
|
|
'is_file',
|
|
'file_name',
|
|
'is_read',
|
|
];
|
|
|
|
public function getElapsedTimeAttribute()
|
|
{
|
|
$time = time() - $this->created_at->timestamp;
|
|
$time = $time < 1 ? 1 : $time;
|
|
$tokens = [
|
|
31536000 => 'year',
|
|
2592000 => 'month',
|
|
604800 => 'week',
|
|
86400 => 'day',
|
|
3600 => 'hour',
|
|
60 => 'min',
|
|
1 => 'second',
|
|
];
|
|
|
|
foreach ($tokens as $unit => $text) {
|
|
if ($time < $unit) {
|
|
continue;
|
|
}
|
|
|
|
$numberOfUnits = floor($time / $unit);
|
|
|
|
return $numberOfUnits . ' ' . $text . ($numberOfUnits > 1 ? 's' : '') . ' ago';
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
public function getFolderPathAttribute()
|
|
{
|
|
return self::PATH_PREFIX . '/' . $this->service_chat_id;
|
|
}
|
|
|
|
public function getFilePathAttribute()
|
|
{
|
|
return ! $this->file_name ? null : $this->getFolderPathAttribute() . "/{$this->file_name}";
|
|
}
|
|
|
|
public function getFileUrlAttribute()
|
|
{
|
|
$path = $this->getFilePathAttribute();
|
|
|
|
if ($path && Storage::exists($path)) {
|
|
return env('AWS_ENDPOINT') . env('AWS_BUCKET') . '/' . $path;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|