|
1 | 1 | // Nostr plugin module implements nostr state store behavior. |
2 | 2 | import { getNostrRuntime } from "./runtime.js"; |
| 3 | +import { normalizeNostrStateAccountId } from "./state-account-id.js"; |
3 | 4 | |
4 | 5 | const STORE_VERSION = 2; |
5 | 6 | const PROFILE_STATE_VERSION = 1; |
@@ -25,14 +26,6 @@ type NostrProfileState = {
|
25 | 26 | lastPublishResults: Record<string, "ok" | "failed" | "timeout"> | null; |
26 | 27 | }; |
27 | 28 | |
28 | | -function normalizeAccountId(accountId?: string): string { |
29 | | -const trimmed = accountId?.trim(); |
30 | | -if (!trimmed) { |
31 | | -return "default"; |
32 | | -} |
33 | | -return trimmed.replace(/[^a-z0-9._-]+/gi, "_"); |
34 | | -} |
35 | | - |
36 | 29 | function openNostrBusStateStore(env?: NodeJS.ProcessEnv) { |
37 | 30 | return getNostrRuntime().state.openKeyedStore<NostrBusState>({ |
38 | 31 | namespace: "bus-state", |
@@ -54,7 +47,9 @@ export async function readNostrBusState(params: {
|
54 | 47 | env?: NodeJS.ProcessEnv; |
55 | 48 | }): Promise<NostrBusState | null> { |
56 | 49 | return ( |
57 | | -(await openNostrBusStateStore(params.env).lookup(normalizeAccountId(params.accountId))) ?? null |
| 50 | +(await openNostrBusStateStore(params.env).lookup( |
| 51 | +normalizeNostrStateAccountId(params.accountId), |
| 52 | +)) ?? null |
58 | 53 | ); |
59 | 54 | } |
60 | 55 | |
@@ -71,7 +66,10 @@ export async function writeNostrBusState(params: {
|
71 | 66 | gatewayStartedAt: params.gatewayStartedAt, |
72 | 67 | recentEventIds: (params.recentEventIds ?? []).filter((x): x is string => typeof x === "string"), |
73 | 68 | }; |
74 | | -await openNostrBusStateStore(params.env).register(normalizeAccountId(params.accountId), payload); |
| 69 | +await openNostrBusStateStore(params.env).register( |
| 70 | +normalizeNostrStateAccountId(params.accountId), |
| 71 | +payload, |
| 72 | +); |
75 | 73 | } |
76 | 74 | |
77 | 75 | /** |
@@ -107,8 +105,9 @@ export async function readNostrProfileState(params: {
|
107 | 105 | env?: NodeJS.ProcessEnv; |
108 | 106 | }): Promise<NostrProfileState | null> { |
109 | 107 | return ( |
110 | | -(await openNostrProfileStateStore(params.env).lookup(normalizeAccountId(params.accountId))) ?? |
111 | | -null |
| 108 | +(await openNostrProfileStateStore(params.env).lookup( |
| 109 | +normalizeNostrStateAccountId(params.accountId), |
| 110 | +)) ?? null |
112 | 111 | ); |
113 | 112 | } |
114 | 113 | |
@@ -126,7 +125,7 @@ export async function writeNostrProfileState(params: {
|
126 | 125 | lastPublishResults: params.lastPublishResults, |
127 | 126 | }; |
128 | 127 | await openNostrProfileStateStore(params.env).register( |
129 | | -normalizeAccountId(params.accountId), |
| 128 | +normalizeNostrStateAccountId(params.accountId), |
130 | 129 | payload, |
131 | 130 | ); |
132 | 131 | } |