80 lines
3.2 KiB
TypeScript
80 lines
3.2 KiB
TypeScript
"use server";
|
|
// Imports from the website: asking for an IMVU product, and creators' grants (bring, change, withdraw).
|
|
import { revalidatePath } from "next/cache";
|
|
import { api, ApiError, unwrap } from "@/lib/api";
|
|
import { attempt, field, type ActionState } from "@/lib/actions";
|
|
import { productIDs } from "@/lib/imvu";
|
|
|
|
const done = () => {
|
|
revalidatePath("/imports");
|
|
revalidatePath("/library");
|
|
};
|
|
|
|
const answers = {
|
|
added: "It's in your library.",
|
|
queued: "Its creator lets T5DE have it: it'll be in your library once the catalog imports it.",
|
|
suggested: "Its creator brings chosen products to T5DE; they'll see you asked for this one.",
|
|
counted: "Its creator hasn't brought it to T5DE. Your request is counted, and they'll see it if they do.",
|
|
} as const;
|
|
|
|
/** Someone asks for an IMVU product (an ID or a shop link). */
|
|
export async function requestProduct(_: ActionState, form: FormData): Promise<ActionState> {
|
|
const ids = productIDs(field(form, "product"));
|
|
if (!ids || ids.length !== 1) return { error: "Give one IMVU product ID or shop link." };
|
|
return attempt(async () => {
|
|
const r = unwrap(await api.imports.requests.post({ imvuProduct: ids[0]! }));
|
|
done();
|
|
return answers[r.status];
|
|
});
|
|
}
|
|
|
|
/** A creator brings their IMVU products to a catalog. */
|
|
export async function createGrant(_: ActionState, form: FormData): Promise<ActionState> {
|
|
if (form.get("agree") !== "on") return { error: "Agree to the terms to go on." };
|
|
const scope = field(form, "scope") === "products" ? "products" : "shop";
|
|
const list = productIDs(field(form, scope === "products" ? "products" : "exclude"));
|
|
if (!list)
|
|
return {
|
|
error: "Products are IMVU product IDs or shop links, separated by spaces, commas or lines.",
|
|
};
|
|
return attempt(async () => {
|
|
unwrap(
|
|
await api.imports.grants.post({
|
|
catalog: field(form, "catalog"),
|
|
imvuId: Number(field(form, "imvuId")),
|
|
scope,
|
|
...(scope === "products" ? { products: list } : { exclude: list }),
|
|
listed: form.get("listed") === "on",
|
|
consentVersion: Number(field(form, "consentVersion")),
|
|
}),
|
|
);
|
|
done();
|
|
return "Sent. The catalog's team accepts it before anything is imported.";
|
|
});
|
|
}
|
|
|
|
/** A creator changes what a grant covers, or withdraws it. */
|
|
export async function changeGrant(_: ActionState, form: FormData): Promise<ActionState> {
|
|
const id = field(form, "id");
|
|
return attempt(async () => {
|
|
if (field(form, "intent") === "withdraw") {
|
|
const r = unwrap(await api.imports.grants({ id }).delete({}));
|
|
done();
|
|
return r.withdrawn.length ? `Withdrawn. ${r.withdrawn.length} imported products were taken away.` : "Withdrawn.";
|
|
}
|
|
const scope = field(form, "scope") === "products" ? "products" : "shop";
|
|
const list = productIDs(field(form, "list"));
|
|
if (!list) throw new ApiError(400, "invalid", "Products are IMVU product IDs or shop links.");
|
|
const r = unwrap(
|
|
await api.imports.grants({ id }).patch({
|
|
...(scope === "products" ? { products: list } : { exclude: list }),
|
|
listed: form.get("listed") === "on",
|
|
}),
|
|
);
|
|
done();
|
|
return r.withdrawn.length
|
|
? `Saved. ${r.withdrawn.length} imported products it no longer covers were taken away.`
|
|
: "Saved.";
|
|
});
|
|
}
|