refactor(plugins): move channel behavior into plugins

This commit is contained in:
Peter Steinberger
2026-04-03 17:54:01 +01:00
parent c52df32878
commit ab96520bba
158 changed files with 5967 additions and 5054 deletions

View File

@@ -1,3 +1,4 @@
import { getChannelPlugin, normalizeChannelId } from "../channels/plugins/index.js";
import { normalizeAccountId } from "./account-id.js";
import { normalizeMessageChannel } from "./message-channel.js";
@@ -72,19 +73,20 @@ export function formatConversationTarget(params: {
if (!channel || !conversationId) {
return undefined;
}
if (channel === "matrix") {
const parentConversationId =
typeof params.parentConversationId === "number" &&
Number.isFinite(params.parentConversationId)
? String(Math.trunc(params.parentConversationId))
: typeof params.parentConversationId === "string"
? params.parentConversationId.trim()
: undefined;
const roomId =
parentConversationId && parentConversationId !== conversationId
? parentConversationId
: conversationId;
return `room:${roomId}`;
const parentConversationId =
typeof params.parentConversationId === "number" && Number.isFinite(params.parentConversationId)
? String(Math.trunc(params.parentConversationId))
: typeof params.parentConversationId === "string"
? params.parentConversationId.trim()
: undefined;
const pluginTarget = normalizeChannelId(channel)
? getChannelPlugin(normalizeChannelId(channel)!)?.messaging?.resolveDeliveryTarget?.({
conversationId,
parentConversationId,
})
: null;
if (pluginTarget?.to?.trim()) {
return pluginTarget.to.trim();
}
return `channel:${conversationId}`;
}
@@ -94,7 +96,6 @@ export function resolveConversationDeliveryTarget(params: {
conversationId?: string | number;
parentConversationId?: string | number;
}): { to?: string; threadId?: string } {
const to = formatConversationTarget(params);
const channel =
typeof params.channel === "string"
? (normalizeMessageChannel(params.channel) ?? params.channel.trim())
@@ -111,15 +112,22 @@ export function resolveConversationDeliveryTarget(params: {
: typeof params.parentConversationId === "string"
? params.parentConversationId.trim()
: undefined;
if (
channel === "matrix" &&
to &&
conversationId &&
parentConversationId &&
parentConversationId !== conversationId
) {
return { to, threadId: conversationId };
const pluginTarget =
channel && conversationId
? getChannelPlugin(
normalizeChannelId(channel) ?? channel,
)?.messaging?.resolveDeliveryTarget?.({
conversationId,
parentConversationId,
})
: null;
if (pluginTarget) {
return {
...(pluginTarget.to?.trim() ? { to: pluginTarget.to.trim() } : {}),
...(pluginTarget.threadId?.trim() ? { threadId: pluginTarget.threadId.trim() } : {}),
};
}
const to = formatConversationTarget(params);
return { to };
}