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

38 lines
790 B
TypeScript
Raw Normal View History

import { acct } from "firefish-js";
2023-01-13 05:40:33 +01:00
import { host as localHost } from "@/config";
2019-04-25 00:46:39 +02:00
export async function genSearchQuery(v: any, q: string) {
2023-09-02 01:27:33 +02:00
let host: string, userId: string;
2023-01-13 05:40:33 +01:00
if (q.split(" ").some((x) => x.startsWith("@"))) {
for (const at of q
.split(" ")
.filter((x) => x.startsWith("@"))
2023-07-13 08:56:22 +02:00
.map((x) => x.slice(1))) {
2023-01-13 05:40:33 +01:00
if (at.includes(".")) {
if (at === localHost || at === ".") {
2019-04-25 00:46:39 +02:00
host = null;
} else {
host = at;
}
} else {
2023-01-13 05:40:33 +01:00
const user = await v.os
.api("users/show", acct.parse(at))
2023-01-13 05:40:33 +01:00
.catch((x) => null);
2019-04-25 00:46:39 +02:00
if (user) {
userId = user.id;
} else {
// todo: show error
}
}
}
}
return {
2023-01-13 05:40:33 +01:00
query: q
.split(" ")
.filter((x) => !(x.startsWith("/") || x.startsWith("@")))
.join(" "),
2023-09-02 01:27:33 +02:00
host,
userId,
2019-04-25 00:46:39 +02:00
};
}