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

105 lines
2.6 KiB
PHP
Raw Normal View History

2025-07-05 02:19:59 +02:00
<?php
namespace App\Models;
2025-07-06 01:35:59 +02:00
use Database\Factories\PlatformChannelFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2025-07-05 02:19:59 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
2025-07-07 00:51:32 +02:00
* @method static create(array<string, mixed> $validated)
2025-07-05 18:26:04 +02:00
* @method static findMany(mixed $channel_ids)
* @property integer $id
* @property integer $platform_instance_id
2025-07-05 02:19:59 +02:00
* @property PlatformInstance $platformInstance
2025-07-05 18:26:04 +02:00
* @property integer $channel_id
* @property string $name
* @property int $language_id
2025-07-06 20:45:40 +02:00
* @property Language|null $language
2025-07-05 18:26:04 +02:00
* @property boolean $is_active
2025-07-05 02:19:59 +02:00
*/
2025-07-05 02:29:50 +02:00
class PlatformChannel extends Model
2025-07-05 02:19:59 +02:00
{
2025-07-06 01:35:59 +02:00
/** @use HasFactory<PlatformChannelFactory> */
use HasFactory;
2025-07-05 02:29:50 +02:00
protected $table = 'platform_channels';
2025-07-05 18:26:04 +02:00
2025-07-05 02:19:59 +02:00
protected $fillable = [
'platform_instance_id',
'name',
'display_name',
2025-07-05 02:29:50 +02:00
'channel_id',
2025-07-05 02:19:59 +02:00
'description',
2025-07-05 18:26:04 +02:00
'language_id',
2025-07-05 02:19:59 +02:00
'is_active'
];
protected $casts = [
'is_active' => 'boolean'
];
2025-07-07 00:51:32 +02:00
/**
* @return BelongsTo<PlatformInstance, $this>
*/
2025-07-05 02:19:59 +02:00
public function platformInstance(): BelongsTo
{
return $this->belongsTo(PlatformInstance::class);
}
2025-07-07 00:51:32 +02:00
/**
* @return BelongsToMany<PlatformAccount, $this>
*/
2025-07-05 02:19:59 +02:00
public function platformAccounts(): BelongsToMany
{
2025-07-05 02:29:50 +02:00
return $this->belongsToMany(PlatformAccount::class, 'platform_account_channels')
2025-07-05 02:19:59 +02:00
->withPivot(['is_active', 'priority'])
->withTimestamps();
}
2025-07-07 00:51:32 +02:00
/**
* @return BelongsToMany<PlatformAccount, $this>
*/
2025-07-06 20:37:55 +02:00
public function activePlatformAccounts(): BelongsToMany
{
return $this->platformAccounts()->where('is_active', true);
}
2025-07-05 02:19:59 +02:00
public function getFullNameAttribute(): string
{
2025-07-06 20:45:40 +02:00
// For Lemmy, use /c/ prefix
return $this->platformInstance->url . '/c/' . $this->name;
2025-07-05 02:19:59 +02:00
}
2025-07-05 18:26:04 +02:00
2025-07-07 00:51:32 +02:00
/**
2025-07-10 11:14:06 +02:00
* @return BelongsToMany<Feed, $this, Route>
2025-07-07 00:51:32 +02:00
*/
2025-07-05 18:26:04 +02:00
public function feeds(): BelongsToMany
{
2025-07-10 11:14:06 +02:00
return $this->belongsToMany(Feed::class, 'routes')
->using(Route::class)
2025-07-05 18:26:04 +02:00
->withPivot(['is_active', 'priority', 'filters'])
->withTimestamps();
}
2025-07-07 00:51:32 +02:00
/**
2025-07-10 11:14:06 +02:00
* @return BelongsToMany<Feed, $this, Route>
2025-07-07 00:51:32 +02:00
*/
2025-07-05 18:26:04 +02:00
public function activeFeeds(): BelongsToMany
{
return $this->feeds()
->wherePivot('is_active', true)
->orderByPivot('priority', 'desc');
}
2025-07-07 00:51:32 +02:00
/**
* @return BelongsTo<Language, $this>
*/
2025-07-05 18:26:04 +02:00
public function language(): BelongsTo
{
return $this->belongsTo(Language::class);
}
2025-07-05 02:19:59 +02:00
}