From 0fac4eabc661e4425cf21d461c0e18b542fa8003 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Fri, 16 May 2025 01:04:52 +0500 Subject: feat: some code refactoring --- public/users.php | 228 ++++++++++++++++++------------------------------------- 1 file changed, 73 insertions(+), 155 deletions(-) (limited to 'public/users.php') diff --git a/public/users.php b/public/users.php index fcfd92b..3f27745 100644 --- a/public/users.php +++ b/public/users.php @@ -5,6 +5,7 @@ include_once "../src/partials.php"; include_once "../src/utils.php"; include_once "../src/accounts.php"; include_once "../src/alert.php"; +include_once "../src/emote.php"; authorize_user(); @@ -131,117 +132,68 @@ if ($id == "" && $alias_id == "") { exit; } -$stmt = null; +// --- fetching user +$user = null; -if ($id != "") { - $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); - $stmt->execute([$id]); -} else if ($alias_id != "") { - $stmt = $db->prepare("SELECT u.* FROM users u - INNER JOIN connections co ON (co.alias_id = ? AND co.platform = 'twitch') - WHERE co.user_id = u.id - "); - $stmt->execute([$alias_id]); -} +// fetching user by connection +if (isset($_GET["alias_id"])) { + $alias_id = $_GET["alias_id"]; + $platform = $_GET["platform"] ?? "twitch"; -$user = null; + $stmt = $db->prepare("SELECT u.id FROM users u + INNER JOIN connections co ON co.alias_id = ? AND co.platform = ? + WHERE co.user_id = u.id + "); + $stmt->execute([$alias_id, $platform]); -if ($row = $stmt->fetch()) { - $user = new User($row); + if ($row = $stmt->fetch()) { + $user = User::get_user_by_id($db, $row["id"]); + } +} +// fetching user by internal id +else if (isset($_GET["id"])) { + $user = User::get_user_by_id($db, $_GET["id"]); } -if ($user == null) { +if (!$user) { generate_alert("/404.php", "The user you requested cannot be found", 404); exit; } // User preferences $stmt = $db->prepare("SELECT * FROM user_preferences WHERE id = ?"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $user_preferences = $stmt->fetch(PDO::FETCH_ASSOC); -$public_profile = !$user_preferences["private_profile"] || $user->id() == ($_SESSION["user_id"] ?? ""); +$public_profile = !$user_preferences["private_profile"] || $user->id == ($_SESSION["user_id"] ?? ""); -// --- EMOTE SETS --- -// TODO: OPTIMIZE IT ASAP!!! -$emote_sets = []; +// fetching emote sets +$emote_sets = Emoteset::get_all_user_emotesets($db, $user->id); $active_emote_set = null; - -// gathering acquired emote sets -$stmt = $db->prepare("SELECT emote_set_id, is_default FROM acquired_emote_sets WHERE user_id = ?"); -$stmt->execute([$user->id()]); - -while ($row = $stmt->fetch()) { - // getting more info about set - $set_stmt = $db->prepare("SELECT id, name FROM emote_sets WHERE id = ?"); - $set_stmt->execute([$row["emote_set_id"]]); - $set = $set_stmt->fetch(); - - // getting info about emote set content - $em_stmt = $db->prepare( - "SELECT e.id, e.created_at, - CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by, - CASE - WHEN esc.code IS NOT NULL THEN esc.code - ELSE e.code - END AS code, - CASE - WHEN esc.code IS NOT NULL THEN e.code - ELSE NULL - END AS original_code - FROM emotes e - LEFT JOIN user_preferences up ON up.id = e.uploaded_by - INNER JOIN emote_set_contents AS esc - ON esc.emote_set_id = ? - WHERE esc.emote_id = e.id - " . ($row["is_default"] ? '' : ' LIMIT 5') - ); - $em_stmt->execute([$_SESSION["user_id"] ?? "", $row["emote_set_id"]]); - - $emote_set_emotes = $em_stmt->fetchAll(PDO::FETCH_ASSOC); - foreach ($emote_set_emotes as &$e) { - $e["ext"] = "webp"; - if ($e["uploaded_by"]) { - $uploaded_by_stmt = $db->prepare("SELECT id, username FROM users WHERE id = ?"); - $uploaded_by_stmt->execute([$e["uploaded_by"]]); - $e["uploaded_by"] = $uploaded_by_stmt->fetch(PDO::FETCH_ASSOC); - } +foreach ($emote_sets as $es) { + if ($es->is_default) { + $active_emote_set = $es; + break; } - - $emote_set = [ - "id" => $set["id"], - "name" => $set["name"], - "emotes" => $emote_set_emotes - ]; - - if ($row["is_default"]) { - $active_emote_set = count($emote_sets); - } - - array_push($emote_sets, $emote_set); } -$active_emote_set = &$emote_sets[$active_emote_set]; - // gathering uploaded emotes $uploaded_emotes = []; if ($public_profile) { - $stmt = $db->prepare("SELECT e.*, - CASE WHEN EXISTS ( - SELECT 1 - FROM emote_set_contents ec - INNER JOIN emote_sets es ON es.id = ec.emote_set_id - WHERE ec.emote_id = e.id AND es.owner_id = ? - ) THEN 1 ELSE 0 END AS is_in_user_set + $stmt = $db->prepare("SELECT e.id, e.code, e.uploaded_by, e.source, e.visibility FROM emotes e WHERE e.uploaded_by = ? ORDER BY e.created_at ASC "); - $stmt->execute([$user->id(), $user->id()]); + $stmt->execute([$user->id]); + + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); - $uploaded_emotes = $stmt->fetchAll(PDO::FETCH_ASSOC); + foreach ($rows as $row) { + array_push($uploaded_emotes, Emote::from_array_with_user($row, $db)); + } } // gathering actions @@ -249,7 +201,7 @@ $actions = []; if ($public_profile) { $stmt = $db->prepare("SELECT a.* FROM actions a WHERE a.user_id = ? ORDER BY a.created_at DESC LIMIT 15"); - $stmt->execute([$user->id()]); + $stmt->execute([$user->id]); $actions = $stmt->fetchAll(PDO::FETCH_ASSOC); } @@ -257,23 +209,23 @@ if ($public_profile) { // calculating contributions $stmt = $db->prepare("SELECT COUNT(*) FROM emotes WHERE uploaded_by = ?"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $contributions = intval($stmt->fetch()[0]); $stmt = $db->prepare("SELECT COUNT(*) FROM ratings WHERE user_id = ?"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $contributions += intval($stmt->fetch()[0]); // getting status $stmt = $db->prepare("SELECT * FROM roles r INNER JOIN role_assigns ra ON ra.user_id = ? WHERE ra.role_id = r.id"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $role = $stmt->fetch(PDO::FETCH_ASSOC) ?? null; // getting reactions $stmt = $db->prepare("SELECT rate, COUNT(*) AS c FROM ratings WHERE user_id = ? GROUP BY rate ORDER BY c DESC"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $fav_reactions = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -285,7 +237,7 @@ $stmt = $db->prepare("SELECT b.* FROM badges b INNER JOIN user_badges ub ON ub.user_id = ? WHERE b.id = ub.badge_id "); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $custom_badge = null; if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { @@ -293,27 +245,25 @@ if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { } if ($is_json) { - header("Content-type: application/json"); - echo json_encode([ + $user_data = (array) $user; + + unset($user_data["private_profile"]); + + $user_data["stats"] = [ + "contributions" => $contributions, + "favorite_reaction_id" => $fav_reactions, + "favorite_emote_id" => $fav_emote + ]; + + $user_data["active_emote_set_id"] = $active_emote_set->id; + $user_data["emote_sets"] = $emote_sets; + $user_data["uploaded_emotes"] = $uploaded_emotes; + $user_data["actions"] = $actions; + + json_response([ "status_code" => 200, "message" => null, - "data" => [ - "id" => $user->id(), - "username" => $user->username(), - "joined_at" => $user->joined_at(), - "last_active_at" => $user->last_active_at(), - "stats" => [ - "role" => $role, - "contributions" => $contributions, - "favorite_reaction_id" => $fav_reaction, - "favorite_emote_id" => $fav_emote - ], - "active_emote_set_id" => $active_emote_set["id"], - "emote_sets" => $emote_sets, - "uploaded_emotes" => $uploaded_emotes, - "actions" => $actions, - "custom_badge" => $custom_badge - ] + "data" => $user_data ]); exit; } @@ -322,14 +272,13 @@ if ($is_json) { - <?php echo sprintf("%s - %s", $user->username(), INSTANCE_NAME) ?> + <?php echo sprintf("%s - %s", $user->username, INSTANCE_NAME) ?> -
+
@@ -342,7 +291,7 @@ if ($is_json) {