diff --git a/src/Draupnir.ts b/src/Draupnir.ts index eacd0110..cd41f042 100644 --- a/src/Draupnir.ts +++ b/src/Draupnir.ts @@ -37,7 +37,7 @@ import { DefaultStateTrackingMeta, ManagerManager, ManagerManagerForMatrixEmitte import { IConfig } from "./config"; import { COMMAND_PREFIX, extractCommandFromMessageBody, handleCommand } from "./commands/CommandHandler"; import { makeProtectedRoomsSet } from "./DraupnirBotMode"; -import { makeStandardConsequenceProvider, renderProtectionFailedToStart } from "./StandardConsequenceProvider"; +import { renderProtectionFailedToStart } from "./StandardConsequenceProvider"; import { htmlEscape } from "./utils"; import { LogLevel } from "matrix-bot-sdk"; @@ -130,7 +130,6 @@ export class Draupnir { ) ); const loadResult = await protectedRoomsSet.protections.loadProtections( - makeStandardConsequenceProvider(client, draupnir.managementRoomID), protectedRoomsSet, draupnir, (error, description) => renderProtectionFailedToStart( diff --git a/src/StandardConsequenceProvider.tsx b/src/StandardConsequenceProvider.tsx index 18ef0f89..c81be7df 100644 --- a/src/StandardConsequenceProvider.tsx +++ b/src/StandardConsequenceProvider.tsx @@ -25,12 +25,13 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { ActionError, ActionException, ActionExceptionKind, ActionResult, ConsequenceProvider, Ok, Permalinks, ProtectionDescription, ProtectionDescriptionInfo, RoomUpdateError, RoomUpdateException, SetMemberBanResultMap, StringEventID, StringRoomID, StringUserID, Task, applyPolicyRevisionToSetMembership, isError } from "matrix-protection-suite"; +import { ActionError, ActionException, ActionExceptionKind, ActionResult, BasicConsequenceProvider, DEFAULT_CONSEQUENCE_PROVIDER, Ok, Permalinks, ProtectionDescription, ProtectionDescriptionInfo, RoomUpdateError, RoomUpdateException, SetMemberBanResultMap, StringEventID, StringRoomID, StringUserID, Task, applyPolicyRevisionToSetMembership, describeConsequenceProvider, isError } from "matrix-protection-suite"; import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk"; import { renderMatrixAndSend } from "./commands/interface-manager/DeadDocumentMatrix"; import { JSXFactory } from "./commands/interface-manager/JSXFactory"; import { DocumentNode } from "./commands/interface-manager/DeadDocument"; import { printActionResult } from "./models/RoomUpdateError"; +import { Draupnir } from "./Draupnir"; interface ProviderContext { client: MatrixSendClient; @@ -49,7 +50,7 @@ async function renderConsequenceForEvent(client: MatrixSendClient, managementRoo return Ok(undefined); } -const consequenceForEvent: ConsequenceProvider['consequenceForEvent'] = async function( +const consequenceForEvent: BasicConsequenceProvider['consequenceForEvent'] = async function( this: ProviderContext, protection, roomID, eventID, reason ): Promise> { Task(renderConsequenceForEvent(this.client, this.managementRoomID, protection, roomID, eventID, reason)) @@ -86,7 +87,7 @@ function banUser(client: MatrixSendClient, protection: ProtectionDescriptionInfo ) } -const consequenceForUserInRoom: ConsequenceProvider['consequenceForUserInRoom'] = async function( +const consequenceForUserInRoom: BasicConsequenceProvider['consequenceForUserInRoom'] = async function( this: ProviderContext, protection, roomID, userID, reason ): Promise> { Task(renderConsequenceForUserInRoom(this.client, this.managementRoomID, protection, roomID, userID, reason)); @@ -109,7 +110,7 @@ function renderSetMembershipBans(title: DocumentNode, map: SetMemberBanResultMap } -const consequenceForUsersInRevision: ConsequenceProvider['consequenceForUsersInRevision'] = async function( +const consequenceForUsersInRevision: BasicConsequenceProvider['consequenceForUsersInRevision'] = async function( this: ProviderContext, description, setMembership, revision ) { const results = await applyPolicyRevisionToSetMembership( @@ -130,14 +131,14 @@ const consequenceForUsersInRevision: ConsequenceProvider['consequenceForUsersInR return Ok(undefined); } -const consequenceForServerACL: ConsequenceProvider['consequenceForServerACL'] = async function( +const consequenceForServerACL: BasicConsequenceProvider['consequenceForServerACL'] = async function( this: ProviderContext, aclContent ): Promise> { // nothing to do return Ok(undefined) } -const consequenceForServerACLInRoom: ConsequenceProvider['consequenceForServerACLInRoom'] = async function( +const consequenceForServerACLInRoom: BasicConsequenceProvider['consequenceForServerACLInRoom'] = async function( this: ProviderContext, _protection, roomID, aclContent ): Promise> { return this.client.sendStateEvent(roomID, 'm.room.server_acl', '', aclContent).then( @@ -149,12 +150,12 @@ const consequenceForServerACLInRoom: ConsequenceProvider['consequenceForServerAC ) } -const consequenceForServerInRoom: ConsequenceProvider['consequenceForServerInRoom'] = async function( +const consequenceForServerInRoom: BasicConsequenceProvider['consequenceForServerInRoom'] = async function( ) { return Ok(undefined); } -const unbanUserFromRoomsInSet: ConsequenceProvider['unbanUserFromRoomsInSet'] = async function( +const unbanUserFromRoomsInSet: BasicConsequenceProvider['unbanUserFromRoomsInSet'] = async function( this: ProviderContext, _protection, userID, protectedRoomsSet ): Promise> { const errors: RoomUpdateError[] = []; @@ -181,10 +182,10 @@ const unbanUserFromRoomsInSet: ConsequenceProvider['unbanUserFromRoomsInSet'] = return Ok(undefined); } -export function makeStandardConsequenceProvider( +export function makeStandardBasicConsequenceProvider( client: MatrixSendClient, managementRoomID: StringRoomID -): ConsequenceProvider { +): BasicConsequenceProvider { return { consequenceForEvent, consequenceForServerACL, @@ -195,9 +196,20 @@ export function makeStandardConsequenceProvider( unbanUserFromRoomsInSet, client, managementRoomID - } as unknown as ConsequenceProvider; + } as unknown as BasicConsequenceProvider; } +describeConsequenceProvider({ + name: DEFAULT_CONSEQUENCE_PROVIDER, + description: 'Does what it says on the tin', + factory: function(draupnir) { + return makeStandardBasicConsequenceProvider( + draupnir.client, + draupnir.managementRoomID + ) + } +}) + export async function renderProtectionFailedToStart( client: MatrixSendClient, managementRoomID: StringRoomID, diff --git a/src/protections/BanPropagation.tsx b/src/protections/BanPropagation.tsx index 451c780b..2fc0a130 100644 --- a/src/protections/BanPropagation.tsx +++ b/src/protections/BanPropagation.tsx @@ -34,7 +34,7 @@ import { renderMentionPill } from "../commands/interface-manager/MatrixHelpRende import { UserID } from "matrix-bot-sdk"; import { renderListRules } from "../commands/Rules"; import { printActionResult } from "../models/RoomUpdateError"; -import { AbstractProtection, ActionResult, ConsequenceProvider, Logger, MatrixRoomID, MatrixRoomReference, MembershipChange, MembershipChangeType, Ok, PermissionError, PolicyRule, PolicyRuleType, ProtectedRoomsSet, ProtectionDescription, Recommendation, RoomActionError, RoomMembershipRevision, RoomUpdateError, StringRoomID, StringUserID, Task, describeProtection, isError, serverName } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, ConsequenceProvider, Logger, MatrixRoomID, MatrixRoomReference, MembershipChange, MembershipChangeType, Ok, PermissionError, PolicyRule, PolicyRuleType, ProtectedRoomsSet, ProtectionDescription, Recommendation, RoomActionError, RoomMembershipRevision, RoomUpdateError, StringRoomID, StringUserID, Task, describeProtection, isError, serverName } from "matrix-protection-suite"; import { Draupnir } from "../Draupnir"; import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk"; import { DraupnirProtection } from "./Protection"; @@ -138,7 +138,7 @@ export class BanPropagationProtection extends AbstractProtection implements Drau constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir, ) { diff --git a/src/protections/BasicFlooding.ts b/src/protections/BasicFlooding.ts index a08a5003..e74ec6a9 100644 --- a/src/protections/BasicFlooding.ts +++ b/src/protections/BasicFlooding.ts @@ -28,7 +28,7 @@ limitations under the License. import { Draupnir } from "../Draupnir"; import { DraupnirProtection } from "./Protection"; import { LogLevel } from "matrix-bot-sdk"; -import { AbstractProtection, ActionResult, ConsequenceProvider, Logger, MatrixRoomID, Ok, ProtectedRoomsSet, ProtectionDescription, RoomEvent, SafeIntegerProtectionSetting, StandardProtectionSettings, StringEventID, StringRoomID, StringUserID, describeProtection, isError } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, Logger, MatrixRoomID, Ok, ProtectedRoomsSet, ProtectionDescription, RoomEvent, SafeIntegerProtectionSetting, StandardProtectionSettings, StringEventID, StringRoomID, StringUserID, describeProtection, isError } from "matrix-protection-suite"; const log = new Logger('BasicFloodingProtection'); @@ -78,7 +78,7 @@ export class BasicFloodingProtection extends AbstractProtection implements Draup public constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir, private readonly settings: BasicFloodingProtectionSettings, diff --git a/src/protections/FirstMessageIsImage.ts b/src/protections/FirstMessageIsImage.ts index c1f1cb96..65922dde 100644 --- a/src/protections/FirstMessageIsImage.ts +++ b/src/protections/FirstMessageIsImage.ts @@ -26,7 +26,7 @@ limitations under the License. */ import { LogLevel, LogService } from "matrix-bot-sdk"; -import { AbstractProtection, ActionResult, ConsequenceProvider, MatrixRoomID, MembershipChange, MembershipChangeType, Ok, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMembershipRevision, RoomMessage, StringRoomID, StringUserID, Value, describeProtection } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, MatrixRoomID, MembershipChange, MembershipChangeType, Ok, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMembershipRevision, RoomMessage, StringRoomID, StringUserID, Value, describeProtection } from "matrix-protection-suite"; import { Draupnir } from "../Draupnir"; type FirstMessageIsImageProtectionSettings = {} @@ -54,7 +54,7 @@ export class FirstMessageIsImageProtection extends AbstractProtection implements constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir, ) { diff --git a/src/protections/JoinWaveShortCircuit.tsx b/src/protections/JoinWaveShortCircuit.tsx index 114dc010..15110dae 100644 --- a/src/protections/JoinWaveShortCircuit.tsx +++ b/src/protections/JoinWaveShortCircuit.tsx @@ -25,7 +25,7 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { AbstractProtection, ActionResult, ConsequenceProvider, Logger, MembershipChange, MembershipChangeType, Ok, ProtectedRoomsSet, ProtectionDescription, RoomMembershipRevision, SafeIntegerProtectionSetting, StandardProtectionSettings, StringRoomID, describeProtection, isError } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, Logger, MembershipChange, MembershipChangeType, Ok, ProtectedRoomsSet, ProtectionDescription, RoomMembershipRevision, SafeIntegerProtectionSetting, StandardProtectionSettings, StringRoomID, describeProtection, isError } from "matrix-protection-suite"; import {LogLevel} from "matrix-bot-sdk"; import { Draupnir } from "../Draupnir"; import { DraupnirProtection } from "./Protection"; @@ -85,7 +85,7 @@ export class JoinWaveShortCircuitProtection extends AbstractProtection implement constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir, public readonly settings: JoinWaveShortCircuitProtectionSettings diff --git a/src/protections/MessageIsMedia.ts b/src/protections/MessageIsMedia.ts index 7ba5e9b4..d7627d95 100644 --- a/src/protections/MessageIsMedia.ts +++ b/src/protections/MessageIsMedia.ts @@ -26,7 +26,7 @@ limitations under the License. */ import { LogLevel } from "matrix-bot-sdk"; -import { AbstractProtection, ActionResult, ConsequenceProvider, MatrixRoomID, Ok, Permalinks, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMessage, Value, describeProtection, serverName } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, MatrixRoomID, Ok, Permalinks, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMessage, Value, describeProtection, serverName } from "matrix-protection-suite"; import { Draupnir } from "../Draupnir"; type MessageIsMediaProtectionSettings = {}; @@ -49,7 +49,7 @@ describeProtection({ export class MessageIsMediaProtection extends AbstractProtection implements Protection { constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir ) { diff --git a/src/protections/MessageIsVoice.ts b/src/protections/MessageIsVoice.ts index e8ae1dc8..30abdfee 100644 --- a/src/protections/MessageIsVoice.ts +++ b/src/protections/MessageIsVoice.ts @@ -26,7 +26,7 @@ limitations under the License. */ import { LogLevel} from "matrix-bot-sdk"; -import { AbstractProtection, ActionResult, ConsequenceProvider, MatrixRoomID, Ok, Permalinks, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMessage, Value, describeProtection, serverName } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, MatrixRoomID, Ok, Permalinks, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMessage, Value, describeProtection, serverName } from "matrix-protection-suite"; import { Draupnir } from "../Draupnir"; describeProtection({ @@ -47,7 +47,7 @@ describeProtection({ export class MessageIsVoiceProtection extends AbstractProtection implements Protection { constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir, ) { diff --git a/src/protections/TrustedReporters.ts b/src/protections/TrustedReporters.ts index 816a459f..73a16a44 100644 --- a/src/protections/TrustedReporters.ts +++ b/src/protections/TrustedReporters.ts @@ -25,7 +25,7 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { AbstractProtection, ActionResult, ConsequenceProvider, EventReport, Ok, ProtectedRoomsSet, Protection, ProtectionDescription, SafeIntegerProtectionSetting, StandardProtectionSettings, StringEventID, StringUserID, StringUserIDSetProtectionSettings, describeProtection, isError } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, EventReport, Ok, ProtectedRoomsSet, Protection, ProtectionDescription, SafeIntegerProtectionSetting, StandardProtectionSettings, StringEventID, StringUserID, StringUserIDSetProtectionSettings, describeProtection, isError } from "matrix-protection-suite"; import { Draupnir } from "../Draupnir"; const MAX_REPORTED_EVENT_BACKLOG = 20; @@ -81,7 +81,7 @@ export class TrustedReporters extends AbstractProtection implements Protection public constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir, public readonly settings: TrustedReportersProtectionSettings diff --git a/src/protections/WordList.ts b/src/protections/WordList.ts index ca9f3bc5..f3dbc778 100644 --- a/src/protections/WordList.ts +++ b/src/protections/WordList.ts @@ -25,7 +25,7 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { AbstractProtection, ActionResult, ConsequenceProvider, Logger, MatrixRoomID, MembershipChange, MembershipChangeType, Ok, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMembershipRevision, RoomMessage, StringRoomID, StringUserID, Value, describeProtection } from "matrix-protection-suite"; +import { AbstractProtection, ActionResult, BasicConsequenceProvider, Logger, MatrixRoomID, MembershipChange, MembershipChangeType, Ok, ProtectedRoomsSet, Protection, ProtectionDescription, RoomEvent, RoomMembershipRevision, RoomMessage, StringRoomID, StringUserID, Value, describeProtection } from "matrix-protection-suite"; import { Draupnir } from "../Draupnir"; const log = new Logger('WordList'); @@ -52,7 +52,7 @@ export class WordListProtection extends AbstractProtection implements Protection constructor( description: ProtectionDescription, - consequenceProvider: ConsequenceProvider, + consequenceProvider: BasicConsequenceProvider, protectedRoomsSet: ProtectedRoomsSet, private readonly draupnir: Draupnir, ) {