refactor(channels): dedupe status account bits
This commit is contained in:
@@ -18,21 +18,49 @@ export type ChannelsStatusOptions = {
|
||||
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[] {
|
||||
const lines: string[] = [];
|
||||
lines.push(theme.success("Gateway reachable."));
|
||||
const accountLines = (provider: ChatChannel, accounts: Array<Record<string, unknown>>) =>
|
||||
accounts.map((account) => {
|
||||
const bits: string[] = [];
|
||||
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");
|
||||
}
|
||||
appendEnabledConfiguredLinkedBits(bits, account);
|
||||
if (typeof account.running === "boolean") {
|
||||
bits.push(account.running ? "running" : "stopped");
|
||||
}
|
||||
@@ -53,9 +81,7 @@ export function formatGatewayChannelsStatusLines(payload: Record<string, unknown
|
||||
if (outboundAt) {
|
||||
bits.push(`out:${formatTimeAgo(Date.now() - outboundAt)}`);
|
||||
}
|
||||
if (typeof account.mode === "string" && account.mode.length > 0) {
|
||||
bits.push(`mode:${account.mode}`);
|
||||
}
|
||||
appendModeBit(bits, account);
|
||||
const botUsername = (() => {
|
||||
const bot = account.bot as { username?: string | null } | undefined;
|
||||
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) {
|
||||
bits.push(`allow:${account.allowFrom.slice(0, 2).join(",")}`);
|
||||
}
|
||||
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}`);
|
||||
}
|
||||
appendTokenSourceBits(bits, account);
|
||||
const application = account.application as
|
||||
| { intents?: { messageContent?: string } }
|
||||
| undefined;
|
||||
@@ -101,9 +119,7 @@ export function formatGatewayChannelsStatusLines(payload: Record<string, unknown
|
||||
if (account.allowUnmentionedGroups === true) {
|
||||
bits.push("groups:unmentioned");
|
||||
}
|
||||
if (typeof account.baseUrl === "string" && account.baseUrl) {
|
||||
bits.push(`url:${account.baseUrl}`);
|
||||
}
|
||||
appendBaseUrlBit(bits, account);
|
||||
const probe = account.probe as { ok?: boolean } | undefined;
|
||||
if (probe && typeof probe.ok === "boolean") {
|
||||
bits.push(probe.ok ? "works" : "probe failed");
|
||||
@@ -179,30 +195,10 @@ async function formatConfigChannelsStatusLines(
|
||||
const accountLines = (provider: ChatChannel, accounts: Array<Record<string, unknown>>) =>
|
||||
accounts.map((account) => {
|
||||
const bits: string[] = [];
|
||||
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");
|
||||
}
|
||||
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}`);
|
||||
}
|
||||
appendEnabledConfiguredLinkedBits(bits, account);
|
||||
appendModeBit(bits, account);
|
||||
appendTokenSourceBits(bits, account);
|
||||
appendBaseUrlBit(bits, account);
|
||||
const accountId = typeof account.accountId === "string" ? account.accountId : "default";
|
||||
const name = typeof account.name === "string" ? account.name.trim() : "";
|
||||
const labelText = formatChannelAccountLabel({
|
||||
|
||||
Reference in New Issue
Block a user