From 8cebfb6fae04084dfc6d382163acd42b0b47923f Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 27 Feb 2019 15:05:27 +0100 Subject: [PATCH] Added raw following/followers pages. --- public/profile.php | 32 ++++++++++++++++++++++++++++++++ src/Users/relations.php | 37 +++++++++++++++++++++++++++++++++++++ src/Users/user.php | 10 +++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/public/profile.php b/public/profile.php index b1d38bd3..9000a0c4 100644 --- a/public/profile.php +++ b/public/profile.php @@ -99,6 +99,38 @@ switch ($mode) { echo file_get_contents($userBackground); break; + case 'following': + $userId = (int)($_GET['u'] ?? 0); + + if (!user_exists($userId)) { + http_response_code(404); + echo tpl_render('user.notfound'); + break; + } + + $followingIds = user_relation_users_from($userId, MSZ_USER_RELATION_FOLLOW); + + foreach ($followingIds as $user) { + echo "{$user['user_id']}|{$user['relation_created']}
"; + } + break; + + case 'followers': + $userId = (int)($_GET['u'] ?? 0); + + if (!user_exists($userId)) { + http_response_code(404); + echo tpl_render('user.notfound'); + break; + } + + $followerIds = user_relation_users_to($userId, MSZ_USER_RELATION_FOLLOW); + + foreach ($followerIds as $user) { + echo "{$user['user_id']}|{$user['relation_created']}
"; + } + break; + default: $userId = user_find_for_profile($_GET['u'] ?? 0); diff --git a/src/Users/relations.php b/src/Users/relations.php index bfcfaa1f..c5323949 100644 --- a/src/Users/relations.php +++ b/src/Users/relations.php @@ -81,3 +81,40 @@ function user_relation_info(int $userId, int $subjectId): array $getRelationInfo->bindValue('subject_id', $subjectId); return db_fetch($getRelationInfo); } + +function user_relation_users(int $userId, int $type, bool $from): array +{ + if ($userId < 1 || $type <= MSZ_USER_RELATION_NONE || !user_relation_is_valid_type($type)) { + return []; + } + + static $getUsers = []; + + if (empty($getUsers[$from])) { + $getUsers[$from] = db_prepare(sprintf( + ' + SELECT `%1$s` AS `user_id`, `relation_created` + FROM `msz_user_relations` + WHERE `%2$s` = :user_id + AND `relation_type` = :type + ', + $from ? 'subject_id' : 'user_id', + $from ? 'user_id' : 'subject_id' + )); + } + + $getUsers[$from]->bindValue('user_id', $userId); + $getUsers[$from]->bindValue('type', $type); + + return db_fetch_all($getUsers[$from]); +} + +function user_relation_users_to(int $userId, int $type): array +{ + return user_relation_users($userId, $type, false); +} + +function user_relation_users_from(int $userId, int $type): array +{ + return user_relation_users($userId, $type, true); +} diff --git a/src/Users/user.php b/src/Users/user.php index 79249195..ab494058 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -165,13 +165,21 @@ function user_exists(int $userId): bool return false; } + static $exists = []; + + if (isset($exists[$userId])) { + return $exists[$userId]; + } + $check = db_prepare(' SELECT COUNT(`user_id`) > 0 FROM `msz_users` WHERE `user_id` = :user_id '); + $check->bindValue('user_id', $userId); - return $check->execute() ? (bool)$check->fetchColumn() : false; + + return $exists[$userId] = (bool)($check->execute() ? $check->fetchColumn() : false); } function user_id_from_username(string $username): int