3dom4wf7p: Poll the feed before reporting a publish failure

A freshly published post can take a moment to surface on the home feed, so
the single immediate confirm reload reported a false failure even though the
post went through. Retry the reload a few times; once the server has accepted
the publish, treat a not-yet-visible post as success instead of an error.
This commit is contained in:
2026-10-19 15:01:17 -06:00
parent 0638c1aa03
commit 0dc9d6b7b3
2 changed files with 82 additions and 19 deletions
+49 -15
View File
@@ -54,33 +54,60 @@ function pageHasPost(page: FeedPage, id: string): boolean {
});
}
export interface ConfirmOpts {
/** How many times to reload the first page before giving up. */
attempts?: number;
/** Delay between reloads, in ms. */
delayMs?: number;
sleep?: (ms: number) => Promise<void>;
}
const defaultSleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
/**
* Reload the first page. Returns the page when it holds the post id, else null.
* Throws PostError when the person is signed out or the reload fails.
* Reload the first page, retrying a few times, because a freshly published post
* can take a moment to surface on the home feed. Returns the page as soon as it
* holds the post id, else null after all attempts. Throws PostError when the
* person is signed out or a reload fails.
*/
export async function confirmOnFirstPage(
id: string,
fetchPage: (cursor: string | null) => Promise<FeedPage> = fetchFeedPage,
opts: ConfirmOpts = {},
): Promise<FeedPage | null> {
try {
const page = await fetchPage(null);
return pageHasPost(page, id) ? page : null;
} catch (e) {
throw new PostError(e instanceof FeedLoadError && e.signedOut);
const attempts = Math.max(1, opts.attempts ?? 6);
const delayMs = opts.delayMs ?? 800;
const sleep = opts.sleep ?? defaultSleep;
for (let i = 0; i < attempts; i++) {
let page: FeedPage;
try {
page = await fetchPage(null);
} catch (e) {
throw new PostError(e instanceof FeedLoadError && e.signedOut);
}
if (pageHasPost(page, id)) return page;
if (i < attempts - 1) await sleep(delayMs);
}
return null;
}
export interface PublishDeps {
send?: (content: string, file: File | null) => Promise<string>;
fetchPage?: (cursor: string | null) => Promise<FeedPage>;
confirm?: ConfirmOpts;
}
/**
* Publish a post (text, or text plus one image file) and confirm it on the
* reloaded first page. Returns that page only when the post id is on it.
* `unconfirmed` holds an id that was created earlier but not yet seen, so a
* retry re-checks the feed and does not create a duplicate. Throws PostError
* otherwise; the caller keeps the composer text and the selected file.
* Publish a post (text, or text plus one image or video file) and confirm it on
* the reloaded first page. `unconfirmed` holds an id that was created but not yet
* confirmed, so if the reload itself fails a retry re-checks the feed instead of
* creating a duplicate.
*
* The server only returns an id once Minnect has accepted the publish (sendPost
* throws otherwise), so once we have an id the post IS published. If it has not
* surfaced on the feed after the retries, that is feed lag, not a failure: we
* clear the pending id and return the latest page — the post appears on a later
* poll — rather than showing the person a false error.
*/
export async function publishAndConfirm(
content: string,
@@ -88,10 +115,17 @@ export async function publishAndConfirm(
unconfirmed: { id: string | null },
deps: PublishDeps = {},
): Promise<FeedPage> {
const fetchPage = deps.fetchPage ?? fetchFeedPage;
const id = unconfirmed.id ?? (await (deps.send ?? sendPost)(content, file));
unconfirmed.id = id;
const page = await confirmOnFirstPage(id, deps.fetchPage);
if (!page) throw new PostError(false);
// confirmOnFirstPage throws only on a sign-out / reload failure; in that case
// unconfirmed.id stays set so a retry re-checks without creating again.
const page = await confirmOnFirstPage(id, fetchPage, deps.confirm);
unconfirmed.id = null;
return page;
if (page) return page;
try {
return await fetchPage(null);
} catch (e) {
throw new PostError(e instanceof FeedLoadError && e.signedOut);
}
}