Newer
Older
import * as GhostAdminAPI from '@tryghost/admin-api';
import { ConfigurationService } from '../configuration/configuration.service';
import { rewriteGhostImgUrl } from '../shared/utils';
@Injectable()
export class PostsService {
// ID's use in ghost tag description in order to categories tags.
public readonly locationCategory = 'commune';
public readonly publicCategory = 'public';
constructor(private readonly configService: ConfigurationService) {
// Configure Ghost client
this.api = new GhostAdminAPI({
url: process.env.GHOST_HOST_AND_PORT,
/**
* Get all tags with admin endpoint, including unused ones
*/
public getTags(): Promise<Tag[]> {
return this.api.tags.browse({ limit: 'all' });
}
/**
* Get all locations tags. Ex: Oullins, Sainte-foy etc...
*/
public async getLocationTags(): Promise<Tag[]> {
const tags = await this.getTags();
return _.filter(tags, { description: this.locationCategory });
}
/**
* Get all public tags. Ex: Moins de 16 ans, adultes...
*/
public async getPublicTags(): Promise<Tag[]> {
const tags = await this.getTags();
return _.filter(tags, { description: this.publicCategory });
}
/**
* Get other tags. Ex: A la une, infos...
*/
public async getRegularTags(): Promise<Tag[]> {
const tags = await this.getTags();
let publicTags = _.filter(tags, { description: null });
// Remove 'Appel a projet'
_.remove(publicTags, { slug: TagEnum.appels });
// Move 'a la une' at index 0
publicTags = this.arraymove(publicTags, _.findIndex(publicTags, { slug: TagEnum.aLaUne }), 0);
// Move 'info' at index 1
publicTags = this.arraymove(publicTags, _.findIndex(publicTags, { slug: TagEnum.infos }), 1);
return publicTags;
}
public arraymove(arr, fromIndex, toIndex): Array<unknown> {
const element = arr[fromIndex];
arr.splice(fromIndex, 1);
arr.splice(toIndex, 0, element);
return arr;
public formatPosts(postData: Post): Post {
// Handle excerpt field in case of no author
if (!postData.custom_excerpt) {
postData.excerpt = 'Inconnu';
}
// Handle image display. Rewrite image URL to fit ghost infra issue.

Marlène SIMONDANT
committed
postData = rewriteGhostImgUrl(this.configService, postData);