mirror of
https://github.com/versia-pub/server.git
synced 2025-12-07 16:58:20 +01:00
fix(api): 🐛 Properly sort users in account search by closeness to query
This commit is contained in:
parent
1f44405ac3
commit
3521dd5eb7
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()));
|
||||
},
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue