From 1366f3cd7072424ac52b4fe950c4732cfb73672c Mon Sep 17 00:00:00 2001 From: martin-mfg <2026226+martin-mfg@users.noreply.github.com> Date: Wed, 10 Sep 2025 13:48:57 +0200 Subject: [PATCH] fix log line, small code cleanup --- src/common/database.js | 11 +++++------ src/repeatRequests.js | 7 ++++++- src/users.js | 21 +++++---------------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/common/database.js b/src/common/database.js index 86248e0b..394136e9 100644 --- a/src/common/database.js +++ b/src/common/database.js @@ -79,21 +79,21 @@ export async function deleteOldRequests() { DELETE FROM requests WHERE user_requested_at < NOW() - INTERVAL '8 days' `; - let result; try { - result = await pool.query(deleteQuery); + let result = await pool.query(deleteQuery); + console.log(`Deleted ${result.rowCount} old requests.`); } catch (err) { if (err.code === "42P01") { console.log("Error deleting requests, table doesn't exist"); } else { throw err; } - console.log(`Deleted ${result.rowCount} old requests.`); } } /** * Fetches all requests which are between 11 hours and 8 days old. + * * @returns {Promise} Array of all requests between 11 hours and 8 days old. */ export async function getRecentRequests() { @@ -123,6 +123,7 @@ export async function getRecentRequests() { /** * Inserts or updates a user in the database. + * * @param {string} userId GitHub userId (login name) * @param {string} accessToken GitHub access token * @param {string|null} userKey Optional user key @@ -179,16 +180,14 @@ export async function deleteUser(userKey) { DELETE FROM authenticated_users WHERE user_key = $1 `; - let result; try { - result = await pool.query(deleteQuery, [userKey]); + await pool.query(deleteQuery, [userKey]); } catch (err) { if (err.code === "42P01") { console.log("Error deleting user, table doesn't exist"); } else { throw err; } - console.log(`Deleted ${result.rowCount} user(s).`); } } diff --git a/src/repeatRequests.js b/src/repeatRequests.js index afd740ae..033f7fe5 100644 --- a/src/repeatRequests.js +++ b/src/repeatRequests.js @@ -1,8 +1,13 @@ import axios from "axios"; -import { pool, deleteOldRequests, getRecentRequests } from "./common/database.js"; +import { + pool, + deleteOldRequests, + getRecentRequests, +} from "./common/database.js"; /** * Processes URLs with a thread pool of given size using axios.get. + * * @param {string[]} urls An array of URLs to process. * @param {number} poolSize The number of concurrent requests to process. * @returns {Promise} A promise that resolves when all requests are processed. diff --git a/src/users.js b/src/users.js index 31fe793b..cb65b89a 100644 --- a/src/users.js +++ b/src/users.js @@ -1,25 +1,13 @@ import axios from "axios"; import { storeUser } from "./common/database.js"; -/** - * Set user key for a given code - * @param {string} code GitHub authentication code from OAuth process - * @param {string} userKey user key to associate with the user - * @returns {Promise} the user key, unchanged - */ -/* -async function setUserKey(code, userKey) { - await storeCodeKey(code, userKey); - return userKey; -} -*/ - /** * Given an access token, return the GitHub login (userId) or null if invalid + * * @param {string} accessToken GitHub access token * @returns {Promise} login name or null if invalid access_token */ -async function getUnknownUser(accessToken) { +async function getUserFromToken(accessToken) { const res = await axios.get("https://api.github.com/user", { headers: { Accept: "application/vnd.github.v3+json", @@ -32,6 +20,7 @@ async function getUnknownUser(accessToken) { /** * Exchanges OAuth code for access token and returns userId + accessToken + * * @param {string} code GitHub authentication code from OAuth process * @returns {Promise<{userId: string, accessToken: string}>} user_id and access_token of authenticated user */ @@ -73,13 +62,13 @@ async function githubAuthenticate(code) { throw new Error("OAuth Error: access_token missing from response"); } - const userId = await getUnknownUser(accessToken); + const userId = await getUserFromToken(accessToken); if (!userId) { throw new Error("OAuth Error: Invalid user_id/access_token"); } - console.log("OAuth SignUp", `${Date.now() - start} ms`); + console.log("GitHub Authentication", `${Date.now() - start} ms`); return { userId, accessToken }; } catch (err) { if (err.response) {