2019-08-26 20:18:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use App\Concerns\Models\HasTags;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-10-10 14:02:10 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2019-10-22 11:19:54 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2019-08-26 20:18:45 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
use Illuminate\Http\Request;
|
2019-10-22 19:29:59 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2019-08-26 20:18:45 +01:00
|
|
|
use Laravel\Scout\Searchable;
|
|
|
|
|
|
|
|
/**
|
2019-10-22 19:29:59 +01:00
|
|
|
* @method Builder|Post pinnedFirst()
|
2019-09-28 16:10:35 +01:00
|
|
|
* @method Builder|Post withPrivate(bool|User|Request $private)
|
|
|
|
* @method Builder|Post withoutChests()
|
2019-08-26 20:18:45 +01:00
|
|
|
*/
|
|
|
|
class Post extends Model
|
|
|
|
{
|
|
|
|
use HasTags,
|
|
|
|
Searchable;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'postable_type',
|
|
|
|
'postable_id',
|
|
|
|
'is_private',
|
2019-10-22 19:29:59 +01:00
|
|
|
'is_pinned',
|
2019-10-10 14:02:10 +01:00
|
|
|
'user_id',
|
2019-08-27 15:39:48 +01:00
|
|
|
'created_at',
|
2019-08-26 20:18:45 +01:00
|
|
|
];
|
|
|
|
protected $casts = [
|
2019-10-22 19:29:59 +01:00
|
|
|
'is_pinned' => 'bool',
|
2019-08-26 20:18:45 +01:00
|
|
|
'is_private' => 'bool',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function postable(): MorphTo
|
|
|
|
{
|
|
|
|
return $this->morphTo();
|
|
|
|
}
|
|
|
|
|
2019-10-10 14:02:10 +01:00
|
|
|
public function user(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2019-10-22 11:19:54 +01:00
|
|
|
public function shares(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Share::class);
|
|
|
|
}
|
|
|
|
|
2019-09-29 20:11:05 +01:00
|
|
|
public function getCreatedAtFormatedAttribute(): string
|
|
|
|
{
|
|
|
|
return $this->created_at->diffForHumans();
|
|
|
|
}
|
|
|
|
|
2019-10-22 19:29:59 +01:00
|
|
|
public function setIsPinnedAttribute($value): void
|
|
|
|
{
|
|
|
|
if ($value instanceof Collection) {
|
|
|
|
$value = (bool)$value->get('is_pinned', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->attributes['is_pinned'] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setIsPrivateAttribute($value): void
|
|
|
|
{
|
|
|
|
if ($value instanceof Collection) {
|
|
|
|
$value = (bool)$value->get('is_private', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->attributes['is_private'] = $value;
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:42:46 +01:00
|
|
|
public function scopeWithPrivate(Builder $query, $user = null): Builder
|
2019-08-26 20:18:45 +01:00
|
|
|
{
|
2019-10-11 14:42:46 +01:00
|
|
|
if ($user instanceof Request) {
|
|
|
|
$user = $user->user();
|
2019-08-26 20:18:45 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:42:46 +01:00
|
|
|
if (empty($user)) {
|
2019-10-10 14:02:10 +01:00
|
|
|
return $query->where('is_private', 0);
|
2019-08-26 20:18:45 +01:00
|
|
|
}
|
|
|
|
|
2019-10-10 14:02:10 +01:00
|
|
|
if ($user->is_admin === false) {
|
|
|
|
return $query
|
|
|
|
->where('is_private', 0)
|
|
|
|
->orWhere(function ($query) use ($user) {
|
|
|
|
return $query
|
|
|
|
->where('is_private', 1)
|
|
|
|
->where('user_id', $user->id);
|
|
|
|
});
|
2019-08-26 20:18:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2019-10-22 19:29:59 +01:00
|
|
|
public function scopePinnedFirst(Builder $query): Builder
|
|
|
|
{
|
|
|
|
return $query->orderByDesc('is_pinned');
|
|
|
|
}
|
|
|
|
|
2019-09-28 16:10:35 +01:00
|
|
|
public function scopeWithoutChests(Builder $query): Builder
|
|
|
|
{
|
|
|
|
return $query->where('postable_type', '!=', Chest::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeOnlyLinks(Builder $query): Builder
|
|
|
|
{
|
|
|
|
return $query->where('postable_type', '=', Link::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeLinksWithArchive(Builder $query): Builder
|
|
|
|
{
|
|
|
|
return $query->whereHasMorph('postable', Link::class, function (Builder $query) {
|
|
|
|
return $query->whereNotNull('archive');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:18:45 +01:00
|
|
|
public function toSearchableArray()
|
|
|
|
{
|
2019-08-27 14:43:44 +01:00
|
|
|
return array_merge([
|
2019-08-26 20:18:45 +01:00
|
|
|
'id' => $this->id,
|
2019-08-27 14:43:44 +01:00
|
|
|
'date' => $this->created_at->toDateTimeString(),
|
|
|
|
], $this->postable->toSearchableArray());
|
2019-08-26 20:18:45 +01:00
|
|
|
}
|
|
|
|
}
|