import GelbooruServiceResponse from "../types/GelbooruServiceResponse"; import Image from "../types/Image"; import BotApiService from "./BotApiService"; import GelbooruApiService from "./GelbooruApiService"; class ImageService { postsQueue: Image[] = []; async get(): Promise { while (this.postsQueue.length === 0) { const validPosts = await this.getNewValidImages(); this.postsQueue = validPosts; } return this.postsQueue.pop() as Image; } private async getNewValidImages(): Promise { const gelbooruResponse: GelbooruServiceResponse = await GelbooruApiService.get(); const posts = gelbooruResponse.posts; const botResponse = await BotApiService.getAll(); const imagesUrls = botResponse.images.map(image => image.url); const validPosts = posts .filter(post => !imagesUrls.some(url => url === post.url)) .map(post => ({ url: post.url, tags: post.tags, content: "TODO" })); return validPosts; } } export default new ImageService();