mirror of
https://github.com/NickKaramoff/toot
synced 2025-02-11 17:20:48 +01:00
Move detection logic to Astro actions
This commit is contained in:
parent
c9f849d1f4
commit
2fa5f8baa8
50
src/actions/index.ts
Normal file
50
src/actions/index.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/*!
|
||||||
|
* This file is part of Share₂Fedi
|
||||||
|
* https://github.com/kytta/share2fedi
|
||||||
|
*
|
||||||
|
* SPDX-FileCopyrightText: © 2023 Nikita Karamov <me@kytta.dev>
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { defineAction, ActionError } from "astro:actions";
|
||||||
|
import { z } from "astro:schema";
|
||||||
|
|
||||||
|
import { getSoftwareName } from "@lib/nodeinfo";
|
||||||
|
import { type ProjectPublishConfig, supportedProjects } from "@lib/project";
|
||||||
|
|
||||||
|
export type Detection = {
|
||||||
|
domain: string;
|
||||||
|
project: keyof typeof supportedProjects;
|
||||||
|
} & ProjectPublishConfig;
|
||||||
|
|
||||||
|
export const server = {
|
||||||
|
detect: defineAction({
|
||||||
|
input: z.string().min(5).includes("."),
|
||||||
|
handler: async (domain): Promise<Detection> => {
|
||||||
|
const softwareName = await getSoftwareName(domain);
|
||||||
|
if (softwareName === undefined) {
|
||||||
|
throw new ActionError({
|
||||||
|
message: "Could not detect Fediverse project.",
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(softwareName in supportedProjects)) {
|
||||||
|
throw new ActionError({
|
||||||
|
message: `Fediverse project "${softwareName}" is not supported yet.`,
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const publishConfig = supportedProjects[
|
||||||
|
softwareName
|
||||||
|
] as ProjectPublishConfig;
|
||||||
|
|
||||||
|
return {
|
||||||
|
domain,
|
||||||
|
project: softwareName,
|
||||||
|
...publishConfig,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
@ -6,38 +6,21 @@
|
|||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getSoftwareName } from "@lib/nodeinfo";
|
|
||||||
import { type ProjectPublishConfig, supportedProjects } from "@lib/project";
|
|
||||||
import { error, json } from "@lib/response";
|
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
|
import { actions } from "astro:actions";
|
||||||
|
|
||||||
export type Detection = {
|
import { error, json } from "@lib/response";
|
||||||
domain: string;
|
|
||||||
project: keyof typeof supportedProjects;
|
|
||||||
} & ProjectPublishConfig;
|
|
||||||
|
|
||||||
export const GET: APIRoute = async ({ params }) => {
|
export const GET: APIRoute = async ({ params, callAction }) => {
|
||||||
const domain = params.domain as string;
|
const domain = params.domain as string;
|
||||||
|
|
||||||
const softwareName = await getSoftwareName(domain);
|
const { data, error: actionError } = await callAction(actions.detect, domain);
|
||||||
if (softwareName === undefined) {
|
|
||||||
return error("Could not detect Fediverse project.");
|
if (actionError) {
|
||||||
|
return error(actionError.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(softwareName in supportedProjects)) {
|
return json(data, 200, {
|
||||||
return error(`Fediverse project "${softwareName}" is not supported yet.`);
|
"Cache-Control": "public, s-maxage=604800, max-age=604800",
|
||||||
}
|
});
|
||||||
|
|
||||||
const publishConfig = supportedProjects[softwareName] as ProjectPublishConfig;
|
|
||||||
return json(
|
|
||||||
{
|
|
||||||
domain,
|
|
||||||
project: softwareName,
|
|
||||||
...publishConfig,
|
|
||||||
},
|
|
||||||
200,
|
|
||||||
{
|
|
||||||
"Cache-Control": "public, s-maxage=604800, max-age=604800",
|
|
||||||
},
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
@ -6,11 +6,13 @@
|
|||||||
* SPDX-FileCopyrightText: © 2023 Nikita Karamov <me@kytta.dev>
|
* SPDX-FileCopyrightText: © 2023 Nikita Karamov <me@kytta.dev>
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
import { actions } from "astro:actions";
|
||||||
|
|
||||||
import Layout from "@layouts/layout.astro";
|
import Layout from "@layouts/layout.astro";
|
||||||
import InstanceSelect from "@components/instance-select.astro";
|
import InstanceSelect from "@components/instance-select.astro";
|
||||||
|
|
||||||
import { getUrlDomain } from "@lib/url";
|
import { getUrlDomain } from "@lib/url";
|
||||||
import type { Detection } from "./api/detect/[domain]";
|
import type { Detection } from "@actions/index.ts";
|
||||||
|
|
||||||
const searchParameters = new URL(Astro.request.url).searchParams;
|
const searchParameters = new URL(Astro.request.url).searchParams;
|
||||||
let text: string | null = searchParameters.get("text");
|
let text: string | null = searchParameters.get("text");
|
||||||
@ -31,15 +33,12 @@ if (Astro.request.method === "POST") {
|
|||||||
errors.instance += "Please enter instance domain. ";
|
errors.instance += "Please enter instance domain. ";
|
||||||
} else {
|
} else {
|
||||||
instance = getUrlDomain(instance);
|
instance = getUrlDomain(instance);
|
||||||
|
const { data, error } = await actions.detect(instance);
|
||||||
|
|
||||||
const detectResponse = await fetch(
|
if (error) {
|
||||||
new URL(`/api/detect/${instance}`, Astro.url),
|
errors.instance += error.message + " ";
|
||||||
);
|
|
||||||
const detectJson = await detectResponse.json();
|
|
||||||
if (detectJson.error) {
|
|
||||||
errors.instance += detectJson.error + " ";
|
|
||||||
} else {
|
} else {
|
||||||
detection = detectJson;
|
detection = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
|
"@actions/*": ["src/actions/*"],
|
||||||
"@components/*": ["src/components/*"],
|
"@components/*": ["src/components/*"],
|
||||||
"@i18n/*": ["src/i18n/*"],
|
"@i18n/*": ["src/i18n/*"],
|
||||||
"@layouts/*": ["src/layouts/*"],
|
"@layouts/*": ["src/layouts/*"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user