summaryrefslogtreecommitdiff
path: root/src/client/scripts/search.ts
blob: 16057dfd34c983519b746e1d8569932027fd4eec (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { faHistory } from '@fortawesome/free-solid-svg-icons';

export async function search(v: any, q: string) {
	q = q.trim();

	if (q.startsWith('@') && !q.includes(' ')) {
		v.$router.push(`/${q}`);
		return;
	}

	if (q.startsWith('#')) {
		v.$router.push(`/tags/${encodeURIComponent(q.substr(1))}`);
		return;
	}

	// like 2018/03/12
	if (/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}/.test(q.replace(/-/g, '/'))) {
		const date = new Date(q.replace(/-/g, '/'));

		// 日付しか指定されてない場合、例えば 2018/03/12 ならユーザーは
		// 2018/03/12 のコンテンツを「含む」結果になることを期待するはずなので
		// 23時間59分進める(そのままだと 2018/03/12 00:00:00 「まで」の
		// 結果になってしまい、2018/03/12 のコンテンツは含まれない)
		if (q.replace(/-/g, '/').match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}$/)) {
			date.setHours(23, 59, 59, 999);
		}

		v.$root.$emit('warp', date);
		v.$root.dialog({
			icon: faHistory,
			iconOnly: true, autoClose: true
		});
		return;
	}

	if (q.startsWith('https://')) {
		const dialog = v.$root.dialog({
			type: 'waiting',
			text: v.$t('fetchingAsApObject') + '...',
			showOkButton: false,
			showCancelButton: false,
			cancelableByBgClick: false
		});

		try {
			const res = await v.$root.api('ap/show', {
				uri: q
			});
			dialog.close();
			if (res.type === 'User') {
				v.$router.push(`/@${res.object.username}@${res.object.host}`);
			} else if (res.type === 'Note') {
				v.$router.push(`/notes/${res.object.id}`);
			}
		} catch (e) {
			dialog.close();
			// TODO: Show error
		}

		return;
	}

	v.$router.push(`/search?q=${encodeURIComponent(q)}`);
}