add util functions for retrieving users

This commit is contained in:
zkldi
2021-04-04 17:57:38 +01:00
parent 619ac0c293
commit 675cb4528c
+75
View File
@@ -0,0 +1,75 @@
import { PrivateUserDocument, PublicUserDocument } from "kamaitachi-common";
import db from "../db";
const PRIVATE_USER_RETURNS = {
password: 0,
email: 0,
// legacy protection - this field does not exist on user documents anymore, but it did, and it *did* hold personal information.
integrations: 0,
};
/**
* Gets a user based on their username case-insensitively.
* @param username The username of the user.
* @returns PublicUserDocument
*/
export function GetUserCaseInsensitive(username: string) {
return db.get<PrivateUserDocument>("users").findOne(
{
usernameLowercase: username.toLowerCase(),
},
{
projection: PRIVATE_USER_RETURNS,
}
);
}
/**
* Returns GetUserCaseInsensitive, but without the private field omission.
* @see GetUserCaseInsensitive
* @param username The username of the user.
* @returns PrivateUserDocument
*/
export function PRIVATEINFO_GetUserCaseInsensitive(username: string) {
return db.get<PrivateUserDocument>("users").findOne({
usernameLowercase: username.toLowerCase(),
});
}
/**
* Returns a formatted string indicating the user. This is used for logging.
* @param userdoc The user document to format.
*/
export function FormatUserDoc(userdoc: PublicUserDocument) {
return `${userdoc.username} (~${userdoc.id})`;
}
/**
* Gets a user based on either their username case-insensitively, or a direct lookup of their ID.
* This is used in URLs to resolve the passed user.
* @param usernameOrID
*/
export function ResolveUser(usernameOrID: string) {
// user ID passed
if (usernameOrID.match(/^[0-9]$/)) {
let intID = parseInt(usernameOrID, 10);
return db.get<PrivateUserDocument>("users").findOne(
{
id: intID,
},
{
projection: PRIVATE_USER_RETURNS,
}
);
}
return db.get<PrivateUserDocument>("users").findOne(
{
usernameLowercase: usernameOrID,
},
{
projection: PRIVATE_USER_RETURNS,
}
);
}