Creators grant a catalog leave to import their IMVU products (whole shop less exclusions, or listed products) from a linked IMVU account, agreeing to a versioned consent text; the catalog's team accepts or declines. Requests queue covered products (or add them to the library when imported already), suggest, or count. Importers claim jobs with the catalog's importer key, get t5de-import tokens, and the catalog registers imported revisions as published products owned by the creator. Narrowing or withdrawing a grant withdraws products; unlinking suspends grants; an hourly batch rechecks imported products with IMVU daily. Website: /imports and the console's catalog imports page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
91 lines
3.7 KiB
TypeScript
91 lines
3.7 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";
|
|
|
|
const done = () => {
|
|
revalidatePath("/imports");
|
|
revalidatePath("/library");
|
|
};
|
|
|
|
/** IMVU product IDs in what someone typed or pasted: numbers, or shop links (`products_id=123`, `product-123`). */
|
|
function productIDs(text: string): number[] | undefined {
|
|
const out = new Set<number>();
|
|
for (const part of text.split(/[\s,]+/).filter(Boolean)) {
|
|
const m = part.match(/(?:products_id=|product-)(\d+)/) ?? part.match(/^(\d+)$/);
|
|
const n = m ? Number(m[1]) : NaN;
|
|
if (!Number.isSafeInteger(n) || n <= 0) return undefined;
|
|
out.add(n);
|
|
}
|
|
return [...out];
|
|
}
|
|
|
|
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.";
|
|
});
|
|
}
|