import { Injectable } from '@nestjs/common'; import * as GhostAdminAPI from '@tryghost/admin-api'; import * as _ from 'lodash'; import { ConfigurationService } from '../configuration/configuration.service'; import { TagEnum } from './enums/tag.enum'; import { Post } from './schemas/post.schema'; import { Tag } from './schemas/tag.schema'; @Injectable() export class PostsService { // ID's use in ghost tag description in order to categories tags. public readonly locationCategory = 'commune'; public readonly publicCategory = 'public'; private api: any; constructor(private readonly configService: ConfigurationService) { // Configure Ghost client this.api = new GhostAdminAPI({ url: process.env.GHOST_HOST_AND_PORT, key: process.env.GHOST_ADMIN_API_KEY, version: 'v3', }); } /** * 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<any> { 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'; } const test = `<p>Test qui va bien Test qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bienTest qui va bien.</p><figure class=\"kg-card kg-image-card\"><img src=\"https://resin-dev.grandlyon.com/content/images/2022/01/resin-logo-1200x630.png\" class=\"kg-image\" alt loading=\"lazy\" width=\"1200\" height=\"630\" srcset=\"https://resin-dev.grandlyon.com/content/images/size/w600/2022/01/resin-logo-1200x630.png 600w, https://resin-dev.grandlyon.com/content/images/size/w1000/2022/01/resin-logo-1200x630.png 1000w, https://resin-dev.grandlyon.com/content/images/2022/01/resin-logo-1200x630.png 1200w\" sizes=\"(min-width: 720px) 720px\"></figure>`; // Handle image display. Rewrite image URL to fit ghost infra issue. if (postData.feature_image && !this.configService.isLocalConf()) { postData.feature_image = `https://${this.configService.config.host}/blog/content${ postData.feature_image.split('/content')[1] }`; const regex = /(https?:\/\/ghost):(\d*)?/g; postData.html = postData.html.replace(regex, `https://${this.configService.config.host}/blog`); } return postData; } }