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

56 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use App\Enums\PlatformEnum;
use Illuminate\Database\Eloquent\Model;
/**
* @method static where(string $string, PlatformEnum $platform)
* @method static updateOrCreate(array $array, array $array1)
*/
class PlatformChannelPost extends Model
{
protected $fillable = [
'platform',
'channel_id',
'channel_name',
'post_id',
'url',
'title',
'posted_at',
];
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(),
]
);
}
}