summaryrefslogtreecommitdiff
path: root/src/client/scripts/gen-search-query.ts
blob: 2520da75df7fc501955eb24b2e6ea74f92b54dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import parseAcct from '../../misc/acct/parse';
import { host as localHost } from '../config';

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.substr(1))) {
			if (at.includes('.')) {
				if (at === localHost || at === '.') {
					host = null;
				} else {
					host = at;
				}
			} else {
				const user = await v.$root.api('users/show', parseAcct(at)).catch(x => null);
				if (user) {
					userId = user.id;
				} else {
					// todo: show error
				}
			}
		}

	}
	return {
		query: q.split(' ').filter(x => !x.startsWith('/') && !x.startsWith('@')).join(' '),
		host: host,
		userId: userId
	};
}