Initial Commit
This commit is contained in:
commit
abdee5d68d
|
@ -0,0 +1,10 @@
|
|||
module.exports = {
|
||||
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:@typescript-eslint/recommended-requiring-type-checking',],
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
tsconfigRootDir: __dirname,
|
||||
project: ['./tsconfig.json'],
|
||||
},
|
||||
plugins: ['@typescript-eslint'],
|
||||
root: true,
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
node_modules
|
||||
ml-bot.json
|
||||
config.yaml
|
||||
ml-bot-store
|
||||
dist
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"name": "spam-ml-bot",
|
||||
"version": "0.1.0",
|
||||
"description": "A Matrix Bot that warns admins about possible spam",
|
||||
"main": "dist/index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"lint": "eslint src/**/*.ts",
|
||||
"run": "node --experimental-specifier-resolution=node ./dist/index.js"
|
||||
},
|
||||
"author": "MTRNord <mtrnord@nordgedanken.dev>",
|
||||
"license": "Apache-2.0",
|
||||
"devDependencies": {
|
||||
"@types/html-to-text": "^8.1.1",
|
||||
"@types/js-yaml": "^4.0.5",
|
||||
"@typescript-eslint/eslint-plugin": "^5.47.0",
|
||||
"@typescript-eslint/parser": "^5.47.0",
|
||||
"eslint": "^8.30.0",
|
||||
"typescript": "^4.9.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tensorflow/tfjs-node": "^4.1.0",
|
||||
"@types/node": "^18.11.17",
|
||||
"html-entities": "^2.3.3",
|
||||
"html-to-text": "^9.0.3",
|
||||
"js-yaml": "^4.1.0",
|
||||
"matrix-bot-sdk": "^0.6.3",
|
||||
"short-uuid": "^4.2.2",
|
||||
"string-strip-html": "^13.0.6"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,499 @@
|
|||
import { MatrixClient, MatrixGlob, RoomCreateOptions } from "matrix-bot-sdk";
|
||||
import { Config } from "../index";
|
||||
import short from "short-uuid";
|
||||
|
||||
export enum EntityType {
|
||||
/// `entity` is to be parsed as a glob of users IDs
|
||||
RULE_USER = "m.policy.rule.user",
|
||||
|
||||
/// `entity` is to be parsed as a glob of room IDs/aliases
|
||||
RULE_ROOM = "m.policy.rule.room",
|
||||
|
||||
/// `entity` is to be parsed as a glob of server names
|
||||
RULE_SERVER = "m.policy.rule.server",
|
||||
}
|
||||
|
||||
export const RULE_USER = EntityType.RULE_USER;
|
||||
export const RULE_ROOM = EntityType.RULE_ROOM;
|
||||
export const RULE_SERVER = EntityType.RULE_SERVER;
|
||||
|
||||
// README! The order here matters for determining whether a type is obsolete, most recent should be first.
|
||||
// These are the current and historical types for each type of rule which were used while MSC2313 was being developed
|
||||
// and were left as an artifact for some time afterwards.
|
||||
// Most rules (as of writing) will have the prefix `m.room.rule.*` as this has been in use for roughly 2 years.
|
||||
export const USER_RULE_TYPES = [RULE_USER, "m.room.rule.user", "org.matrix.mjolnir.rule.user"];
|
||||
export const ROOM_RULE_TYPES = [RULE_ROOM, "m.room.rule.room", "org.matrix.mjolnir.rule.room"];
|
||||
export const SERVER_RULE_TYPES = [RULE_SERVER, "m.room.rule.server", "org.matrix.mjolnir.rule.server"];
|
||||
export const ALL_RULE_TYPES = [...USER_RULE_TYPES, ...ROOM_RULE_TYPES, ...SERVER_RULE_TYPES];
|
||||
|
||||
export enum Recommendation {
|
||||
/// The rule recommends a "ban".
|
||||
///
|
||||
/// The actual semantics for this "ban" may vary, e.g. room ban,
|
||||
/// server ban, ignore user, etc. To determine the semantics for
|
||||
/// this "ban", clients need to take into account the context for
|
||||
/// the list, e.g. how the rule was imported.
|
||||
Ban = "m.ban",
|
||||
|
||||
/// The rule specifies an "opinion", as a number in [-100, +100],
|
||||
/// where -100 represents a user who is considered absolutely toxic
|
||||
/// by whoever issued this ListRule and +100 represents a user who
|
||||
/// is considered absolutely absolutely perfect by whoever issued
|
||||
/// this ListRule.
|
||||
Opinion = "org.matrix.msc3845.opinion",
|
||||
|
||||
/**
|
||||
* This is a rule that recommends allowing a user to participate.
|
||||
* Used for the construction of allow lists.
|
||||
*/
|
||||
Allow = "org.matrix.mjolnir.allow",
|
||||
}
|
||||
|
||||
interface PolicyStateEvent {
|
||||
type: string,
|
||||
content: {
|
||||
entity: string,
|
||||
reason: string,
|
||||
recommendation: Recommendation,
|
||||
opinion?: number,
|
||||
},
|
||||
event_id: string,
|
||||
state_key: string,
|
||||
}
|
||||
|
||||
/**
|
||||
* All variants of recommendation `m.ban`
|
||||
*/
|
||||
const RECOMMENDATION_BAN_VARIANTS = [
|
||||
// Stable
|
||||
Recommendation.Ban,
|
||||
// Unstable prefix, for compatibility.
|
||||
"org.matrix.mjolnir.ban"
|
||||
];
|
||||
|
||||
/**
|
||||
* All variants of recommendation `m.opinion`
|
||||
*/
|
||||
const RECOMMENDATION_OPINION_VARIANTS: string[] = [
|
||||
// Unstable
|
||||
Recommendation.Opinion
|
||||
];
|
||||
|
||||
const RECOMMENDATION_ALLOW_VARIANTS: string[] = [
|
||||
// Unstable
|
||||
Recommendation.Allow
|
||||
]
|
||||
|
||||
export const OPINION_MIN = -100;
|
||||
export const OPINION_MAX = +100;
|
||||
|
||||
/**
|
||||
* Representation of a rule within a Policy List.
|
||||
*/
|
||||
export abstract class ListRule {
|
||||
/**
|
||||
* A glob for `entity`.
|
||||
*/
|
||||
private glob: MatrixGlob;
|
||||
constructor(
|
||||
/**
|
||||
* The event source for the rule.
|
||||
*/
|
||||
public readonly sourceEvent: PolicyStateEvent,
|
||||
/**
|
||||
* The entity covered by this rule, e.g. a glob user ID, a room ID, a server domain.
|
||||
*/
|
||||
public readonly entity: string,
|
||||
/**
|
||||
* A human-readable reason for this rule, for audit purposes.
|
||||
*/
|
||||
public readonly reason: string,
|
||||
/**
|
||||
* The type of entity for this rule, e.g. user, server domain, etc.
|
||||
*/
|
||||
public readonly kind: EntityType,
|
||||
/**
|
||||
* The recommendation for this rule, e.g. "ban" or "opinion", or `null`
|
||||
* if the recommendation is one that Mjölnir doesn't understand.
|
||||
*/
|
||||
public readonly recommendation: Recommendation | null) {
|
||||
this.glob = new MatrixGlob(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether this rule should apply to a given entity.
|
||||
*/
|
||||
public isMatch(entity: string): boolean {
|
||||
return this.glob.test(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns Whether the entity in he rule represents a Matrix glob (and not a literal).
|
||||
*/
|
||||
public isGlob(): boolean {
|
||||
return /[*?]/.test(this.entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate and parse an event into a ListRule.
|
||||
*
|
||||
* @param event An *untrusted* event.
|
||||
* @returns null if the ListRule is invalid or not recognized by Mjölnir.
|
||||
*/
|
||||
public static parse(event: PolicyStateEvent): ListRule | null {
|
||||
// Parse common fields.
|
||||
// If a field is ill-formed, discard the rule.
|
||||
const content = event['content'];
|
||||
if (!content || typeof content !== "object") {
|
||||
return null;
|
||||
}
|
||||
const entity = content['entity'];
|
||||
if (!entity || typeof entity !== "string") {
|
||||
return null;
|
||||
}
|
||||
const recommendation = content['recommendation'];
|
||||
if (!recommendation || typeof recommendation !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const reason = content['reason'] || '<no reason>';
|
||||
if (typeof reason !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const type = event['type'];
|
||||
let kind;
|
||||
if (USER_RULE_TYPES.includes(type)) {
|
||||
kind = EntityType.RULE_USER;
|
||||
} else if (ROOM_RULE_TYPES.includes(type)) {
|
||||
kind = EntityType.RULE_ROOM;
|
||||
} else if (SERVER_RULE_TYPES.includes(type)) {
|
||||
kind = EntityType.RULE_SERVER;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
// From this point, we may need specific fields.
|
||||
if (RECOMMENDATION_BAN_VARIANTS.includes(recommendation)) {
|
||||
return new ListRuleBan(event, entity, reason, kind);
|
||||
} else if (RECOMMENDATION_OPINION_VARIANTS.includes(recommendation)) {
|
||||
const opinion = content['opinion'];
|
||||
if (!Number.isInteger(opinion)) {
|
||||
return null;
|
||||
}
|
||||
return new ListRuleOpinion(event, entity, reason, kind, opinion);
|
||||
} else if (RECOMMENDATION_ALLOW_VARIANTS.includes(recommendation)) {
|
||||
return new ListRuleAllow(event, entity, reason, kind);
|
||||
} else {
|
||||
// As long as the `recommendation` is defined, we assume
|
||||
// that the rule is correct, just unknown.
|
||||
return new ListRuleUnknown(event, entity, reason, kind, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A rule representing a "ban".
|
||||
*/
|
||||
export class ListRuleBan extends ListRule {
|
||||
constructor(
|
||||
/**
|
||||
* The event source for the rule.
|
||||
*/
|
||||
sourceEvent: PolicyStateEvent,
|
||||
/**
|
||||
* The entity covered by this rule, e.g. a glob user ID, a room ID, a server domain.
|
||||
*/
|
||||
entity: string,
|
||||
/**
|
||||
* A human-readable reason for this rule, for audit purposes.
|
||||
*/
|
||||
reason: string,
|
||||
/**
|
||||
* The type of entity for this rule, e.g. user, server domain, etc.
|
||||
*/
|
||||
kind: EntityType,
|
||||
) {
|
||||
super(sourceEvent, entity, reason, kind, Recommendation.Ban)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A rule representing an "allow".
|
||||
*/
|
||||
export class ListRuleAllow extends ListRule {
|
||||
constructor(
|
||||
/**
|
||||
* The event source for the rule.
|
||||
*/
|
||||
sourceEvent: PolicyStateEvent,
|
||||
/**
|
||||
* The entity covered by this rule, e.g. a glob user ID, a room ID, a server domain.
|
||||
*/
|
||||
entity: string,
|
||||
/**
|
||||
* A human-readable reason for this rule, for audit purposes.
|
||||
*/
|
||||
reason: string,
|
||||
/**
|
||||
* The type of entity for this rule, e.g. user, server domain, etc.
|
||||
*/
|
||||
kind: EntityType,
|
||||
) {
|
||||
super(sourceEvent, entity, reason, kind, Recommendation.Allow)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A rule representing an "opinion"
|
||||
*/
|
||||
export class ListRuleOpinion extends ListRule {
|
||||
constructor(
|
||||
/**
|
||||
* The event source for the rule.
|
||||
*/
|
||||
sourceEvent: PolicyStateEvent,
|
||||
/**
|
||||
* The entity covered by this rule, e.g. a glob user ID, a room ID, a server domain.
|
||||
*/
|
||||
entity: string,
|
||||
/**
|
||||
* A human-readable reason for this rule, for audit purposes.
|
||||
*/
|
||||
reason: string,
|
||||
/**
|
||||
* The type of entity for this rule, e.g. user, server domain, etc.
|
||||
*/
|
||||
kind: EntityType,
|
||||
/**
|
||||
* A number in [-100, +100] where -100 represents the worst possible opinion
|
||||
* on the entity (e.g. toxic user or community) and +100 represents the best
|
||||
* possible opinion on the entity (e.g. pillar of the community).
|
||||
*/
|
||||
public readonly opinion: number | undefined
|
||||
) {
|
||||
super(sourceEvent, entity, reason, kind, Recommendation.Opinion);
|
||||
if (!Number.isInteger(opinion)) {
|
||||
throw new TypeError(`The opinion must be an integer, got ${opinion ?? 'undefined'}`);
|
||||
}
|
||||
if ((opinion ?? 0) < OPINION_MIN || (opinion ?? 0) > OPINION_MAX) {
|
||||
throw new TypeError(`The opinion must be within [-100, +100], got ${opinion ?? 'undefined'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Any list rule that we do not understand.
|
||||
*/
|
||||
export class ListRuleUnknown extends ListRule {
|
||||
constructor(
|
||||
/**
|
||||
* The event source for the rule.
|
||||
*/
|
||||
sourceEvent: PolicyStateEvent,
|
||||
/**
|
||||
* The entity covered by this rule, e.g. a glob user ID, a room ID, a server domain.
|
||||
*/
|
||||
entity: string,
|
||||
/**
|
||||
* A human-readable reason for this rule, for audit purposes.
|
||||
*/
|
||||
reason: string,
|
||||
/**
|
||||
* The type of entity for this rule, e.g. user, server domain, etc.
|
||||
*/
|
||||
kind: EntityType,
|
||||
/**
|
||||
* The event used to create the rule.
|
||||
*/
|
||||
public readonly content: unknown,
|
||||
) {
|
||||
super(sourceEvent, entity, reason, kind, null);
|
||||
}
|
||||
}
|
||||
|
||||
/* Soom of this code is taken from. (Not copied but based upon. This is needed for compat reasons.) */
|
||||
export default class BanListHandler {
|
||||
private readonly uuidGen = short(short.constants.cookieBase90);
|
||||
constructor(private readonly client: MatrixClient, private readonly config: Config) { }
|
||||
|
||||
/**
|
||||
* This is used to annotate state events we store with the rule they are associated with.
|
||||
* If we refactor this, it is important to also refactor any listeners to 'PolicyList.update'
|
||||
* which may assume `ListRule`s that are removed will be identital (Object.is) to when they were added.
|
||||
* If you are adding new listeners, you should check the source event_id of the rule.
|
||||
*/
|
||||
private static readonly EVENT_RULE_ANNOTATION_KEY = 'org.matrix.mjolnir.annotation.rule';
|
||||
|
||||
public static async createPolicyRoom(client: MatrixClient): Promise<string> {
|
||||
const powerLevels: { [key: string]: number | object } = {
|
||||
"ban": 50,
|
||||
"events": {
|
||||
"m.room.name": 100,
|
||||
"m.room.power_levels": 100,
|
||||
},
|
||||
"events_default": 50, // non-default
|
||||
"invite": 0,
|
||||
"kick": 50,
|
||||
"notifications": {
|
||||
"room": 20,
|
||||
},
|
||||
"redact": 50,
|
||||
"state_default": 50,
|
||||
"users": {
|
||||
[await client.getUserId()]: 100,
|
||||
},
|
||||
"users_default": 0,
|
||||
};
|
||||
// Support for MSC3784.
|
||||
const roomOptions: RoomCreateOptions = {
|
||||
creation_content: {
|
||||
type: "support.feline.policy.lists.msc.v1"
|
||||
},
|
||||
preset: "public_chat",
|
||||
|
||||
power_level_content_override: powerLevels,
|
||||
}
|
||||
const listRoomId = await client.createRoom(roomOptions);
|
||||
return listRoomId
|
||||
}
|
||||
|
||||
// Adds a rule to the ban list and bans the user.
|
||||
public async banUser(userId: string, reason: string): Promise<void> {
|
||||
await this.addRule(userId, reason, RULE_USER, Recommendation.Ban);
|
||||
|
||||
// Get all rooms the user is in
|
||||
const rooms = await this.client.getJoinedRooms();
|
||||
for (const room of rooms) {
|
||||
// Get all members in the room
|
||||
const members = await this.client.getJoinedRoomMembers(room);
|
||||
for (const member of members) {
|
||||
// If the member is the user we want to ban
|
||||
if (member == userId) {
|
||||
// Ban the user
|
||||
// TODO: allow setting a reason
|
||||
await this.client.banUser(room, member, reason);
|
||||
|
||||
// Redact all messages the user sent
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const messages = await this.client.doRequest("GET", `/_matrix/client/v3/rooms/${room}/messages?dir=b&limit=100`);
|
||||
// Load more if we have more than 100 messages
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (messages.chunk.length == 100) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
|
||||
const moreMessages = await this.client.doRequest("GET", `/_matrix/client/v3/rooms/${room}/messages?dir=b&limit=100&from=${messages.end}`);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
||||
messages.chunk = messages.chunk.concat(moreMessages.chunk);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
for (const message of messages.chunk) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
|
||||
await this.client.redactEvent(room, message.event_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This kicks the user. It doesn't actually add a rule to the ban list.
|
||||
public async kickUser(userId: string, reason: string): Promise<void> {
|
||||
// Get all rooms the user is in
|
||||
const rooms = await this.client.getJoinedRooms();
|
||||
for (const room of rooms) {
|
||||
// Get all members in the room
|
||||
const members = await this.client.getJoinedRoomMembers(room);
|
||||
for (const member of members) {
|
||||
// If the member is the user we want to ban
|
||||
if (member == userId) {
|
||||
// Ban the user
|
||||
// TODO: allow setting a reason
|
||||
await this.client.kickUser(room, member, reason);
|
||||
|
||||
// Redact all messages the user sent
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const messages = await this.client.doRequest("GET", `/_matrix/client/v3/rooms/${room}/messages?dir=b&limit=100`);
|
||||
// Load more if we have more than 100 messages
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (messages.chunk.length == 100) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
|
||||
const moreMessages = await this.client.doRequest("GET", `/_matrix/client/v3/rooms/${room}/messages?dir=b&limit=100&from=${messages.end}`);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
||||
messages.chunk = messages.chunk.concat(moreMessages.chunk);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
for (const message of messages.chunk) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
|
||||
await this.client.redactEvent(room, message.event_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async addRule(userId: string, reason: string, kind: string, recommendation: Recommendation): Promise<void> {
|
||||
await this.client.sendStateEvent(this.config.banlistRoom, kind, this.uuidGen.new(), {
|
||||
entity: userId,
|
||||
reason: reason,
|
||||
recommendation: recommendation,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the active rules of a given kind.
|
||||
* @param kind e.g. RULE_SERVER (m.policy.rule.server). Rule types are always normalised when they are interned into the PolicyList.
|
||||
* @param recommendation A specific recommendation to filter for e.g. `m.ban`. Please remember recommendation varients are normalized.
|
||||
* @returns The active ListRules for the ban list of that kind.
|
||||
*/
|
||||
public async rulesOfKind(roomId: string, kind: string, recommendation?: Recommendation): Promise<ListRule[]> {
|
||||
const rules: ListRule[] = []
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const stateKeyMap = await this.client.getRoomStateEvent(roomId, kind, "");
|
||||
if (stateKeyMap) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
for (const event of stateKeyMap.values()) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
|
||||
const rule = event[BanListHandler.EVENT_RULE_ANNOTATION_KEY];
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (rule && rule.kind === kind) {
|
||||
if (recommendation === undefined) {
|
||||
rules.push(rule as ListRuleUnknown);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
} else if (rule.recommendation === recommendation) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
switch (rule.recommendation) {
|
||||
case Recommendation.Ban:
|
||||
rules.push(rule as ListRuleBan);
|
||||
break;
|
||||
case Recommendation.Allow:
|
||||
rules.push(rule as ListRuleAllow);
|
||||
break;
|
||||
case Recommendation.Opinion:
|
||||
rules.push(rule as ListRuleOpinion);
|
||||
break;
|
||||
default:
|
||||
rules.push(rule as ListRuleUnknown);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
|
||||
public async serverRules(roomId: string): Promise<ListRule[]> {
|
||||
return this.rulesOfKind(roomId, RULE_SERVER);
|
||||
}
|
||||
|
||||
public async userRules(roomId: string): Promise<ListRule[]> {
|
||||
return this.rulesOfKind(roomId, RULE_USER);
|
||||
}
|
||||
|
||||
public async roomRules(roomId: string): Promise<ListRule[]> {
|
||||
return this.rulesOfKind(roomId, RULE_ROOM);
|
||||
}
|
||||
|
||||
public async allRules(roomId: string): Promise<ListRule[]> {
|
||||
return [...await this.serverRules(roomId), ...await this.userRules(roomId), ...await this.roomRules(roomId)];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
//import { MatrixClient } from "matrix-bot-sdk";
|
||||
|
||||
// TODO: Add needed functions and then implement
|
||||
// abstract class CommandHandler {
|
||||
// constructor(protected readonly client: MatrixClient) { }
|
||||
// }
|
|
@ -0,0 +1,53 @@
|
|||
import { RoomEvent } from "matrix-bot-sdk";
|
||||
|
||||
/**
|
||||
* The defintion for the relation
|
||||
* @category Matrix event contents
|
||||
* @see ReactionEventContent
|
||||
*/
|
||||
export interface RelatesTo {
|
||||
rel_type: "m.annotation";
|
||||
event_id: string;
|
||||
key: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The content definition for m.reaction events
|
||||
* @category Matrix event contents
|
||||
* @see ReactionEvent
|
||||
*/
|
||||
export interface ReactionEventContent {
|
||||
"m.relates_to": RelatesTo | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents an m.reaction room event
|
||||
* @category Matrix events
|
||||
*/
|
||||
export class ReactionEvent<T extends ReactionEventContent> extends RoomEvent<T> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
constructor(event: any) {
|
||||
super(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the event is redacted (or looked redacted).
|
||||
*/
|
||||
public get isRedacted(): boolean {
|
||||
// Presume the event redacted if we're missing content
|
||||
return this.content["m.relates_to"] === undefined;
|
||||
}
|
||||
|
||||
public get relatesTo(): RelatesTo {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-extra-non-null-assertion
|
||||
return this.content["m.relates_to"]!!;
|
||||
}
|
||||
|
||||
public get reaction(): string | undefined {
|
||||
return this.content["m.relates_to"]?.key;
|
||||
}
|
||||
|
||||
public get targetEventId(): string | undefined {
|
||||
return this.content["m.relates_to"]?.event_id;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,241 @@
|
|||
import {
|
||||
MatrixClient,
|
||||
SimpleFsStorageProvider,
|
||||
AutojoinRoomsMixin,
|
||||
RustSdkCryptoStorageProvider,
|
||||
LogService,
|
||||
RichConsoleLogger,
|
||||
MessageEvent,
|
||||
TextualMessageEventContent,
|
||||
MatrixProfileInfo,
|
||||
CanonicalAliasEventContent,
|
||||
RoomNameEventContent,
|
||||
} from "matrix-bot-sdk";
|
||||
import { readFile } from "fs/promises";
|
||||
import { load } from "js-yaml";
|
||||
import * as tf from "@tensorflow/tfjs-node";
|
||||
import { Rank, Tensor } from "@tensorflow/tfjs-node";
|
||||
import { TFSavedModel } from "@tensorflow/tfjs-node/dist/saved_model";
|
||||
import { ReactionEvent } from "./events/ReactionEvent";
|
||||
import { htmlToText } from "html-to-text";
|
||||
import { BAN_REACTION, FALSE_POSITIVE_REACTION, getReactionHandler, KICK_REACTION } from "./reactionHandlers/reactionHandler";
|
||||
import BanListHandler from "./banlist/banlist";
|
||||
import { decode } from "html-entities";
|
||||
import { stripHtml } from "string-strip-html";
|
||||
|
||||
export type Config = {
|
||||
homeserver: string;
|
||||
accessToken: string;
|
||||
modelPath: string;
|
||||
// A private room for admins to see responses on reports and other secret activity.
|
||||
adminRoom: string;
|
||||
// A possibly public room where warnings land which also is used to issue actions from as an admin
|
||||
warningsRoom: string;
|
||||
// TODO: Dont set this via config but via a setup in the room on first launch or via the cli or something.
|
||||
banlistRoom: string;
|
||||
};
|
||||
|
||||
const THRESHOLD = 0.8;
|
||||
const startUpMessage = "Bot is starting up...";
|
||||
|
||||
class Bot {
|
||||
private readonly policyRoomHandler = new BanListHandler(this.client, this.config);
|
||||
public static async createBot() {
|
||||
LogService.setLogger(new RichConsoleLogger());
|
||||
LogService.muteModule("Metrics");
|
||||
const config = load(await readFile("./config.yaml", "utf8")) as Config;
|
||||
|
||||
// Add some divider for clarity after tensorflow loaded up
|
||||
const line = '-'.repeat(process.stdout.columns);
|
||||
console.log(line);
|
||||
|
||||
// Check if required fields are set
|
||||
if (config.homeserver == undefined && config.accessToken == undefined) {
|
||||
LogService.error("index", "Missing homeserver and accessToken config values");
|
||||
process.exit(1);
|
||||
} else if (config.homeserver == undefined) {
|
||||
LogService.error("index", "Missing homeserver config value");
|
||||
process.exit(1);
|
||||
} else if (config.accessToken == undefined) {
|
||||
LogService.error("index", "Missing accessToken config value");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (config.adminRoom == undefined && config.warningsRoom == undefined) {
|
||||
LogService.error("index", "Missing adminRoom and warningsRoom config values");
|
||||
process.exit(1);
|
||||
} else if (config.adminRoom == undefined) {
|
||||
LogService.error("index", "Missing adminRoom config value");
|
||||
process.exit(1);
|
||||
} else if (config.warningsRoom == undefined) {
|
||||
LogService.error("index", "Missing warningsRoom config value");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (config.adminRoom.startsWith("#")) {
|
||||
LogService.error("index", "adminRoom config value needs to be a roomid starting with a \"!\"");
|
||||
process.exit(1);
|
||||
}
|
||||
if (config.warningsRoom.startsWith("#")) {
|
||||
LogService.error("index", "warningsRoom config value needs to be a roomid starting with a \"!\"");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
||||
// This will be the URL where clients can reach your homeserver. Note that this might be different
|
||||
// from where the web/chat interface is hosted. The server must support password registration without
|
||||
// captcha or terms of service (public servers typically won't work).
|
||||
const homeserverUrl = config.homeserver;
|
||||
|
||||
// Use the access token you got from login or registration above.
|
||||
const accessToken = config.accessToken;
|
||||
|
||||
// In order to make sure the bot doesn't lose its state between restarts, we'll give it a place to cache
|
||||
// any information it needs to. You can implement your own storage provider if you like, but a JSON file
|
||||
// will work fine for this example.
|
||||
const storage = new SimpleFsStorageProvider("ml-bot.json");
|
||||
const cryptoProvider = new RustSdkCryptoStorageProvider("./ml-bot-store");
|
||||
|
||||
tf.enableProdMode()
|
||||
const model = await tf.node.loadSavedModel(config.modelPath);
|
||||
|
||||
// Finally, let's create the client and set it to autojoin rooms. Autojoining is typical of bots to ensure
|
||||
// they can be easily added to any room.
|
||||
const client = new MatrixClient(homeserverUrl, accessToken, storage, cryptoProvider);
|
||||
|
||||
// TODO: replace with manual handling to need admin approval
|
||||
AutojoinRoomsMixin.setupOnClient(client);
|
||||
|
||||
// Join rooms as needed but crash if missing
|
||||
await client.joinRoom(config.adminRoom);
|
||||
await client.joinRoom(config.warningsRoom);
|
||||
|
||||
|
||||
return new Bot(config, client, model);
|
||||
}
|
||||
|
||||
private constructor(private config: Config, private client: MatrixClient, private model: TFSavedModel) {
|
||||
// Before we start the bot, register our command handler
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
client.on("room.message", this.handleMessage.bind(this));
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
client.on("room.event", this.handleEvents.bind(this));
|
||||
|
||||
// Now that everything is set up, start the bot. This will start the sync loop and run until killed.
|
||||
client.start().then(async () => {
|
||||
LogService.info("index", "Bot started!");
|
||||
// Send notice that bot is starting into both rooms
|
||||
await client.sendNotice(config.adminRoom, startUpMessage);
|
||||
await client.sendNotice(config.warningsRoom, startUpMessage);
|
||||
}).catch(console.error);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private async handleEvents(roomId: string, ev: any): Promise<void> {
|
||||
// For now only handle reactions
|
||||
const event = new ReactionEvent(ev);
|
||||
if (event.isRedacted) return; // Ignore redacted events
|
||||
if (event.sender === await this.client.getUserId()) return; // Ignore ourselves
|
||||
|
||||
|
||||
try {
|
||||
await getReactionHandler(roomId, event, this.client, this.config, this.policyRoomHandler).handleReaction();
|
||||
} catch (e) {
|
||||
return;
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
//LogService.error("index", `Error handling reaction: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
// This is the command handler we registered a few lines up
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private async handleMessage(roomId: string, ev: any): Promise<void> {
|
||||
const event = new MessageEvent(ev);
|
||||
if (event.isRedacted) return; // Ignore redacted events that come through
|
||||
if (event.sender === await this.client.getUserId()) return; // Ignore ourselves
|
||||
if (event.messageType !== "m.text") return; // Ignore non-text messages
|
||||
const text_event = new MessageEvent<TextualMessageEventContent>(ev);
|
||||
|
||||
if (roomId !== this.config.adminRoom && roomId !== this.config.warningsRoom) {
|
||||
await this.checkSpam(event, text_event.content.formatted_body ?? text_event.content.body.trim(), roomId);
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private async checkSpam(event: MessageEvent<TextualMessageEventContent>, body: string, roomId: string) {
|
||||
const body_without_reply = body.replace(/<mx-reply>.*<\/mx-reply>/g, "");
|
||||
const body_decoded = decode(body_without_reply, { level: 'html5' });
|
||||
const body_html_removed = stripHtml(body_decoded).result;
|
||||
// Check if spam
|
||||
const data = tf.tensor([body_html_removed.toLowerCase()])
|
||||
const prediction: Tensor<Rank.R2> = this.model.predict(data) as Tensor<Rank.R2>;
|
||||
const prediction_data: number[][] = await prediction.array();
|
||||
LogService.info("index", `Checking: "${body_html_removed.toLowerCase()}"`);
|
||||
LogService.info("index", `Prediction: ${prediction_data.toString()}`);
|
||||
|
||||
const prediction_value = ((prediction_data[0] ?? [])[0] ?? 0);
|
||||
if (prediction_value > THRESHOLD) {
|
||||
const mxid = event.sender;
|
||||
const displayname = (await (this.client.getUserProfile(mxid) as Promise<MatrixProfileInfo>)).displayname ?? mxid;
|
||||
let alias = roomId;
|
||||
let roomname = roomId;
|
||||
let room_url = `matrix:roomid/${roomId.replace("!", "")}?via=${this.config.homeserver.replace("https://", "")}`;
|
||||
try {
|
||||
alias = (await (this.client.getRoomStateEvent(roomId, "m.room.canonical_alias", "") as Promise<CanonicalAliasEventContent>)).alias;
|
||||
roomname = alias;
|
||||
room_url = `matrix:r/${alias.replace("#", "").replace("!", "")}`;
|
||||
} catch (e) {
|
||||
LogService.debug("index", `Failed to get alias for ${roomId}`);
|
||||
}
|
||||
try {
|
||||
roomname = (await (this.client.getRoomStateEvent(roomId, "m.room.name", "") as Promise<RoomNameEventContent>)).name;
|
||||
} catch (e) {
|
||||
LogService.debug("index", `Failed to get name for ${roomId}`);
|
||||
}
|
||||
|
||||
const html = `<blockquote>\n<p>${body}</p>\n</blockquote>\n<p>Above message was detected as spam. See json file for full event and use reactions to take action or no action.</p><p>It was sent by <a href="matrix:u/${mxid.replace("@", "")}">${displayname}</a> in <a href="${room_url}">${roomname}</a></p><p>Spam Score is: ${prediction_value.toFixed(3)}</p>\n`;
|
||||
const roominfo: { roomId: string; name: string | undefined; alias: string | undefined; } = {
|
||||
roomId: roomId,
|
||||
name: undefined,
|
||||
alias: undefined
|
||||
};
|
||||
if (roomname !== roomId) {
|
||||
roominfo["name"] = roomname;
|
||||
}
|
||||
if (alias !== roomId) {
|
||||
roominfo["alias"] = alias;
|
||||
}
|
||||
const alert_event_id = await this.client.sendMessage(this.config.warningsRoom, {
|
||||
body: htmlToText(html, { wordwrap: false }),
|
||||
msgtype: "m.text",
|
||||
format: "org.matrix.custom.html",
|
||||
formatted_body: html,
|
||||
"space.midnightthoughts.spam_score": prediction_value.toFixed(3),
|
||||
"space.midnightthoughts.sending_user": mxid,
|
||||
"space.midnightthoughts.sending_room": roominfo,
|
||||
"space.midnightthoughts.event_id": event.eventId,
|
||||
});
|
||||
|
||||
await this.client.unstableApis.addReactionToEvent(this.config.warningsRoom, alert_event_id, BAN_REACTION);
|
||||
await this.client.unstableApis.addReactionToEvent(this.config.warningsRoom, alert_event_id, KICK_REACTION);
|
||||
await this.client.unstableApis.addReactionToEvent(this.config.warningsRoom, alert_event_id, FALSE_POSITIVE_REACTION);
|
||||
const eventContent = Buffer.from(JSON.stringify(event.raw), 'utf8');
|
||||
const media = await this.client.uploadContent(eventContent, "application/json", "event.json");
|
||||
await this.client.sendMessage(this.config.warningsRoom, {
|
||||
msgtype: "m.file",
|
||||
body: "event.json",
|
||||
filename: "event.json",
|
||||
info: {
|
||||
mimetype: "application/json",
|
||||
size: eventContent.length,
|
||||
},
|
||||
url: media,
|
||||
})
|
||||
} else {
|
||||
// const textEvent = new MessageEvent<TextualMessageEventContent>(event.raw);
|
||||
//await this.client.unstableApis.addReactionToEvent(roomId, textEvent.eventId, "Classified Not Spam")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Bot.createBot();
|
|
@ -0,0 +1,219 @@
|
|||
import { LogService, MatrixClient } from "matrix-bot-sdk";
|
||||
import BanListHandler from "../banlist/banlist";
|
||||
import { ReactionEvent, ReactionEventContent } from "../events/ReactionEvent";
|
||||
import { Config } from "../index";
|
||||
|
||||
export const BAN_REACTION = "🚨 Ban User";
|
||||
export const KICK_REACTION = "⚠️ Kick User";
|
||||
export const FALSE_POSITIVE_REACTION = "✅ False positive";
|
||||
|
||||
|
||||
export function getReactionHandler<T extends ReactionEventContent>(roomId: string, event: ReactionEvent<T>, client: MatrixClient, config: Config, policyRoomHandler: BanListHandler): ReactionHandler<T> {
|
||||
switch (event.reaction) {
|
||||
case BAN_REACTION:
|
||||
return new BanReactionHandler(roomId, event, client, config, policyRoomHandler);
|
||||
case KICK_REACTION:
|
||||
return new KickReactionHandler(roomId, event, client, config, policyRoomHandler);
|
||||
case FALSE_POSITIVE_REACTION:
|
||||
return new FalsePositiveReactionHandler(roomId, event, client, config, policyRoomHandler);
|
||||
default:
|
||||
throw new Error("Reaction not supported");
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class ReactionHandler<T extends ReactionEventContent> {
|
||||
constructor(protected readonly roomId: string, protected readonly event: ReactionEvent<T>, protected readonly client: MatrixClient, protected readonly config: Config, protected readonly policyRoomHandler: BanListHandler) { }
|
||||
|
||||
public abstract handleReaction(): Promise<void>;
|
||||
}
|
||||
|
||||
|
||||
class BanReactionHandler<T extends ReactionEventContent> extends ReactionHandler<T> {
|
||||
public async handleReaction(): Promise<void> {
|
||||
// Check if reaction was issued by an admin that can do this action
|
||||
// Get users in the admin room
|
||||
const users = await this.client.getJoinedRoomMembers(this.config.adminRoom);
|
||||
|
||||
// Get power levels in the admin room
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const powerLevels = await this.client.getRoomStateEvent(this.config.adminRoom, "m.room.power_levels", "");
|
||||
|
||||
// Map users to their power level
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
const admins = users.filter(u => powerLevels.content.users[u] >= powerLevels.content.ban);
|
||||
|
||||
// Check if sender is an admin
|
||||
if (!admins.includes(this.event.sender)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
let reactedToEvent: { content: { "space.midnightthoughts.sending_user": string; "space.midnightthoughts.previous_action": string; } } | undefined;
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
reactedToEvent = await this.client.getEvent(this.roomId, this.event.targetEventId ?? "");
|
||||
|
||||
} catch (e) {
|
||||
LogService.error("index", "Could not get event", e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Making typescript happy
|
||||
if (reactedToEvent == undefined) {
|
||||
LogService.error("index", "Could not get event");
|
||||
return;
|
||||
}
|
||||
|
||||
if (process.env["DEBUG"] == "true") {
|
||||
LogService.info("BanReactionHandler", `Admin selected ban for ${reactedToEvent.content["space.midnightthoughts.sending_user"]} on ${this.event.targetEventId ?? "unknown"}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// We dont need to issue this twice for the same user
|
||||
// TODO: check if the user is already banned
|
||||
if (reactedToEvent.content["space.midnightthoughts.previous_action"] !== "ban") {
|
||||
await this.policyRoomHandler.banUser(reactedToEvent.content["space.midnightthoughts.sending_user"], "Banned by admin");
|
||||
|
||||
// Get the message we sent in the warning room to update it
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const warningMessage = await this.client.getEvent(this.roomId, this.event.targetEventId ?? "");
|
||||
// Update the event to reflect the action
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["space.midnightthoughts.previous_action"] = "ban";
|
||||
// Add relates_to to the event
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["m.relates_to"] = {
|
||||
"rel_type": "m.replace",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
"event_id": this.event.targetEventId ?? ""
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["m.new_content"] = {
|
||||
"msgtype": "m.text",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
"body": warningMessage.content.body,
|
||||
"format": "org.matrix.custom.html",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
"formatted_body": warningMessage.content.formatted_body
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
await this.client.sendEvent(this.roomId, "m.room.message", warningMessage.content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class KickReactionHandler<T extends ReactionEventContent> extends ReactionHandler<T> {
|
||||
public async handleReaction(): Promise<void> {
|
||||
// Check if reaction was issued by an admin that can do this action
|
||||
// Get users in the admin room
|
||||
const users = await this.client.getJoinedRoomMembers(this.config.adminRoom);
|
||||
|
||||
// Get power levels in the admin room
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const powerLevels = await this.client.getRoomStateEvent(this.config.adminRoom, "m.room.power_levels", "");
|
||||
|
||||
// Map users to their power level
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
const admins = users.filter(u => powerLevels.content.users[u] >= powerLevels.content.kick);
|
||||
|
||||
// Check if sender is an admin
|
||||
if (!admins.includes(this.event.sender)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let reactedToEvent: { content: { "space.midnightthoughts.sending_user": string; "space.midnightthoughts.previous_action": string; } } | undefined;
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
reactedToEvent = await this.client.getEvent(this.roomId, this.event.targetEventId ?? "");
|
||||
|
||||
} catch (e) {
|
||||
LogService.error("index", "Could not get event", e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Making typescript happy
|
||||
if (reactedToEvent == undefined) {
|
||||
LogService.error("index", "Could not get event");
|
||||
return;
|
||||
}
|
||||
|
||||
if (process.env["DEBUG"]?.toLowerCase() == "true" || process.env["DEBUG"] == "1") {
|
||||
LogService.info("BanReactionHandler", `Admin selected kick for ${reactedToEvent.content["space.midnightthoughts.sending_user"]} on ${this.event.targetEventId ?? "unknown"}`);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.policyRoomHandler.kickUser(reactedToEvent.content["space.midnightthoughts.sending_user"], "Kicked by admin");
|
||||
// Get the message we sent in the warning room to update it
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const warningMessage = await this.client.getEvent(this.roomId, this.event.targetEventId ?? "");
|
||||
// Update the event to reflect the action
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["space.midnightthoughts.previous_action"] = "kick";
|
||||
// Add relates_to to the event
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["m.relates_to"] = {
|
||||
"rel_type": "m.replace",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
"event_id": this.event.targetEventId ?? ""
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["m.new_content"] = {
|
||||
"msgtype": "m.text",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
"body": warningMessage.content.body,
|
||||
"format": "org.matrix.custom.html",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
"formatted_body": warningMessage.content.formatted_body
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
await this.client.sendEvent(this.roomId, "m.room.message", warningMessage.content)
|
||||
}
|
||||
}
|
||||
|
||||
class FalsePositiveReactionHandler<T extends ReactionEventContent> extends ReactionHandler<T> {
|
||||
public async handleReaction(): Promise<void> {
|
||||
let reactedToEvent: { content: { "space.midnightthoughts.sending_user": string; "space.midnightthoughts.previous_action": string; } } | undefined;
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
reactedToEvent = await this.client.getEvent(this.roomId, this.event.targetEventId ?? "");
|
||||
|
||||
} catch (e) {
|
||||
LogService.error("index", "Could not get event", e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Making typescript happy
|
||||
if (reactedToEvent == undefined) {
|
||||
LogService.error("index", "Could not get event");
|
||||
return;
|
||||
}
|
||||
|
||||
LogService.info("BanReactionHandler", `Admin selected false positive for ${reactedToEvent.content["space.midnightthoughts.sending_user"]} on ${this.event.targetEventId ?? "unknown"}`);
|
||||
|
||||
// Get the message we sent in the warning room to update it
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const warningMessage = await this.client.getEvent(this.roomId, this.event.targetEventId ?? "");
|
||||
// Update the event to reflect the action
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["space.midnightthoughts.previous_action"] = "false_positive";
|
||||
// Add relates_to to the event
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
warningMessage.content["m.relates_to"] = {
|
||||
"rel_type": "m.replace",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
"event_id": this.event.targetEventId ?? ""
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
|
||||
warningMessage.content["m.new_content"] = {
|
||||
"msgtype": "m.text",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
"body": warningMessage.content.body,
|
||||
"format": "org.matrix.custom.html",
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
"formatted_body": warningMessage.content.formatted_body
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
await this.client.sendEvent(this.roomId, "m.room.message", warningMessage.content)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||
/* Projects */
|
||||
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
||||
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
||||
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
||||
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
||||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||
/* Language and Environment */
|
||||
"target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||
"lib": [
|
||||
"ESNext"
|
||||
],
|
||||
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
||||
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
||||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
||||
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
||||
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
||||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
||||
/* Modules */
|
||||
"module": "ESNext", /* Specify what module code is generated. */
|
||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
||||
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
||||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
||||
// "resolveJsonModule": true, /* Enable importing .json files. */
|
||||
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
||||
/* JavaScript Support */
|
||||
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
||||
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
||||
/* Emit */
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
||||
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
||||
"removeComments": true, /* Disable emitting comments. */
|
||||
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
||||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
||||
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
||||
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
||||
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
||||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
||||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
||||
/* Type Checking */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
||||
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
||||
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
||||
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
||||
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
||||
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
||||
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
||||
"alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
||||
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
||||
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
||||
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
||||
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
||||
"noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
||||
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
||||
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
||||
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
||||
"allowUnreachableCode": false, /* Disable error reporting for unreachable code. */
|
||||
/* Completeness */
|
||||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules/**/*"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue