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

77 lines
1.9 KiB
PHP
Raw Normal View History

2025-06-30 19:54:43 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\Factory;
2025-08-10 15:20:28 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2025-06-30 19:54:43 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2025-06-30 19:54:43 +02:00
/**
2025-07-07 00:51:32 +02:00
* @method static updateOrCreate(array<string, mixed> $array, array<string, mixed> $array1)
2025-06-30 19:54:43 +02:00
*/
class PlatformChannelPost extends Model
{
/** @use HasFactory<Factory<PlatformChannelPost>> */
2025-08-10 15:20:28 +02:00
use HasFactory;
2025-06-30 19:54:43 +02:00
protected $fillable = [
'platform_channel_id',
2025-06-30 19:54:43 +02:00
'post_id',
'url',
'title',
'posted_at',
];
2025-07-07 00:51:32 +02:00
/**
* @return array<string, string>
*/
2025-06-30 19:54:43 +02:00
protected function casts(): array
{
return [
'posted_at' => 'datetime',
];
}
/**
* @return BelongsTo<PlatformChannel, $this>
*/
public function platformChannel(): BelongsTo
2025-06-30 19:54:43 +02:00
{
return $this->belongsTo(PlatformChannel::class);
2025-06-30 19:54:43 +02:00
}
public static function duplicateExists(PlatformChannel $channel, ?string $url, ?string $title): bool
{
if (! $url && ! $title) {
return false;
}
return self::where('platform_channel_id', $channel->id)
->where(function ($query) use ($url, $title) {
if ($url) {
$query->orWhere('url', $url);
}
if ($title) {
$query->orWhere('title', $title);
}
})
->exists();
}
public static function storePost(PlatformChannel $channel, string $postId, ?string $url, ?string $title, ?\DateTime $postedAt = null): self
2025-06-30 19:54:43 +02:00
{
return self::updateOrCreate(
[
'platform_channel_id' => $channel->id,
2025-06-30 19:54:43 +02:00
'post_id' => $postId,
],
[
'url' => $url,
'title' => $title,
'posted_at' => $postedAt ?? now(),
]
);
}
}