rsshub
Version:
Make RSS Great Again!
89 lines (87 loc) • 3.82 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs";
import { t as config } from "./config-C37vj7VH.mjs";
import "./dist-BInvbO1W.mjs";
import "./logger-Czu8UMNd.mjs";
import "./ofetch-BIyrKU3Y.mjs";
import { t as parseDate } from "./parse-date-BrP7mxXf.mjs";
import "./cache-Bo__VnGm.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import { t as invalid_parameter_default } from "./invalid-parameter-rr4AgGpp.mjs";
import { t as config_not_found_default } from "./config-not-found-Dyp3RlZZ.mjs";
import { n as queryToBoolean } from "./readable-social-DoIL4WB3.mjs";
import { a as getGuild, n as baseUrl, o as searchGuildMessages, t as VALID_HAS_TYPES } from "./discord-api-D9fu6pLr.mjs";
import path from "node:path";
//#region lib/routes/discord/search.ts
init_esm_shims();
const route = {
path: "/search/:guildId/:routeParams",
categories: ["social-media"],
example: "/discord/search/302094807046684672/content=friendly&has=image,video",
parameters: {
guildId: "Guild ID",
routeParams: "Search parameters, support content, author_id, mentions, has, min_id, max_id, channel_id, pinned"
},
features: {
requireConfig: [{
name: "DISCORD_AUTHORIZATION",
description: "Discord authorization header"
}],
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
name: "Guild Search",
maintainers: ["NekoAria"],
handler
};
const parseSearchParams = (routeParams) => {
const parsed = new URLSearchParams(routeParams);
const validHasTypes = (parsed.get("has")?.split(",").filter(Boolean))?.filter((type) => VALID_HAS_TYPES.has(type));
const params = {
content: parsed.get("content") ?? void 0,
author_id: parsed.get("author_id") ?? void 0,
mentions: parsed.get("mentions") ?? void 0,
has: validHasTypes?.length ? validHasTypes : void 0,
min_id: parsed.get("min_id") ?? void 0,
max_id: parsed.get("max_id") ?? void 0,
channel_id: parsed.get("channel_id") ?? void 0,
pinned: parsed.has("pinned") ? queryToBoolean(parsed.get("pinned")) : void 0
};
return Object.fromEntries(Object.entries(params).filter(([, value]) => value !== void 0));
};
async function handler(ctx) {
const { authorization } = config.discord || {};
if (!authorization) throw new config_not_found_default("Discord RSS is disabled due to the lack of authorization config");
const { guildId } = ctx.req.param();
const searchParams = parseSearchParams(ctx.req.param("routeParams"));
if (!Object.keys(searchParams).length) throw new invalid_parameter_default("At least one valid search parameter is required");
const [guildInfo, searchResult] = await Promise.all([getGuild(guildId, authorization), searchGuildMessages(guildId, authorization, searchParams)]);
if (!searchResult?.messages?.length) return {
title: `Search Results - ${guildInfo.name}`,
link: `${baseUrl}/channels/${guildId}`,
item: [],
allowEmpty: true
};
const messages = searchResult.messages.flat().map((message) => ({
title: message.content.split("\n")[0] || "(no content)",
description: art(path.join(__dirname, "templates/message-311bc85b.art"), {
message,
guildInfo
}),
author: message.author.global_name ?? message.author.username,
pubDate: parseDate(message.timestamp),
updated: message.edited_timestamp ? parseDate(message.edited_timestamp) : void 0,
category: [`#${message.channel_id}`],
link: `${baseUrl}/channels/${guildId}/${message.channel_id}/${message.id}`
}));
return {
title: `Search "${Object.entries(searchParams).filter(([, value]) => value !== void 0).map(([key, value]) => `${key}:${Array.isArray(value) ? value.join(",") : value}`).join(" ")}" in ${guildInfo.name} - Discord`,
link: `${baseUrl}/channels/${guildId}`,
item: messages,
allowEmpty: true
};
}
//#endregion
export { route };