egirlskey/packages/frontend/src/scripts/gen-search-query.ts

36 lines
895 B
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import * as Misskey from 'misskey-js';
2023-09-19 07:37:43 +00:00
import { host as localHost } from '@/config.js';
2019-04-24 22:46:39 +00:00
export async function genSearchQuery(v: any, q: string) {
let host: string;
let userId: string;
if (q.split(' ').some(x => x.startsWith('@'))) {
for (const at of q.split(' ').filter(x => x.startsWith('@')).map(x => x.substring(1))) {
2019-04-24 22:46:39 +00:00
if (at.includes('.')) {
if (at === localHost || at === '.') {
host = null;
} else {
host = at;
}
} else {
const user = await v.os.api('users/show', Misskey.acct.parse(at)).catch(x => null);
2019-04-24 22:46:39 +00:00
if (user) {
userId = user.id;
} else {
// todo: show error
}
}
}
}
return {
query: q.split(' ').filter(x => !x.startsWith('/') && !x.startsWith('@')).join(' '),
host: host,
userId: userId,
2019-04-24 22:46:39 +00:00
};
}