firefish/packages/client/src/scripts/gen-search-query.ts

38 lines
802 B
TypeScript
Raw Normal View History

2023-07-20 04:17:05 +09:00
import * as Acct from "firefish-js/built/acct";
import { host as localHost } from "@/config";
export async function genSearchQuery(v: any, q: string) {
2023-09-04 17:47:24 +09:00
let host: string, userId: string;
2023-07-20 04:17:05 +09:00
if (q.split(" ").some((x) => x.startsWith("@"))) {
for (const at of q
.split(" ")
.filter((x) => x.startsWith("@"))
.map((x) => x.slice(1))) {
if (at.includes(".")) {
if (at === localHost || at === ".") {
host = null;
} else {
host = at;
}
} else {
const user = await v.os
.api("users/show", Acct.parse(at))
.catch((x) => null);
if (user) {
userId = user.id;
} else {
// todo: show error
}
}
}
}
return {
query: q
.split(" ")
.filter((x) => !(x.startsWith("/") || x.startsWith("@")))
.join(" "),
2023-09-04 17:47:24 +09:00
host,
userId,
2023-07-20 04:17:05 +09:00
};
}