Skip to content

Commit f9d06ae

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(sidebar): scope channel sections storage to relay URL (#1477)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 453b8b1 commit f9d06ae

6 files changed

Lines changed: 460 additions & 47 deletions

File tree

‎desktop/src/features/sidebar/lib/channelSectionsStorage.test.mjs‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
stripOrphanedAssignments,
1010
writeChannelSectionsStore,
1111
} from "./channelSectionsStorage.ts";
12+
import { normalizeRelayUrl } from "@/features/profile/lib/selfProfileStorage";
1213

1314
if (typeof globalThis.window === "undefined") {
1415
const storage = new Map();
@@ -202,3 +203,125 @@ test("writeChannelSectionsStore: returns false when setItem throws", () => {
202203
test("storageKey: returns expected format with pubkey", () => {
203204
assert.equal(storageKey("abc123"), "buzz-channel-sections.v1:abc123");
204205
});
206+
207+
// ─── Relay-scoped key tests ───────────────────────────────────────────────────
208+
209+
test("storageKey: with relayUrl includes normalized+encoded relay in key", () => {
210+
const relay = "wss://relay.example.com";
211+
const key = storageKey("pk1", relay);
212+
assert.equal(
213+
key,
214+
`buzz-channel-sections.v1:pk1:${encodeURIComponent(normalizeRelayUrl(relay))}`,
215+
);
216+
});
217+
218+
test("storageKey: without relayUrl returns legacy pubkey-only key", () => {
219+
assert.equal(storageKey("pk1"), "buzz-channel-sections.v1:pk1");
220+
assert.equal(storageKey("pk1", undefined), "buzz-channel-sections.v1:pk1");
221+
});
222+
223+
test("storageKey: two different relays produce different keys for same pubkey", () => {
224+
const k1 = storageKey("pk1", "wss://relay-a.example.com");
225+
const k2 = storageKey("pk1", "wss://relay-b.example.com");
226+
assert.notEqual(k1, k2);
227+
});
228+
229+
test("storageKey: equivalent relay URLs (case + trailing slash) map to the same key", () => {
230+
const k1 = storageKey("pk1", "WSS://Relay.Example/");
231+
const k2 = storageKey("pk1", "wss://relay.example");
232+
assert.equal(k1, k2);
233+
});
234+
235+
test("readChannelSectionsStore + writeChannelSectionsStore: scoped write/read roundtrip", () => {
236+
const pubkey = "pk-relay-roundtrip";
237+
const relay = "wss://relay.example.com";
238+
const store = makeStore({
239+
sections: [makeSection({ id: "s1", name: "Work", order: 0 })],
240+
assignments: { chan1: "s1" },
241+
});
242+
assert.equal(writeChannelSectionsStore(pubkey, store, relay), true);
243+
const result = readChannelSectionsStore(pubkey, relay);
244+
assert.deepEqual(result, store);
245+
});
246+
247+
test("readChannelSectionsStore: scoped key is isolated from other relay's data", () => {
248+
const pubkey = "pk-isolation";
249+
const relayA = "wss://relay-a.example.com";
250+
const relayB = "wss://relay-b.example.com";
251+
const storeA = makeStore({
252+
sections: [makeSection({ id: "sa", name: "Relay A section", order: 0 })],
253+
assignments: {},
254+
});
255+
writeChannelSectionsStore(pubkey, storeA, relayA);
256+
// Relay B should see empty store, not relay A's data.
257+
const resultB = readChannelSectionsStore(pubkey, relayB);
258+
assert.deepEqual(resultB, DEFAULT_STORE);
259+
});
260+
261+
test("readChannelSectionsStore: migrates legacy unscoped data on first scoped read", () => {
262+
const pubkey = "pk-migrate";
263+
const relay = "wss://relay-migrate.example.com";
264+
const legacyStore = makeStore({
265+
sections: [makeSection({ id: "sl", name: "Legacy Section", order: 0 })],
266+
assignments: {},
267+
});
268+
// Write under legacy (pubkey-only) key.
269+
writeChannelSectionsStore(pubkey, legacyStore);
270+
// First scoped read should migrate and return the legacy data.
271+
const result = readChannelSectionsStore(pubkey, relay);
272+
assert.deepEqual(result, legacyStore);
273+
// Legacy key must be deleted after migration (globally one-time guarantee).
274+
const legacyKey = storageKey(pubkey);
275+
assert.equal(window.localStorage.getItem(legacyKey), null);
276+
// Subsequent scoped reads should hit the scoped key directly.
277+
const result2 = readChannelSectionsStore(pubkey, relay);
278+
assert.deepEqual(result2, legacyStore);
279+
});
280+
281+
test("readChannelSectionsStore: migration is globally one-time — relay B sees DEFAULT_STORE after relay A migrates", () => {
282+
const pubkey = "pk-migrate-once";
283+
const relayA = "wss://relay-migrate-once-a.example.com";
284+
const relayB = "wss://relay-migrate-once-b.example.com";
285+
const legacyStore = makeStore({
286+
sections: [makeSection({ id: "sm", name: "Migrated Section", order: 0 })],
287+
assignments: {},
288+
});
289+
// Write under legacy key then migrate via relay A.
290+
writeChannelSectionsStore(pubkey, legacyStore);
291+
readChannelSectionsStore(pubkey, relayA); // triggers migration, deletes legacy key
292+
// Relay B must see DEFAULT_STORE — legacy data must not bleed in.
293+
const resultB = readChannelSectionsStore(pubkey, relayB);
294+
assert.deepEqual(resultB, DEFAULT_STORE);
295+
// Relay B scoped key must not have been created.
296+
const scopedBKey = storageKey(pubkey, relayB);
297+
assert.equal(window.localStorage.getItem(scopedBKey), null);
298+
});
299+
300+
test("readChannelSectionsStore: migration only copies non-empty legacy stores", () => {
301+
const pubkey = "pk-migrate-empty";
302+
const relay = "wss://relay-migrate-empty.example.com";
303+
// Write an explicitly empty store under the legacy key.
304+
writeChannelSectionsStore(pubkey, DEFAULT_STORE);
305+
// Empty legacy store should NOT be migrated — we get DEFAULT_STORE.
306+
const result = readChannelSectionsStore(pubkey, relay);
307+
assert.deepEqual(result, DEFAULT_STORE);
308+
});
309+
310+
test("readChannelSectionsStore: scoped key takes precedence over legacy key after migration", () => {
311+
const pubkey = "pk-precedence";
312+
const relay = "wss://relay-precedence.example.com";
313+
const legacyStore = makeStore({
314+
sections: [makeSection({ id: "sold", name: "Old Section", order: 0 })],
315+
assignments: {},
316+
});
317+
const newStore = makeStore({
318+
sections: [makeSection({ id: "snew", name: "New Section", order: 0 })],
319+
assignments: {},
320+
});
321+
// Write legacy data then scoped data (simulates post-migration state).
322+
writeChannelSectionsStore(pubkey, legacyStore);
323+
writeChannelSectionsStore(pubkey, newStore, relay);
324+
// Scoped read must return the scoped (newer) store, not the legacy one.
325+
const result = readChannelSectionsStore(pubkey, relay);
326+
assert.deepEqual(result, newStore);
327+
});

‎desktop/src/features/sidebar/lib/channelSectionsStorage.ts‎

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { normalizeRelayUrl } from "@/features/profile/lib/selfProfileStorage";
2+
13
const STORAGE_KEY_PREFIX = "buzz-channel-sections.v1";
24

35
export type ChannelSection = {
@@ -18,8 +20,20 @@ export const DEFAULT_STORE: ChannelSectionStore = Object.freeze({
1820
assignments: {},
1921
});
2022

21-
export function storageKey(pubkey: string): string {
22-
return `${STORAGE_KEY_PREFIX}:${pubkey}`;
23+
/**
24+
* Returns the localStorage key for channel sections.
25+
*
26+
* When `relayUrl` is provided the key is scoped to that relay (normalized via
27+
* the same `normalizeRelayUrl` used by all relay-scoped local stores) so
28+
* sections from different workspaces/relays don't bleed across each other.
29+
* When omitted the legacy pubkey-only key is returned (used only during
30+
* one-time migration in `readChannelSectionsStore`).
31+
*/
32+
export function storageKey(pubkey: string, relayUrl?: string): string {
33+
if (!relayUrl) return `${STORAGE_KEY_PREFIX}:${pubkey}`;
34+
const normalized = normalizeRelayUrl(relayUrl);
35+
// Encode the normalized relay so it can't contain the `:` delimiter.
36+
return `${STORAGE_KEY_PREFIX}:${pubkey}:${encodeURIComponent(normalized)}`;
2337
}
2438

2539
export function stripOrphanedAssignments(
@@ -62,17 +76,62 @@ export function parseChannelSectionPayload(
6276
return stripOrphanedAssignments({ version: 1, sections, assignments });
6377
}
6478

65-
export function readChannelSectionsStore(pubkey: string): ChannelSectionStore {
79+
function parseRaw(raw: string | null): ChannelSectionStore | null {
80+
if (!raw) return null;
6681
try {
67-
const raw = window.localStorage.getItem(storageKey(pubkey));
68-
if (!raw) {
69-
return DEFAULT_STORE;
70-
}
7182
const parsed = JSON.parse(raw);
7283
if (typeof parsed !== "object" || parsed === null || parsed.version !== 1) {
73-
return DEFAULT_STORE;
84+
return null;
7485
}
75-
return parseChannelSectionPayload(parsed) ?? DEFAULT_STORE;
86+
return parseChannelSectionPayload(parsed);
87+
} catch {
88+
return null;
89+
}
90+
}
91+
92+
/**
93+
* Read the section store for `pubkey` scoped to `relayUrl`.
94+
*
95+
* On first access for a scoped key, migrates any existing data from the
96+
* legacy pubkey-only key so users don't lose their sections on upgrade.
97+
* After a successful migration write the legacy key is deleted, making
98+
* this a globally one-time operation — subsequent empty scoped-key reads
99+
* (i.e. a different relay) won't see legacy data and won't trigger
100+
* cross-relay contamination via the seed-publish path.
101+
*/
102+
export function readChannelSectionsStore(
103+
pubkey: string,
104+
relayUrl?: string,
105+
): ChannelSectionStore {
106+
try {
107+
const key = storageKey(pubkey, relayUrl);
108+
const raw = window.localStorage.getItem(key);
109+
110+
// Scoped key already has data — use it directly.
111+
if (raw !== null) {
112+
return parseRaw(raw) ?? DEFAULT_STORE;
113+
}
114+
115+
// No scoped data yet. If we were given a relay scope, attempt a
116+
// one-time migration from the legacy pubkey-only key.
117+
if (relayUrl) {
118+
const legacyKey = storageKey(pubkey);
119+
const legacyRaw = window.localStorage.getItem(legacyKey);
120+
const migrated = parseRaw(legacyRaw);
121+
if (migrated && migrated.sections.length > 0) {
122+
// Persist under the scoped key and remove the legacy key so this
123+
// migration cannot fire again for any other relay scope.
124+
try {
125+
window.localStorage.setItem(key, JSON.stringify(migrated));
126+
window.localStorage.removeItem(legacyKey);
127+
} catch {
128+
// Ignore write failures — we still return the migrated value.
129+
}
130+
return migrated;
131+
}
132+
}
133+
134+
return DEFAULT_STORE;
76135
} catch {
77136
return DEFAULT_STORE;
78137
}
@@ -81,9 +140,13 @@ export function readChannelSectionsStore(pubkey: string): ChannelSectionStore {
81140
export function writeChannelSectionsStore(
82141
pubkey: string,
83142
store: ChannelSectionStore,
143+
relayUrl?: string,
84144
): boolean {
85145
try {
86-
window.localStorage.setItem(storageKey(pubkey), JSON.stringify(store));
146+
window.localStorage.setItem(
147+
storageKey(pubkey, relayUrl),
148+
JSON.stringify(store),
149+
);
87150
return true;
88151
} catch {
89152
return false;

0 commit comments

Comments
 (0)