fix log line, small code cleanup

This commit is contained in:
martin-mfg
2025-09-10 13:48:57 +02:00
parent 1ab3d66024
commit 1366f3cd70
3 changed files with 16 additions and 23 deletions
+5 -6
View File
@@ -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<string[]>} 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).`);
}
}
+6 -1
View File
@@ -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<void>} A promise that resolves when all requests are processed.
+5 -16
View File
@@ -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<string>} 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<string|null>} 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) {