2024-04-10 14:44:24 +03:00
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import Service from '@ember/service';
|
|
|
|
import {isBlank, isEmpty} from '@ember/utils';
|
|
|
|
import {pluralize} from 'ember-inflector';
|
|
|
|
import {inject as service} from '@ember/service';
|
2024-05-07 00:04:13 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2024-04-10 14:44:24 +03:00
|
|
|
|
|
|
|
export default class SearchService extends Service {
|
|
|
|
@service ajax;
|
|
|
|
@service notifications;
|
|
|
|
@service store;
|
|
|
|
|
|
|
|
content = [];
|
|
|
|
contentExpiresAt = false;
|
|
|
|
contentExpiry = 30000;
|
|
|
|
|
|
|
|
searchables = [
|
|
|
|
{
|
|
|
|
name: 'Posts',
|
|
|
|
model: 'post',
|
2024-04-18 20:18:37 +03:00
|
|
|
fields: ['id', 'url', 'title', 'status'],
|
2024-04-10 14:44:24 +03:00
|
|
|
idField: 'id',
|
|
|
|
titleField: 'title'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Pages',
|
|
|
|
model: 'page',
|
2024-04-18 20:18:37 +03:00
|
|
|
fields: ['id', 'url', 'title', 'status'],
|
2024-04-10 14:44:24 +03:00
|
|
|
idField: 'id',
|
|
|
|
titleField: 'title'
|
|
|
|
},
|
|
|
|
{
|
2024-04-22 17:49:43 +03:00
|
|
|
name: 'Staff',
|
2024-04-10 14:44:24 +03:00
|
|
|
model: 'user',
|
2024-04-11 17:01:39 +03:00
|
|
|
fields: ['id', 'slug', 'url', 'name'], // id not used but required for API to have correct url
|
2024-04-10 14:44:24 +03:00
|
|
|
idField: 'slug',
|
|
|
|
titleField: 'name'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Tags',
|
|
|
|
model: 'tag',
|
2024-04-11 17:01:39 +03:00
|
|
|
fields: ['slug', 'url', 'name'],
|
2024-04-10 14:44:24 +03:00
|
|
|
idField: 'slug',
|
|
|
|
titleField: 'name'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
@task({restartable: true})
|
|
|
|
*searchTask(term) {
|
|
|
|
if (isBlank(term)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// start loading immediately in the background
|
|
|
|
this.refreshContentTask.perform();
|
|
|
|
|
|
|
|
// debounce searches to 200ms to avoid thrashing CPU
|
|
|
|
yield timeout(200);
|
|
|
|
|
|
|
|
// wait for any on-going refresh to finish
|
|
|
|
if (this.refreshContentTask.isRunning) {
|
2024-05-07 00:04:13 +03:00
|
|
|
yield this.refreshContentTask.lastRunning;
|
2024-04-10 14:44:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const searchResult = this._searchContent(term);
|
|
|
|
|
|
|
|
return searchResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
_searchContent(term) {
|
|
|
|
const normalizedTerm = term.toString().toLowerCase();
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
this.searchables.forEach((searchable) => {
|
|
|
|
const matchedContent = this.content.filter((item) => {
|
|
|
|
const normalizedTitle = item.title.toString().toLowerCase();
|
|
|
|
return (
|
2024-04-11 17:01:39 +03:00
|
|
|
item.groupName === searchable.name &&
|
2024-04-10 14:44:24 +03:00
|
|
|
normalizedTitle.indexOf(normalizedTerm) >= 0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isEmpty(matchedContent)) {
|
|
|
|
results.push({
|
|
|
|
groupName: searchable.name,
|
|
|
|
options: matchedContent
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
@task({drop: true})
|
|
|
|
*refreshContentTask() {
|
|
|
|
const now = new Date();
|
|
|
|
const contentExpiresAt = this.contentExpiresAt;
|
|
|
|
|
|
|
|
if (contentExpiresAt > now) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = [];
|
|
|
|
const promises = this.searchables.map(searchable => this._loadSearchable(searchable, content));
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield RSVP.all(promises);
|
|
|
|
this.content = content;
|
|
|
|
} catch (error) {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.contentExpiresAt = new Date(now.getTime() + this.contentExpiry);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _loadSearchable(searchable, content) {
|
|
|
|
const url = `${this.store.adapterFor(searchable.model).urlForQuery({}, searchable.model)}/`;
|
|
|
|
const maxSearchableLimit = '10000';
|
|
|
|
const query = {fields: searchable.fields, limit: maxSearchableLimit};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.ajax.request(url, {data: query});
|
|
|
|
|
|
|
|
const items = response[pluralize(searchable.model)].map(
|
|
|
|
item => ({
|
|
|
|
id: `${searchable.model}.${item[searchable.idField]}`,
|
2024-04-11 17:01:39 +03:00
|
|
|
url: item.url,
|
2024-04-10 14:44:24 +03:00
|
|
|
title: item[searchable.titleField],
|
2024-04-18 20:18:37 +03:00
|
|
|
groupName: searchable.name,
|
|
|
|
status: item.status
|
2024-04-10 14:44:24 +03:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
content.push(...items);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error); // eslint-disable-line
|
|
|
|
|
|
|
|
this.notifications.showAPIError(error, {
|
|
|
|
key: `search.load${searchable.name}.error`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|