jormungandr-bite/packages/client/src/scripts/gen-search-query.ts

39 lines
821 B
TypeScript
Raw Normal View History

2023-01-12 21:40:33 -07:00
import * as Acct from "calckey-js/built/acct";
import { host as localHost } from "@/config";
2019-04-24 16:46:39 -06:00
export async function genSearchQuery(v: any, q: string) {
let host: string;
let userId: string;
2023-01-12 21:40:33 -07:00
if (q.split(" ").some((x) => x.startsWith("@"))) {
for (const at of q
.split(" ")
.filter((x) => x.startsWith("@"))
.map((x) => x.substr(1))) {
if (at.includes(".")) {
if (at === localHost || at === ".") {
2019-04-24 16:46:39 -06:00
host = null;
} else {
host = at;
}
} else {
2023-01-12 21:40:33 -07:00
const user = await v.os
.api("users/show", Acct.parse(at))
.catch((x) => null);
2019-04-24 16:46:39 -06:00
if (user) {
userId = user.id;
} else {
// todo: show error
}
}
}
}
return {
2023-01-12 21:40:33 -07:00
query: q
.split(" ")
.filter((x) => !(x.startsWith("/") || x.startsWith("@")))
.join(" "),
2019-04-24 16:46:39 -06:00
host: host,
2023-01-12 21:40:33 -07:00
userId: userId,
2019-04-24 16:46:39 -06:00
};
}