fedi-feed-router/app/Models/PlatformChannelPost.php
myrmidex d2919758f5
All checks were successful
CI / ci (push) Successful in 5m52s
CI / ci (pull_request) Successful in 5m46s
Build and Push Docker Image / build (push) Successful in 4m6s
Fix Pint 1.29.0 lint issues and update CI workflow
2026-03-18 20:01:25 +01:00

83 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\PlatformEnum;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @method static where(string $string, PlatformEnum $platform)
* @method static updateOrCreate(array<string, mixed> $array, array<string, mixed> $array1)
*/
class PlatformChannelPost extends Model
{
/** @use HasFactory<Factory<PlatformChannelPost>> */
use HasFactory;
protected $fillable = [
'platform',
'channel_id',
'channel_name',
'post_id',
'url',
'title',
'posted_at',
];
/**
* @return array<string, string>
*/
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 duplicateExists(PlatformEnum $platform, string $channelId, ?string $url, ?string $title): bool
{
if (! $url && ! $title) {
return false;
}
return self::where('platform', $platform)
->where('channel_id', $channelId)
->where(function ($query) use ($url, $title) {
if ($url) {
$query->orWhere('url', $url);
}
if ($title) {
$query->orWhere('title', $title);
}
})
->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(),
]
);
}
}