fedi-feed-router/backend/app/Models/Article.php

151 lines
3.6 KiB
PHP
Raw Normal View History

2025-06-29 09:37:49 +02:00
<?php
namespace App\Models;
2025-07-10 14:57:10 +02:00
use App\Events\ArticleApproved;
2025-07-05 18:26:04 +02:00
use App\Events\NewArticleFetched;
2025-06-29 09:37:49 +02:00
use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2025-07-05 18:26:04 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2025-06-30 18:18:30 +02:00
use Illuminate\Database\Eloquent\Relations\HasOne;
2025-06-29 19:46:50 +02:00
use Illuminate\Support\Carbon;
2025-06-29 09:37:49 +02:00
/**
2025-07-07 00:51:32 +02:00
* @method static firstOrCreate(array<string, mixed> $array)
2025-07-03 21:34:39 +02:00
* @method static where(string $string, string $url)
2025-07-07 00:51:32 +02:00
* @method static create(array<string, mixed> $array)
2025-06-29 19:46:50 +02:00
* @property integer $id
2025-07-05 18:26:04 +02:00
* @property int $feed_id
* @property Feed $feed
2025-06-29 19:46:50 +02:00
* @property string $url
* @property bool|null $is_valid
* @property Carbon|null $validated_at
* @property Carbon $created_at
* @property Carbon $updated_at
2025-06-30 18:18:30 +02:00
* @property ArticlePublication $articlePublication
2025-06-29 09:37:49 +02:00
*/
class Article extends Model
{
/** @use HasFactory<ArticleFactory> */
use HasFactory;
protected $fillable = [
2025-07-05 18:26:04 +02:00
'feed_id',
2025-06-29 09:37:49 +02:00
'url',
2025-06-29 21:20:45 +02:00
'title',
'description',
2025-06-29 19:46:50 +02:00
'is_valid',
2025-06-30 19:54:43 +02:00
'is_duplicate',
2025-07-10 14:57:10 +02:00
'approval_status',
'approved_at',
'approved_by',
2025-06-29 21:20:45 +02:00
'fetched_at',
2025-06-29 19:46:50 +02:00
'validated_at',
2025-06-29 09:37:49 +02:00
];
2025-07-07 00:51:32 +02:00
/**
* @return array<string, string>
*/
2025-06-29 09:37:49 +02:00
public function casts(): array
{
return [
2025-06-30 19:54:43 +02:00
'is_valid' => 'boolean',
'is_duplicate' => 'boolean',
2025-07-10 14:57:10 +02:00
'approval_status' => 'string',
'approved_at' => 'datetime',
2025-06-29 21:20:45 +02:00
'fetched_at' => 'datetime',
2025-06-29 19:46:50 +02:00
'validated_at' => 'datetime',
2025-06-29 09:37:49 +02:00
'created_at' => 'datetime',
2025-06-29 17:13:18 +02:00
'updated_at' => 'datetime',
2025-06-29 09:37:49 +02:00
];
}
2025-06-29 09:48:45 +02:00
2025-06-29 19:46:50 +02:00
public function isValid(): bool
{
if (is_null($this->validated_at)) {
return false;
}
if (is_null($this->is_valid)) {
return false;
}
return $this->is_valid;
}
2025-07-10 14:57:10 +02:00
public function isApproved(): bool
{
return $this->approval_status === 'approved';
}
public function isPending(): bool
{
return $this->approval_status === 'pending';
}
public function isRejected(): bool
{
return $this->approval_status === 'rejected';
}
public function approve(string $approvedBy = null): void
{
$this->update([
'approval_status' => 'approved',
'approved_at' => now(),
'approved_by' => $approvedBy,
]);
// Fire event to trigger publishing
event(new ArticleApproved($this));
}
public function reject(string $rejectedBy = null): void
{
$this->update([
'approval_status' => 'rejected',
'approved_at' => now(),
'approved_by' => $rejectedBy,
]);
}
public function canBePublished(): bool
{
if (!$this->isValid()) {
return false;
}
// If approval system is disabled, auto-approve valid articles
if (!\App\Models\Setting::isPublishingApprovalsEnabled()) {
return true;
}
// If approval system is enabled, only approved articles can be published
return $this->isApproved();
}
2025-07-07 00:51:32 +02:00
/**
* @return HasOne<ArticlePublication, $this>
*/
2025-06-30 18:18:30 +02:00
public function articlePublication(): HasOne
{
return $this->hasOne(ArticlePublication::class);
}
2025-07-07 00:51:32 +02:00
/**
* @return BelongsTo<Feed, $this>
*/
2025-07-05 18:26:04 +02:00
public function feed(): BelongsTo
2025-06-30 18:18:30 +02:00
{
2025-07-05 18:26:04 +02:00
return $this->belongsTo(Feed::class);
2025-06-30 18:18:30 +02:00
}
2025-06-29 19:46:50 +02:00
protected static function booted(): void
2025-06-29 09:48:45 +02:00
{
static::created(function ($article) {
2025-07-05 18:26:04 +02:00
event(new NewArticleFetched($article));
2025-06-29 09:48:45 +02:00
});
}
2025-06-29 09:37:49 +02:00
}