moderator-toolbox-nxg-for-reddit / modules/shared/usernotes/noteMutations

modules/shared/usernotes/noteMutations

Functions

applyUserNoteMutation()

applyUserNoteMutation(notes, user, mutation): string | undefined

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:163

Applies a mutation to the in-memory usernotes data, mutating it in place. Also consolidates notes stored under both a lowercase and mixed-case username key (reindexing collision-free via mergeDualKeyNotes).

Parameters

notes

UserNotesData

user

string

mutation

UserNoteMutation

Returns

string | undefined

A wiki revision message describing the change, or undefined if the mutation could not be applied (e.g. the target index does not exist).


makeUserNoteEntry()

makeUserNoteEntry(fields): UserNoteEntry

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:56

Builds a stored usernote entry from resolved field values, applying the conventions every note-creation path must agree on so they cannot drift apart: the note text is trimmed; time defaults to the current time in epoch seconds (the wiki format and all readers expect seconds, never the milliseconds Date.now() returns); and the optional type/link/ messageLink fields are omitted entirely when unset rather than stored as empty values. An absent link round-trips identically to '' through the codec, so omitting it is wire-compatible with notes that stored an empty link.

Parameters

fields

NewUserNoteFields

The note’s resolved field values.

Returns

UserNoteEntry

A UserNoteEntry ready to hand to an add mutation.


mergeDualKeyNotes()

mergeDualKeyNotes(lowercaseNotes, canonical): object

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:79

Merges the note lists of a user stored under both a lowercase and a canonical-cased key. Each storage key has its own index space, so the merged record must reindex one side to stay collision-free: the canonical side keeps its indexes and the lowercase side’s are offset past them. The same rule is applied by the read path (getUser), so indexes shown in the UI match the ones a subsequent mutation consolidates to.

Parameters

lowercaseNotes

UserNoteEntry[]

Notes stored under the lowercase key (listed first).

canonical

UsernotesUser

The user record stored under the canonical-cased key.

Returns

object

nextIndex

nextIndex: number

notes

notes: UserNoteEntry[]


resolveDualKeyUser()

resolveDualKeyUser(users, name, opts): FoundUser | undefined

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:120

Resolves a user that may be stored under both a lowercase and a canonical-cased key, returning a deep-cloned record ready to read or mutate. When both keys hold the user their notes are merged collision-free via mergeDualKeyNotes; when only one does, that record is returned with nonCanonicalName set if it came from a legacy lowercase key (so the caller can drop that key on write-back).

The read path and the mutation path differ only in two policies, expressed as options: the read path drops note-less users and sorts newest-first for display, while the mutation path keeps a note-less record (its nextIndex continues the add index sequence) and preserves order.

Parameters

users

Record<string, UsernotesUser>

The notes map, keyed by username (lowercase and/or canonical case).

name

string

The username being looked up, in the caller’s casing.

opts
keepEmptyNotes

boolean

Keep a single-key record that has zero notes.

sort

boolean

Sort a single-key record’s notes newest-first.

Returns

FoundUser | undefined

Interfaces

FoundUser

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:99

A deep-copied user record merged across dual storage keys, ready to mutate.

Extends

Properties

name

name: string

Defined in: extension/data/util/wiki/schemas/usernotes/schema.ts:123

Inherited from

UsernotesUser.name

nextIndex?

optional nextIndex?: number

Defined in: extension/data/util/wiki/schemas/usernotes/schema.ts:129

The next note index to assign for this user. Persisted (not derived) so indexes are never reused, even after notes are deleted or pruned.

Inherited from

UsernotesUser.nextIndex

nonCanonicalName?

optional nonCanonicalName?: string

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:101

The legacy lowercase key that should be removed from storage when writing back.

notes

notes: UserNoteEntry[]

Defined in: extension/data/util/wiki/schemas/usernotes/schema.ts:124

Inherited from

UsernotesUser.notes


NewUserNoteFields

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:29

Resolved field values for a new usernote, before the shared conventions are applied.

Properties

mod

mod: string

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:33

Username of the moderator creating the note.

note

note: string

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:31

Raw note text; trimmed by makeUserNoteEntry.

time?

optional time?: number

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:35

Creation time in epoch seconds; defaults to now when omitted.

type?

optional type?: string

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:37

Note type key, or undefined for no type.

Type Aliases

UserNoteMutation

UserNoteMutation = { change: "add"; note: UserNoteEntry; } | { change: "delete"; index: number; } | { change: "edit"; index: number; note: { note: string; type: string | undefined; }; } | { by: string; change: "archive"; index: number; } | { change: "unarchive"; index: number; }

Defined in: extension/data/modules/shared/usernotes/noteMutations.ts:13

A discriminated union describing a single change to a user’s note list.

Union Members

Type Literal

{ change: "add"; note: UserNoteEntry; }

Creates a note; its index is assigned from the user’s nextIndex.


Type Literal

{ change: "delete"; index: number; }

Removes a note. Its index is never reused (nextIndex survives), and a user emptied of notes keeps their record so the index stays stable.


Type Literal

{ change: "edit"; index: number; note: { note: string; type: string | undefined; }; }

Updates a note’s text/type in place.


Type Literal

{ by: string; change: "archive"; index: number; }

Hides a note without deleting it, with attribution.


Type Literal

{ change: "unarchive"; index: number; }

Clears a note’s archived state.