fedi-feed-router/app/Models/PlatformChannel.php
2025-07-06 20:37:55 +02:00

87 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use Database\Factories\PlatformChannelFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* @method static create(array $validated)
* @method static findMany(mixed $channel_ids)
* @property integer $id
* @property integer $platform_instance_id
* @property PlatformInstance $platformInstance
* @property integer $channel_id
* @property string $name
* @property int $language_id
* @property Language $language
* @property boolean $is_active
*/
class PlatformChannel extends Model
{
/** @use HasFactory<PlatformChannelFactory> */
use HasFactory;
protected $table = 'platform_channels';
protected $fillable = [
'platform_instance_id',
'name',
'display_name',
'channel_id',
'description',
'language_id',
'is_active'
];
protected $casts = [
'is_active' => 'boolean'
];
public function platformInstance(): BelongsTo
{
return $this->belongsTo(PlatformInstance::class);
}
public function platformAccounts(): BelongsToMany
{
return $this->belongsToMany(PlatformAccount::class, 'platform_account_channels')
->withPivot(['is_active', 'priority'])
->withTimestamps();
}
public function activePlatformAccounts(): BelongsToMany
{
return $this->platformAccounts()->where('is_active', true);
}
public function getFullNameAttribute(): string
{
// For Lemmy, use /c/ prefix. Other platforms may use different formats
$prefix = $this->platformInstance->platform === 'lemmy' ? '/c/' : '/';
return $this->platformInstance->url . $prefix . $this->name;
}
public function feeds(): BelongsToMany
{
return $this->belongsToMany(Feed::class, 'feed_platform_channels')
->using(FeedPlatformChannel::class)
->withPivot(['is_active', 'priority', 'filters'])
->withTimestamps();
}
public function activeFeeds(): BelongsToMany
{
return $this->feeds()
->wherePivot('is_active', true)
->orderByPivot('priority', 'desc');
}
public function language(): BelongsTo
{
return $this->belongsTo(Language::class);
}
}