Tlon plugin: handle channelRules as JSON string
Settings-store doesn't support nested objects as values, so channelRules is stored as a JSON string and parsed on read.
This commit is contained in:
parent
5d30ee2232
commit
9cf27393bf
@ -31,6 +31,31 @@ export type TlonSettingsState = {
|
|||||||
const SETTINGS_DESK = "moltbot";
|
const SETTINGS_DESK = "moltbot";
|
||||||
const SETTINGS_BUCKET = "tlon";
|
const SETTINGS_BUCKET = "tlon";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse channelRules - handles both JSON string and object formats.
|
||||||
|
* Settings-store doesn't support nested objects, so we store as JSON string.
|
||||||
|
*/
|
||||||
|
function parseChannelRules(
|
||||||
|
value: unknown
|
||||||
|
): Record<string, { mode?: "restricted" | "open"; allowedShips?: string[] }> | undefined {
|
||||||
|
if (!value) return undefined;
|
||||||
|
|
||||||
|
// If it's a string, try to parse as JSON
|
||||||
|
if (typeof value === "string") {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(value);
|
||||||
|
if (isChannelRulesObject(parsed)) return parsed;
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's already an object, use directly
|
||||||
|
if (isChannelRulesObject(value)) return value;
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse settings from the raw Urbit settings-store response.
|
* Parse settings from the raw Urbit settings-store response.
|
||||||
* The response shape is: { [bucket]: { [key]: value } }
|
* The response shape is: { [bucket]: { [key]: value } }
|
||||||
@ -57,9 +82,7 @@ function parseSettingsResponse(raw: unknown): TlonSettingsStore {
|
|||||||
showModelSig: typeof settings.showModelSig === "boolean"
|
showModelSig: typeof settings.showModelSig === "boolean"
|
||||||
? settings.showModelSig
|
? settings.showModelSig
|
||||||
: undefined,
|
: undefined,
|
||||||
channelRules: isChannelRulesObject(settings.channelRules)
|
channelRules: parseChannelRules(settings.channelRules),
|
||||||
? settings.channelRules
|
|
||||||
: undefined,
|
|
||||||
defaultAuthorizedShips: Array.isArray(settings.defaultAuthorizedShips)
|
defaultAuthorizedShips: Array.isArray(settings.defaultAuthorizedShips)
|
||||||
? settings.defaultAuthorizedShips.filter((x): x is string => typeof x === "string")
|
? settings.defaultAuthorizedShips.filter((x): x is string => typeof x === "string")
|
||||||
: undefined,
|
: undefined,
|
||||||
@ -139,7 +162,7 @@ function applySettingsUpdate(
|
|||||||
next.showModelSig = typeof value === "boolean" ? value : undefined;
|
next.showModelSig = typeof value === "boolean" ? value : undefined;
|
||||||
break;
|
break;
|
||||||
case "channelRules":
|
case "channelRules":
|
||||||
next.channelRules = isChannelRulesObject(value) ? value : undefined;
|
next.channelRules = parseChannelRules(value);
|
||||||
break;
|
break;
|
||||||
case "defaultAuthorizedShips":
|
case "defaultAuthorizedShips":
|
||||||
next.defaultAuthorizedShips = Array.isArray(value)
|
next.defaultAuthorizedShips = Array.isArray(value)
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
b6d3dea7c656c8a480059c32e954c4d39053ff79c4e9c69b38f4c04e3f0280d4
|
bd5789522e6bde45274e15fdd45b10c9a41da378b190d6f42cef5ef2a69d72a7
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user