refactor: reuse channel config resolver in matrix extension
Co-authored-by: thewilloftheshadow <thewilloftheshadow@users.noreply.github.com>
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
import type { MatrixConfig, MatrixRoomConfig } from "../../types.js";
|
import type { MatrixConfig, MatrixRoomConfig } from "../../types.js";
|
||||||
|
import {
|
||||||
|
buildChannelKeyCandidates,
|
||||||
|
resolveChannelEntryMatch,
|
||||||
|
} from "../../../../../src/channels/plugins/channel-config.js";
|
||||||
|
|
||||||
export type MatrixRoomConfigResolved = {
|
export type MatrixRoomConfigResolved = {
|
||||||
allowed: boolean;
|
allowed: boolean;
|
||||||
@@ -15,22 +19,18 @@ export function resolveMatrixRoomConfig(params: {
|
|||||||
const rooms = params.rooms ?? {};
|
const rooms = params.rooms ?? {};
|
||||||
const keys = Object.keys(rooms);
|
const keys = Object.keys(rooms);
|
||||||
const allowlistConfigured = keys.length > 0;
|
const allowlistConfigured = keys.length > 0;
|
||||||
const candidates = [
|
const candidates = buildChannelKeyCandidates(
|
||||||
params.roomId,
|
params.roomId,
|
||||||
`room:${params.roomId}`,
|
`room:${params.roomId}`,
|
||||||
...params.aliases,
|
...params.aliases,
|
||||||
params.name ?? "",
|
params.name ?? "",
|
||||||
].filter(Boolean);
|
);
|
||||||
let matched: MatrixRoomConfigResolved["config"] | undefined;
|
const { entry: matched, wildcardEntry } = resolveChannelEntryMatch({
|
||||||
for (const candidate of candidates) {
|
entries: rooms,
|
||||||
if (rooms[candidate]) {
|
keys: candidates,
|
||||||
matched = rooms[candidate];
|
wildcardKey: "*",
|
||||||
break;
|
});
|
||||||
}
|
const resolved = matched ?? wildcardEntry;
|
||||||
}
|
const allowed = resolved ? resolved.enabled !== false && resolved.allow !== false : false;
|
||||||
if (!matched && rooms["*"]) {
|
return { allowed, allowlistConfigured, config: resolved };
|
||||||
matched = rooms["*"];
|
|
||||||
}
|
|
||||||
const allowed = matched ? matched.enabled !== false && matched.allow !== false : false;
|
|
||||||
return { allowed, allowlistConfigured, config: matched };
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user