1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +00:00

accounts: filter by date, with =, <, >

Uses "any-date-parser" library to understand more date/time types.
Format is: "<field>:<equals, less than, greater than><date>", where the
part after the colon uses =, <, >. Omitting a symbol is the same as
using "=".
This commit is contained in:
Harvey Tindall 2023-06-13 17:19:24 +01:00
parent 90c433443f
commit ff62f8821a
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
3 changed files with 69 additions and 4 deletions

11
package-lock.json generated
View File

@ -12,6 +12,7 @@
"@ts-stack/markdown": "^1.4.0", "@ts-stack/markdown": "^1.4.0",
"@types/node": "^20.3.0", "@types/node": "^20.3.0",
"a17t": "^0.10.1", "a17t": "^0.10.1",
"any-date-parser": "^1.5.4",
"browserslist": "^4.21.7", "browserslist": "^4.21.7",
"cheerio": "^1.0.0-rc.12", "cheerio": "^1.0.0-rc.12",
"esbuild": "^0.18.2", "esbuild": "^0.18.2",
@ -630,6 +631,11 @@
"url": "https://github.com/chalk/ansi-styles?sponsor=1" "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": { "node_modules/any-promise": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz",
@ -7110,6 +7116,11 @@
"color-convert": "^2.0.1" "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": { "any-promise": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz",

View File

@ -20,6 +20,7 @@
"@ts-stack/markdown": "^1.4.0", "@ts-stack/markdown": "^1.4.0",
"@types/node": "^20.3.0", "@types/node": "^20.3.0",
"a17t": "^0.10.1", "a17t": "^0.10.1",
"any-date-parser": "^1.5.4",
"browserslist": "^4.21.7", "browserslist": "^4.21.7",
"cheerio": "^1.0.0-rc.12", "cheerio": "^1.0.0-rc.12",
"esbuild": "^0.18.2", "esbuild": "^0.18.2",

View File

@ -3,6 +3,7 @@ import { templateEmail } from "../modules/settings.js";
import { Marked } from "@ts-stack/markdown"; import { Marked } from "@ts-stack/markdown";
import { stripMarkdown } from "../modules/stripmd.js"; import { stripMarkdown } from "../modules/stripmd.js";
import { DiscordUser, newDiscordSearch } from "../modules/discord.js"; import { DiscordUser, newDiscordSearch } from "../modules/discord.js";
const dateParser = require("any-date-parser");
interface User { interface User {
id: string; id: string;
@ -790,7 +791,6 @@ export class accountsList {
} }
search = (query: String): string[] => { search = (query: String): string[] => {
console.log("called!");
const queries: { [field: string]: { name: string, getter: string, bool: boolean, string: boolean, date: boolean }} = { const queries: { [field: string]: { name: string, getter: string, bool: boolean, string: boolean, date: boolean }} = {
"id": { "id": {
name: "Jellyfin ID", name: "Jellyfin ID",
@ -873,7 +873,7 @@ export class accountsList {
query = query.toLowerCase(); query = query.toLowerCase();
let result: string[] = [...this._ordering]; let result: string[] = [...this._ordering];
console.log("initial:", result); // console.log("initial:", result);
if (!(query.includes(":"))) { if (!(query.includes(":"))) {
let cachedResult = [...result]; let cachedResult = [...result];
for (let id of cachedResult) { for (let id of cachedResult) {
@ -918,7 +918,7 @@ export class accountsList {
query = ""; query = "";
for (let word of words) { 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; if (!(split[0] in queries)) continue;
@ -963,7 +963,60 @@ export class accountsList {
continue; continue;
} }
if (queryFormat.date) { 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 < <time> with no date, we need to ignore the rest of the Date object
if (compareType != 0 && Object.keys(attempt).length == 2 && "hour" in attempt && "minute" in attempt) {
const temp = new Date(date.valueOf());
temp.setHours(value.getHours(), value.getMinutes());
value = temp;
console.log("just hours/minutes workaround, value set to", value);
}
let match = true;
if (compareType == 0) {
for (let pair of getterPairs) {
if (pair[0] in attempt) {
if (compareType == 0 && attempt[pair[0]] != pair[1].call(value)) {
match = false;
break;
}
}
}
} else if (compareType == -1) {
match = (value < date);
} else if (compareType == 1) {
match = (value > date);
}
if (!match) {
result.splice(result.indexOf(id), 1);
}
}
} }
} }
return result return result