refactor: share nextcloud target normalization

This commit is contained in:
Peter Steinberger 2026-03-13 16:33:09 +00:00
parent ef8cc3d0fb
commit 6b07604d64
3 changed files with 37 additions and 18 deletions

View File

@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import {
looksLikeNextcloudTalkTargetId,
normalizeNextcloudTalkMessagingTarget,
stripNextcloudTalkTargetPrefix,
} from "./normalize.js";
describe("nextcloud-talk target normalization", () => {
it("strips supported prefixes to a room token", () => {
expect(stripNextcloudTalkTargetPrefix(" room:abc123 ")).toBe("abc123");
expect(stripNextcloudTalkTargetPrefix("nextcloud-talk:room:AbC123")).toBe("AbC123");
expect(stripNextcloudTalkTargetPrefix("nc-talk:room:ops")).toBe("ops");
expect(stripNextcloudTalkTargetPrefix("nc:room:ops")).toBe("ops");
expect(stripNextcloudTalkTargetPrefix("room: ")).toBeUndefined();
});
it("normalizes messaging targets to lowercase channel ids", () => {
expect(normalizeNextcloudTalkMessagingTarget("room:AbC123")).toBe("nextcloud-talk:abc123");
expect(normalizeNextcloudTalkMessagingTarget("nc-talk:room:Ops")).toBe("nextcloud-talk:ops");
});
it("detects prefixed and bare room ids", () => {
expect(looksLikeNextcloudTalkTargetId("nextcloud-talk:room:abc12345")).toBe(true);
expect(looksLikeNextcloudTalkTargetId("nc:opsroom1")).toBe(true);
expect(looksLikeNextcloudTalkTargetId("abc12345")).toBe(true);
expect(looksLikeNextcloudTalkTargetId("")).toBe(false);
});
});

View File

@ -1,4 +1,4 @@
export function normalizeNextcloudTalkMessagingTarget(raw: string): string | undefined {
export function stripNextcloudTalkTargetPrefix(raw: string): string | undefined {
const trimmed = raw.trim();
if (!trimmed) {
return undefined;
@ -22,7 +22,12 @@ export function normalizeNextcloudTalkMessagingTarget(raw: string): string | und
return undefined;
}
return `nextcloud-talk:${normalized}`.toLowerCase();
return normalized;
}
export function normalizeNextcloudTalkMessagingTarget(raw: string): string | undefined {
const normalized = stripNextcloudTalkTargetPrefix(raw);
return normalized ? `nextcloud-talk:${normalized}`.toLowerCase() : undefined;
}
export function looksLikeNextcloudTalkTargetId(raw: string): boolean {

View File

@ -1,4 +1,5 @@
import { resolveNextcloudTalkAccount } from "./accounts.js";
import { stripNextcloudTalkTargetPrefix } from "./normalize.js";
import { getNextcloudTalkRuntime } from "./runtime.js";
import { generateNextcloudTalkSignature } from "./signature.js";
import type { CoreConfig, NextcloudTalkSendResult } from "./types.js";
@ -34,22 +35,7 @@ function resolveCredentials(
}
function normalizeRoomToken(to: string): string {
const trimmed = to.trim();
if (!trimmed) {
throw new Error("Room token is required for Nextcloud Talk sends");
}
let normalized = trimmed;
if (normalized.startsWith("nextcloud-talk:")) {
normalized = normalized.slice("nextcloud-talk:".length).trim();
} else if (normalized.startsWith("nc:")) {
normalized = normalized.slice("nc:".length).trim();
}
if (normalized.startsWith("room:")) {
normalized = normalized.slice("room:".length).trim();
}
const normalized = stripNextcloudTalkTargetPrefix(to);
if (!normalized) {
throw new Error("Room token is required for Nextcloud Talk sends");
}