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

135 lines
3.2 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-08-10 21:18:20 +02:00
'content',
'image_url',
'published_at',
'author',
2025-07-10 14:57:10 +02:00
'approval_status',
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-07-10 14:57:10 +02:00
'approval_status' => 'string',
2025-08-10 21:18:20 +02:00
'published_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
{
2025-08-10 21:18:20 +02:00
// In the consolidated schema, we only have approval_status
// Consider 'approved' status as valid
return $this->approval_status === 'approved';
2025-06-29 19:46:50 +02:00
}
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',
]);
// Fire event to trigger publishing
event(new ArticleApproved($this));
}
public function reject(string $rejectedBy = null): void
{
$this->update([
'approval_status' => 'rejected',
]);
}
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
}