-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Experimental
MentionLimitProtection
. (#518)
Based on https://github.com/the-draupnir-project/Draupnir/pull/495/files. Documentation pending.
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2024 Gnuxie <[email protected]> | ||
// | ||
// SPDX-License-Identifier: AFL-3.0 | ||
|
||
import { | ||
AbstractProtection, | ||
ActionResult, | ||
EventConsequences, | ||
Ok, | ||
ProtectedRoomsSet, | ||
Protection, | ||
ProtectionDescription, | ||
RoomEvent, | ||
UnknownSettings, | ||
Value, | ||
describeProtection, | ||
} from "matrix-protection-suite"; | ||
import { Draupnir } from "../Draupnir"; | ||
import { IConfig } from "../config"; | ||
import { MatrixRoomID } from "@the-draupnir-project/matrix-basic-types"; | ||
import { Type } from "@sinclair/typebox"; | ||
|
||
const MentionsContentSchema = Type.Object({ | ||
"m.mentions": Type.Object({ | ||
user_ids: Type.Array(Type.String()), | ||
}), | ||
}); | ||
|
||
const NewContentMentionsSchema = Type.Object({ | ||
"m.new_content": MentionsContentSchema, | ||
}); | ||
|
||
export type MentionLimitProtectionDescription = ProtectionDescription< | ||
unknown, | ||
UnknownSettings<string>, | ||
MentionLimitProtectionCapabilities | ||
>; | ||
|
||
export class MentionLimitProtection | ||
extends AbstractProtection<MentionLimitProtectionDescription> | ||
implements Protection<MentionLimitProtectionDescription> | ||
{ | ||
private readonly eventConsequences: EventConsequences; | ||
private readonly redactReason: string; | ||
private readonly maxMentions: number; | ||
constructor( | ||
description: MentionLimitProtectionDescription, | ||
capabilities: MentionLimitProtectionCapabilities, | ||
protectedRoomsSet: ProtectedRoomsSet, | ||
draupnirConfig: IConfig | ||
) { | ||
super(description, capabilities, protectedRoomsSet, {}); | ||
this.eventConsequences = capabilities.eventConsequences; | ||
this.maxMentions = | ||
draupnirConfig.protections.mentionLimitProtection.maxMentions; | ||
this.redactReason = | ||
draupnirConfig.protections.mentionLimitProtection.redactReason; | ||
} | ||
public async handleTimelineEvent( | ||
_room: MatrixRoomID, | ||
event: RoomEvent | ||
): Promise<ActionResult<void>> { | ||
const isOverLimit = (user_ids: string[]): boolean => | ||
user_ids.length > this.maxMentions; | ||
if ( | ||
Value.Check(NewContentMentionsSchema, event.content) && | ||
isOverLimit(event.content["m.new_content"]["m.mentions"].user_ids) | ||
) { | ||
return await this.eventConsequences.consequenceForEvent( | ||
event.room_id, | ||
event.event_id, | ||
this.redactReason | ||
); | ||
} else if ( | ||
Value.Check(MentionsContentSchema, event.content) && | ||
isOverLimit(event.content["m.mentions"].user_ids) | ||
) { | ||
return await this.eventConsequences.consequenceForEvent( | ||
event.room_id, | ||
event.event_id, | ||
this.redactReason | ||
); | ||
} else { | ||
return Ok(undefined); | ||
} | ||
} | ||
} | ||
|
||
export type MentionLimitProtectionCapabilities = { | ||
eventConsequences: EventConsequences; | ||
}; | ||
|
||
describeProtection<MentionLimitProtectionCapabilities, Draupnir>({ | ||
name: "MentionLimitProtection", | ||
description: `Highly experimental protection that will remove any messages with | ||
a number of mentions over a preconfigured limit. | ||
Please read the documentation https://the-draupnir-project.github.io/draupnir-documentation/protections/mention-limit-protection.`, | ||
capabilityInterfaces: { | ||
eventConsequences: "EventConsequences", | ||
}, | ||
defaultCapabilities: { | ||
eventConsequences: "StandardEventConsequences", | ||
}, | ||
factory: (decription, protectedRoomsSet, draupnir, capabilitySet) => | ||
Ok( | ||
new MentionLimitProtection( | ||
decription, | ||
capabilitySet, | ||
protectedRoomsSet, | ||
draupnir.config | ||
) | ||
), | ||
}); |