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

76 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @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',
'post_id',
'url',
'title',
'posted_at',
];
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'posted_at' => 'datetime',
];
}
/**
* @return BelongsTo<PlatformChannel, $this>
*/
public function platformChannel(): BelongsTo
{
return $this->belongsTo(PlatformChannel::class);
}
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
{
return self::updateOrCreate(
[
'platform_channel_id' => $channel->id,
'post_id' => $postId,
],
[
'url' => $url,
'title' => $title,
'posted_at' => $postedAt ?? now(),
]
);
}
}