44 lines
1.8 KiB
TypeScript
44 lines
1.8 KiB
TypeScript
// Direkt veröffentlichen: nach dem Speichern (Editor) und beim Mehrfach-Upload („Meine Karten“) ohne Dialog.
|
|
// Titel = Name der Karte. Ist die Karte schon veröffentlicht, wird die alte Veröffentlichung zurückgezogen und die neue Fassung
|
|
// veröffentlicht, damit die Galerie immer den gespeicherten Stand zeigt. Fehlt der Benutzername, fragt der Namensdialog einmal nach.
|
|
import type { SavedCard } from '../account/cards';
|
|
import { requireUserId } from '../account/cards';
|
|
import { usernameDialog } from '../account/username-dialog';
|
|
import { withLogin } from '../account/with-login';
|
|
import { isAlreadyPublished, isUsernameRequired, withdrawCard } from './api';
|
|
import { myPublications } from './gallery';
|
|
import { publishSavedCard, type PublishForm } from './publish';
|
|
|
|
const TITLE_MAX = 60;
|
|
|
|
export function autoPublishForm(name: string): PublishForm {
|
|
const title = name.trim().slice(0, TITLE_MAX).trim();
|
|
return { title: title === '' ? 'AirCard' : title, issuer: '', tags: '' };
|
|
}
|
|
|
|
export async function autoPublish(card: SavedCard, signal?: AbortSignal): Promise<{ id: string }> {
|
|
const form = autoPublishForm(card.name);
|
|
const publish = () => withLogin(() => publishSavedCard(card, form));
|
|
let askedName = false;
|
|
let replaced = false;
|
|
for (;;) {
|
|
try {
|
|
return await publish();
|
|
} catch (err) {
|
|
if (isUsernameRequired(err) && !askedName) {
|
|
askedName = true;
|
|
if ((await usernameDialog({ mode: 'required', signal })) === null) throw err;
|
|
continue;
|
|
}
|
|
if (isAlreadyPublished(err) && !replaced) {
|
|
replaced = true;
|
|
const old = (await myPublications(requireUserId())).get(card.id);
|
|
if (!old) throw err;
|
|
await withLogin(() => withdrawCard(old.id));
|
|
continue;
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
}
|