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:
Hunter Miller 2026-01-29 20:26:08 -06:00
parent 5d30ee2232
commit 9cf27393bf
2 changed files with 28 additions and 5 deletions

View File

@ -31,6 +31,31 @@ export type TlonSettingsState = {
const SETTINGS_DESK = "moltbot";
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.
* The response shape is: { [bucket]: { [key]: value } }
@ -57,9 +82,7 @@ function parseSettingsResponse(raw: unknown): TlonSettingsStore {
showModelSig: typeof settings.showModelSig === "boolean"
? settings.showModelSig
: undefined,
channelRules: isChannelRulesObject(settings.channelRules)
? settings.channelRules
: undefined,
channelRules: parseChannelRules(settings.channelRules),
defaultAuthorizedShips: Array.isArray(settings.defaultAuthorizedShips)
? settings.defaultAuthorizedShips.filter((x): x is string => typeof x === "string")
: undefined,
@ -139,7 +162,7 @@ function applySettingsUpdate(
next.showModelSig = typeof value === "boolean" ? value : undefined;
break;
case "channelRules":
next.channelRules = isChannelRulesObject(value) ? value : undefined;
next.channelRules = parseChannelRules(value);
break;
case "defaultAuthorizedShips":
next.defaultAuthorizedShips = Array.isArray(value)

View File

@ -1 +1 @@
b6d3dea7c656c8a480059c32e954c4d39053ff79c4e9c69b38f4c04e3f0280d4
bd5789522e6bde45274e15fdd45b10c9a41da378b190d6f42cef5ef2a69d72a7