Post-API implemented, testing required.
This commit is contained in:
@@ -1,46 +1,115 @@
|
|||||||
|
import { requestJSON } from "@root/structures/apiRequest.ts";
|
||||||
|
import { DiscordEmbed } from "npm:discordeno@18.0.1";
|
||||||
|
|
||||||
type PostResponse = {
|
type PostResponse = {
|
||||||
posts: {
|
id: number;
|
||||||
id: number;
|
tags: string;
|
||||||
tags: string;
|
created_at: string;
|
||||||
created_at: string;
|
creator_id: number;
|
||||||
creator_id: number;
|
approver_id: number;
|
||||||
approver_id: number;
|
author: string;
|
||||||
author: string;
|
change: number;
|
||||||
change: number;
|
source: string;
|
||||||
source: string;
|
score: number;
|
||||||
score: number;
|
md5: string;
|
||||||
md5: string;
|
file_size: number;
|
||||||
file_size: number;
|
file_ext: string;
|
||||||
file_ext: string;
|
file_url: string;
|
||||||
file_url: string;
|
is_shown_in_index: boolean;
|
||||||
is_shown_in_index: boolean;
|
preview_url: string;
|
||||||
preview_url: string;
|
preview_width: number;
|
||||||
preview_width: number;
|
preview_height: number;
|
||||||
preview_height: number;
|
actual_preview_width: number;
|
||||||
actual_preview_width: number;
|
actual_preview_height: number;
|
||||||
actual_preview_height: number;
|
sample_url: string;
|
||||||
sample_url: string;
|
sample_width: number;
|
||||||
sample_width: number;
|
sample_height: number;
|
||||||
sample_height: number;
|
sample_file_size: number;
|
||||||
sample_file_size: number;
|
jpeg_url: string;
|
||||||
jpeg_url: string;
|
jpeg_width: number;
|
||||||
jpeg_width: number;
|
jpeg_height: number;
|
||||||
jpeg_height: number;
|
rating: string;
|
||||||
rating: string;
|
is_rating_locked: boolean;
|
||||||
is_rating_locked: boolean;
|
has_children: boolean;
|
||||||
has_children: boolean;
|
parent_id: number;
|
||||||
parent_id: number;
|
status: string;
|
||||||
status: string;
|
is_pending: boolean;
|
||||||
is_pending: boolean;
|
width: number;
|
||||||
width: number;
|
height: number;
|
||||||
height: number;
|
is_held: boolean;
|
||||||
is_held: boolean;
|
frames_pending_string: string;
|
||||||
frames_pending_string: string;
|
frames_pending: [];
|
||||||
frames_pending: [];
|
frames_string: string;
|
||||||
frames_string: string;
|
frames: [];
|
||||||
frames: [];
|
is_note_locked: boolean;
|
||||||
is_note_locked: boolean;
|
last_noted_at: string;
|
||||||
last_noted_at: string;
|
last_commented_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<DiscordEmbed[]> {
|
||||||
|
const response: PostResponse = await requestJSON<PostResponse>(
|
||||||
|
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<T>(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<DiscordEmbed[]> {
|
||||||
|
const urlCopy = copyObject(url);
|
||||||
|
//parse
|
||||||
|
if (search) {
|
||||||
|
return await returnDiscordEmbeds(parsePostListArgs(urlCopy, search));
|
||||||
|
} else {
|
||||||
|
return await returnDiscordEmbeds(urlCopy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user