mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-07 14:28:09 +03:00
Bigger patches
This commit is contained in:
@@ -6,7 +6,7 @@ import {
|
||||
ReinstateInvite,
|
||||
} from "./auth-core";
|
||||
import t from "tap";
|
||||
import db, { CloseConnection } from "../db";
|
||||
import db, { CloseConnection } from "../db/db";
|
||||
import { PrivateUserDocument } from "kamaitachi-common";
|
||||
import prAssert from "../test-utils/prassert";
|
||||
import Prudence from "prudence";
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
PublicAPIKeyDocument,
|
||||
PublicUserDocument,
|
||||
} from "kamaitachi-common";
|
||||
import db from "../db";
|
||||
import db from "../db/db";
|
||||
import { GetNextCounterValue } from "./db-core";
|
||||
import { InsertResult } from "monk";
|
||||
import CreateLogCtx from "../logger";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { GetNextCounterValue } from "./db-core";
|
||||
import t from "tap";
|
||||
import db, { CloseConnection } from "../db";
|
||||
import db, { CloseConnection } from "../db/db";
|
||||
import ResetDBState from "../test-utils/reset-db-state";
|
||||
|
||||
t.test("#GetNextCounterValue", (t) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import db from "../db";
|
||||
import db from "../db/db";
|
||||
import CreateLogCtx from "../logger";
|
||||
const logger = CreateLogCtx("db-core.ts");
|
||||
import { integer } from "../types";
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { PublicUserDocument } from "kamaitachi-common";
|
||||
import t from "tap";
|
||||
import { FormatUserDoc } from "./format-user";
|
||||
|
||||
t.test("#FormatUserDoc", (t) => {
|
||||
t.is(
|
||||
FormatUserDoc({ username: "zkldi", id: 123 } as PublicUserDocument),
|
||||
"zkldi (~123)",
|
||||
"Should format a user document into username ~id format."
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import t from "tap";
|
||||
import prAssert from "../test-utils/prassert";
|
||||
import { GetUserCaseInsensitive } from "./user-core";
|
||||
|
||||
t.test("#GetUserCaseInsensitive", async (t) => {
|
||||
t.test("Should return the user for an exact username", async (t) => {
|
||||
let result = await GetUserCaseInsensitive("zkldi");
|
||||
|
||||
t.isNot(result, null, "Should not return null");
|
||||
|
||||
prAssert(result,)
|
||||
})
|
||||
|
||||
|
||||
t.end()
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { integer, PublicUserDocument } from "kamaitachi-common";
|
||||
import db from "../db";
|
||||
import db from "../db/db";
|
||||
|
||||
const OMIT_PRIVATE_USER_RETURNS = {
|
||||
password: 0,
|
||||
|
||||
@@ -1,98 +1,98 @@
|
||||
import {
|
||||
ChartDocument,
|
||||
config,
|
||||
CounterDocument,
|
||||
FolderDocument,
|
||||
Game,
|
||||
GoalDocument,
|
||||
IIDXBPIData,
|
||||
IIDXEamusementScoreDocument,
|
||||
ImportDocument,
|
||||
InviteCodeDocument,
|
||||
MilestoneDocument,
|
||||
NotificationDocument,
|
||||
PrivateUserDocument,
|
||||
PublicAPIKeyDocument,
|
||||
ScoreDocument,
|
||||
SessionDocument,
|
||||
SongDocument,
|
||||
UserGoalDocument,
|
||||
UserMilestoneDocument,
|
||||
} from "kamaitachi-common";
|
||||
import monk, { ICollection } from "monk";
|
||||
import CreateLogCtx from "./logger";
|
||||
|
||||
const logger = CreateLogCtx("db.ts");
|
||||
|
||||
const url =
|
||||
process.env.NODE_ENV === "test" ? "localhost:27017/testingdb" : "localhost:27017/ktblackdb";
|
||||
|
||||
let dbtime: [number, number] = [0, 0];
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
logger.info(`Connecting to database ${url}...`);
|
||||
dbtime = process.hrtime();
|
||||
}
|
||||
|
||||
export let monkDB = monk(url);
|
||||
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
monkDB.then(() => {
|
||||
let time = process.hrtime(dbtime);
|
||||
let elapsed = time[0] + time[1] / 1000000;
|
||||
logger.info(`Database connection successful: took ${elapsed}ms`);
|
||||
});
|
||||
}
|
||||
|
||||
export async function ReOpenConnection() {
|
||||
monkDB = monk(url);
|
||||
await monkDB.then();
|
||||
}
|
||||
|
||||
export async function CloseConnection() {
|
||||
await monkDB.close();
|
||||
}
|
||||
|
||||
const StaticCollections = {
|
||||
scores: monkDB.get<ScoreDocument>("scores"),
|
||||
folders: monkDB.get<FolderDocument>("folders"),
|
||||
goals: monkDB.get<GoalDocument>("goals"),
|
||||
"user-goals": monkDB.get<UserGoalDocument>("user-goals"),
|
||||
milestones: monkDB.get<MilestoneDocument>("milestones"),
|
||||
"user-milestones": monkDB.get<UserMilestoneDocument>("user-milestones"),
|
||||
users: monkDB.get<PrivateUserDocument>("users"),
|
||||
imports: monkDB.get<ImportDocument>("imports"),
|
||||
notifications: monkDB.get<NotificationDocument>("notifications"),
|
||||
sessions: monkDB.get<SessionDocument>("sessions"),
|
||||
"iidx-bpi-data": monkDB.get<IIDXBPIData>("iidx-bpi-data"),
|
||||
"public-api-keys": monkDB.get<PublicAPIKeyDocument>("public-api-keys"),
|
||||
invites: monkDB.get<InviteCodeDocument>("invites"),
|
||||
counters: monkDB.get<CounterDocument>("counters"),
|
||||
"iidx-eam-scores": monkDB.get<IIDXEamusementScoreDocument>("iidx-eam-scores"),
|
||||
};
|
||||
|
||||
type GameCollections = Record<Game, ICollection>;
|
||||
|
||||
interface GCPartial {
|
||||
songs: Partial<GameCollections>;
|
||||
charts: Partial<GameCollections>;
|
||||
}
|
||||
|
||||
interface GameCollectionFull {
|
||||
songs: GameCollections;
|
||||
charts: GameCollections;
|
||||
}
|
||||
|
||||
let GameCollectionPartial: GCPartial = {
|
||||
songs: {},
|
||||
charts: {},
|
||||
};
|
||||
|
||||
for (const game of config.supportedGames) {
|
||||
GameCollectionPartial.songs[game] = monkDB.get<SongDocument>(`songs-${game}`);
|
||||
GameCollectionPartial.charts[game] = monkDB.get<ChartDocument>(`charts-${game}`);
|
||||
}
|
||||
|
||||
// a typescript-friendly interface for the database.
|
||||
const db = Object.assign(StaticCollections, GameCollectionPartial as GameCollectionFull);
|
||||
|
||||
export default db;
|
||||
import {
|
||||
ChartDocument,
|
||||
config,
|
||||
CounterDocument,
|
||||
FolderDocument,
|
||||
Game,
|
||||
GoalDocument,
|
||||
IIDXBPIData,
|
||||
IIDXEamusementScoreDocument,
|
||||
ImportDocument,
|
||||
InviteCodeDocument,
|
||||
MilestoneDocument,
|
||||
NotificationDocument,
|
||||
PrivateUserDocument,
|
||||
PublicAPIKeyDocument,
|
||||
ScoreDocument,
|
||||
SessionDocument,
|
||||
SongDocument,
|
||||
UserGoalDocument,
|
||||
UserMilestoneDocument,
|
||||
} from "kamaitachi-common";
|
||||
import monk, { ICollection } from "monk";
|
||||
import CreateLogCtx from "../logger";
|
||||
|
||||
const logger = CreateLogCtx("db.ts");
|
||||
|
||||
const url =
|
||||
process.env.NODE_ENV === "test" ? "localhost:27017/testingdb" : "localhost:27017/ktblackdb";
|
||||
|
||||
let dbtime: [number, number] = [0, 0];
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
logger.info(`Connecting to database ${url}...`);
|
||||
dbtime = process.hrtime();
|
||||
}
|
||||
|
||||
export let monkDB = monk(url);
|
||||
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
monkDB.then(() => {
|
||||
let time = process.hrtime(dbtime);
|
||||
let elapsed = time[0] + time[1] / 1000000;
|
||||
logger.info(`Database connection successful: took ${elapsed}ms`);
|
||||
});
|
||||
}
|
||||
|
||||
export async function ReOpenConnection() {
|
||||
monkDB = monk(url);
|
||||
await monkDB.then();
|
||||
}
|
||||
|
||||
export async function CloseConnection() {
|
||||
await monkDB.close();
|
||||
}
|
||||
|
||||
const StaticCollections = {
|
||||
scores: monkDB.get<ScoreDocument>("scores"),
|
||||
folders: monkDB.get<FolderDocument>("folders"),
|
||||
goals: monkDB.get<GoalDocument>("goals"),
|
||||
"user-goals": monkDB.get<UserGoalDocument>("user-goals"),
|
||||
milestones: monkDB.get<MilestoneDocument>("milestones"),
|
||||
"user-milestones": monkDB.get<UserMilestoneDocument>("user-milestones"),
|
||||
users: monkDB.get<PrivateUserDocument>("users"),
|
||||
imports: monkDB.get<ImportDocument>("imports"),
|
||||
notifications: monkDB.get<NotificationDocument>("notifications"),
|
||||
sessions: monkDB.get<SessionDocument>("sessions"),
|
||||
"iidx-bpi-data": monkDB.get<IIDXBPIData>("iidx-bpi-data"),
|
||||
"public-api-keys": monkDB.get<PublicAPIKeyDocument>("public-api-keys"),
|
||||
invites: monkDB.get<InviteCodeDocument>("invites"),
|
||||
counters: monkDB.get<CounterDocument>("counters"),
|
||||
"iidx-eam-scores": monkDB.get<IIDXEamusementScoreDocument>("iidx-eam-scores"),
|
||||
};
|
||||
|
||||
type GameCollections = Record<Game, ICollection>;
|
||||
|
||||
interface GCPartial {
|
||||
songs: Partial<GameCollections>;
|
||||
charts: Partial<GameCollections>;
|
||||
}
|
||||
|
||||
interface GameCollectionFull {
|
||||
songs: GameCollections;
|
||||
charts: GameCollections;
|
||||
}
|
||||
|
||||
let GameCollectionPartial: GCPartial = {
|
||||
songs: {},
|
||||
charts: {},
|
||||
};
|
||||
|
||||
for (const game of config.supportedGames) {
|
||||
GameCollectionPartial.songs[game] = monkDB.get<SongDocument>(`songs-${game}`);
|
||||
GameCollectionPartial.charts[game] = monkDB.get<ChartDocument>(`charts-${game}`);
|
||||
}
|
||||
|
||||
// a typescript-friendly interface for the database.
|
||||
const db = Object.assign(StaticCollections, GameCollectionPartial as GameCollectionFull);
|
||||
|
||||
export default db;
|
||||
@@ -0,0 +1,17 @@
|
||||
import Prudence, { PrudenceSchema } from "prudence";
|
||||
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
const LAZY_EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
|
||||
export const PRUDENCE_PUBLIC_USER: PrudenceSchema = {
|
||||
username: Prudence.regex(/^[a-zA-Z_-][a-zA-Z0-9_-]{2,20}$/),
|
||||
usernameLowercase: (self, parent) => self === (parent!.username as string).toLowerCase(),
|
||||
password: "string", // could be a tighter fit related to bcrypt?
|
||||
email: Prudence.regex(LAZY_EMAIL_REGEX),
|
||||
id: Prudence.isPositiveInteger,
|
||||
settings: {
|
||||
nsfwsplashes: "boolean",
|
||||
invisible: "boolean",
|
||||
useSimpleLadderColours: "boolean",
|
||||
}
|
||||
};
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import { ValidateCaptcha } from "../../core/captcha-core";
|
||||
import { FormatUserDoc } from "../../core/format-user";
|
||||
import { GetUserCaseInsensitive, PRIVATEINFO_GetUserCaseInsensitive } from "../../core/user-core";
|
||||
import db from "../../db";
|
||||
import db from "../../db/db";
|
||||
import CreateLogCtx from "../../logger";
|
||||
import prValidate from "../../middleware/prudence-validate";
|
||||
import { RequireLoggedIn } from "../../middleware/require-logged-in";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Chart "Playtype+Difficulty" lookup. This is a common way of resolving Kamaitachi charts.
|
||||
|
||||
import { Difficulties, Game, integer, Playtypes, IDStrings } from "kamaitachi-common";
|
||||
import db from "../../db";
|
||||
import db from "../../db/db";
|
||||
|
||||
export function FindChartWithPTDF<
|
||||
G extends Game = Game,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Game } from "kamaitachi-common";
|
||||
import db from "../../db";
|
||||
import db from "../../db/db";
|
||||
|
||||
/**
|
||||
* Finds a song document for the given game with the given title (or alt-title)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ChartDocument, ESDCore, integer } from "kamaitachi-common";
|
||||
import { Logger } from "winston";
|
||||
import db from "../../../../db";
|
||||
import db from "../../../../db/db";
|
||||
import { DryScore } from "../../../../types";
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ChartDocument, Game, Playtypes } from "kamaitachi-common";
|
||||
import { Logger } from "winston";
|
||||
import db from "../../../../db";
|
||||
import db from "../../../../db/db";
|
||||
import { DryScore } from "../../../../types";
|
||||
import {
|
||||
CalculateBPI,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ScoreDocument } from "kamaitachi-common";
|
||||
import t from "tap";
|
||||
import db, { CloseConnection } from "../../../db";
|
||||
import db, { CloseConnection } from "../../../db/db";
|
||||
import ResetDBState from "../../../test-utils/reset-db-state";
|
||||
import { InsertQueue, QueueScoreInsert } from "./insert-score";
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ScoreDocument } from "kamaitachi-common";
|
||||
import db from "../../../db";
|
||||
import db from "../../../db/db";
|
||||
|
||||
let ScoreQueue: ScoreDocument[] = [];
|
||||
const MAX_PIPELINE_LENGTH = 500;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { integer, DryScore } from "../../../types";
|
||||
import crypto from "crypto";
|
||||
import db from "../../../db";
|
||||
import db from "../../../db/db";
|
||||
|
||||
function CreateScoreIDString(userID: integer, partialScore: DryScore, chartID: string) {
|
||||
const { lamp, grade } = partialScore.scoreData;
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
KTDataNotFoundFailure,
|
||||
} from "../core/converter-errors";
|
||||
import { CreateScoreID } from "../core/score-id";
|
||||
import db from "../../../db";
|
||||
import db from "../../../db/db";
|
||||
import { Logger } from "winston";
|
||||
import { AppendLogCtx } from "../../../logger";
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import path from "path";
|
||||
import { config, Difficulties, ESDCore, IIDXBPIData } from "kamaitachi-common";
|
||||
import { FindSongOnTitleVersion } from "../score-import/database-lookup/song-title";
|
||||
import { FindChartWithPTDF } from "../score-import/database-lookup/chart-ptdf";
|
||||
import db from "../db";
|
||||
import db from "../db/db";
|
||||
import CreateLogCtx from "../logger";
|
||||
const program = new Command();
|
||||
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
"id": 1,
|
||||
"settings": {
|
||||
"nsfwsplashes": true,
|
||||
"invisible": false,
|
||||
"useSimpleLadderColours": true,
|
||||
"trustEamIIDXTimestamps": false
|
||||
"invisible": false
|
||||
},
|
||||
"friends": [],
|
||||
"socialmedia": {},
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Resets the state of the database.
|
||||
*/
|
||||
import db from "../db";
|
||||
import db from "../db/db";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user