diff --git a/src/plugins/yandere/api/post.ts b/src/plugins/yandere/api/post.ts index 88367db..84023a9 100644 --- a/src/plugins/yandere/api/post.ts +++ b/src/plugins/yandere/api/post.ts @@ -1,46 +1,115 @@ +import { requestJSON } from "@root/structures/apiRequest.ts"; +import { DiscordEmbed } from "npm:discordeno@18.0.1"; + type PostResponse = { - posts: { - id: number; - tags: string; - created_at: string; - creator_id: number; - approver_id: number; - author: string; - change: number; - source: string; - score: number; - md5: string; - file_size: number; - file_ext: string; - file_url: string; - is_shown_in_index: boolean; - preview_url: string; - preview_width: number; - preview_height: number; - actual_preview_width: number; - actual_preview_height: number; - sample_url: string; - sample_width: number; - sample_height: number; - sample_file_size: number; - jpeg_url: string; - jpeg_width: number; - jpeg_height: number; - rating: string; - is_rating_locked: boolean; - has_children: boolean; - parent_id: number; - status: string; - is_pending: boolean; - width: number; - height: number; - is_held: boolean; - frames_pending_string: string; - frames_pending: []; - frames_string: string; - frames: []; - is_note_locked: boolean; - last_noted_at: string; - last_commented_at: string; - }[]; -}; + id: number; + tags: string; + created_at: string; + creator_id: number; + approver_id: number; + author: string; + change: number; + source: string; + score: number; + md5: string; + file_size: number; + file_ext: string; + file_url: string; + is_shown_in_index: boolean; + preview_url: string; + preview_width: number; + preview_height: number; + actual_preview_width: number; + actual_preview_height: number; + sample_url: string; + sample_width: number; + sample_height: number; + sample_file_size: number; + jpeg_url: string; + jpeg_width: number; + jpeg_height: number; + rating: string; + is_rating_locked: boolean; + has_children: boolean; + parent_id: number; + status: string; + is_pending: boolean; + width: number; + height: number; + is_held: boolean; + frames_pending_string: string; + frames_pending: []; + frames_string: string; + frames: []; + is_note_locked: boolean; + last_noted_at: string; + last_commented_at: string; +}[]; + +const postSearchkeys = ["limit", "page", "tags"]; + +function parsePostListArgs(url: URL, args: string) { + const urlCopy: URL = copyObject(url); + const argarr = args.replaceAll(/(\[|\|\ ])/g, "").split(","); + for (const arg of argarr) { + const [k, v] = arg.split(":"); + if (k === undefined || v === undefined) { + throw Error( + `undefined key or value in ${parsePostListArgs.name}, got k:${k}, v:${v}`, + ); + } + if (k in postSearchkeys) { + urlCopy.searchParams.append(k, v); + } else { + throw Error( + `unknown parameter was given in ${parsePostListArgs.name}, got k:${k}, v:${v}`, + ); + } + } + return urlCopy; +} + +async function returnDiscordEmbeds( + url: URL, +): Promise { + const response: PostResponse = await requestJSON( + url.toString(), + ); + const embeds: DiscordEmbed[] = []; + for (const post of response) { + embeds.push( + { + url: post.source, + title: post.id.toString(), + author: { + name: post.author, + }, + image: { + url: post.file_url, + width: post.width, + height: post.height, + }, + }, + ); + } + return embeds; +} + +function copyObject(obj: T) { + return JSON.parse(JSON.stringify(obj)); +} + +/** + * Post-Request Handler Function, that returns DiscordEmbeds. + * @param url URL Object + * @param search Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss. + */ +export async function handlePostRequest(url: URL, search?: string): Promise { + const urlCopy = copyObject(url); + //parse + if (search) { + return await returnDiscordEmbeds(parsePostListArgs(urlCopy, search)); + } else { + return await returnDiscordEmbeds(urlCopy); + } +}