refactor(channels): dedupe status account bits

This commit is contained in:
Peter Steinberger
2026-02-16 01:47:52 +00:00
parent 22c1210a16
commit 31d1ed351f

View File

@@ -18,21 +18,49 @@ export type ChannelsStatusOptions = {
timeout?: string; timeout?: string;
}; };
function appendEnabledConfiguredLinkedBits(bits: string[], account: Record<string, unknown>) {
if (typeof account.enabled === "boolean") {
bits.push(account.enabled ? "enabled" : "disabled");
}
if (typeof account.configured === "boolean") {
bits.push(account.configured ? "configured" : "not configured");
}
if (typeof account.linked === "boolean") {
bits.push(account.linked ? "linked" : "not linked");
}
}
function appendModeBit(bits: string[], account: Record<string, unknown>) {
if (typeof account.mode === "string" && account.mode.length > 0) {
bits.push(`mode:${account.mode}`);
}
}
function appendTokenSourceBits(bits: string[], account: Record<string, unknown>) {
if (typeof account.tokenSource === "string" && account.tokenSource) {
bits.push(`token:${account.tokenSource}`);
}
if (typeof account.botTokenSource === "string" && account.botTokenSource) {
bits.push(`bot:${account.botTokenSource}`);
}
if (typeof account.appTokenSource === "string" && account.appTokenSource) {
bits.push(`app:${account.appTokenSource}`);
}
}
function appendBaseUrlBit(bits: string[], account: Record<string, unknown>) {
if (typeof account.baseUrl === "string" && account.baseUrl) {
bits.push(`url:${account.baseUrl}`);
}
}
export function formatGatewayChannelsStatusLines(payload: Record<string, unknown>): string[] { export function formatGatewayChannelsStatusLines(payload: Record<string, unknown>): string[] {
const lines: string[] = []; const lines: string[] = [];
lines.push(theme.success("Gateway reachable.")); lines.push(theme.success("Gateway reachable."));
const accountLines = (provider: ChatChannel, accounts: Array<Record<string, unknown>>) => const accountLines = (provider: ChatChannel, accounts: Array<Record<string, unknown>>) =>
accounts.map((account) => { accounts.map((account) => {
const bits: string[] = []; const bits: string[] = [];
if (typeof account.enabled === "boolean") { appendEnabledConfiguredLinkedBits(bits, account);
bits.push(account.enabled ? "enabled" : "disabled");
}
if (typeof account.configured === "boolean") {
bits.push(account.configured ? "configured" : "not configured");
}
if (typeof account.linked === "boolean") {
bits.push(account.linked ? "linked" : "not linked");
}
if (typeof account.running === "boolean") { if (typeof account.running === "boolean") {
bits.push(account.running ? "running" : "stopped"); bits.push(account.running ? "running" : "stopped");
} }
@@ -53,9 +81,7 @@ export function formatGatewayChannelsStatusLines(payload: Record<string, unknown
if (outboundAt) { if (outboundAt) {
bits.push(`out:${formatTimeAgo(Date.now() - outboundAt)}`); bits.push(`out:${formatTimeAgo(Date.now() - outboundAt)}`);
} }
if (typeof account.mode === "string" && account.mode.length > 0) { appendModeBit(bits, account);
bits.push(`mode:${account.mode}`);
}
const botUsername = (() => { const botUsername = (() => {
const bot = account.bot as { username?: string | null } | undefined; const bot = account.bot as { username?: string | null } | undefined;
const probeBot = (account.probe as { bot?: { username?: string | null } } | undefined)?.bot; const probeBot = (account.probe as { bot?: { username?: string | null } } | undefined)?.bot;
@@ -78,15 +104,7 @@ export function formatGatewayChannelsStatusLines(payload: Record<string, unknown
if (Array.isArray(account.allowFrom) && account.allowFrom.length > 0) { if (Array.isArray(account.allowFrom) && account.allowFrom.length > 0) {
bits.push(`allow:${account.allowFrom.slice(0, 2).join(",")}`); bits.push(`allow:${account.allowFrom.slice(0, 2).join(",")}`);
} }
if (typeof account.tokenSource === "string" && account.tokenSource) { appendTokenSourceBits(bits, account);
bits.push(`token:${account.tokenSource}`);
}
if (typeof account.botTokenSource === "string" && account.botTokenSource) {
bits.push(`bot:${account.botTokenSource}`);
}
if (typeof account.appTokenSource === "string" && account.appTokenSource) {
bits.push(`app:${account.appTokenSource}`);
}
const application = account.application as const application = account.application as
| { intents?: { messageContent?: string } } | { intents?: { messageContent?: string } }
| undefined; | undefined;
@@ -101,9 +119,7 @@ export function formatGatewayChannelsStatusLines(payload: Record<string, unknown
if (account.allowUnmentionedGroups === true) { if (account.allowUnmentionedGroups === true) {
bits.push("groups:unmentioned"); bits.push("groups:unmentioned");
} }
if (typeof account.baseUrl === "string" && account.baseUrl) { appendBaseUrlBit(bits, account);
bits.push(`url:${account.baseUrl}`);
}
const probe = account.probe as { ok?: boolean } | undefined; const probe = account.probe as { ok?: boolean } | undefined;
if (probe && typeof probe.ok === "boolean") { if (probe && typeof probe.ok === "boolean") {
bits.push(probe.ok ? "works" : "probe failed"); bits.push(probe.ok ? "works" : "probe failed");
@@ -179,30 +195,10 @@ async function formatConfigChannelsStatusLines(
const accountLines = (provider: ChatChannel, accounts: Array<Record<string, unknown>>) => const accountLines = (provider: ChatChannel, accounts: Array<Record<string, unknown>>) =>
accounts.map((account) => { accounts.map((account) => {
const bits: string[] = []; const bits: string[] = [];
if (typeof account.enabled === "boolean") { appendEnabledConfiguredLinkedBits(bits, account);
bits.push(account.enabled ? "enabled" : "disabled"); appendModeBit(bits, account);
} appendTokenSourceBits(bits, account);
if (typeof account.configured === "boolean") { appendBaseUrlBit(bits, account);
bits.push(account.configured ? "configured" : "not configured");
}
if (typeof account.linked === "boolean") {
bits.push(account.linked ? "linked" : "not linked");
}
if (typeof account.mode === "string" && account.mode.length > 0) {
bits.push(`mode:${account.mode}`);
}
if (typeof account.tokenSource === "string" && account.tokenSource) {
bits.push(`token:${account.tokenSource}`);
}
if (typeof account.botTokenSource === "string" && account.botTokenSource) {
bits.push(`bot:${account.botTokenSource}`);
}
if (typeof account.appTokenSource === "string" && account.appTokenSource) {
bits.push(`app:${account.appTokenSource}`);
}
if (typeof account.baseUrl === "string" && account.baseUrl) {
bits.push(`url:${account.baseUrl}`);
}
const accountId = typeof account.accountId === "string" ? account.accountId : "default"; const accountId = typeof account.accountId === "string" ? account.accountId : "default";
const name = typeof account.name === "string" ? account.name.trim() : ""; const name = typeof account.name === "string" ? account.name.trim() : "";
const labelText = formatChannelAccountLabel({ const labelText = formatChannelAccountLabel({