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

140 lines
4 KiB
PHP
Raw Permalink Normal View History

2025-07-05 01:55:53 +02:00
<?php
namespace App\Models;
2025-07-06 01:35:59 +02:00
use Database\Factories\PlatformAccountFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2025-07-05 01:55:53 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
2025-07-05 02:19:59 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2025-07-05 01:55:53 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Crypt;
use App\Enums\PlatformEnum;
/**
* @property int $id
* @property PlatformEnum $platform
* @property string $instance_url
* @property string $username
* @property string $password
* @property string $settings
* @property bool $is_active
* @property Carbon $last_tested_at
* @property string $status
* @property Carbon $created_at
* @property Carbon $updated_at
2025-07-07 00:51:32 +02:00
* @property Collection<int, PlatformChannel> $activeChannels
2025-07-05 01:55:53 +02:00
* @method static where(string $string, PlatformEnum $platform)
* @method static orderBy(string $string)
2025-07-07 00:51:32 +02:00
* @method static create(array<string, mixed> $validated)
2025-07-05 01:55:53 +02:00
*/
class PlatformAccount extends Model
{
2025-07-06 01:35:59 +02:00
/** @use HasFactory<PlatformAccountFactory> */
use HasFactory;
2025-07-05 01:55:53 +02:00
protected $fillable = [
'platform',
'instance_url',
'username',
'password',
'settings',
'is_active',
'last_tested_at',
'status'
];
protected $casts = [
'platform' => PlatformEnum::class,
'settings' => 'array',
'is_active' => 'boolean',
'last_tested_at' => 'datetime'
];
// Encrypt password when storing
2025-07-07 00:51:32 +02:00
/**
* @return Attribute<string|null, string|null>
*/
2025-07-05 01:55:53 +02:00
protected function password(): Attribute
{
return Attribute::make(
2025-08-10 15:20:28 +02:00
get: function ($value, array $attributes) {
// Return null if the raw value is null
if (is_null($value)) {
return null;
}
// Return empty string if value is empty
if (empty($value)) {
return '';
}
try {
return Crypt::decryptString($value);
} catch (\Exception $e) {
// If decryption fails, return null to be safe
return null;
}
},
set: function ($value) {
// Store null if null is passed
if (is_null($value)) {
return null;
}
// Store empty string as null
if (empty($value)) {
return null;
}
return Crypt::encryptString($value);
},
)->withoutObjectCaching();
2025-07-05 01:55:53 +02:00
}
// Get the active accounts for a platform (returns collection)
2025-07-07 00:51:32 +02:00
/**
* @return Collection<int, PlatformAccount>
*/
2025-07-05 01:55:53 +02:00
public static function getActive(PlatformEnum $platform): Collection
{
return static::where('platform', $platform)
->where('is_active', true)
->get();
}
// Set this account as active (deactivates others for same platform)
public function setAsActive(): void
{
// Deactivate other accounts for this platform
static::where('platform', $this->platform)
->where('id', '!=', $this->id)
->update(['is_active' => false]);
// Activate this account
$this->update(['is_active' => true]);
}
2025-07-05 02:19:59 +02:00
2025-07-07 00:51:32 +02:00
/**
* @return BelongsToMany<PlatformChannel, $this>
*/
2025-07-05 02:29:50 +02:00
public function channels(): BelongsToMany
2025-07-05 02:19:59 +02:00
{
2025-07-05 02:29:50 +02:00
return $this->belongsToMany(PlatformChannel::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<PlatformChannel, $this>
*/
2025-07-05 02:29:50 +02:00
public function activeChannels(): BelongsToMany
2025-07-05 02:19:59 +02:00
{
2025-07-05 02:29:50 +02:00
return $this->channels()
2025-07-05 02:19:59 +02:00
->wherePivot('is_active', true)
->orderByPivot('priority', 'desc');
}
2025-07-05 01:55:53 +02:00
}