feat: use native Matrix Power Level to control check-in immunity and cross-room syncing
1. Added RoomManagerBot for cross-room permission and leave syncing based on native power levels. 3. Added invite() to MatrixSession and added onMemberLeave/onPowerLevel hooks to Dispatcher. 4. Updated README and example configs.
This commit is contained in:
1 parent
440f572834
commit
0a3843630a
16 files changed
+257
-154
No files matched your search
@@ -26,6 +26,10 @@ export class DefaultBotContext implements BotContext {
|
||||
return this.transport.kick(roomId, userId, reason);
|
||||
}
|
||||
|
||||
invite(roomId: string, userId: string): Promise<void> {
|
||||
return this.transport.invite(roomId, userId);
|
||||
}
|
||||
|
||||
getMemberCount(roomId: string): Promise<number> {
|
||||
return this.transport.getMemberCount(roomId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InboundMessage, MemberEvent } from '../command/types';
|
||||
import { InboundMessage, MemberEvent, PowerLevelEvent } from '../command/types';
|
||||
import { CommandError, parseCommand } from '../command/command-parser';
|
||||
import { MatrixEventSink } from '../matrix/types';
|
||||
import { BotRegistry } from './bot-registry.service';
|
||||
@@ -49,12 +49,14 @@ export class Dispatcher implements MatrixEventSink {
|
||||
}
|
||||
|
||||
async onMember(ev: MemberEvent): Promise<void> {
|
||||
if (ev.membership !== 'join') return;
|
||||
const targets = this.registry.resolve(ev.account, ev.roomId);
|
||||
for (const { bot, ctx } of targets) {
|
||||
if (!bot.onMemberJoin) continue;
|
||||
try {
|
||||
await bot.onMemberJoin(ev, ctx);
|
||||
if (ev.membership === 'join' && bot.onMemberJoin) {
|
||||
await bot.onMemberJoin(ev, ctx);
|
||||
} else if (ev.membership === 'leave' && bot.onMemberLeave) {
|
||||
await bot.onMemberLeave(ev, ctx);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.warn(
|
||||
`bot ${bot.name} 处理成员事件失败: ${(err as Error).message}`,
|
||||
@@ -62,4 +64,18 @@ export class Dispatcher implements MatrixEventSink {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async onPowerLevel(ev: PowerLevelEvent): Promise<void> {
|
||||
const targets = this.registry.resolve(ev.account, ev.roomId);
|
||||
for (const { bot, ctx } of targets) {
|
||||
if (!bot.onPowerLevel) continue;
|
||||
try {
|
||||
await bot.onPowerLevel(ev, ctx);
|
||||
} catch (err) {
|
||||
this.logger.warn(
|
||||
`bot ${bot.name} 处理权限变更事件失败: ${(err as Error).message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
InboundMessage,
|
||||
MemberEvent,
|
||||
ParsedCommand,
|
||||
PowerLevelEvent,
|
||||
} from '../command/types';
|
||||
|
||||
/**
|
||||
@@ -15,6 +16,7 @@ export interface BotContext {
|
||||
send(roomId: string, text: string, replyToEventId?: string): Promise<string>;
|
||||
reply(msg: InboundMessage, text: string): Promise<string>;
|
||||
kick(roomId: string, userId: string, reason?: string): Promise<void>;
|
||||
invite(roomId: string, userId: string): Promise<void>;
|
||||
getMemberCount(roomId: string): Promise<number>;
|
||||
getJoinedMembers(roomId: string): Promise<string[]>;
|
||||
/** 读取用户在房间内的 power level,用于按房间权限判断管理员(读不到返回 0)。 */
|
||||
@@ -39,5 +41,7 @@ export interface RoomBot {
|
||||
onMessage?(msg: InboundMessage, ctx: BotContext): Promise<void>;
|
||||
onCommand?(cmd: ParsedCommand, ctx: BotContext): Promise<void>;
|
||||
onMemberJoin?(ev: MemberEvent, ctx: BotContext): Promise<void>;
|
||||
onMemberLeave?(ev: MemberEvent, ctx: BotContext): Promise<void>;
|
||||
onPowerLevel?(ev: PowerLevelEvent, ctx: BotContext): Promise<void>;
|
||||
registerCrons?(ctx: BotContext): CronSpec[];
|
||||
}
|
||||
@@ -16,6 +16,13 @@ export interface MemberEvent {
|
||||
membership: 'join' | 'leave' | 'invite' | 'ban';
|
||||
}
|
||||
|
||||
/** 房间权限变更事件 */
|
||||
export interface PowerLevelEvent {
|
||||
account: string;
|
||||
roomId: string;
|
||||
users: Record<string, number>;
|
||||
}
|
||||
|
||||
/** 解析后的命令。 */
|
||||
export interface ParsedCommand {
|
||||
raw: InboundMessage;
|
||||
|
||||
@@ -345,6 +345,14 @@ export class MatrixSession implements MatrixTransport {
|
||||
await this.client.kickUser(userId, roomId, reason);
|
||||
}
|
||||
|
||||
async invite(roomId: string, userId: string): Promise<void> {
|
||||
if (this.dryRun || !this.client) {
|
||||
this.logger.log(`[dry-run] invite ${userId} to ${roomId}`);
|
||||
return;
|
||||
}
|
||||
await this.client.inviteUser(userId, roomId);
|
||||
}
|
||||
|
||||
async getMemberCount(roomId: string): Promise<number> {
|
||||
if (this.dryRun || !this.client) return 0;
|
||||
const members = await this.client
|
||||
@@ -405,17 +413,27 @@ export class MatrixSession implements MatrixTransport {
|
||||
void this.sink.onMessage(msg);
|
||||
});
|
||||
|
||||
// room.event:所有房间事件;在此筛成员变更。
|
||||
// room.event:所有房间事件;在此筛成员变更及权限变更。
|
||||
client.on('room.event', (roomId: string, event: Record<string, any>) => {
|
||||
if (event['type'] !== 'm.room.member') return;
|
||||
const type = event['type'];
|
||||
const content = (event['content'] ?? {}) as Record<string, unknown>;
|
||||
const ev: MemberEvent = {
|
||||
account: this.account,
|
||||
roomId,
|
||||
userId: (event['state_key'] as string) ?? '',
|
||||
membership: content['membership'] as MemberEvent['membership'],
|
||||
};
|
||||
void this.sink.onMember(ev);
|
||||
|
||||
if (type === 'm.room.member') {
|
||||
const ev: MemberEvent = {
|
||||
account: this.account,
|
||||
roomId,
|
||||
userId: (event['state_key'] as string) ?? '',
|
||||
membership: content['membership'] as MemberEvent['membership'],
|
||||
};
|
||||
void this.sink.onMember(ev);
|
||||
} else if (type === 'm.room.power_levels') {
|
||||
const users = (content['users'] ?? {}) as Record<string, number>;
|
||||
void this.sink.onPowerLevel({
|
||||
account: this.account,
|
||||
roomId,
|
||||
users,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 解密失败:最常见是历史消息(bot 加入前的消息,发送方不会共享旧密钥)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { InboundMessage, MemberEvent } from '../command/types';
|
||||
import { InboundMessage, MemberEvent, PowerLevelEvent } from '../command/types';
|
||||
|
||||
/** bot 与 Matrix 交互的统一出站能力(BotContext 内部依赖它)。 */
|
||||
export interface MatrixTransport {
|
||||
readonly account: string;
|
||||
send(roomId: string, text: string, replyToEventId?: string): Promise<string>;
|
||||
kick(roomId: string, userId: string, reason?: string): Promise<void>;
|
||||
invite(roomId: string, userId: string): Promise<void>;
|
||||
getMemberCount(roomId: string): Promise<number>;
|
||||
getJoinedMembers(roomId: string): Promise<string[]>;
|
||||
/** 读取用户在房间内的 power level(读不到或无权限时返回 0)。 */
|
||||
@@ -15,4 +16,5 @@ export interface MatrixTransport {
|
||||
export interface MatrixEventSink {
|
||||
onMessage(msg: InboundMessage): Promise<void>;
|
||||
onMember(ev: MemberEvent): Promise<void>;
|
||||
onPowerLevel(ev: PowerLevelEvent): Promise<void>;
|
||||
}
|
||||
Reference in new issue
Block a user