From cb47c585f93c0ac03cbbb1e8259778db21d53b28 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Mon, 12 Jun 2023 16:35:04 +0200 Subject: [PATCH 1/2] Add webfinger support to from: and allow UNIX timestamps in date filters --- packages/backend/src/db/meilisearch.ts | 58 +++++++++++++++++++++----- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/backend/src/db/meilisearch.ts b/packages/backend/src/db/meilisearch.ts index 74e53bb601..690a71a751 100644 --- a/packages/backend/src/db/meilisearch.ts +++ b/packages/backend/src/db/meilisearch.ts @@ -135,6 +135,24 @@ export type MeilisearchNote = { createdAt: number; }; +function timestampToUnix(timestamp: string) { + let unix = 0; + + // Only contains numbers => UNIX timestamp + if (/^\d+$/.test(timestamp)) { + unix = Number.parseInt(timestamp); + } + + if (unix === 0) { + // Try to parse the timestamp as JavaScript Date + const date = Date.parse(timestamp); + if (isNaN(date)) return 0; + unix = date / 1000; + } + + return unix; +} + export default hasConfig ? { search: async ( @@ -166,8 +184,27 @@ export default hasConfig constructedFilters.push(`mediaAttachment = "${fileType}"`); return null; } else if (term.startsWith("from:")) { - const user = term.slice(5); - constructedFilters.push(`userName = ${user}`); + let user = term.slice(5); + + // Cut off leading @, those aren't saved in the DB + if (user.charAt(0) === "@") { + user = user.slice(1); + } + + // Determine if we got a webfinger address or a single username + if (user.split("@").length > 0) { + let splitUser = user.split("@"); + + let domain = splitUser.pop(); + user = splitUser.join("@"); + + constructedFilters.push( + `userName = ${user} AND userHost = ${domain}`, + ); + } else { + constructedFilters.push(`userName = ${user}`); + } + return null; } else if (term.startsWith("domain:")) { const domain = term.slice(7); @@ -175,17 +212,18 @@ export default hasConfig return null; } else if (term.startsWith("after:")) { const timestamp = term.slice(6); - // Try to parse the timestamp as JavaScript Date - const date = Date.parse(timestamp); - if (isNaN(date)) return null; - constructedFilters.push(`createdAt > ${date / 1000}`); + + let unix = timestampToUnix(timestamp); + + if (unix !== 0) constructedFilters.push(`createdAt > ${unix}`); + return null; } else if (term.startsWith("before:")) { const timestamp = term.slice(7); - // Try to parse the timestamp as JavaScript Date - const date = Date.parse(timestamp); - if (isNaN(date)) return null; - constructedFilters.push(`createdAt < ${date / 1000}`); + + let unix = timestampToUnix(timestamp); + if (unix !== 0) constructedFilters.push(`createdAt < ${unix}`); + return null; } else if (term.startsWith("filter:following")) { // Check if we got a context user From 40f77561761d1a81acc9e9823e975008887eeef9 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Mon, 12 Jun 2023 16:46:30 +0200 Subject: [PATCH 2/2] use actually correct webfinger check --- packages/backend/src/db/meilisearch.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/db/meilisearch.ts b/packages/backend/src/db/meilisearch.ts index 690a71a751..4a8985d080 100644 --- a/packages/backend/src/db/meilisearch.ts +++ b/packages/backend/src/db/meilisearch.ts @@ -186,13 +186,15 @@ export default hasConfig } else if (term.startsWith("from:")) { let user = term.slice(5); + if (user.length === 0) return null; + // Cut off leading @, those aren't saved in the DB if (user.charAt(0) === "@") { user = user.slice(1); } // Determine if we got a webfinger address or a single username - if (user.split("@").length > 0) { + if (user.split("@").length > 1) { let splitUser = user.split("@"); let domain = splitUser.pop();