From ff62f8821a57e4fd8ef244efc7da492e4980a5e7 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 13 Jun 2023 17:19:24 +0100 Subject: [PATCH] accounts: filter by date, with =, <, > Uses "any-date-parser" library to understand more date/time types. Format is: ":", where the part after the colon uses =, <, >. Omitting a symbol is the same as using "=". --- package-lock.json | 11 ++++++++ package.json | 1 + ts/modules/accounts.ts | 61 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index b84905b..bca893c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@ts-stack/markdown": "^1.4.0", "@types/node": "^20.3.0", "a17t": "^0.10.1", + "any-date-parser": "^1.5.4", "browserslist": "^4.21.7", "cheerio": "^1.0.0-rc.12", "esbuild": "^0.18.2", @@ -630,6 +631,11 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/any-date-parser": { + "version": "1.5.4", + "resolved": "https://registry.npmjs.org/any-date-parser/-/any-date-parser-1.5.4.tgz", + "integrity": "sha512-S4gl9UmXNk9XXSQxp5w5harUD6aM0fepyL3dZM/B3znX57sWf792hS2UvvCFIHECfpsqfKbQ+cqWBky4AKyRIg==" + }, "node_modules/any-promise": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", @@ -7110,6 +7116,11 @@ "color-convert": "^2.0.1" } }, + "any-date-parser": { + "version": "1.5.4", + "resolved": "https://registry.npmjs.org/any-date-parser/-/any-date-parser-1.5.4.tgz", + "integrity": "sha512-S4gl9UmXNk9XXSQxp5w5harUD6aM0fepyL3dZM/B3znX57sWf792hS2UvvCFIHECfpsqfKbQ+cqWBky4AKyRIg==" + }, "any-promise": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", diff --git a/package.json b/package.json index fbf21d2..1a630ad 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@ts-stack/markdown": "^1.4.0", "@types/node": "^20.3.0", "a17t": "^0.10.1", + "any-date-parser": "^1.5.4", "browserslist": "^4.21.7", "cheerio": "^1.0.0-rc.12", "esbuild": "^0.18.2", diff --git a/ts/modules/accounts.ts b/ts/modules/accounts.ts index 6724a86..b0577d3 100644 --- a/ts/modules/accounts.ts +++ b/ts/modules/accounts.ts @@ -3,6 +3,7 @@ import { templateEmail } from "../modules/settings.js"; import { Marked } from "@ts-stack/markdown"; import { stripMarkdown } from "../modules/stripmd.js"; import { DiscordUser, newDiscordSearch } from "../modules/discord.js"; +const dateParser = require("any-date-parser"); interface User { id: string; @@ -790,7 +791,6 @@ export class accountsList { } search = (query: String): string[] => { - console.log("called!"); const queries: { [field: string]: { name: string, getter: string, bool: boolean, string: boolean, date: boolean }} = { "id": { name: "Jellyfin ID", @@ -873,7 +873,7 @@ export class accountsList { query = query.toLowerCase(); let result: string[] = [...this._ordering]; - console.log("initial:", result); + // console.log("initial:", result); if (!(query.includes(":"))) { let cachedResult = [...result]; for (let id of cachedResult) { @@ -918,7 +918,7 @@ export class accountsList { query = ""; for (let word of words) { - const split = word.split(":"); + const split = [word.substring(0, word.indexOf(":")), word.substring(word.indexOf(":")+1)]; if (!(split[0] in queries)) continue; @@ -963,7 +963,60 @@ export class accountsList { continue; } if (queryFormat.date) { - + // -1 = Before, 0 = On, 1 = After, 2 = No symbol, assume 0 + let compareType = (split[1][0] == ">") ? 1 : ((split[1][0] == "<") ? -1 : ((split[1][0] == "=") ? 0 : 2)); + if (compareType != 2) { + split[1] = split[1].substring(1); + } + if (compareType == 2) compareType = 0; + + let attempt: { year?: number, month?: number, day?: number, hour?: number, minute?: number } = dateParser.attempt(split[1]); + // Month in Date objects is 0-based, so make our parsed date that way too + if ("month" in attempt) attempt["month"] -= 1; + + let date: Date = (Date as any).fromString(split[1]) as Date; + console.log("Read", attempt, "and", date); + if ("invalid" in (date as any)) continue; + let cachedResult = [...result]; + for (let id of cachedResult) { + const u = this._users[id]; + const unixValue = Object.getOwnPropertyDescriptor(user.prototype, queryFormat.getter).get.call(u); + if (unixValue == 0) { + result.splice(result.indexOf(id), 1); + continue; + } + let value = new Date(unixValue*1000); + + const getterPairs: [string, () => number][] = [["year", Date.prototype.getFullYear], ["month", Date.prototype.getMonth], ["day", Date.prototype.getDate], ["hour", Date.prototype.getHours], ["minute", Date.prototype.getMinutes]]; + + // When doing > or <