fix(api): 🐛 Properly sort users in account search by closeness to query

This commit is contained in:
Jesse Wierzbinski 2024-04-24 19:21:44 -10:00
parent 1f44405ac3
commit 3521dd5eb7
No known key found for this signature in database
3 changed files with 119 additions and 106 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -102,6 +102,7 @@
"pg": "^8.11.5",
"request-parser": "workspace:*",
"sharp": "^0.33.3",
"string-comparison": "^1.3.0",
"zod": "^3.22.4",
"zod-validation-error": "^3.2.0"
}

View file

@ -16,6 +16,7 @@ import { z } from "zod";
import { resolveWebFinger } from "~database/entities/User";
import { Users } from "~drizzle/schema";
import { User } from "~packages/database-interface/user";
import stringComparison from "string-comparison";
export const meta = applyConfig({
allowedMethods: ["GET"],
@ -100,6 +101,17 @@ export default apiRoute<typeof meta, typeof schema>(
);
}
return jsonResponse(accounts.map((acct) => acct.toAPI()));
// Sort accounts by closest match
// Returns array of numbers (indexes of accounts array)
const indexOfCorrectSort = stringComparison.jaccardIndex
.sortMatch(
q,
accounts.map((acct) => acct.getAcct()),
)
.map((sort) => sort.index);
const result = indexOfCorrectSort.map((index) => accounts[index]);
return jsonResponse(result.map((acct) => acct.toAPI()));
},
);