Get stuff a user did in the last (16) hours

This commit is contained in:
zkldi
2021-11-22 11:44:27 +00:00
parent 028b31a54d
commit 428764a1f3
5 changed files with 99 additions and 15 deletions
+6 -1
View File
@@ -1,6 +1,7 @@
import crypto from "crypto";
import { ONE_HOUR } from "lib/constants/time";
import { TachiConfig } from "lib/setup/config";
import { Game, GamePTConfig, GetGameConfig, Playtypes } from "tachi-common";
import { Game, GamePTConfig, GetGameConfig, Playtypes, integer } from "tachi-common";
// https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js
// the developer of this has migrated everything to Force ES6 style modules,
@@ -128,3 +129,7 @@ export function IsValidURL(string: string) {
export function Sleep(ms: number) {
return new Promise<void>((resolve) => setTimeout(() => resolve(), ms));
}
export function GetTimeXHoursAgo(hours: integer) {
return Date.now() - ONE_HOUR * hours;
}
+47
View File
@@ -0,0 +1,47 @@
import db from "external/mongo/db";
import { integer } from "tachi-common";
import { GetTimeXHoursAgo } from "utils/misc";
// Various utils related to the player summary endpoint.
const REASONABLE_HOURS_AGO = 16;
export function GetRecentPlaycount(userID: integer) {
const time = GetTimeXHoursAgo(REASONABLE_HOURS_AGO);
return db.scores.count({ userID, timeAchieved: { $gte: time } });
}
export function GetRecentSessions(userID: integer) {
const time = GetTimeXHoursAgo(REASONABLE_HOURS_AGO);
return db.sessions.find({
userID,
timeEnded: { $gte: time },
});
}
export async function GetRecentlyViewedFoldersAnyGPT(userID: integer) {
const time = GetTimeXHoursAgo(REASONABLE_HOURS_AGO);
const views = await db["recent-folder-views"].find(
{
userID,
lastViewed: { $gte: time },
},
{
sort: {
lastViewed: -1,
},
limit: 4,
}
);
const folders = await db.folders.find({
folderID: { $in: views.map((e) => e.folderID) },
});
// TODO: Sort recently viewed folders based on how recently viewed
// they were.
return folders;
}