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

62 lines
1.6 KiB
PHP
Raw Normal View History

2025-06-30 19:54:43 +02:00
<?php
namespace App\Models;
use App\Enums\PlatformEnum;
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;
/**
* @method static where(string $string, PlatformEnum $platform)
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
{
2025-08-10 15:20:28 +02:00
use HasFactory;
2025-06-30 19:54:43 +02:00
protected $fillable = [
'platform',
'channel_id',
'channel_name',
'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',
'platform' => PlatformEnum::class,
];
}
public static function urlExists(PlatformEnum $platform, string $channelId, string $url): bool
{
return self::where('platform', $platform)
->where('channel_id', $channelId)
->where('url', $url)
->exists();
}
public static function storePost(PlatformEnum $platform, string $channelId, ?string $channelName, string $postId, ?string $url, ?string $title, ?\DateTime $postedAt = null): self
{
return self::updateOrCreate(
[
'platform' => $platform,
'channel_id' => $channelId,
'post_id' => $postId,
],
[
'channel_name' => $channelName,
'url' => $url,
'title' => $title,
'posted_at' => $postedAt ?? now(),
]
);
}
}