feat: importer IMVU accounts and settings from the console (#14)
Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #14.
This commit is contained in:
1 parent
7ece681eef
commit
e6a97be894
16 files changed
+5538
-13
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"use server";
|
||||
// Managing a catalog from the console: settings, key, team, members, invitations, what it hosts, suspension.
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { api, unwrap } from "@/lib/api";
|
||||
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. */
|
||||
@@ -173,3 +173,89 @@ export async function retryJob(_: ActionState, form: FormData): Promise<ActionSt
|
||||
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.";
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user