2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2021-11-12 19:10:19 +03:00
|
|
|
import fetch from 'fetch';
|
|
|
|
import {TrackedArray} from 'tracked-built-ins';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task, taskGroup, timeout} from 'ember-concurrency';
|
2021-11-12 19:10:19 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2022-08-03 16:11:55 +03:00
|
|
|
const API_URL = 'https://tenor.googleapis.com';
|
|
|
|
const API_VERSION = 'v2';
|
2021-11-12 19:10:19 +03:00
|
|
|
const DEBOUNCE_MS = 600;
|
|
|
|
|
|
|
|
export default class TenorService extends Service {
|
|
|
|
@service config;
|
|
|
|
|
|
|
|
@tracked columnCount = 4;
|
|
|
|
@tracked columns = null;
|
|
|
|
@tracked error = null;
|
2022-08-03 16:11:55 +03:00
|
|
|
@tracked htmlError = null;
|
2021-11-12 19:10:19 +03:00
|
|
|
@tracked gifs = new TrackedArray([]);
|
|
|
|
@tracked searchTerm = '';
|
2022-08-03 16:11:55 +03:00
|
|
|
@tracked loadedType = '';
|
2021-11-12 19:10:19 +03:00
|
|
|
|
|
|
|
_columnHeights = [];
|
|
|
|
_nextPos = null;
|
|
|
|
|
|
|
|
get apiKey() {
|
2022-08-05 14:13:27 +03:00
|
|
|
return this.config.get('tenor.googleApiKey');
|
2021-11-16 20:44:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get contentfilter() {
|
|
|
|
return this.config.get('tenor.contentFilter') || 'off';
|
2021-11-12 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get isLoading() {
|
|
|
|
return this.searchTask.isRunning || this.loadingTasks.isRunning;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this._resetColumns();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateSearch(term) {
|
|
|
|
if (term === this.searchTerm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.searchTerm = term;
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
if (term) {
|
|
|
|
return this.searchTask.perform(term);
|
|
|
|
} else {
|
|
|
|
return this.loadTrendingTask.perform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
loadNextPage() {
|
|
|
|
// protect against scroll trigger firing when the gifs are reset
|
|
|
|
if (this.searchTask.isRunning) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmpty(this.gifs)) {
|
|
|
|
return this.loadTrendingTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._nextPos !== null) {
|
|
|
|
this.loadNextPageTask.perform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:18:25 +03:00
|
|
|
@action
|
|
|
|
changeColumnCount(columnCount) {
|
|
|
|
this.columnCount = columnCount;
|
|
|
|
this._resetColumns();
|
|
|
|
}
|
|
|
|
|
2021-11-12 19:10:19 +03:00
|
|
|
@task({restartable: true})
|
|
|
|
*searchTask(term) {
|
|
|
|
yield timeout(DEBOUNCE_MS);
|
|
|
|
|
|
|
|
this.loadedType = 'search';
|
|
|
|
|
|
|
|
yield this._makeRequest(this.loadedType, {params: {
|
|
|
|
q: term,
|
|
|
|
media_filter: 'minimal'
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
@taskGroup loadingTasks;
|
|
|
|
|
|
|
|
@task({group: 'loadingTasks'})
|
|
|
|
*loadTrendingTask() {
|
2022-08-03 16:11:55 +03:00
|
|
|
this.loadedType = 'featured';
|
2021-11-12 19:10:19 +03:00
|
|
|
|
|
|
|
yield this._makeRequest(this.loadedType, {params: {
|
2022-08-03 16:11:55 +03:00
|
|
|
q: 'excited',
|
2021-11-12 19:10:19 +03:00
|
|
|
media_filter: 'minimal'
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
@task({group: 'loadingTasks'})
|
|
|
|
*loadNextPageTask() {
|
|
|
|
const params = {
|
|
|
|
pos: this._nextPos,
|
|
|
|
media_filter: 'minimal'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.loadedType === 'search') {
|
|
|
|
params.q = this.searchTerm;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield this._makeRequest(this.loadedType, {params});
|
|
|
|
}
|
|
|
|
|
|
|
|
@task({group: 'loadingTasks'})
|
|
|
|
*retryLastRequestTask() {
|
|
|
|
if (this._lastRequestArgs) {
|
|
|
|
yield this._makeRequest(...this._lastRequestArgs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.gifs = new TrackedArray([]);
|
|
|
|
this._nextPos = null;
|
|
|
|
this._resetColumns();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _makeRequest(path, options) {
|
|
|
|
const versionedPath = `${API_VERSION}/${path}`.replace(/\/+/, '/');
|
|
|
|
const url = new URL(versionedPath, API_URL);
|
|
|
|
|
|
|
|
const params = new URLSearchParams(options.params);
|
2021-11-16 20:44:31 +03:00
|
|
|
params.set('key', this.apiKey);
|
2022-08-03 16:11:55 +03:00
|
|
|
params.set('client_key', 'ghost-editor');
|
2021-11-16 20:44:31 +03:00
|
|
|
params.set('contentfilter', this.contentfilter);
|
2021-11-12 19:10:19 +03:00
|
|
|
|
|
|
|
url.search = params.toString();
|
|
|
|
|
|
|
|
// store the url so it can be retried if needed
|
|
|
|
this._lastRequestArgs = arguments;
|
|
|
|
|
|
|
|
this.error = '';
|
|
|
|
|
|
|
|
return fetch(url)
|
|
|
|
.then(response => this._checkStatus(response))
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => this._extractPagination(response))
|
|
|
|
.then(response => this._addGifsFromResponse(response))
|
|
|
|
.catch((e) => {
|
|
|
|
// if the error text isn't already set then we've get a connection error from `fetch`
|
|
|
|
if (!options.ignoreErrors && !this.error) {
|
|
|
|
this.error = 'Uh-oh! Trouble reaching the Tenor API, please check your connection';
|
|
|
|
}
|
2022-08-03 16:11:55 +03:00
|
|
|
|
|
|
|
if (this.error && this.error.startsWith('API key not valid')) {
|
|
|
|
// Added an html error field, so that we don't pass raw API errors from tenor to triple-braces in the frontend
|
|
|
|
this.htmlError = `This version of the Tenor API is no longer supported. Please update your API key by following our
|
|
|
|
<a href="https://ghost.org/docs/config/#tenor" target="_blank" rel="noopener noreferrer"> documentation here</a>.<br />`;
|
|
|
|
}
|
2021-11-12 19:10:19 +03:00
|
|
|
console.error(e); // eslint-disable-line
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _checkStatus(response) {
|
|
|
|
// successful request
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
let responseText;
|
|
|
|
|
2022-08-03 16:11:55 +03:00
|
|
|
if (response.headers.map['content-type'].startsWith('application/json')) {
|
|
|
|
responseText = await response.json().then(json => json.error.message || json.error);
|
2021-11-12 19:10:19 +03:00
|
|
|
} else if (response.headers.map['content-type'] === 'text/xml') {
|
|
|
|
responseText = await response.text();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.error = responseText;
|
|
|
|
|
|
|
|
const error = new Error(responseText);
|
|
|
|
error.response = response;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _extractPagination(response) {
|
|
|
|
this._nextPos = response.next;
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _addGifsFromResponse(response) {
|
|
|
|
const gifs = response.results;
|
|
|
|
gifs.forEach(gif => this._addGif(gif));
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
_addGif(gif) {
|
|
|
|
// re-calculate ratio for later use
|
2022-08-03 16:11:55 +03:00
|
|
|
const [width, height] = gif.media_formats.tinygif.dims;
|
2021-11-12 19:10:19 +03:00
|
|
|
gif.ratio = height / width;
|
|
|
|
|
|
|
|
// add to general gifs list
|
|
|
|
this.gifs.push(gif);
|
|
|
|
|
2021-11-30 13:52:36 +03:00
|
|
|
// store index for use in templates and keyboard nav
|
|
|
|
gif.index = this.gifs.indexOf(gif);
|
|
|
|
|
2021-11-12 19:10:19 +03:00
|
|
|
// add to least populated column
|
|
|
|
this._addGifToColumns(gif);
|
|
|
|
}
|
|
|
|
|
|
|
|
_addGifToColumns(gif) {
|
|
|
|
const min = Math.min(...this._columnHeights);
|
|
|
|
const columnIndex = this._columnHeights.indexOf(min);
|
|
|
|
|
|
|
|
// use a fixed width when calculating height to compensate for different overall sizes
|
|
|
|
this._columnHeights[columnIndex] += 300 * gif.ratio;
|
|
|
|
this.columns[columnIndex].push(gif);
|
2021-11-30 13:52:36 +03:00
|
|
|
|
|
|
|
// store the column indexes on the gif for use in keyboard nav
|
|
|
|
gif.columnIndex = columnIndex;
|
|
|
|
gif.columnRowIndex = this.columns[columnIndex].length - 1;
|
2021-11-12 19:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_resetColumns() {
|
|
|
|
let columns = new TrackedArray([]);
|
|
|
|
let _columnHeights = [];
|
|
|
|
|
|
|
|
// pre-fill column arrays based on columnCount
|
|
|
|
for (let i = 0; i < this.columnCount; i += 1) {
|
|
|
|
columns[i] = new TrackedArray([]);
|
|
|
|
_columnHeights[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.columns = columns;
|
|
|
|
this._columnHeights = _columnHeights;
|
|
|
|
|
|
|
|
if (!isEmpty(this.gifs)) {
|
|
|
|
this.gifs.forEach((gif) => {
|
|
|
|
this._addGifToColumns(gif);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|