seria/src/Users/ProfileRoutes.php

88 lines
2.7 KiB
PHP

<?php
namespace Seria\Users;
use RuntimeException;
use Index\Routing\Route;
use Index\Routing\RouteHandler;
use Sasae\SasaeEnvironment;
use Seria\Auth\AuthInfo;
use Seria\Torrents\TorrentsContext;
use Seria\Torrents\TorrentInfo;
use Seria\Torrents\TorrentPeerInfo;
class ProfileRoutes extends RouteHandler {
public function __construct(
private AuthInfo $authInfo,
private TorrentsContext $torrentsCtx,
private UsersContext $usersCtx,
private ?SasaeEnvironment $templating
) {}
#[Route('GET', '/profile/:name')]
public function getProfile($response, $request, string $name) {
if(!$this->authInfo->isLoggedIn())
return 403;
$users = $this->usersCtx->getUsers();
$torrents = $this->torrentsCtx->getTorrents();
$peers = $this->torrentsCtx->getPeers();
try {
$userInfo = $users->getUser($name, 'name');
} catch(RuntimeException $ex) {
return 404;
}
$submissions = $torrents->getTorrents(approved: true, userInfo: $userInfo, take: 3);
$uploading = $peers->countUserUploading($userInfo);
$downloading = $peers->countUserDownloading($userInfo);
return $this->templating->render('profile', [
'profile_user' => $userInfo,
'profile_submissions' => $submissions,
'profile_uploading' => $uploading,
'profile_downloading' => $downloading,
]);
}
#[Route('GET', '/profile/:name/history')]
public function getHistory($response, $request, string $name) {
if(!$this->authInfo->isLoggedIn())
return 403;
$users = $this->usersCtx->getUsers();
try {
$userInfo = $users->getUser($name, 'name');
} catch(RuntimeException $ex) {
return 404;
}
return $this->templating->render('history', [
'history_user_info' => $userInfo,
]);
}
#[Route('GET', '/profile.php')]
public function getProfilePHP($response, $request): void {
$response->redirect(sprintf('/profile/%s', (string)$request->getParam('name')), true);
}
#[Route('GET', '/history.php')]
public function getHistoryPHP($response, $request) {
$userName = (string)$request->getParam('name');
if($userName === '' && $this->authInfo->isLoggedIn())
$userName = $this->authInfo->getUserName();
if($userName === '')
return 404;
$url = sprintf('/profile/%s/history', $userName);
$filter = (string)$request->getParam('filter');
if($filter !== '')
$url .= '?filter=' . $filter;
$response->redirect($url, true);
}
}