moderator-toolbox-nxg-for-reddit / modules/subredditnotes/moduleapi

modules/subredditnotes/moduleapi

Functions

loadNoteIndex()

loadNoteIndex(subreddit): Promise<{ bootstrapped: boolean; index: SubredditNoteIndex; }>

Defined in: extension/data/modules/subredditnotes/moduleapi.ts:29

Reads the note index for a subreddit, normalizing and migrating as needed. If the stored index is absent or malformed, builds one from existing wiki pages. With 6.x compatibility on, the legacy index is union-merged in so notes created from 6.x show up - the merge sets bootstrapped, and the caller’s persist writes both pages, healing the divergence. The bootstrapped flag is true when the caller should persist the index.

Parameters

subreddit

string

Returns

Promise<{ bootstrapped: boolean; index: SubredditNoteIndex; }>


readNotePage()

readNotePage(subreddit, slug): Promise<{ ok: false; reason: "no_page" | "unknown_error" | "invalid_json"; } | { data: string; ok: true; } | { ok: false; reason: "no_page"; }>

Defined in: extension/data/modules/subredditnotes/moduleapi.ts:183

Reads a single note page from the wiki as raw markdown. Returns a discriminated union - check .ok before using .data.

With 6.x compatibility on, the legacy page is consulted too. When the two sides diverge, the newer revision wins: a newer legacy page means a 6.x edit (it’s adopted and immediately copied to the NXG page), while a newer NXG page means a previous mirror write failed (the next save heals it). Newer-wins, rather than blind legacy-wins, because under NXG-first writes a partial failure leaves the NXG side ahead.

Parameters

subreddit

string

slug

string

Returns

Promise<{ ok: false; reason: "no_page" | "unknown_error" | "invalid_json"; } | { data: string; ok: true; } | { ok: false; reason: "no_page"; }>


updateNoteIndex()

updateNoteIndex(subreddit, reason, apply): Promise<SubredditNoteIndex | null>

Defined in: extension/data/modules/subredditnotes/moduleapi.ts:144

Serialized read-merge-write for a subreddit’s note index: re-reads the current index from the wiki inside a per-subreddit queue, hands its note list to apply, then writes the result.

The index is a single shared page, so rewriting it from a snapshot captured when the popup opened silently drops any note a concurrent editor added since. By re-loading the live index (which also folds in the 6.x legacy mirror and rebuilds from wiki pages when the page is missing) and applying only the caller’s change to it, those notes survive. apply must therefore express the change against whatever list it receives - typically upserting a note meta by slug - rather than assume it matches what the UI last showed.

Parameters

subreddit

string

The subreddit whose index to update.

reason

string

Wiki revision reason for the write.

apply

(notes) => SubredditNoteMeta[] | null

Transforms the freshly-read note list, returning the new list or null to abort the write.

Returns

Promise<SubredditNoteIndex | null>

The merged index that was written, or null when apply aborted.


writeNoteIndex()

writeNoteIndex(subreddit, index, reason): Promise<void>

Defined in: extension/data/modules/subredditnotes/moduleapi.ts:105

Writes the note index for a subreddit to the wiki, fanning out to the legacy mirror after the canonical NXG write when 6.x compatibility is on. Each destination gets its own schema: the NXG page is written as v2 (with freshly recomputed tag and author aggregates), while the legacy page stays in the v1 shape older toolbox builds expect.

Parameters

subreddit

string

Target subreddit.

index

SubredditNoteIndex

Index to persist.

reason

string

Edit reason shown in the wiki revision history.

Returns

Promise<void>


writeNotePage()

writeNotePage(subreddit, slug, content, reason): Promise<void>

Defined in: extension/data/modules/subredditnotes/moduleapi.ts:232

Writes a single note page to the wiki, fanning out to the legacy mirror after the canonical NXG write when 6.x compatibility is on.

Parameters

subreddit

string

Target subreddit.

slug

string

Note slug (page name suffix after the notes prefix).

content

string

Markdown content to write.

reason

string

Edit reason shown in the wiki revision history.

Returns

Promise<void>