summaryrefslogtreecommitdiff
path: root/src/client/app/common/scripts/parse-search-query.ts
blob: 4f09d2b93f40b68adbcdda5637b71b8f0dadf261 (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
export default function(qs: string) {
	const q = {
		text: ''
	};

	qs.split(' ').forEach(x => {
		if (/^([a-z_]+?):(.+?)$/.test(x)) {
			const [key, value] = x.split(':');
			switch (key) {
				case 'user':
					q['includeUserUsernames'] = value.split(',');
					break;
				case 'exclude_user':
					q['excludeUserUsernames'] = value.split(',');
					break;
				case 'follow':
					q['following'] = value == 'null' ? null : value == 'true';
					break;
				case 'reply':
					q['reply'] = value == 'null' ? null : value == 'true';
					break;
				case 'repost':
					q['repost'] = value == 'null' ? null : value == 'true';
					break;
				case 'media':
					q['media'] = value == 'null' ? null : value == 'true';
					break;
				case 'poll':
					q['poll'] = value == 'null' ? null : value == 'true';
					break;
				case 'until':
				case 'since':
					// YYYY-MM-DD
					if (/^[0-9]+\-[0-9]+\-[0-9]+$/) {
						const [yyyy, mm, dd] = value.split('-');
						q[`${key}_date`] = (new Date(parseInt(yyyy, 10), parseInt(mm, 10) - 1, parseInt(dd, 10))).getTime();
					}
					break;
				default:
					q[key] = value;
					break;
			}
		} else {
			q.text += x + ' ';
		}
	});

	if (q.text) {
		q.text = q.text.trim();
	}

	return q;
}