Allow config.disableServerACL to disable ServerBanSynchronisationProtection (#339)

* Update for MPS ~0.16.0.

* Update for new MPS ProtectionsManager.

Part of https://github.com/the-draupnir-project/Draupnir/issues/317.

* `config.disableServerACL` now disabled `ServerBanSynchronisationProtection`

Fixes https://github.com/the-draupnir-project/Draupnir/issues/317.
This commit is contained in:
Gnuxie
2024-04-14 14:20:50 +01:00
committed by GitHub
parent 198b65e96e
commit 97482aab2f
5 changed files with 91 additions and 26 deletions
+2 -2
View File
@@ -63,8 +63,8 @@
"js-yaml": "^4.1.0",
"jsdom": "^24.0.0",
"matrix-appservice-bridge": "^9.0.1",
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@0.15.0",
"matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.15.0",
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@0.16.0",
"matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.16.1",
"parse-duration": "^1.0.2",
"pg": "^8.8.0",
"shell-quote": "^1.7.3",
+2 -1
View File
@@ -39,7 +39,8 @@ export class DraupnirFactory {
roomMembershipManager,
client,
clientPlatform,
clientUserID
clientUserID,
config
);
if (isError(protectedRoomsSet)) {
return protectedRoomsSet;
@@ -25,10 +25,14 @@ limitations under the License.
* are NOT distributed, contributed, committed, or licensed under the Apache License.
*/
import { ActionResult, ClientPlatform, MJOLNIR_PROTECTED_ROOMS_EVENT_TYPE, MJOLNIR_WATCHED_POLICY_ROOMS_EVENT_TYPE, MatrixRoomID, MjolnirEnabledProtectionsEvent, MjolnirEnabledProtectionsEventType, MjolnirPolicyRoomsConfig, MjolnirProtectedRoomsConfig, MjolnirProtectedRoomsEvent, MjolnirProtectionSettingsEventType, MjolnirProtectionsConfig, MjolnirWatchedPolicyRoomsEvent, Ok, PolicyListConfig, PolicyRoomManager, ProtectedRoomsConfig, ProtectedRoomsSet, ProtectionsConfig, RoomJoiner, RoomMembershipManager, RoomStateManager, SetMembership, SetRoomState, StandardProtectedRoomsSet, StandardSetMembership, StandardSetRoomState, StringUserID, isError } from "matrix-protection-suite";
import { ActionResult, ClientPlatform, Logger, MJOLNIR_PROTECTED_ROOMS_EVENT_TYPE, MJOLNIR_WATCHED_POLICY_ROOMS_EVENT_TYPE, MatrixRoomID, MissingProtectionCB, MjolnirEnabledProtectionsEvent, MjolnirEnabledProtectionsEventType, MjolnirPolicyRoomsConfig, MjolnirProtectedRoomsConfig, MjolnirProtectedRoomsEvent, MjolnirProtectionSettingsEventType, MjolnirProtectionsConfig, MjolnirWatchedPolicyRoomsEvent, Ok, PolicyListConfig, PolicyRoomManager, ProtectedRoomsConfig, ProtectedRoomsSet, ProtectionsManager, RoomJoiner, RoomMembershipManager, RoomStateManager, SetMembership, SetRoomState, StandardProtectedRoomsSet, StandardProtectionsManager, StandardSetMembership, StandardSetRoomState, StringUserID, isError } from "matrix-protection-suite";
import { BotSDKMatrixAccountData, BotSDKMatrixStateData, MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
import { DefaultEnabledProtectionsMigration } from "../protections/DefaultEnabledProtectionsMigration";
import '../protections/DraupnirProtectionsIndex';
import { IConfig } from "../config";
import { runProtectionConfigHooks } from "../protections/ConfigHooks";
const log = new Logger('DraupnirProtectedRoomsSet');
async function makePolicyListConfig(
client: MatrixSendClient,
@@ -81,30 +85,55 @@ async function makeSetRoomState(
);
}
async function makeProtectionConfig(
function missingProtectionCB(protectionName: string): void {
log.warn(`Unable to find a protection description for the protection named`, protectionName);
}
// FIXME: https://github.com/the-draupnir-project/Draupnir/issues/338
function makeMissingProtectionCB(): MissingProtectionCB {
return missingProtectionCB
}
async function makeProtectionsManager(
client: MatrixSendClient,
roomStateManager: RoomStateManager,
managementRoom: MatrixRoomID
): Promise<ActionResult<ProtectionsConfig>> {
managementRoom: MatrixRoomID,
config: IConfig
): Promise<ActionResult<ProtectionsManager>> {
const result = await roomStateManager.getRoomStateRevisionIssuer(
managementRoom
);
if (isError(result)) {
return result;
}
return Ok(new MjolnirProtectionsConfig(
const protectionsConfigResult = await MjolnirProtectionsConfig.create(
new BotSDKMatrixAccountData<MjolnirEnabledProtectionsEvent>(
MjolnirEnabledProtectionsEventType,
MjolnirEnabledProtectionsEvent,
client
),
new BotSDKMatrixStateData(
MjolnirProtectionSettingsEventType,
result.ok,
client
),
DefaultEnabledProtectionsMigration,
));
{
migrationHandler: DefaultEnabledProtectionsMigration,
missingProtectionCB: makeMissingProtectionCB()
}
);
if (isError(protectionsConfigResult)) {
return protectionsConfigResult;
}
const hookResult = await runProtectionConfigHooks(config, protectionsConfigResult.ok);
if (isError(hookResult)) {
return hookResult;
}
return Ok(
new StandardProtectionsManager(
protectionsConfigResult.ok,
new BotSDKMatrixStateData(
MjolnirProtectionSettingsEventType,
result.ok,
client
)
)
);
}
@@ -115,7 +144,8 @@ export async function makeProtectedRoomsSet(
roomMembershipManager: RoomMembershipManager,
client: MatrixSendClient,
clientPlatform: ClientPlatform,
userID: StringUserID
userID: StringUserID,
config: IConfig,
): Promise<ActionResult<ProtectedRoomsSet>> {
const protectedRoomsConfig = await makeProtectedRoomsConfig(client, clientPlatform.toRoomJoiner())
if (isError(protectedRoomsConfig)) {
@@ -139,10 +169,11 @@ export async function makeProtectedRoomsSet(
if (isError(policyListConfig)) {
return policyListConfig;
}
const protectionsConfig = await makeProtectionConfig(
const protectionsConfig = await makeProtectionsManager(
client,
roomStateManager,
managementRoom
managementRoom,
config
);
if (isError(protectionsConfig)) {
return protectionsConfig;
+33
View File
@@ -0,0 +1,33 @@
// Copyright 2024 Gnuxie <Gnuxie@protonmail.com>
//
// SPDX-License-Identifier: AFL-3.0
import { ActionResult, Ok, ProtectionsConfig, ServerBanSynchronisationProtection, isError } from "matrix-protection-suite";
import { IConfig } from "../config";
type ConfigHook = (config: IConfig, protectionsConfig: ProtectionsConfig) => Promise<ActionResult<void>>;
const hooks: ConfigHook[] = [
async function disableServerACL (config, protectionsConfig) {
if (config.disableServerACL) {
return await protectionsConfig.disableProtection(ServerBanSynchronisationProtection.name);
} else {
return Ok(undefined);
}
}
]
/**
* Introduced to allow the legacy option `config.disableServerACL` to map onto
* MPS's ServerBanSynchronisationProtection. I think we need to deprecate the
* option and offer something else.
*/
export async function runProtectionConfigHooks(config: IConfig, protectionsConfig: ProtectionsConfig): Promise<ActionResult<void>> {
for (const hook of hooks) {
const result = await hook(config, protectionsConfig);
if (isError(result)) {
return result;
}
}
return Ok(undefined);
}
+8 -8
View File
@@ -2505,15 +2505,15 @@ matrix-appservice@^2.0.0:
request-promise "^4.2.6"
sanitize-html "^2.8.0"
"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.15.0":
version "0.15.0"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-0.15.0.tgz#d59c51af79b3d198791a065fed16bd65b52dac33"
integrity sha512-U4CqeTBBwuI/kQFjT5muFwIoNkCopb5yG4TXvEEJqWy+Vt2NrAHV3UGwr5t/fcBYzzC+GLDYlRQkG5B9x0n3YA==
"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@0.16.1":
version "0.16.1"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-0.16.1.tgz#322d6cb88cb158140f432e1f853a69e8697cd6a5"
integrity sha512-fipZknx+QNuwNHV+er6LWdnUJ8/6NhuIpqP9O8zdMVC8Wn+/vNcAzTKCO3k9ulDNYtTgA8M+dyOlg0nOJVCJAQ==
"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@0.15.0":
version "0.15.0"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-0.15.0.tgz#7a876e4f670ac8d513e23eb4d4919d1f67f19d1c"
integrity sha512-2E75q9KEj1nz9V/5I7dOLRDTh1czDEftppbcr4DvPnJeXiJsLJ5eiSqd/hbr0AN0/PS+rICPgOqGOEbXXsk3uQ==
"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@0.16.0":
version "0.16.0"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-0.16.0.tgz#380367f76721373e13cc04399ddffa0b9b1ee9c0"
integrity sha512-XNAaECsv3CUYZN2mBiwVrG+qOs+KP0NDIw353/KtZFOAoAKwCFltXpPoQe/TT+Lc0eSVxfKtW6+rofWBccjfVQ==
dependencies:
await-lock "^2.2.2"
crypto-js "^4.1.1"