41
src/web/app/common/scripts/parse-search-query.ts
Normal file
41
src/web/app/common/scripts/parse-search-query.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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['username'] = value;
|
||||
break;
|
||||
case 'reply':
|
||||
q['include_replies'] = value == 'true';
|
||||
break;
|
||||
case 'media':
|
||||
q['with_media'] = 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;
|
||||
}
|
@@ -16,7 +16,7 @@ export default (mios: MiOS) => {
|
||||
route('/i/messaging/:user', messaging);
|
||||
route('/i/mentions', mentions);
|
||||
route('/post::post', post);
|
||||
route('/search::query', search);
|
||||
route('/search', search);
|
||||
route('/:user', user.bind(null, 'home'));
|
||||
route('/:user/graphs', user.bind(null, 'graphs'));
|
||||
route('/:user/:post', post);
|
||||
@@ -47,7 +47,7 @@ export default (mios: MiOS) => {
|
||||
|
||||
function search(ctx) {
|
||||
const el = document.createElement('mk-search-page');
|
||||
el.setAttribute('query', ctx.params.query);
|
||||
el.setAttribute('query', ctx.querystring.substr(2));
|
||||
mount(el);
|
||||
}
|
||||
|
||||
|
@@ -33,6 +33,8 @@
|
||||
|
||||
</style>
|
||||
<script>
|
||||
import parse from '../../common/scripts/parse-search-query';
|
||||
|
||||
this.mixin('api');
|
||||
|
||||
this.query = this.opts.query;
|
||||
@@ -45,9 +47,7 @@
|
||||
document.addEventListener('keydown', this.onDocumentKeydown);
|
||||
window.addEventListener('scroll', this.onScroll);
|
||||
|
||||
this.api('posts/search', {
|
||||
query: this.query
|
||||
}).then(posts => {
|
||||
this.api('posts/search', parse(this.query)).then(posts => {
|
||||
this.update({
|
||||
isLoading: false,
|
||||
isEmpty: posts.length == 0
|
||||
|
@@ -180,7 +180,7 @@
|
||||
|
||||
this.onsubmit = e => {
|
||||
e.preventDefault();
|
||||
this.page('/search:' + this.refs.q.value);
|
||||
this.page('/search?q=' + encodeURIComponent(this.refs.q.value));
|
||||
};
|
||||
</script>
|
||||
</mk-ui-header-search>
|
||||
|
@@ -23,7 +23,7 @@ export default (mios: MiOS) => {
|
||||
route('/i/settings/authorized-apps', settingsAuthorizedApps);
|
||||
route('/post/new', newPost);
|
||||
route('/post::post', post);
|
||||
route('/search::query', search);
|
||||
route('/search', search);
|
||||
route('/:user', user.bind(null, 'overview'));
|
||||
route('/:user/graphs', user.bind(null, 'graphs'));
|
||||
route('/:user/followers', userFollowers);
|
||||
@@ -83,7 +83,7 @@ export default (mios: MiOS) => {
|
||||
|
||||
function search(ctx) {
|
||||
const el = document.createElement('mk-search-page');
|
||||
el.setAttribute('query', ctx.params.query);
|
||||
el.setAttribute('query', ctx.querystring.substr(2));
|
||||
mount(el);
|
||||
}
|
||||
|
||||
|
@@ -15,6 +15,8 @@
|
||||
width calc(100% - 32px)
|
||||
</style>
|
||||
<script>
|
||||
import parse from '../../common/scripts/parse-search-query';
|
||||
|
||||
this.mixin('api');
|
||||
|
||||
this.max = 30;
|
||||
@@ -24,9 +26,7 @@
|
||||
this.withMedia = this.opts.withMedia;
|
||||
|
||||
this.init = new Promise((res, rej) => {
|
||||
this.api('posts/search', {
|
||||
query: this.query
|
||||
}).then(posts => {
|
||||
this.api('posts/search', parse(this.query)).then(posts => {
|
||||
res(posts);
|
||||
this.trigger('loaded');
|
||||
});
|
||||
|
@@ -413,7 +413,7 @@
|
||||
this.search = () => {
|
||||
const query = window.prompt('%i18n:mobile.tags.mk-ui-nav.search%');
|
||||
if (query == null || query == '') return;
|
||||
this.page('/search:' + query);
|
||||
this.page('/search?q=' + encodeURIComponent(query));
|
||||
};
|
||||
</script>
|
||||
</mk-ui-nav>
|
||||
|
Reference in New Issue
Block a user