From 4ca43f27cd8df97559bda64acf2156222ac8b5bb Mon Sep 17 00:00:00 2001 From: adueck Date: Fri, 25 Nov 2022 19:56:15 +0500 Subject: [PATCH] u --- functions/src/index.ts | 12 ++++++------ website/src/lib/dictionary.ts | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/functions/src/index.ts b/functions/src/index.ts index 57a241f..acffc06 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -8,12 +8,12 @@ export const publishDictionary = functions.runWith({ timeoutSeconds: 60, memory: "2GB" }).https.onRequest( - // lingdocsAuth( + lingdocsAuth( async (req, res: functions.Response) => { - // if (req.user.level !== "editor") { - // res.status(403).send({ ok: false, error: "403 forbidden" }); - // return; - // } + if (req.user.level !== "editor") { + res.status(403).send({ ok: false, error: "403 forbidden" }); + return; + } try { const response = await publish(); res.send(response); @@ -22,7 +22,7 @@ export const publishDictionary = functions.runWith({ res.status(500).send({ ok: false, error: e.message }); } } - // ) + ) ); export const submissions = functions.runWith({ diff --git a/website/src/lib/dictionary.ts b/website/src/lib/dictionary.ts index b80c3ea..23909f6 100644 --- a/website/src/lib/dictionary.ts +++ b/website/src/lib/dictionary.ts @@ -151,7 +151,10 @@ function englishLookup({ searchString, page, tpFilt page: number, tpFilter?: (e: T.DictionaryEntry) => e is S, }): S[] { - let resultsGiven: number[] = [];; + function sortByR(a: T.DictionaryEntry, b: T.DictionaryEntry) { + return (b.r || 3) - (a.r || 3); + }; + let resultsGiven: number[] = []; // get exact results const exactQuery = { e: { @@ -164,6 +167,7 @@ function englishLookup({ searchString, page, tpFilt .limit(exactResultsLimit) .simplesort("i") .data(); + exactResults.sort(sortByR); resultsGiven = exactResults.map((mpd: any) => mpd.$loki); // get results with full word match at beginning of string const startingQuery = { @@ -178,6 +182,7 @@ function englishLookup({ searchString, page, tpFilt .limit(startingResultsLimit) .simplesort("i") .data(); + startingResults.sort(sortByR); resultsGiven = [...resultsGiven, ...startingResults.map((mpd: any) => mpd.$loki)]; // get results with full word match anywhere const fullWordQuery = { @@ -192,6 +197,7 @@ function englishLookup({ searchString, page, tpFilt .limit(fullWordResultsLimit) .simplesort("i") .data(); + fullWordResults.sort(sortByR); resultsGiven = [...resultsGiven, ...fullWordResults.map((mpd: any) => mpd.$loki)] // get results with partial match anywhere const partialMatchQuery = { @@ -202,11 +208,12 @@ function englishLookup({ searchString, page, tpFilt }; const partialMatchLimit = (pageSize * page) - resultsGiven.length; const partialMatchResults = dictDb.collection.chain() - .where(tpFilter ? tpFilter : () => true) - .find(partialMatchQuery) - .limit(partialMatchLimit) - .simplesort("i") - .data(); + .where(tpFilter ? tpFilter : () => true) + .find(partialMatchQuery) + .limit(partialMatchLimit) + .simplesort("i") + .data(); + partialMatchResults.sort(sortByR); const results = [ ...exactResults, ...startingResults, @@ -216,6 +223,7 @@ function englishLookup({ searchString, page, tpFilt if (tpFilter) { return results.filter(tpFilter); } + console.log({ results }); return results; }