fedi-feed-router/app/Http/Controllers/Api/V1/PlatformAccountsController.php

139 lines
4.4 KiB
PHP
Raw Normal View History

2025-08-02 15:20:09 +02:00
<?php
namespace App\Http\Controllers\Api\V1;
use App\Actions\CreatePlatformAccountAction;
use App\Exceptions\PlatformAuthException;
use App\Http\Requests\StorePlatformAccountRequest;
2025-08-02 15:20:09 +02:00
use App\Http\Resources\PlatformAccountResource;
use App\Models\PlatformAccount;
use Exception;
2025-08-02 15:20:09 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class PlatformAccountsController extends BaseController
{
/**
* Display a listing of platform accounts
*/
public function index(): JsonResponse
{
2025-08-05 21:53:49 +02:00
$accounts = PlatformAccount::orderBy('platform')
2025-08-02 15:20:09 +02:00
->orderBy('created_at', 'desc')
->get();
return $this->sendResponse(
PlatformAccountResource::collection($accounts),
'Platform accounts retrieved successfully.'
);
}
/**
* Store a newly created platform account
*/
public function store(StorePlatformAccountRequest $request, CreatePlatformAccountAction $action): JsonResponse
2025-08-02 15:20:09 +02:00
{
try {
$validated = $request->validated();
2025-08-02 15:20:09 +02:00
$account = $action->execute(
$validated['instance_domain'],
$validated['username'],
$validated['password'],
$validated['platform'],
);
2025-08-02 15:20:09 +02:00
return $this->sendResponse(
2025-08-05 21:53:49 +02:00
new PlatformAccountResource($account),
2025-08-02 15:20:09 +02:00
'Platform account created successfully!',
201
);
} catch (PlatformAuthException $e) {
if (str_contains($e->getMessage(), 'Rate limited by')) {
return $this->sendError($e->getMessage(), [], 429);
}
return $this->sendError('Invalid username or password. Please check your credentials and try again.', [], 422);
} catch (Exception $e) {
return $this->sendError('Unable to connect to the Lemmy instance. Please check the URL and try again.', [], 422);
2025-08-02 15:20:09 +02:00
}
}
/**
* Display the specified platform account
*/
public function show(PlatformAccount $platformAccount): JsonResponse
{
return $this->sendResponse(
2025-08-05 21:53:49 +02:00
new PlatformAccountResource($platformAccount),
2025-08-02 15:20:09 +02:00
'Platform account retrieved successfully.'
);
}
/**
* Update the specified platform account
*/
public function update(Request $request, PlatformAccount $platformAccount): JsonResponse
{
try {
$validated = $request->validate([
'instance_url' => 'required|url',
'username' => 'required|string|max:255',
'password' => 'nullable|string',
'settings' => 'nullable|array',
]);
// Don't update password if not provided
if (empty($validated['password'])) {
unset($validated['password']);
}
$platformAccount->update($validated);
return $this->sendResponse(
2025-08-05 21:53:49 +02:00
new PlatformAccountResource($platformAccount->fresh()),
2025-08-02 15:20:09 +02:00
'Platform account updated successfully!'
);
} catch (ValidationException $e) {
return $this->sendValidationError($e->errors());
} catch (Exception $e) {
return $this->sendError('Failed to update platform account: '.$e->getMessage(), [], 500);
2025-08-02 15:20:09 +02:00
}
}
/**
* Remove the specified platform account
*/
public function destroy(PlatformAccount $platformAccount): JsonResponse
{
try {
$platformAccount->delete();
return $this->sendResponse(
null,
'Platform account deleted successfully!'
);
} catch (Exception $e) {
return $this->sendError('Failed to delete platform account: '.$e->getMessage(), [], 500);
2025-08-02 15:20:09 +02:00
}
}
/**
* Set platform account as active
*/
public function setActive(PlatformAccount $platformAccount): JsonResponse
{
try {
$platformAccount->setAsActive();
return $this->sendResponse(
2025-08-05 21:53:49 +02:00
new PlatformAccountResource($platformAccount->fresh()),
2025-08-02 15:20:09 +02:00
"Set {$platformAccount->username}@{$platformAccount->instance_url} as active for {$platformAccount->platform->value}!"
);
} catch (Exception $e) {
return $this->sendError('Failed to set platform account as active: '.$e->getMessage(), [], 500);
2025-08-02 15:20:09 +02:00
}
}
}