Whole-shop grants now import the shop's products, not only those people asked for. The hub lists the shop on IMVU's public API (general-rated products) when the grant is accepted, daily after, and on "List the shop again". Each listing keeps the products' info and queues what isn't imported yet, less exclusions. IMVU's small pages overlap and miss products, so the hub asks for pages of 1000 and drops repeats. GET /imports/available tells the proxy what T5DE has of IMVU products and shops, from what the hub knows only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
274 lines
11 KiB
TypeScript
274 lines
11 KiB
TypeScript
"use server";
|
|
// Managing a catalog from the console: settings, key, team, members, invitations, what it hosts, suspension.
|
|
import { revalidatePath } from "next/cache";
|
|
import { api, ApiError, unwrap } from "@/lib/api";
|
|
import { attempt, field, type ActionState } from "@/lib/actions";
|
|
|
|
/** Runs a change to a catalog, then shows its pages again. */
|
|
async function onCatalog(form: FormData, change: (id: string) => Promise<string | void>): Promise<ActionState> {
|
|
const id = field(form, "id");
|
|
return attempt(async () => {
|
|
const notice = await change(id);
|
|
revalidatePath(`/console/catalogs/${id}`);
|
|
revalidatePath(`/catalogs/${id}`);
|
|
revalidatePath("/console/catalogs");
|
|
revalidatePath("/catalogs");
|
|
return notice;
|
|
});
|
|
}
|
|
|
|
const pick = <T extends string>(v: string, options: readonly T[]): T | undefined => options.find((o) => o === v);
|
|
|
|
export async function saveCatalog(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
unwrap(
|
|
await api.catalogs({ id }).patch({
|
|
name: field(form, "name"),
|
|
url: field(form, "url"),
|
|
contact: field(form, "contact"),
|
|
visibility: pick(field(form, "visibility"), ["public", "private"] as const),
|
|
membership: pick(field(form, "membership"), ["open", "approval", "invite"] as const),
|
|
maxRating: pick(field(form, "maxRating"), ["general", "adult"] as const),
|
|
uploads: pick(field(form, "uploads"), ["anyone", "members", "team"] as const),
|
|
mirrors: pick(field(form, "mirrors"), ["auto", "approve", "off"] as const),
|
|
}),
|
|
);
|
|
return "Saved.";
|
|
});
|
|
}
|
|
|
|
export async function rotateKey(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
const { key } = unwrap(await api.catalogs({ id }).key.post());
|
|
return `New key (shown once; put it in the catalog's CATALOG_KEY and restart it): ${key}`;
|
|
});
|
|
}
|
|
|
|
export async function setSuspended(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
if (field(form, "intent") === "reinstate") unwrap(await api.catalogs({ id }).reinstate.post());
|
|
else unwrap(await api.catalogs({ id }).suspend.post({ reason: field(form, "reason") }));
|
|
});
|
|
}
|
|
|
|
export async function addOperator(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
const username = field(form, "username");
|
|
unwrap(await api.catalogs({ id }).team({ username }).put({ role: "catalog-operator" }));
|
|
return `${username} is an operator now.`;
|
|
});
|
|
}
|
|
|
|
export async function removeFromTeam(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
unwrap(await api.catalogs({ id }).team({ username: field(form, "username") }).delete({}, { query: { role: "catalog-operator" } }));
|
|
});
|
|
}
|
|
|
|
/** A member or request: approve, decline, add, ban, unban or remove. */
|
|
export async function decideMember(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
const member = api.catalogs({ id }).members({ username: field(form, "username") });
|
|
switch (field(form, "decision")) {
|
|
case "approve":
|
|
case "add":
|
|
unwrap(await member.put({ status: "member" }));
|
|
break;
|
|
case "ban":
|
|
unwrap(await member.put({ status: "banned" }));
|
|
break;
|
|
default: // decline, unban, remove
|
|
unwrap(await member.delete());
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function createInvite(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
const maxUses = Number(field(form, "maxUses")) || undefined;
|
|
const expiresInDays = Number(field(form, "expiresInDays")) || undefined;
|
|
const { invite } = unwrap(await api.catalogs({ id }).invites.post({ maxUses, expiresInDays }));
|
|
return `Invitation link: ${invite.url}`;
|
|
});
|
|
}
|
|
|
|
export async function revokeInvite(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
unwrap(await api.catalogs({ id }).invites({ code: field(form, "code") }).delete());
|
|
});
|
|
}
|
|
|
|
export async function delist(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
const product = field(form, "product");
|
|
unwrap(await api.catalogs({ id }).products({ product }).delete({}, { query: { reason: field(form, "reason") } }));
|
|
revalidatePath(`/products/${product}`);
|
|
});
|
|
}
|
|
|
|
/** Someone joins a catalog (or asks to), with an invitation code if they have one; or leaves it. */
|
|
export async function membership(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
if (field(form, "intent") === "leave") {
|
|
unwrap(await api.catalogs({ id }).join.delete());
|
|
revalidatePath("/library");
|
|
return "You left.";
|
|
}
|
|
const invite = field(form, "invite");
|
|
const { membership } = unwrap(await api.catalogs({ id }).join.post(invite ? { invite } : {}));
|
|
revalidatePath("/library");
|
|
return membership === "member" ? "You're a member." : "Asked. The catalog's team will decide.";
|
|
});
|
|
}
|
|
|
|
// ---- imports (docs/imports.md) ----
|
|
|
|
/** Turns the importer on (its key, shown once) or off, pauses or resumes it, or makes a new key. */
|
|
export async function setImporter(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
const intent = field(form, "intent");
|
|
if (intent === "key") {
|
|
const { key } = unwrap(await api.catalogs({ id }).importer.key.post());
|
|
return `New importer key (shown once; put it in the importer's IMPORTER_KEY and restart it): ${key}`;
|
|
}
|
|
const body =
|
|
intent === "on" ? { enabled: true } : intent === "off" ? { enabled: false } : { paused: intent === "pause" };
|
|
const r = unwrap(await api.catalogs({ id }).importer.post(body));
|
|
if (r.key) return `Imports are on. The importer's key (shown once; put it in IMPORTER_KEY): ${r.key}`;
|
|
});
|
|
}
|
|
|
|
/** The catalog's team accepts, declines or ends a creator's grant. */
|
|
export async function decideGrant(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
const grant = field(form, "grant");
|
|
const intent = field(form, "intent");
|
|
const reason = field(form, "reason");
|
|
if (intent === "end") {
|
|
unwrap(await api.imports.grants({ id: grant }).delete(reason ? { reason } : {}));
|
|
return "Ended. Its imported products were taken away.";
|
|
}
|
|
const decision = intent === "accept" ? "accept" : "decline";
|
|
const r = unwrap(
|
|
await api.imports
|
|
.grants({ id: grant })({ decision })
|
|
.post(reason ? { reason } : {}),
|
|
);
|
|
if (decision !== "accept") return "Declined.";
|
|
return r.grant.scope === "shop"
|
|
? `Accepted. ${r.queued} queued for the importer; the shop's other products follow once it's listed on IMVU.`
|
|
: `Accepted. ${r.queued} queued for the importer.`;
|
|
});
|
|
}
|
|
|
|
/** Lists a whole-shop grant's products on IMVU again (new ones are queued). */
|
|
export async function listShop(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
const r = unwrap(await api.imports.grants({ id: field(form, "grant") }).list.post());
|
|
return `${r.listed} product${r.listed === 1 ? "" : "s"} on IMVU; ${r.queued} new queued.`;
|
|
});
|
|
}
|
|
|
|
/** Retries a failed import job (or imports a product again). */
|
|
export async function retryJob(_: ActionState, form: FormData): Promise<ActionState> {
|
|
return onCatalog(form, async (id) => {
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
unwrap(
|
|
await api
|
|
.catalogs({ id })
|
|
.importer.jobs({ job: field(form, "job") })
|
|
.retry.post(),
|
|
);
|
|
return "Queued.";
|
|
});
|
|
}
|
|
|
|
// ---- the importer program and its IMVU accounts (docs/imports.md "IMVU accounts") ----
|
|
|
|
export type ImvuAccountInfo = {
|
|
imvuId: number;
|
|
username: string;
|
|
picture: string | null;
|
|
accessPass: boolean;
|
|
ageVerified: boolean;
|
|
};
|
|
|
|
/** Called from the browser: an error to show, or what was asked for. */
|
|
async function answered<T>(fn: () => Promise<T>): Promise<{ error: string } | { ok: T }> {
|
|
try {
|
|
return { ok: await fn() };
|
|
} catch (e) {
|
|
if (e instanceof ApiError) return { error: e.message };
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
/** Looks an IMVU account up for the confirmation dialog (public info only). */
|
|
export async function lookupImporterAccount(id: string, username: string) {
|
|
return answered<ImvuAccountInfo>(async () =>
|
|
unwrap(await api.catalogs({ id }).importer["imvu-account"].get({ query: { username: username.trim() } })),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Adds an account, or gives one a new password (account set). The password arrives sealed in the browser for the
|
|
* importer's key; it never passes through here readable.
|
|
*/
|
|
export async function saveImporterAccount(
|
|
id: string,
|
|
a: { imvuId: number; account?: string; sealed: string; fingerprint: string },
|
|
) {
|
|
const r = await answered(async () => {
|
|
if (a.account)
|
|
unwrap(
|
|
await api
|
|
.catalogs({ id })
|
|
.importer.accounts({ account: a.account })
|
|
.password.put({ sealed: a.sealed, fingerprint: a.fingerprint }),
|
|
);
|
|
else unwrap(await api.catalogs({ id }).importer.accounts.post({ imvuId: a.imvuId, sealed: a.sealed, fingerprint: a.fingerprint }));
|
|
return true;
|
|
});
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
return r;
|
|
}
|
|
|
|
/** Moves an account up or down, or removes it. */
|
|
export async function changeImporterAccount(_: ActionState, form: FormData): Promise<ActionState> {
|
|
const id = field(form, "id");
|
|
const account = field(form, "account");
|
|
return attempt(async () => {
|
|
const intent = field(form, "intent");
|
|
if (intent === "remove") {
|
|
unwrap(await api.catalogs({ id }).importer.accounts({ account }).delete());
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
return "Removed. The importer forgets it within a minute.";
|
|
}
|
|
unwrap(
|
|
await api
|
|
.catalogs({ id })
|
|
.importer.accounts({ account })
|
|
.move.post({ direction: intent === "up" ? "up" : "down" }),
|
|
);
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
});
|
|
}
|
|
|
|
/** The importer's settings. */
|
|
export async function setImporterSettings(_: ActionState, form: FormData): Promise<ActionState> {
|
|
const id = field(form, "id");
|
|
return attempt(async () => {
|
|
unwrap(
|
|
await api.catalogs({ id }).importer.settings.post({
|
|
rate: Number(field(form, "rate")),
|
|
concurrency: Number(field(form, "concurrency")),
|
|
}),
|
|
);
|
|
revalidatePath(`/console/catalogs/${id}/imports`);
|
|
return "Saved. The importer picks it up within a minute.";
|
|
});
|
|
}
|