From 9b2c409a24a66e3f6164618faf1ac8d39fbf79e4 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 6 Sep 2023 13:50:19 +0000 Subject: [PATCH] Moved user related stuff into its own context object. --- public-legacy/auth/login.php | 2 +- public-legacy/auth/password.php | 2 +- public-legacy/auth/register.php | 5 +- public-legacy/auth/twofactor.php | 2 +- public-legacy/forum/forum.php | 37 ++------ public-legacy/forum/index.php | 26 ++---- public-legacy/forum/leaderboard.php | 7 +- public-legacy/forum/posting.php | 20 ++--- public-legacy/forum/topic.php | 18 ++-- public-legacy/manage/changelog/index.php | 21 +---- public-legacy/manage/general/logs.php | 6 +- public-legacy/manage/users/ban.php | 7 +- public-legacy/manage/users/bans.php | 37 ++------ public-legacy/manage/users/index.php | 5 +- public-legacy/manage/users/note.php | 14 +-- public-legacy/manage/users/notes.php | 37 ++------ public-legacy/manage/users/role.php | 5 +- public-legacy/manage/users/roles.php | 2 +- public-legacy/manage/users/user.php | 5 +- public-legacy/manage/users/warning.php | 6 +- public-legacy/manage/users/warnings.php | 37 ++------ public-legacy/members.php | 7 +- public-legacy/profile.php | 13 +-- public-legacy/search.php | 68 +++----------- public-legacy/settings/account.php | 5 +- public/index.php | 2 +- src/Changelog/ChangeInfo.php | 4 + src/Changelog/ChangelogRoutes.php | 64 +++---------- src/Comments/CommentsEx.php | 32 ++----- src/Home/HomeRoutes.php | 72 ++++----------- src/MisuzuContext.php | 109 +++++++++++------------ src/News/NewsRoutes.php | 82 +++-------------- src/Satori/SatoriRoutes.php | 24 ++--- src/SharpChat/SharpChatRoutes.php | 73 ++++++--------- src/Users/Assets/AssetsRoutes.php | 22 ++--- src/Users/Users.php | 13 ++- src/Users/UsersContext.php | 100 +++++++++++++++++++++ 37 files changed, 367 insertions(+), 624 deletions(-) create mode 100644 src/Users/UsersContext.php diff --git a/public-legacy/auth/login.php b/public-legacy/auth/login.php index 6ebc58a..b5e3436 100644 --- a/public-legacy/auth/login.php +++ b/public-legacy/auth/login.php @@ -9,7 +9,7 @@ if($msz->isLoggedIn()) { return; } -$users = $msz->getUsers(); +$users = $msz->getUsersContext()->getUsers(); $sessions = $msz->getSessions(); $loginAttempts = $msz->getLoginAttempts(); diff --git a/public-legacy/auth/password.php b/public-legacy/auth/password.php index 20282af..e0791b2 100644 --- a/public-legacy/auth/password.php +++ b/public-legacy/auth/password.php @@ -9,7 +9,7 @@ if($msz->isLoggedIn()) { return; } -$users = $msz->getUsers(); +$users = $msz->getUsersContext()->getUsers(); $recoveryTokens = $msz->getRecoveryTokens(); $loginAttempts = $msz->getLoginAttempts(); diff --git a/public-legacy/auth/register.php b/public-legacy/auth/register.php index 5824807..aa518a3 100644 --- a/public-legacy/auth/register.php +++ b/public-legacy/auth/register.php @@ -9,8 +9,9 @@ if($msz->isLoggedIn()) { return; } -$users = $msz->getUsers(); -$roles = $msz->getRoles(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); +$roles = $usersCtx->getRoles(); $config = $msz->getConfig(); $register = !empty($_POST['register']) && is_array($_POST['register']) ? $_POST['register'] : []; diff --git a/public-legacy/auth/twofactor.php b/public-legacy/auth/twofactor.php index 297e013..d438086 100644 --- a/public-legacy/auth/twofactor.php +++ b/public-legacy/auth/twofactor.php @@ -10,7 +10,7 @@ if($msz->isLoggedIn()) { return; } -$users = $msz->getUsers(); +$users = $msz->getUsersContext()->getUsers(); $sessions = $msz->getSessions(); $tfaSessions = $msz->getTFASessions(); $loginAttempts = $msz->getLoginAttempts(); diff --git a/public-legacy/forum/forum.php b/public-legacy/forum/forum.php index 61d6bd4..ab2f7c8 100644 --- a/public-legacy/forum/forum.php +++ b/public-legacy/forum/forum.php @@ -5,7 +5,7 @@ use stdClass; use RuntimeException; $forum = $msz->getForum(); -$users = $msz->getUsers(); +$usersCtx = $msz->getUsersContext(); $categoryId = (int)filter_input(INPUT_GET, 'f', FILTER_SANITIZE_NUMBER_INT); @@ -45,8 +45,6 @@ $forumPagination = new Pagination($forum->countTopics( if(!$forumPagination->hasValidOffset()) Template::throwError(404); -$userInfos = []; -$userColours = []; $children = []; $topics = []; @@ -110,15 +108,8 @@ if($categoryInfo->mayHaveChildren()) { $child->lastPost->topicInfo = $forum->getTopic(postInfo: $lastPostInfo); if($lastPostInfo->hasUserId()) { - $lastPostUserId = $lastPostInfo->getUserId(); - if(!array_key_exists($lastPostUserId, $userInfos)) { - $userInfo = $users->getUser($lastPostUserId, 'id'); - $userInfos[$lastPostUserId] = $userInfo; - $userColours[$lastPostUserId] = $users->getUserColour($userInfo); - } - - $child->lastPost->user = $userInfos[$lastPostUserId]; - $child->lastPost->colour = $userColours[$lastPostUserId]; + $child->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId()); + $child->lastPost->colour = $usersCtx->getUserColour($child->lastPost->user); } } } @@ -147,15 +138,8 @@ if($categoryInfo->mayHaveTopics()) { $topic->lastPost = new stdClass; if($topicInfo->hasUserId()) { - $lastTopicUserId = $topicInfo->getUserId(); - if(!array_key_exists($lastTopicUserId, $userInfos)) { - $userInfo = $users->getUser($lastTopicUserId, 'id'); - $userInfos[$lastTopicUserId] = $userInfo; - $userColours[$lastTopicUserId] = $users->getUserColour($userInfo); - } - - $topic->user = $userInfos[$lastTopicUserId]; - $topic->colour = $userColours[$lastTopicUserId]; + $topic->user = $usersCtx->getUserInfo($topicInfo->getUserId()); + $topic->colour = $usersCtx->getUserColour($topic->user); } try { @@ -166,15 +150,8 @@ if($categoryInfo->mayHaveTopics()) { ); if($lastPostInfo->hasUserId()) { - $lastPostUserId = $lastPostInfo->getUserId(); - if(!array_key_exists($lastPostUserId, $userInfos)) { - $userInfo = $users->getUser($lastPostUserId, 'id'); - $userInfos[$lastPostUserId] = $userInfo; - $userColours[$lastPostUserId] = $users->getUserColour($userInfo); - } - - $topic->lastPost->user = $userInfos[$lastPostUserId]; - $topic->lastPost->colour = $userColours[$lastPostUserId]; + $topic->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId()); + $topic->lastPost->colour = $usersCtx->getUserColour($topic->lastPost->user); } } catch(RuntimeException $ex) {} } diff --git a/public-legacy/forum/index.php b/public-legacy/forum/index.php index a048bbf..89740af 100644 --- a/public-legacy/forum/index.php +++ b/public-legacy/forum/index.php @@ -5,7 +5,7 @@ use stdClass; use RuntimeException; $forum = $msz->getForum(); -$users = $msz->getUsers(); +$usersCtx = $msz->getUsersContext(); $mode = (string)filter_input(INPUT_GET, 'm'); $currentUser = $msz->getActiveUser(); @@ -46,8 +46,6 @@ if($mode === 'mark') { if($mode !== '') Template::throwError(404); -$userInfos = []; -$userColours = []; $categories = $forum->getCategories(hidden: false, asTree: true); foreach($categories as $categoryId => $category) { @@ -118,15 +116,8 @@ foreach($categories as $categoryId => $category) { $child->lastPost->topicInfo = $forum->getTopic(postInfo: $lastPostInfo); if($lastPostInfo->hasUserId()) { - $lastPostUserId = $lastPostInfo->getUserId(); - if(!array_key_exists($lastPostUserId, $userInfos)) { - $userInfo = $users->getUser($lastPostUserId, 'id'); - $userInfos[$lastPostUserId] = $userInfo; - $userColours[$lastPostUserId] = $users->getUserColour($userInfo); - } - - $child->lastPost->user = $userInfos[$lastPostUserId]; - $child->lastPost->colour = $userColours[$lastPostUserId]; + $child->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId()); + $child->lastPost->colour = $usersCtx->getUserColour($child->lastPost->user); } } } @@ -178,15 +169,8 @@ foreach($categories as $categoryId => $category) { $category->lastPost->topicInfo = $forum->getTopic(postInfo: $lastPostInfo); if($lastPostInfo->hasUserId()) { - $lastPostUserId = $lastPostInfo->getUserId(); - if(!array_key_exists($lastPostUserId, $userInfos)) { - $userInfo = $users->getUser($lastPostInfo->getUserId(), 'id'); - $userInfos[$lastPostUserId] = $userInfo; - $userColours[$lastPostUserId] = $users->getUserColour($userInfo); - } - - $category->lastPost->user = $userInfos[$lastPostUserId]; - $category->lastPost->colour = $userColours[$lastPostUserId]; + $category->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId()); + $category->lastPost->colour = $usersCtx->getUserColour($category->lastPost->user); } } } diff --git a/public-legacy/forum/leaderboard.php b/public-legacy/forum/leaderboard.php index 7984949..6caa01c 100644 --- a/public-legacy/forum/leaderboard.php +++ b/public-legacy/forum/leaderboard.php @@ -7,7 +7,7 @@ if(!$msz->getAuthInfo()->getPerms('global')->check(Perm::G_FORUM_LEADERBOARD_VIE Template::throwError(403); $forum = $msz->getForum(); -$users = $msz->getUsers(); +$usersCtx = $msz->getUsersContext(); $config = $cfg->getValues([ ['forum_leader.first_year:i', 2018], ['forum_leader.first_month:i', 12], @@ -66,10 +66,7 @@ foreach($rankings as $ranking) { $ranking->user = $ranking->colour = null; if($ranking->userId !== '') - try { - $ranking->user = $users->getUser($ranking->userId); - $ranking->colour = $users->getUserColour($ranking->user); - } catch(RuntimeException $ex) {} + $ranking->user = $usersCtx->getUserInfo($ranking->userId); } $name = 'All Time'; diff --git a/public-legacy/forum/posting.php b/public-legacy/forum/posting.php index 74fe268..0104293 100644 --- a/public-legacy/forum/posting.php +++ b/public-legacy/forum/posting.php @@ -16,10 +16,8 @@ if($msz->hasActiveBan()) Template::throwError(403); $forum = $msz->getForum(); -$users = $msz->getUsers(); +$usersCtx = $msz->getUsersContext(); -$userInfos = []; -$userColours = []; $userPostsCounts = []; $forumPostingModes = [ @@ -277,16 +275,12 @@ if($mode === 'edit') { // $post is pretty much sure to be populated at this poin $post->info = $postInfo; if($postInfo->hasUserId()) { - $postUserId = $postInfo->getUserId(); - if(!array_key_exists($postUserId, $userInfos)) { - $userInfo = $users->getUser($postUserId, 'id'); - $userInfos[$postUserId] = $userInfo; - $userColours[$postUserId] = $users->getUserColour($userInfo); - $userPostsCounts[$postUserId] = $forum->countPosts(userInfo: $userInfo, deleted: false); - } + $post->user = $usersCtx->getUserInfo($postInfo->getUserId()); + $post->colour = $usersCtx->getUserColour($post->user); - $post->user = $userInfos[$postUserId]; - $post->colour = $userColours[$postUserId]; + $postUserId = $postInfo->getUserId(); + if(!array_key_exists($postUserId, $userPostsCounts)) + $userPostsCounts[$postUserId] = $forum->countPosts(userInfo: $post->user, deleted: false); $post->postsCount = $userPostsCounts[$postUserId]; } @@ -308,7 +302,7 @@ Template::render('forum.posting', [ 'posting_breadcrumbs' => $forum->getCategoryAncestry($categoryInfo), 'global_accent_colour' => $forum->getCategoryColour($categoryInfo), 'posting_user' => $currentUser, - 'posting_user_colour' => $userColours[$currentUser->getId()] ?? $users->getUserColour($currentUser), + 'posting_user_colour' => $usersCtx->getUserColour($currentUser), 'posting_user_posts_count' => $userPostsCounts[$currentUser->getId()] ?? $forum->countPosts(userInfo: $currentUser, deleted: false), 'posting_user_preferred_parser' => $selectedParser, 'posting_forum' => $categoryInfo, diff --git a/public-legacy/forum/topic.php b/public-legacy/forum/topic.php index d9867b6..8d1fb9d 100644 --- a/public-legacy/forum/topic.php +++ b/public-legacy/forum/topic.php @@ -5,7 +5,7 @@ use stdClass; use RuntimeException; $forum = $msz->getForum(); -$users = $msz->getUsers(); +$usersCtx = $msz->getUsersContext(); $postId = !empty($_GET['p']) && is_string($_GET['p']) ? (int)$_GET['p'] : 0; $topicId = !empty($_GET['t']) && is_string($_GET['t']) ? (int)$_GET['t'] : 0; @@ -284,8 +284,6 @@ if(empty($postInfos)) $originalPostInfo = $forum->getPost(topicInfo: $topicInfo); -$userInfos = []; -$userColours = []; $userPostsCounts = []; $posts = []; @@ -294,16 +292,12 @@ foreach($postInfos as $postInfo) { $post->info = $postInfo; if($postInfo->hasUserId()) { - $postUserId = $postInfo->getUserId(); - if(!array_key_exists($postUserId, $userInfos)) { - $userInfo = $users->getUser($postUserId, 'id'); - $userInfos[$postUserId] = $userInfo; - $userColours[$postUserId] = $users->getUserColour($userInfo); - $userPostsCounts[$postUserId] = $forum->countPosts(userInfo: $userInfo, deleted: false); - } + $post->user = $usersCtx->getUserInfo($postInfo->getUserId()); + $post->colour = $usersCtx->getUserColour($post->user); - $post->user = $userInfos[$postUserId]; - $post->colour = $userColours[$postUserId]; + $postUserId = $postInfo->getUserId(); + if(!array_key_exists($postUserId, $userPostsCounts)) + $userPostsCounts[$postUserId] = $forum->countPosts(userInfo: $post->user, deleted: false); $post->postsCount = $userPostsCounts[$postUserId]; } diff --git a/public-legacy/manage/changelog/index.php b/public-legacy/manage/changelog/index.php index e6c37df..6ad8c3d 100644 --- a/public-legacy/manage/changelog/index.php +++ b/public-legacy/manage/changelog/index.php @@ -7,6 +7,8 @@ if(!$msz->getAuthInfo()->getPerms('global')->check(Perm::G_CL_CHANGES_MANAGE)) Template::throwError(403); $changelog = $msz->getChangelog(); +$usersCtx = $msz->getUsersContext(); + $changelogPagination = new Pagination($changelog->countChanges(), 30); if(!$changelogPagination->hasValidOffset()) @@ -14,30 +16,15 @@ if(!$changelogPagination->hasValidOffset()) $changeInfos = $changelog->getChanges(pagination: $changelogPagination); $changes = []; -$userInfos = []; -$userColours = []; foreach($changeInfos as $changeInfo) { - $userId = $changeInfo->getUserId(); - - if(array_key_exists($userId, $userInfos)) { - $userInfo = $userInfos[$userId]; - } else { - try { - $userInfo = $users->getUser($userId, 'id'); - $userColours[$userId] = $users->getUserColour($userInfo); - } catch(RuntimeException $ex) { - $userInfo = null; - } - - $userInfos[$userId] = $userInfo; - } + $userInfo = $changeInfo->hasUserId() ? $usersCtx->getUserInfo($changeInfo->getUserId()) : null; $changes[] = [ 'change' => $changeInfo, 'tags' => $changelog->getTags(changeInfo: $changeInfo), 'user' => $userInfo, - 'user_colour' => $userColours[$userId] ?? \Index\Colour\Colour::none(), + 'user_colour' => $usersCtx->getUserColour($userInfo), ]; } diff --git a/public-legacy/manage/general/logs.php b/public-legacy/manage/general/logs.php index ecf7635..c98d5c0 100644 --- a/public-legacy/manage/general/logs.php +++ b/public-legacy/manage/general/logs.php @@ -6,7 +6,7 @@ use Misuzu\Pagination; if(!$msz->getAuthInfo()->getPerms('global')->check(Perm::G_LOGS_VIEW)) Template::throwError(403); -$users = $msz->getUsers(); +$usersCtx = $msz->getUsersContext(); $auditLog = $msz->getAuditLog(); $pagination = new Pagination($auditLog->countLogs(), 50); @@ -21,8 +21,8 @@ foreach($logs as $log) if($log->hasUserId()) { $userId = $log->getUserId(); if(!array_key_exists($userId, $userInfos)) { - $userInfos[$userId] = $users->getUser($userId, 'id'); - $userColours[$userId] = $users->getUserColour($userInfos[$userId]); + $userInfos[$userId] = $usersCtx->getUserInfo($userId); + $userColours[$userId] = $usersCtx->getUserColour($userId); } } diff --git a/public-legacy/manage/users/ban.php b/public-legacy/manage/users/ban.php index 5f62f6d..b1a58cf 100644 --- a/public-legacy/manage/users/ban.php +++ b/public-legacy/manage/users/ban.php @@ -8,7 +8,8 @@ use Index\DateTime; if(!$msz->getAuthInfo()->getPerms('user')->check(Perm::U_BANS_MANAGE)) Template::throwError(403); -$bans = $msz->getBans(); +$usersCtx = $msz->getUsersContext(); +$bans = $usersCtx->getBans(); if($_SERVER['REQUEST_METHOD'] === 'GET' && filter_has_var(INPUT_GET, 'delete')) { if(!CSRF::validateRequest()) @@ -26,10 +27,8 @@ if($_SERVER['REQUEST_METHOD'] === 'GET' && filter_has_var(INPUT_GET, 'delete')) return; } -$users = $msz->getUsers(); - try { - $userInfo = $users->getUser(filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT), 'id'); + $userInfo = $usersCtx->getUserInfo(filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT), 'id'); } catch(RuntimeException $ex) { Template::throwError(404); } diff --git a/public-legacy/manage/users/bans.php b/public-legacy/manage/users/bans.php index 4c726aa..f864086 100644 --- a/public-legacy/manage/users/bans.php +++ b/public-legacy/manage/users/bans.php @@ -6,28 +6,19 @@ use RuntimeException; if(!$msz->getAuthInfo()->getPerms('user')->check(Perm::U_BANS_MANAGE)) Template::throwError(403); -$users = $msz->getUsers(); - -$userInfos = [ - $msz->getActiveUser()->getId() => $msz->getActiveUser(), -]; -$userColours = [ - $msz->getActiveUser()->getId() => $users->getUserColour($msz->getActiveUser()), -]; +$usersCtx = $msz->getUsersContext(); +$bans = $usersCtx->getBans(); $filterUser = null; if(filter_has_var(INPUT_GET, 'u')) { $filterUserId = filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT); try { - $filterUser = $users->getUser($filterUserId, 'id'); - $userInfos[$filterUserId] = $filterUser; - $userColours[$filterUserId] = $users->getUserColour($filterUser); + $filterUser = $usersCtx->getUserInfo($filterUserId); } catch(RuntimeException $ex) { Template::throwError(404); } } -$bans = $msz->getBans(); $pagination = new Pagination($bans->countBans(userInfo: $filterUser), 10); if(!$pagination->hasValidOffset()) @@ -37,29 +28,15 @@ $banList = []; $banInfos = $bans->getBans(userInfo: $filterUser, activeFirst: true, pagination: $pagination); foreach($banInfos as $banInfo) { - if(array_key_exists($banInfo->getUserId(), $userInfos)) - $userInfo = $userInfos[$banInfo->getUserId()]; - else - $userInfos[$banInfo->getUserId()] = $userInfo = $users->getUser($banInfo->getUserId(), 'id'); - - if(array_key_exists($userInfo->getId(), $userColours)) - $userColour = $userColours[$userInfo->getId()]; - else - $userColours[$userInfo->getId()] = $userColour = $users->getUserColour($userInfo); + $userInfo = $usersCtx->getUserInfo($banInfo->getUserId()); + $userColour = $usersCtx->getUserColour($userInfo); if(!$banInfo->hasModId()) { $modInfo = null; $modColour = null; } else { - if(array_key_exists($banInfo->getModId(), $userInfos)) - $modInfo = $userInfos[$banInfo->getModId()]; - else - $userInfos[$banInfo->getModId()] = $modInfo = $users->getUser($banInfo->getModId(), 'id'); - - if(array_key_exists($modInfo->getId(), $userColours)) - $modColour = $userColours[$modInfo->getId()]; - else - $userColours[$modInfo->getId()] = $modColour = $users->getUserColour($modInfo); + $modInfo = $usersCtx->getUserInfo($banInfo->getModId()); + $modColour = $usersCtx->getUserColour($modInfo); } $banList[] = [ diff --git a/public-legacy/manage/users/index.php b/public-legacy/manage/users/index.php index 4e41191..b791848 100644 --- a/public-legacy/manage/users/index.php +++ b/public-legacy/manage/users/index.php @@ -4,8 +4,9 @@ namespace Misuzu; if(!$msz->getAuthInfo()->getPerms('user')->check(Perm::U_USERS_MANAGE)) Template::throwError(403); -$users = $msz->getUsers(); -$roles = $msz->getRoles(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); +$roles = $usersCtx->getRoles(); $pagination = new Pagination($users->countUsers(), 30); if(!$pagination->hasValidOffset()) diff --git a/public-legacy/manage/users/note.php b/public-legacy/manage/users/note.php index ffc94ba..5d43500 100644 --- a/public-legacy/manage/users/note.php +++ b/public-legacy/manage/users/note.php @@ -12,14 +12,14 @@ $hasUserId = filter_has_var(INPUT_GET, 'u'); if((!$hasNoteId && !$hasUserId) || ($hasNoteId && $hasUserId)) Template::throwError(400); -$users = $msz->getUsers(); -$modNotes = $msz->getModNotes(); +$usersCtx = $msz->getUsersContext(); +$modNotes = $usersCtx->getModNotes(); if($hasUserId) { $isNew = true; try { - $userInfo = $users->getUser(filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT), 'id'); + $userInfo = $usersCtx->getUserInfo(filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT)); } catch(RuntimeException $ex) { Template::throwError(404); } @@ -44,8 +44,8 @@ if($hasUserId) { return; } - $userInfo = $users->getUser($noteInfo->getUserId(), 'id'); - $authorInfo = $noteInfo->hasAuthorId() ? $users->getUser($noteInfo->getAuthorId(), 'id') : null; + $userInfo = $usersCtx->getUserInfo($noteInfo->getUserId()); + $authorInfo = $noteInfo->hasAuthorId() ? $usersCtx->getUserInfo($noteInfo->getAuthorId()) : null; } while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) { @@ -78,7 +78,7 @@ Template::render('manage.users.note', [ 'note_new' => $isNew, 'note_info' => $noteInfo ?? null, 'note_user' => $userInfo, - 'note_user_colour' => $users->getUserColour($userInfo), + 'note_user_colour' => $usersCtx->getUserColour($userInfo), 'note_author' => $authorInfo, - 'note_author_colour' => $users->getUserColour($authorInfo), + 'note_author_colour' => $usersCtx->getUserColour($authorInfo), ]); diff --git a/public-legacy/manage/users/notes.php b/public-legacy/manage/users/notes.php index 0826250..618a345 100644 --- a/public-legacy/manage/users/notes.php +++ b/public-legacy/manage/users/notes.php @@ -6,28 +6,19 @@ use RuntimeException; if(!$msz->getAuthInfo()->getPerms('user')->check(Perm::U_NOTES_MANAGE)) Template::throwError(403); -$users = $msz->getUsers(); - -$userInfos = [ - $msz->getActiveUser()->getId() => $msz->getActiveUser(), -]; -$userColours = [ - $msz->getActiveUser()->getId() => $users->getUserColour($msz->getActiveUser()), -]; +$usersCtx = $msz->getUsersContext(); +$modNotes = $usersCtx->getModNotes(); $filterUser = null; if(filter_has_var(INPUT_GET, 'u')) { $filterUserId = filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT); try { - $filterUser = $users->getUser($filterUserId, 'id'); - $userInfos[$filterUserId] = $filterUser; - $userColours[$filterUserId] = $users->getUserColour($filterUser); + $filterUser = $usersCtx->getUserInfo($filterUserId); } catch(RuntimeException $ex) { Template::throwError(404); } } -$modNotes = $msz->getModNotes(); $pagination = new Pagination($modNotes->countNotes(userInfo: $filterUser), 10); if(!$pagination->hasValidOffset()) @@ -37,29 +28,15 @@ $notes = []; $noteInfos = $modNotes->getNotes(userInfo: $filterUser, pagination: $pagination); foreach($noteInfos as $noteInfo) { - if(array_key_exists($noteInfo->getUserId(), $userInfos)) - $userInfo = $userInfos[$noteInfo->getUserId()]; - else - $userInfos[$noteInfo->getUserId()] = $userInfo = $users->getUser($noteInfo->getUserId(), 'id'); - - if(array_key_exists($userInfo->getId(), $userColours)) - $userColour = $userColours[$userInfo->getId()]; - else - $userColours[$userInfo->getId()] = $userColour = $users->getUserColour($userInfo); + $userInfo = $usersCtx->getUserInfo($noteInfo->getUserId()); + $userColour = $usersCtx->getUserColour($userInfo); if(!$noteInfo->hasAuthorId()) { $authorInfo = null; $authorColour = null; } else { - if(array_key_exists($noteInfo->getAuthorId(), $userInfos)) - $authorInfo = $userInfos[$noteInfo->getAuthorId()]; - else - $userInfos[$noteInfo->getAuthorId()] = $modInfo = $users->getUser($noteInfo->getAuthorId(), 'id'); - - if(array_key_exists($authorInfo->getId(), $userColours)) - $authorColour = $userColours[$authorInfo->getId()]; - else - $userColours[$authorInfo->getId()] = $authorColour = $users->getUserColour($authorInfo); + $authorInfo = $usersCtx->getUserInfo($noteInfo->getAuthorId()); + $authorColour = $usersCtx->getUserColour($authorInfo); } $notes[] = [ diff --git a/public-legacy/manage/users/role.php b/public-legacy/manage/users/role.php index ab2b59c..5a2632e 100644 --- a/public-legacy/manage/users/role.php +++ b/public-legacy/manage/users/role.php @@ -10,8 +10,9 @@ $viewerPerms = $msz->getAuthInfo()->getPerms('user'); if(!$viewerPerms->check(Perm::U_ROLES_MANAGE)) Template::throwError(403); -$users = $msz->getUsers(); -$roles = $msz->getRoles(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); +$roles = $usersCtx->getRoles(); $perms = $msz->getPerms(); if(filter_has_var(INPUT_GET, 'r')) { diff --git a/public-legacy/manage/users/roles.php b/public-legacy/manage/users/roles.php index c94e883..2efc714 100644 --- a/public-legacy/manage/users/roles.php +++ b/public-legacy/manage/users/roles.php @@ -4,7 +4,7 @@ namespace Misuzu; if(!$msz->getAuthInfo()->getPerms('user')->check(Perm::U_ROLES_MANAGE)) Template::throwError(403); -$roles = $msz->getRoles(); +$roles = $msz->getUsersContext()->getRoles(); $pagination = new Pagination($roles->countRoles(), 10); if(!$pagination->hasValidOffset()) diff --git a/public-legacy/manage/users/user.php b/public-legacy/manage/users/user.php index 2726ee0..b858c69 100644 --- a/public-legacy/manage/users/user.php +++ b/public-legacy/manage/users/user.php @@ -11,8 +11,9 @@ $viewerPerms = $msz->getAuthInfo()->getPerms('user'); if(!$msz->isLoggedIn()) Template::throwError(403); -$users = $msz->getUsers(); -$roles = $msz->getRoles(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); +$roles = $usersCtx->getRoles(); $perms = $msz->getPerms(); $currentUser = $msz->getActiveUser(); diff --git a/public-legacy/manage/users/warning.php b/public-legacy/manage/users/warning.php index e945583..22965fd 100644 --- a/public-legacy/manage/users/warning.php +++ b/public-legacy/manage/users/warning.php @@ -6,7 +6,9 @@ use RuntimeException; if(!$msz->getAuthInfo()->getPerms('user')->check(Perm::U_WARNINGS_MANAGE)) Template::throwError(403); -$warns = $msz->getWarnings(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); +$warns = $usersCtx->getWarnings(); if($_SERVER['REQUEST_METHOD'] === 'GET' && filter_has_var(INPUT_GET, 'delete')) { if(!CSRF::validateRequest()) @@ -24,8 +26,6 @@ if($_SERVER['REQUEST_METHOD'] === 'GET' && filter_has_var(INPUT_GET, 'delete')) return; } -$users = $msz->getUsers(); - try { $userInfo = $users->getUser(filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT), 'id'); } catch(RuntimeException $ex) { diff --git a/public-legacy/manage/users/warnings.php b/public-legacy/manage/users/warnings.php index 2bb6322..acf8d69 100644 --- a/public-legacy/manage/users/warnings.php +++ b/public-legacy/manage/users/warnings.php @@ -6,28 +6,19 @@ use RuntimeException; if(!$msz->getAuthInfo()->getPerms('user')->check(Perm::U_WARNINGS_MANAGE)) Template::throwError(403); -$users = $msz->getUsers(); - -$userInfos = [ - $msz->getActiveUser()->getId() => $msz->getActiveUser(), -]; -$userColours = [ - $msz->getActiveUser()->getId() => $users->getUserColour($msz->getActiveUser()), -]; +$usersCtx = $msz->getUsersContext(); +$warns = $usersCtx->getWarnings(); $filterUser = null; if(filter_has_var(INPUT_GET, 'u')) { $filterUserId = filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT); try { - $filterUser = $users->getUser($filterUserId, 'id'); - $userInfos[$filterUserId] = $filterUser; - $userColours[$filterUserId] = $users->getUserColour($filterUser); + $filterUser = $usersCtx->getUserInfo($filterUserId); } catch(RuntimeException $ex) { Template::throwError(404); } } -$warns = $msz->getWarnings(); $pagination = new Pagination($warns->countWarnings(userInfo: $filterUser), 10); if(!$pagination->hasValidOffset()) @@ -37,29 +28,15 @@ $warnList = []; $warnInfos = $warns->getWarnings(userInfo: $filterUser, pagination: $pagination); foreach($warnInfos as $warnInfo) { - if(array_key_exists($warnInfo->getUserId(), $userInfos)) - $userInfo = $userInfos[$warnInfo->getUserId()]; - else - $userInfos[$warnInfo->getUserId()] = $userInfo = $users->getUser($warnInfo->getUserId(), 'id'); - - if(array_key_exists($userInfo->getId(), $userColours)) - $userColour = $userColours[$userInfo->getId()]; - else - $userColours[$userInfo->getId()] = $userColour = $users->getUserColour($userInfo); + $userInfo = $usersCtx->getUserInfo($warnInfo->getUserId()); + $userColour = $usersCtx->getUserColour($userInfo); if(!$warnInfo->hasModId()) { $modInfo = null; $modColour = null; } else { - if(array_key_exists($warnInfo->getModId(), $userInfos)) - $modInfo = $userInfos[$warnInfo->getModId()]; - else - $userInfos[$warnInfo->getModId()] = $modInfo = $users->getUser($warnInfo->getModId(), 'id'); - - if(array_key_exists($modInfo->getId(), $userColours)) - $modColour = $userColours[$modInfo->getId()]; - else - $userColours[$modInfo->getId()] = $modColour = $users->getUserColour($modInfo); + $modInfo = $usersCtx->getUserInfo($warnInfo->getModId()); + $modColour = $usersCtx->getUserColour($modInfo); } $warnList[] = [ diff --git a/public-legacy/members.php b/public-legacy/members.php index 508d5e5..737bb21 100644 --- a/public-legacy/members.php +++ b/public-legacy/members.php @@ -8,9 +8,10 @@ if(!$msz->isLoggedIn()) // TODO: restore forum-topics and forum-posts orderings -$users = $msz->getUsers(); -$roles = $msz->getRoles(); $forum = $msz->getForum(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); +$roles = $usersCtx->getRoles(); $roleId = filter_has_var(INPUT_GET, 'r') ? (string)filter_input(INPUT_GET, 'r') : null; $orderBy = strtolower((string)filter_input(INPUT_GET, 'ss')); @@ -91,7 +92,7 @@ $userInfos = $users->getUsers( foreach($userInfos as $userInfo) $userList[] = [ 'info' => $userInfo, - 'colour' => $users->getUserColour($userInfo), + 'colour' => $usersCtx->getUserColour($userInfo), 'ftopics' => $forum->countTopics(userInfo: $userInfo, deleted: false), 'fposts' => $forum->countPosts(userInfo: $userInfo, deleted: false), ]; diff --git a/public-legacy/profile.php b/public-legacy/profile.php index 077fbd9..efa9aa5 100644 --- a/public-legacy/profile.php +++ b/public-legacy/profile.php @@ -15,7 +15,8 @@ $userId = !empty($_GET['u']) && is_string($_GET['u']) ? trim($_GET['u']) : 0; $profileMode = !empty($_GET['m']) && is_string($_GET['m']) ? (string)$_GET['m'] : ''; $isEditing = !empty($_GET['edit']) && is_string($_GET['edit']) ? (bool)$_GET['edit'] : !empty($_POST) && is_array($_POST); -$users = $msz->getUsers(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); $forum = $msz->getForum(); $viewerInfo = $msz->getActiveUser(); @@ -23,7 +24,7 @@ $viewingAsGuest = $viewerInfo === null; $viewerId = $viewingAsGuest ? '0' : $viewerInfo->getId(); try { - $userInfo = $users->getUser($userId, 'profile'); + $userInfo = $usersCtx->getUserInfo($userId, 'profile'); } catch(RuntimeException $ex) { http_response_code(404); Template::render('profile.index', [ @@ -62,8 +63,8 @@ switch($profileMode) { $notices = []; -$userRank = $users->getUserRank($userInfo); -$viewerRank = $viewingAsGuest ? 0 : $users->getUserRank($viewerInfo); +$userRank = $usersCtx->getUserRank($userInfo); +$viewerRank = $usersCtx->getUserRank($viewerInfo); $viewerPerms = $msz->getAuthInfo()->getPerms('user'); @@ -292,7 +293,7 @@ $profileStats->forum_post_count = $forum->countPosts(userInfo: $userInfo, delete $profileStats->comments_count = $msz->getComments()->countPosts(userInfo: $userInfo, deleted: false); if(!$viewingAsGuest) { - Template::set('profile_warnings', $msz->getWarnings()->getWarningsWithDefaultBacklog($userInfo)); + Template::set('profile_warnings', $usersCtx->getWarnings()->getWarningsWithDefaultBacklog($userInfo)); if((!$isBanned || $canEdit)) { $unranked = $cfg->getValues([ @@ -366,7 +367,7 @@ if(!$viewingAsGuest) { Template::render('profile.index', [ 'profile_viewer' => $viewerInfo, 'profile_user' => $userInfo, - 'profile_colour' => $users->getUserColour($userInfo), + 'profile_colour' => $usersCtx->getUserColour($userInfo), 'profile_stats' => $profileStats, 'profile_mode' => $profileMode, 'profile_notices' => $notices, diff --git a/public-legacy/search.php b/public-legacy/search.php index 1682487..6d5278d 100644 --- a/public-legacy/search.php +++ b/public-legacy/search.php @@ -38,14 +38,12 @@ Template::addFunction('search_merge_query', function($attrs) use (&$searchQueryE return rawurlencode(implode(' ', $existing)); }); if(!empty($searchQuery)) { - $users = $msz->getUsers(); + $usersCtx = $msz->getUsersContext(); + $users = $usersCtx->getUsers(); $forum = $msz->getForum(); $news = $msz->getNews(); $comments = $msz->getComments(); - $userInfos = []; - $userColours = []; - $searchQueryAttributes = ['type', 'author', 'after']; $searchQueryParts = explode(' ', $searchQuery); foreach($searchQueryParts as $queryPart) { @@ -70,7 +68,7 @@ if(!empty($searchQuery)) { if(!empty($searchQueryEvaluated['author'])) try { - $searchQueryEvaluated['author'] = $users->getUser($searchQueryEvaluated['author'], 'search'); + $searchQueryEvaluated['author'] = $usersCtx->getUserInfo($searchQueryEvaluated['author'], 'search'); } catch(RuntimeException $ex) { unset($searchQueryEvaluated['author']); } @@ -95,15 +93,8 @@ if(!empty($searchQuery)) { $topic->lastPost = new stdClass; if($topicInfo->hasUserId()) { - $lastTopicUserId = $topicInfo->getUserId(); - if(!array_key_exists($lastTopicUserId, $userInfos)) { - $userInfo = $users->getUser($lastTopicUserId, 'id'); - $userInfos[$lastTopicUserId] = $userInfo; - $userColours[$lastTopicUserId] = $users->getUserColour($userInfo); - } - - $topic->user = $userInfos[$lastTopicUserId]; - $topic->colour = $userColours[$lastTopicUserId]; + $topic->user = $usersCtx->getUserInfo($topicInfo->getUserId(), 'id'); + $topic->colour = $usersCtx->getUserColour($topic->user); } try { @@ -114,15 +105,8 @@ if(!empty($searchQuery)) { ); if($lastPostInfo->hasUserId()) { - $lastPostUserId = $lastPostInfo->getUserId(); - if(!array_key_exists($lastPostUserId, $userInfos)) { - $userInfo = $users->getUser($lastPostUserId, 'id'); - $userInfos[$lastPostUserId] = $userInfo; - $userColours[$lastPostUserId] = $users->getUserColour($userInfo); - } - - $topic->lastPost->user = $userInfos[$lastPostUserId]; - $topic->lastPost->colour = $userColours[$lastPostUserId]; + $topic->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId(), 'id'); + $topic->lastPost->colour = $usersCtx->getUserColour($topic->lastPost->user); } } catch(RuntimeException $ex) {} } @@ -135,17 +119,9 @@ if(!empty($searchQuery)) { $post->info = $postInfo; if($postInfo->hasUserId()) { - $postUserId = $postInfo->getUserId(); - if(!array_key_exists($postUserId, $userInfos)) { - $userInfo = $users->getUser($postUserId, 'id'); - $userInfos[$postUserId] = $userInfo; - $userColours[$postUserId] = $users->getUserColour($userInfo); - $userPostsCounts[$postUserId] = $forum->countPosts(userInfo: $userInfo, deleted: false); - } - - $post->user = $userInfos[$postUserId]; - $post->colour = $userColours[$postUserId]; - $post->postsCount = null; + $post->user = $usersCtx->getUserInfo($postInfo->getUserId(), 'id'); + $post->colour = $usersCtx->getUserColour($post->user); + $post->postsCount = $forum->countPosts(userInfo: $post->user, deleted: false); } // can't be bothered sorry @@ -161,19 +137,8 @@ if(!empty($searchQuery)) { foreach($newsPostInfos as $postInfo) { $userId = $postInfo->getUserId(); $categoryId = $postInfo->getCategoryId(); - - if(array_key_exists($userId, $userInfos)) { - $userInfo = $userInfos[$userId]; - } else { - try { - $userInfo = $users->getUser($userId, 'id'); - $userColours[$userId] = $users->getUserColour($userInfo); - } catch(RuntimeException $ex) { - $userInfo = null; - } - - $userInfos[$userId] = $userInfo; - } + $userInfo = $postInfo->hasUserId() ? $usersCtx->getUserInfo($postInfo->getUserId()) : null; + $userColour = $usersCtx->getUserColour($userInfo); if(array_key_exists($categoryId, $newsCategoryInfos)) $categoryInfo = $newsCategoryInfos[$categoryId]; @@ -187,7 +152,7 @@ if(!empty($searchQuery)) { 'post' => $postInfo, 'category' => $categoryInfo, 'user' => $userInfo, - 'user_colour' => $userColours[$userId] ?? \Index\Colour\Colour::none(), + 'user_colour' => $userColour, 'comments_count' => $commentsCount, ]; } @@ -198,12 +163,7 @@ if(!empty($searchQuery)) { foreach($memberInfos as $memberInfo) { $members[] = $member = new stdClass; $member->info = $memberInfo; - - $memberId = $memberInfo->getId(); - if(!array_key_exists($memberId, $userColours)) - $userColours[$memberId] = $users->getUserColour($memberId); - - $member->colour = $userColours[$memberId]; + $member->colour = $usersCtx->getUserColour($memberInfo); $member->ftopics = $forum->countTopics(userInfo: $memberInfo, deleted: false); $member->fposts = $forum->countPosts(userInfo: $memberInfo, deleted: false); } diff --git a/public-legacy/settings/account.php b/public-legacy/settings/account.php index 58a59bc..0b52aa8 100644 --- a/public-legacy/settings/account.php +++ b/public-legacy/settings/account.php @@ -10,8 +10,9 @@ if(!$msz->isLoggedIn()) Template::throwError(401); $errors = []; -$users = $msz->getUsers(); -$roles = $msz->getRoles(); +$usersCtx = $msz->getUsersContext(); +$users = $usersCtx->getUsers(); +$roles = $usersCtx->getRoles(); $userInfo = $msz->getActiveUser(); $isRestricted = $msz->hasActiveBan(); $isVerifiedRequest = CSRF::validateRequest(); diff --git a/public/index.php b/public/index.php index 9c2e9d4..deaac90 100644 --- a/public/index.php +++ b/public/index.php @@ -57,7 +57,7 @@ $sessionInfo = null; $userInfoReal = null; if($tokenInfo->hasUserId() && $tokenInfo->hasSessionToken()) { - $users = $msz->getUsers(); + $users = $msz->getUsersContext()->getUsers(); $sessions = $msz->getSessions(); $tokenBuilder = new AuthTokenBuilder($tokenInfo); diff --git a/src/Changelog/ChangeInfo.php b/src/Changelog/ChangeInfo.php index 6820d38..20bcbe2 100644 --- a/src/Changelog/ChangeInfo.php +++ b/src/Changelog/ChangeInfo.php @@ -25,6 +25,10 @@ class ChangeInfo { return $this->id; } + public function hasUserId(): bool { + return $this->userId !== null; + } + public function getUserId(): ?string { return $this->userId; } diff --git a/src/Changelog/ChangelogRoutes.php b/src/Changelog/ChangelogRoutes.php index 320f3be..5a52af7 100644 --- a/src/Changelog/ChangelogRoutes.php +++ b/src/Changelog/ChangelogRoutes.php @@ -15,31 +15,16 @@ use Misuzu\Feeds\Feed; use Misuzu\Feeds\FeedItem; use Misuzu\Feeds\AtomFeedSerializer; use Misuzu\Feeds\RssFeedSerializer; -use Misuzu\Users\Users; +use Misuzu\Users\UsersContext; final class ChangelogRoutes implements IRouteHandler { - private IConfig $config; - private Changelog $changelog; - private Users $users; - private AuthInfo $authInfo; - private Comments $comments; - - private array $userInfos = []; - private array $userColours = []; - public function __construct( - IConfig $config, - Changelog $changelog, - Users $users, - AuthInfo $authInfo, - Comments $comments - ) { - $this->config = $config; - $this->changelog = $changelog; - $this->users = $users; - $this->authInfo = $authInfo; - $this->comments = $comments; - } + private IConfig $config, + private Changelog $changelog, + private UsersContext $usersCtx, + private AuthInfo $authInfo, + private Comments $comments + ) {} public function registerRoutes(IRouter $router): void { $router->get('/changelog', $this->getIndex(...)); @@ -62,7 +47,7 @@ final class ChangelogRoutes implements IRouteHandler { } private function getCommentsInfo(string $categoryName): object { - $comments = new CommentsEx($this->authInfo, $this->comments, $this->users, $this->userInfos, $this->userColours); + $comments = new CommentsEx($this->authInfo, $this->comments, $this->usersCtx); return $comments->getCommentsForLayout($categoryName); } @@ -85,7 +70,7 @@ final class ChangelogRoutes implements IRouteHandler { $filterUser = null; else try { - $filterUser = $this->users->getUser($filterUser, 'id'); + $filterUser = $this->usersCtx->getUserInfo($filterUser); } catch(RuntimeException $ex) { return 404; } @@ -110,28 +95,12 @@ final class ChangelogRoutes implements IRouteHandler { $changes = []; foreach($changeInfos as $changeInfo) { - $userId = $changeInfo->getUserId(); - - if(array_key_exists($userId, $this->userInfos)) { - $userInfo = $this->userInfos[$userId]; - $userColour = $this->userColours[$userId]; - } else { - try { - $userInfo = $this->users->getUser($userId, 'id'); - $userColour = $this->users->getUserColour($userInfo); - } catch(RuntimeException $ex) { - $userInfo = null; - $userColour = null; - } - - $this->userInfos[$userId] = $userInfo; - $this->userColours[$userId] = $userColour; - } + $userInfo = $changeInfo->hasUserId() ? $this->usersCtx->getUserInfo($changeInfo->getUserId()) : null; $changes[] = [ 'change' => $changeInfo, 'user' => $userInfo, - 'user_colour' => $userColour, + 'user_colour' => $this->usersCtx->getUserColour($userInfo), ]; } @@ -153,20 +122,13 @@ final class ChangelogRoutes implements IRouteHandler { } $tagInfos = $this->changelog->getTags(changeInfo: $changeInfo); - - try { - $userInfo = $this->users->getUser($changeInfo->getUserId(), 'id'); - $userColour = $this->users->getUserColour($userInfo); - } catch(RuntimeException $ex) { - $userInfo = null; - $userColour = null; - } + $userInfo = $changeInfo->hasUserId() ? $this->usersCtx->getUserInfo($changeInfo->getUserId()) : null; return Template::renderRaw('changelog.change', [ 'change_info' => $changeInfo, 'change_tags' => $tagInfos, 'change_user_info' => $userInfo, - 'change_user_colour' => $userColour, + 'change_user_colour' => $this->usersCtx->getUserColour($userInfo), 'comments_info' => $this->getCommentsInfo($changeInfo->getCommentsCategoryName()), ]); } diff --git a/src/Comments/CommentsEx.php b/src/Comments/CommentsEx.php index e5891bd..b3d0223 100644 --- a/src/Comments/CommentsEx.php +++ b/src/Comments/CommentsEx.php @@ -6,15 +6,13 @@ use RuntimeException; use Misuzu\MisuzuContext; use Misuzu\Perm; use Misuzu\Auth\AuthInfo; -use Misuzu\Users\Users; +use Misuzu\Users\UsersContext; class CommentsEx { public function __construct( private AuthInfo $authInfo, private Comments $comments, - private Users $users, - private array $userInfos = [], - private array $userColours = [] + private UsersContext $usersCtx ) {} public function getCommentsForLayout(CommentsCategoryInfo|string $category): object { @@ -24,7 +22,7 @@ class CommentsEx { $hasUser = $this->authInfo->isLoggedIn(); $info->user = $hasUser ? $this->authInfo->getUserInfo() : null; - $info->colour = $hasUser ? $this->users->getUserColour($info->user) : null; + $info->colour = $this->usersCtx->getUserColour($info->user); $info->perms = $this->authInfo->getPerms('global')->checkMany([ 'can_post' => Perm::G_COMMENTS_CREATE, 'can_delete' => Perm::G_COMMENTS_DELETE_OWN | Perm::G_COMMENTS_DELETE_ANY, @@ -44,32 +42,12 @@ class CommentsEx { } public function decorateComment(CommentsPostInfo $postInfo): object { - if($postInfo->hasUserId()) { - $userId = $postInfo->getUserId(); - if(array_key_exists($userId, $this->userInfos)) { - $userInfo = $this->userInfos[$userId]; - $userColour = $this->userColours[$userId]; - } else { - try { - $userInfo = $this->users->getUser($userId, 'id'); - $userColour = $this->users->getUserColour($userInfo); - } catch(RuntimeException $ex) { - $userInfo = null; - $userColour = null; - } - - $this->userInfos[$userId] = $userInfo; - $this->userColours[$userId] = $userColour; - } - } else { - $userInfo = null; - $userColour = null; - } + $userInfo = $postInfo->hasUserId() ? $this->usersCtx->getUserInfo($postInfo->getUserId()) : null; $info = new stdClass; $info->post = $postInfo; $info->user = $userInfo; - $info->colour = $userColour; + $info->colour = $this->usersCtx->getUserColour($userInfo); $info->vote = $this->comments->getPostVote($postInfo, $userInfo); $info->replies = []; diff --git a/src/Home/HomeRoutes.php b/src/Home/HomeRoutes.php index c0f41f3..e752361 100644 --- a/src/Home/HomeRoutes.php +++ b/src/Home/HomeRoutes.php @@ -15,37 +15,19 @@ use Misuzu\Comments\Comments; use Misuzu\Config\IConfig; use Misuzu\Counters\Counters; use Misuzu\News\News; -use Misuzu\Users\Users; +use Misuzu\Users\UsersContext; class HomeRoutes implements IRouteHandler { - private IConfig $config; - private IDbConnection $dbConn; - private AuthInfo $authInfo; - private Changelog $changelog; - private Comments $comments; - private Counters $counters; - private News $news; - private Users $users; - public function __construct( - IConfig $config, - IDbConnection $dbConn, - AuthInfo $authInfo, - Changelog $changelog, - Comments $comments, - Counters $counters, - News $news, - Users $users - ) { - $this->config = $config; - $this->dbConn = $dbConn; - $this->authInfo = $authInfo; - $this->changelog = $changelog; - $this->comments = $comments; - $this->counters = $counters; - $this->news = $news; - $this->users = $users; - } + private IConfig $config, + private IDbConnection $dbConn, + private AuthInfo $authInfo, + private Changelog $changelog, + private Comments $comments, + private Counters $counters, + private News $news, + private UsersContext $usersCtx + ) {} public function registerRoutes(IRouter $router): void { $router->get('/', $this->getIndex(...)); @@ -70,15 +52,13 @@ class HomeRoutes implements IRouteHandler { } private function getOnlineUsers(): array { - return $this->users->getUsers( + return $this->usersCtx->getUsers()->getUsers( lastActiveInMinutes: 5, deleted: false, orderBy: 'random', ); } - private array $userInfos = []; - private array $userColours = []; private array $newsCategoryInfos = []; private function getFeaturedNewsPosts(int $amount, bool $decorate): array { @@ -93,24 +73,8 @@ class HomeRoutes implements IRouteHandler { $posts = []; foreach($postInfos as $postInfo) { - $userId = $postInfo->getUserId(); $categoryId = $postInfo->getCategoryId(); - - if(array_key_exists($userId, $this->userInfos)) { - $userInfo = $this->userInfos[$userId]; - $userColour = $this->userColours[$userId]; - } else { - try { - $userInfo = $this->users->getUser($userId, 'id'); - $userColour = $this->users->getUserColour($userInfo); - } catch(RuntimeException $ex) { - $userInfo = null; - $userColour = null; - } - - $this->userInfos[$userId] = $userInfo; - $this->userColours[$userId] = $userColour; - } + $userInfo = $postInfo->hasUserId() ? $this->usersCtx->getUserInfo($postInfo->getUserId()) : null; if(array_key_exists($categoryId, $this->newsCategoryInfos)) $categoryInfo = $this->newsCategoryInfos[$categoryId]; @@ -124,7 +88,7 @@ class HomeRoutes implements IRouteHandler { 'post' => $postInfo, 'category' => $categoryInfo, 'user' => $userInfo, - 'user_colour' => $userColour, + 'user_colour' => $this->usersCtx->getUserColour($userInfo), 'comments_count' => $commentsCount, ]; } @@ -213,11 +177,11 @@ class HomeRoutes implements IRouteHandler { $stats['users:online:recent'] = count($onlineUserInfos); $birthdays = []; - $birthdayInfos = $this->users->getUsers(deleted: false, birthdate: DateTime::now(), orderBy: 'random'); + $birthdayInfos = $this->usersCtx->getUsers()->getUsers(deleted: false, birthdate: DateTime::now(), orderBy: 'random'); foreach($birthdayInfos as $birthdayInfo) $birthdays[] = [ 'info' => $birthdayInfo, - 'colour' => $this->users->getUserColour($birthdayInfo), + 'colour' => $this->usersCtx->getUserColour($birthdayInfo), ]; $newestMember = []; @@ -225,10 +189,8 @@ class HomeRoutes implements IRouteHandler { $newestMemberId = $this->config->getString('users.newest'); if(!empty($newestMemberId)) try { - $newestMemberInfo = $this->users->getUser($newestMemberId, 'id'); - $newestMemberColour = $this->users->getUserColour($newestMemberInfo); - $newestMember['info'] = $newestMemberInfo; - $newestMember['colour'] = $newestMemberColour; + $newestMember['info'] = $this->usersCtx->getUserInfo($newestMemberId); + $newestMember['colour'] = $this->usersCtx->getUserColour($newestMemberId); } catch(RuntimeException $ex) { $newestMember = []; $this->config->removeValues('users.newest'); diff --git a/src/MisuzuContext.php b/src/MisuzuContext.php index a1292e6..550cfc4 100644 --- a/src/MisuzuContext.php +++ b/src/MisuzuContext.php @@ -32,13 +32,9 @@ use Misuzu\Perms\Permissions; use Misuzu\Profile\ProfileFields; use Misuzu\Satori\SatoriRoutes; use Misuzu\SharpChat\SharpChatRoutes; -use Misuzu\Users\Bans; +use Misuzu\Users\UsersContext; use Misuzu\Users\BanInfo; -use Misuzu\Users\ModNotes; -use Misuzu\Users\Roles; -use Misuzu\Users\Users; use Misuzu\Users\UserInfo; -use Misuzu\Users\Warnings; use Misuzu\Users\Assets\AssetsRoutes; // this class should function as the root for everything going forward @@ -51,49 +47,51 @@ class MisuzuContext { private IDbConnection $dbConn; private IConfig $config; private HttpFx $router; + private SasaeEnvironment $templating; + private AuditLog $auditLog; + private Counters $counters; private Emotes $emotes; + private Changelog $changelog; + private News $news; + private Comments $comments; + + private Sessions $sessions; private LoginAttempts $loginAttempts; private RecoveryTokens $recoveryTokens; - private ModNotes $modNotes; - private Bans $bans; - private Warnings $warnings; private TwoFactorAuthSessions $tfaSessions; - private Roles $roles; - private Users $users; - private Sessions $sessions; - private Counters $counters; + + private UsersContext $usersCtx; + private ProfileFields $profileFields; private Forum $forum; + private Permissions $perms; private AuthInfo $authInfo; - private SasaeEnvironment $templating; public function __construct(IDbConnection $dbConn, IConfig $config) { $this->dbConn = $dbConn; $this->config = $config; + + $this->usersCtx = new UsersContext($this->dbConn); + $this->perms = new Permissions($this->dbConn); $this->authInfo = new AuthInfo($this->perms); $this->auditLog = new AuditLog($this->dbConn); - $this->bans = new Bans($this->dbConn); $this->changelog = new Changelog($this->dbConn); $this->comments = new Comments($this->dbConn); $this->counters = new Counters($this->dbConn); $this->emotes = new Emotes($this->dbConn); $this->forum = new Forum($this->dbConn); $this->loginAttempts = new LoginAttempts($this->dbConn); - $this->modNotes = new ModNotes($this->dbConn); $this->news = new News($this->dbConn); $this->profileFields = new ProfileFields($this->dbConn); $this->recoveryTokens = new RecoveryTokens($this->dbConn); - $this->roles = new Roles($this->dbConn); $this->sessions = new Sessions($this->dbConn); $this->tfaSessions = new TwoFactorAuthSessions($this->dbConn); - $this->users = new Users($this->dbConn); - $this->warnings = new Warnings($this->dbConn); } public function getDbConn(): IDbConnection { @@ -149,30 +147,10 @@ class MisuzuContext { return $this->recoveryTokens; } - public function getModNotes(): ModNotes { - return $this->modNotes; - } - - public function getBans(): Bans { - return $this->bans; - } - - public function getWarnings(): Warnings { - return $this->warnings; - } - public function getTFASessions(): TwoFactorAuthSessions { return $this->tfaSessions; } - public function getRoles(): Roles { - return $this->roles; - } - - public function getUsers(): Users { - return $this->users; - } - public function getSessions(): Sessions { return $this->sessions; } @@ -193,6 +171,10 @@ class MisuzuContext { return $this->perms; } + public function getUsersContext(): UsersContext { + return $this->usersCtx; + } + public function createAuthTokenPacker(): AuthTokenPacker { return new AuthTokenPacker($this->config->getString('auth.secret', 'meow')); } @@ -210,8 +192,6 @@ class MisuzuContext { return $this->authInfo->getUserInfo(); } - private array $activeBansCache = []; - public function tryGetActiveBan(UserInfo|string|null $userInfo = null): ?BanInfo { if($userInfo === null) { if($this->isLoggedIn()) @@ -219,11 +199,7 @@ class MisuzuContext { else return null; } - $userId = (string)$userInfo->getId(); - if(!array_key_exists($userId, $this->activeBansCache)) - $this->activeBansCache[$userId] = $this->bans->tryGetActiveBan($userId); - - return $this->activeBansCache[$userId]; + return $this->usersCtx->tryGetActiveBan($userInfo); } public function hasActiveBan(UserInfo|string|null $userInfo = null): bool { @@ -321,35 +297,54 @@ class MisuzuContext { private function registerHttpRoutes(): void { $this->router->register(new HomeRoutes( - $this->config, $this->dbConn, $this->authInfo, - $this->changelog, $this->comments, $this->counters, $this->news, - $this->users + $this->config, + $this->dbConn, + $this->authInfo, + $this->changelog, + $this->comments, + $this->counters, + $this->news, + $this->usersCtx )); - $this->router->register(new AssetsRoutes($this->authInfo, $this->bans, $this->users)); + $this->router->register(new AssetsRoutes( + $this->authInfo, + $this->usersCtx + )); $this->router->register(new InfoRoutes); $this->router->register(new NewsRoutes( - $this->config, $this->authInfo, - $this->news, $this->users, $this->comments + $this->config, + $this->authInfo, + $this->news, + $this->usersCtx, + $this->comments )); $this->router->register(new ChangelogRoutes( - $this->config, $this->changelog, - $this->users, $this->authInfo, $this->comments + $this->config, + $this->changelog, + $this->usersCtx, + $this->authInfo, + $this->comments )); $this->router->register(new SharpChatRoutes( $this->config->scopeTo('sockChat'), - $this->bans, $this->emotes, $this->users, - $this->sessions, $this->perms, $this->authInfo, + $this->usersCtx, + $this->emotes, + $this->sessions, + $this->perms, + $this->authInfo, $this->createAuthTokenPacker(...) )); $this->router->register(new SatoriRoutes( - $this->dbConn, $this->config->scopeTo('satori'), - $this->users, $this->profileFields + $this->dbConn, + $this->config->scopeTo('satori'), + $this->usersCtx, + $this->profileFields )); // below is still only otherwise available as stinky php files diff --git a/src/News/NewsRoutes.php b/src/News/NewsRoutes.php index f4293b3..2e68f11 100644 --- a/src/News/NewsRoutes.php +++ b/src/News/NewsRoutes.php @@ -20,29 +20,17 @@ use Misuzu\Feeds\AtomFeedSerializer; use Misuzu\Feeds\RssFeedSerializer; use Misuzu\News\News; use Misuzu\News\NewsCategoryInfo; -use Misuzu\Users\Users; +use Misuzu\Users\UsersContext; use Misuzu\Parsers\Parser; class NewsRoutes implements IRouteHandler { - private IConfig $config; - private AuthInfo $authInfo; - private News $news; - private Users $users; - private Comments $comments; - public function __construct( - IConfig $config, - AuthInfo $authInfo, - News $news, - Users $users, - Comments $comments - ) { - $this->config = $config; - $this->authInfo = $authInfo; - $this->news = $news; - $this->users = $users; - $this->comments = $comments; - } + private IConfig $config, + private AuthInfo $authInfo, + private News $news, + private UsersContext $usersCtx, + private Comments $comments + ) {} public function registerRoutes(IRouter $router): void { $router->get('/news', $this->getIndex(...)); @@ -114,8 +102,6 @@ class NewsRoutes implements IRouteHandler { }); } - private array $userInfos = []; - private array $userColours = []; private array $categoryInfos = []; private function getNewsPostsForView(Pagination $pagination, ?NewsCategoryInfo $categoryInfo = null): array { @@ -127,32 +113,8 @@ class NewsRoutes implements IRouteHandler { ); foreach($postInfos as $postInfo) { - $userId = $postInfo->getUserId(); $categoryId = $postInfo->getCategoryId(); - - if(array_key_exists($userId, $this->userInfos)) { - $userInfo = $this->userInfos[$userId]; - } else { - try { - $userInfo = $this->users->getUser($userId, 'id'); - } catch(RuntimeException $ex) { - $userInfo = null; - } - - $this->userInfos[$userId] = $userInfo; - } - - if(array_key_exists($userId, $this->userColours)) { - $userColour = $this->userColours[$userId]; - } else { - try { - $userColour = $this->users->getUserColour($userInfo); - } catch(RuntimeException $ex) { - $userColour = null; - } - - $this->userColours[$userId] = $userColour; - } + $userInfo = $postInfo->hasUserId() ? $this->usersCtx->getUserInfo($postInfo->getUserId()) : null; if(array_key_exists($categoryId, $this->categoryInfos)) $categoryInfo = $this->categoryInfos[$categoryId]; @@ -167,7 +129,7 @@ class NewsRoutes implements IRouteHandler { 'post' => $postInfo, 'category' => $categoryInfo, 'user' => $userInfo, - 'user_colour' => $userColour, + 'user_colour' => $this->usersCtx->getUserColour($userInfo), 'comments_count' => $commentsCount, ]; } @@ -186,18 +148,7 @@ class NewsRoutes implements IRouteHandler { foreach($postInfos as $postInfo) { $userId = $postInfo->getUserId(); $categoryId = $postInfo->getCategoryId(); - - if(array_key_exists($userId, $this->userInfos)) { - $userInfo = $this->userInfos[$userId]; - } else { - try { - $userInfo = $this->users->getUser($userId, 'id'); - } catch(RuntimeException $ex) { - $userInfo = null; - } - - $this->userInfos[$userId] = $userInfo; - } + $userInfo = $postInfo->hasUserId() ? $this->usersCtx->getUserInfo($postInfo->getUserId()) : null; $posts[] = [ 'post' => $postInfo, @@ -293,21 +244,14 @@ class NewsRoutes implements IRouteHandler { $this->news->updatePostCommentCategory($postInfo, $commentsCategory); } - $userInfo = null; - $userColour = null; - if($postInfo->hasUserId()) - try { - $userInfo = $this->users->getUser($postInfo->getUserId(), 'id'); - $userColour = $this->users->getUserColour($userInfo); - } catch(RuntimeException $ex) {} - - $comments = new CommentsEx($this->authInfo, $this->comments, $this->users); + $userInfo = $postInfo->hasUserId() ? $this->usersCtx->getUserInfo($postInfo->getUserId()) : null; + $comments = new CommentsEx($this->authInfo, $this->comments, $this->usersCtx); return Template::renderRaw('news.post', [ 'post_info' => $postInfo, 'post_category_info' => $categoryInfo, 'post_user_info' => $userInfo, - 'post_user_colour' => $userColour, + 'post_user_colour' => $this->usersCtx->getUserColour($userInfo), 'comments_info' => $comments->getCommentsForLayout($commentsCategory), ]); } diff --git a/src/Satori/SatoriRoutes.php b/src/Satori/SatoriRoutes.php index 7069ce3..cdf2c69 100644 --- a/src/Satori/SatoriRoutes.php +++ b/src/Satori/SatoriRoutes.php @@ -10,25 +10,15 @@ use Index\Routing\IRouteHandler; use Misuzu\Pagination; use Misuzu\Config\IConfig; use Misuzu\Profile\ProfileFields; -use Misuzu\Users\Users; +use Misuzu\Users\UsersContext; final class SatoriRoutes implements IRouteHandler { - private IDbConnection $dbConn; - private IConfig $config; - private Users $users; - private ProfileFields $profileFields; - public function __construct( - IDbConnection $dbConn, - IConfig $config, - Users $users, - ProfileFields $profileFields - ) { - $this->dbConn = $dbConn; - $this->config = $config; - $this->users = $users; - $this->profileFields = $profileFields; - } + private IDbConnection $dbConn, + private IConfig $config, + private UsersContext $usersCtx, + private ProfileFields $profileFields + ) {} public function registerRoutes(IRouter $router): void { // Simplify default error pages @@ -145,7 +135,7 @@ final class SatoriRoutes implements IRouteHandler { $backlogDays = $this->config->getInteger('users.backlog', 7); $startId = (string)$request->getParam('start', FILTER_SANITIZE_NUMBER_INT); - $userInfos = $this->users->getUsers( + $userInfos = $this->usersCtx->getUsers()->getUsers( after: $startId, newerThanDays: $backlogDays, orderBy: 'id', diff --git a/src/SharpChat/SharpChatRoutes.php b/src/SharpChat/SharpChatRoutes.php index 3bc5ef9..88bdeaf 100644 --- a/src/SharpChat/SharpChatRoutes.php +++ b/src/SharpChat/SharpChatRoutes.php @@ -13,37 +13,20 @@ use Misuzu\Config\IConfig; use Misuzu\Emoticons\Emotes; use Misuzu\Perms\Permissions; use Misuzu\Users\Bans; -use Misuzu\Users\Users; +use Misuzu\Users\UsersContext; final class SharpChatRoutes implements IRouteHandler { - private IConfig $config; - private Bans $bans; - private Emotes $emotes; - private Users $users; - private Sessions $sessions; - private Permissions $perms; - private AuthInfo $authInfo; - private Closure $createAuthTokenPacker; private string $hashKey; public function __construct( - IConfig $config, - Bans $bans, - Emotes $emotes, - Users $users, - Sessions $sessions, - Permissions $perms, - AuthInfo $authInfo, - Closure $createAuthTokenPacker // this sucks lol + private IConfig $config, + private UsersContext $usersCtx, + private Emotes $emotes, + private Sessions $sessions, + private Permissions $perms, + private AuthInfo $authInfo, + private Closure $createAuthTokenPacker // this sucks lol ) { - $this->config = $config; - $this->bans = $bans; - $this->emotes = $emotes; - $this->users = $users; - $this->sessions = $sessions; - $this->perms = $perms; - $this->authInfo = $authInfo; - $this->createAuthTokenPacker = $createAuthTokenPacker; $this->hashKey = $this->config->getString('hashKey', 'woomy'); } @@ -157,7 +140,7 @@ final class SharpChatRoutes implements IRouteHandler { if($sessionInfo->getUserId() !== $tokenInfo->getUserId()) return ['ok' => false, 'err' => 'user']; - $userInfo = $this->users->getUser($sessionInfo->getUserId(), 'id'); + $userInfo = $this->usersCtx->getUsers()->getUser($sessionInfo->getUserId(), 'id'); $userId = $tokenInfo->hasImpersonatedUserId() && $userInfo->isSuperUser() ? $tokenInfo->getImpersonatedUserId() : $userInfo->getId(); @@ -197,7 +180,7 @@ final class SharpChatRoutes implements IRouteHandler { return 403; foreach($bumpList as $userId => $ipAddr) - $this->users->recordUserActivity($userId, remoteAddr: $ipAddr); + $this->usersCtx->getUsers()->recordUserActivity($userId, remoteAddr: $ipAddr); } public function postVerify($response, $request) { @@ -249,12 +232,12 @@ final class SharpChatRoutes implements IRouteHandler { $this->sessions->recordSessionActivity(sessionInfo: $sessionInfo, remoteAddr: $ipAddress); - $userInfo = $this->users->getUser($sessionInfo->getUserId(), 'id'); + $userInfo = $this->usersCtx->getUsers()->getUser($sessionInfo->getUserId(), 'id'); if($tokenInfo->hasImpersonatedUserId() && $userInfo->isSuperUser()) { $userInfoReal = $userInfo; try { - $userInfo = $this->users->getUser($tokenInfo->getImpersonatedUserId(), 'id'); + $userInfo = $this->usersCtx->getUsers()->getUser($tokenInfo->getImpersonatedUserId(), 'id'); } catch(RuntimeException $ex) { $userInfo = $userInfoReal; } @@ -263,9 +246,9 @@ final class SharpChatRoutes implements IRouteHandler { return ['success' => false, 'reason' => 'unsupported']; } - $this->users->recordUserActivity($userInfo, remoteAddr: $ipAddress); - $userColour = $this->users->getUserColour($userInfo); - $userRank = $this->users->getUserRank($userInfo); + $this->usersCtx->getUsers()->recordUserActivity($userInfo, remoteAddr: $ipAddress); + $userColour = $this->usersCtx->getUsers()->getUserColour($userInfo); + $userRank = $this->usersCtx->getUsers()->getUserRank($userInfo); return [ 'success' => true, @@ -290,18 +273,14 @@ final class SharpChatRoutes implements IRouteHandler { return 403; $list = []; - $bans = $this->bans->getBans(activeOnly: true); - $userInfos = []; + $bans = $this->usersCtx->getBans()->getBans(activeOnly: true); foreach($bans as $banInfo) { $userId = $banInfo->getUserId(); - if(array_key_exists($userId, $userInfos)) - $userInfo = $userInfos[$userId]; - else - $userInfos[$userId] = $userInfo = $this->users->getUser($userId, 'id'); - - $userColour = $this->users->getUserColour($userInfo); + $userInfo = $this->usersCtx->getUserInfo($userId); + $userColour = $this->usersCtx->getUserColour($userInfo); $isPerma = $banInfo->isPermanent(); + $list[] = [ 'is_ban' => true, 'user_id' => $userId, @@ -332,13 +311,13 @@ final class SharpChatRoutes implements IRouteHandler { if($userIdIsName) try { - $userInfo = $this->users->getUser($userId, 'name'); + $userInfo = $this->usersCtx->getUsers()->getUser($userId, 'name'); $userId = (string)$userInfo->getId(); } catch(RuntimeException $ex) { $userId = ''; } - $banInfo = $this->bans->tryGetActiveBan($userId); + $banInfo = $this->usersCtx->getBans()->tryGetActiveBan($userId); if($banInfo === null) return ['is_ban' => false]; @@ -400,19 +379,19 @@ final class SharpChatRoutes implements IRouteHandler { $modId = 69; try { - $modInfo = $this->users->getUser($modId, 'id'); + $modInfo = $this->usersCtx->getUsers()->getUser($modId, 'id'); } catch(RuntimeException $ex) { return 404; } try { - $userInfo = $this->users->getUser($userId, 'id'); + $userInfo = $this->usersCtx->getUsers()->getUser($userId, 'id'); } catch(RuntimeException $ex) { return 404; } try { - $this->bans->createBan( + $this->usersCtx->getBans()->createBan( $userInfo, $expires, $reason, @@ -442,11 +421,11 @@ final class SharpChatRoutes implements IRouteHandler { if($type !== 'user') return 404; - $banInfo = $this->bans->tryGetActiveBan($subject); + $banInfo = $this->usersCtx->getBans()->tryGetActiveBan($subject); if($banInfo === null) return 404; - $this->bans->deleteBans($banInfo); + $this->usersCtx->getBans()->deleteBans($banInfo); return 204; } diff --git a/src/Users/Assets/AssetsRoutes.php b/src/Users/Assets/AssetsRoutes.php index 272e1b6..a546634 100644 --- a/src/Users/Assets/AssetsRoutes.php +++ b/src/Users/Assets/AssetsRoutes.php @@ -7,20 +7,14 @@ use Index\Routing\IRouter; use Index\Routing\IRouteHandler; use Misuzu\Perm; use Misuzu\Auth\AuthInfo; -use Misuzu\Users\Bans; -use Misuzu\Users\Users; +use Misuzu\Users\UsersContext; use Misuzu\Users\UserInfo; class AssetsRoutes implements IRouteHandler { - private AuthInfo $authInfo; - private Bans $bans; - private Users $users; - - public function __construct(AuthInfo $authInfo, Bans $bans, Users $users) { - $this->authInfo = $authInfo; - $this->bans = $bans; - $this->users = $users; - } + public function __construct( + private AuthInfo $authInfo, + private UsersContext $usersCtx + ) {} public function registerRoutes(IRouter $router): void { $router->get('/assets/avatar', $this->getAvatar(...)); @@ -31,7 +25,7 @@ class AssetsRoutes implements IRouteHandler { } private function canViewAsset($request, UserInfo $assetUser): bool { - if($this->bans->countActiveBans($assetUser)) + if($this->usersCtx->getBans()->countActiveBans($assetUser)) // allow staff viewing profile to still see banned user assets return $this->authInfo->getPerms('user')->check(Perm::U_USERS_MANAGE) && parse_url($request->getHeaderFirstLine('Referer'), PHP_URL_PATH) === url('user-profile'); @@ -44,7 +38,7 @@ class AssetsRoutes implements IRouteHandler { $assetInfo = new StaticUserImageAsset(MSZ_PUBLIC . '/images/no-avatar.png', MSZ_PUBLIC); try { - $userInfo = $this->users->getUser($userId, 'id'); + $userInfo = $this->usersCtx->getUserInfo($userId); if(!$this->canViewAsset($request, $userInfo)) { $assetInfo = new StaticUserImageAsset(MSZ_PUBLIC . '/images/banned-avatar.png', MSZ_PUBLIC); @@ -63,7 +57,7 @@ class AssetsRoutes implements IRouteHandler { $userId = pathinfo($fileName, PATHINFO_FILENAME); try { - $userInfo = $this->users->getUser($userId, 'id'); + $userInfo = $this->usersCtx->getUserInfo($userId); } catch(RuntimeException $ex) { } catch(InvalidArgumentException $ex) {} diff --git a/src/Users/Users.php b/src/Users/Users.php index 56fcd2c..1aafc8b 100644 --- a/src/Users/Users.php +++ b/src/Users/Users.php @@ -234,10 +234,7 @@ class Users { 'recovery' => self::GET_USER_MAIL, ]; - public function getUser(string $value, int|string $select = self::GET_USER_ID): UserInfo { - if($value === '') - throw new InvalidArgumentException('$value may not be empty.'); - + public static function resolveGetUserSelectAlias(int|string $select): int { if(is_string($select)) { if(!array_key_exists($select, self::GET_USER_SELECT_ALIASES)) throw new InvalidArgumentException('Invalid $select alias.'); @@ -245,6 +242,14 @@ class Users { } elseif($select === 0) throw new InvalidArgumentException('$select may not be zero.'); + return $select; + } + + public function getUser(string $value, int|string $select = self::GET_USER_ID): UserInfo { + if($value === '') + throw new InvalidArgumentException('$value may not be empty.'); + + $select = self::resolveGetUserSelectAlias($select); $selectId = ($select & self::GET_USER_ID) > 0; $selectName = ($select & self::GET_USER_NAME) > 0; $selectMail = ($select & self::GET_USER_MAIL) > 0; diff --git a/src/Users/UsersContext.php b/src/Users/UsersContext.php new file mode 100644 index 0000000..68f5545 --- /dev/null +++ b/src/Users/UsersContext.php @@ -0,0 +1,100 @@ +users = new Users($dbConn); + $this->roles = new Roles($dbConn); + $this->bans = new Bans($dbConn); + $this->warnings = new Warnings($dbConn); + $this->modNotes = new ModNotes($dbConn); + } + + public function getUsers(): Users { + return $this->users; + } + + public function getRoles(): Roles { + return $this->roles; + } + + public function getBans(): Bans { + return $this->bans; + } + + public function getWarnings(): Warnings { + return $this->warnings; + } + + public function getModNotes(): ModNotes { + return $this->modNotes; + } + + public function getUserInfo(string $value, int|string|null $select = null): UserInfo { + $select = Users::resolveGetUserSelectAlias($select ?? Users::GET_USER_ID); + if(($select & Users::GET_USER_ID) > 0 && array_key_exists($value, $this->userInfos)) + return $this->userInfos[$value]; + + $userInfo = $this->users->getUser($value, $select); + $userId = $userInfo->getId(); + + return $this->userInfos[$userId] = $userInfo; + } + + public function getUserColour(UserInfo|string|null $userInfo): Colour { + if($userInfo === null) + return Colour::none(); + $userId = $userInfo instanceof UserInfo ? $userInfo->getId() : $userInfo; + + if(array_key_exists($userId, $this->userColours)) + return $this->userColours[$userId]; + + return $this->userColours[$userId] = $this->users->getUserColour($userInfo); + } + + public function getUserRank(UserInfo|string|null $userInfo): int { + if($userInfo === null) + return 0; + if($userInfo instanceof UserInfo) + $userInfo = $userInfo->getId(); + + if(array_key_exists($userInfo, $this->userRanks)) + return $this->userRanks[$userInfo]; + + return $this->userRanks[$userInfo] = $this->users->getUserRank($userInfo); + } + + public function tryGetActiveBan( + UserInfo|string|null $userInfo = null, + int $minimumSeverity = Bans::SEVERITY_MIN + ): ?BanInfo { + if($userInfo === null) + return null; + if($userInfo instanceof UserInfo) + $userInfo = $userInfo->getId(); + + if(!array_key_exists($userInfo, $this->activeBans)) + $this->activeBans[$userInfo] = $this->bans->tryGetActiveBan($userInfo); + + return $this->activeBans[$userInfo]; + } + + public function hasActiveBan(UserInfo|string|null $userInfo = null): bool { + return $this->tryGetActiveBan($userInfo) !== null; + } +}