2024-04-10 14:44:24 +03:00
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import Service from '@ember/service';
|
2024-05-07 17:24:20 +03:00
|
|
|
import {action} from '@ember/object';
|
2024-04-10 14:44:24 +03:00
|
|
|
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 = [];
|
2024-05-07 17:24:20 +03:00
|
|
|
isContentStale = true;
|
2024-04-10 14:44:24 +03:00
|
|
|
|
|
|
|
searchables = [
|
|
|
|
{
|
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'
|
2024-05-08 16:35:23 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Posts',
|
|
|
|
model: 'post',
|
2024-05-08 19:04:05 +03:00
|
|
|
fields: ['id', 'url', 'title', 'status', 'published_at', 'visibility'],
|
2024-05-08 16:35:23 +03:00
|
|
|
idField: 'id',
|
|
|
|
titleField: 'title'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Pages',
|
|
|
|
model: 'page',
|
2024-05-08 19:04:05 +03:00
|
|
|
fields: ['id', 'url', 'title', 'status', 'published_at', 'visibility'],
|
2024-05-08 16:35:23 +03:00
|
|
|
idField: 'id',
|
|
|
|
titleField: 'title'
|
2024-04-10 14:44:24 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-05-07 17:24:20 +03:00
|
|
|
@action
|
|
|
|
expireContent() {
|
|
|
|
this.isContentStale = true;
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:44:24 +03:00
|
|
|
@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})
|
2024-05-07 17:24:20 +03:00
|
|
|
*refreshContentTask({forceRefresh = false} = {}) {
|
|
|
|
if (!forceRefresh && !this.isContentStale) {
|
2024-04-10 14:44:24 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-07 17:24:20 +03:00
|
|
|
this.isContentStale = true;
|
|
|
|
|
2024-04-10 14:44:24 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-05-07 17:24:20 +03:00
|
|
|
this.isContentStale = false;
|
2024-04-10 14:44:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2024-05-08 19:04:05 +03:00
|
|
|
status: item.status,
|
|
|
|
visibility: item.visibility,
|
|
|
|
publishedAt: item.published_at
|
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`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|