import type { BridgeContext , MatrixRoom , MatrixUser , Request , WeakEvent , } from 'matrix-appservice-bridge' ; import { log , } from './config.js' ; import type { AppserviceManager } from './appservice-manager.js' ; import type { WechatyManager } from './wechaty-manager.js' ; export interface DirectMessageUserPair { user : MatrixUser ; service : MatrixUser ; } export class SuperEvent { public event : WeakEvent ; constructor ( public request , public context , public appserviceManager , public wechatyManager ) { log . verbose ( 'SuperEvent' , 'constructor(request[event_id]="%s", context[sender]="%s", appserviceManager, wechatyManager.count()=%s)' , request . getData ( ) . event_id , context ?. senders . matrix . getId ( ) , wechatyManager . count ( ) ) ; this . event = request . getData ( ) ; } public text ( ) { return this . event . content ! [ 'body' ] as string || '' ; } /**
     * Return event age in seconds.
     */ public age ( ) { return this . event . unsigned ?. age ?? 0 / 1000 ; } public sender ( ) { return this . context ! . senders ! . matrix ; } public target ( ) { return this . context ?. targets . matrix || null ; } public room ( ) { return this . context ! . rooms ! . matrix ; } public type ( ) { return this . event . type ; } public targetIsBot ( ) { if ( ! this . context ?. targets . matrix ) { return false ; } const matrixUserId = this . context . targets . matrix . getId ( ) ; return this . appserviceManager . isBot ( matrixUserId ) ; } public targetIsVirtual ( ) { const target = this . target ( ) ; if ( ! target ) { return false ; } return this . appserviceManager . isVirtual ( target . getId ( ) ) ; } public targetIsUser ( ) { return ( ! this . targetIsVirtual ( ) && ! this . targetIsBot ( ) ) ; } /**
     * from @wechaty:
     */ public senderIsBot ( ) { const matrixUserId = this . context ?. senders . matrix . getId ( ) ; return this . appserviceManager . isBot ( matrixUserId ! ) ; } /**
     * from @wechaty_.*
     */ public senderIsVirtual ( ) { const sender = this . sender ( ) ; return this . appserviceManager . isVirtual ( sender . getId ( ) ) ; } public senderIsUser ( ) { return ( ! this . senderIsVirtual ( ) && ! this . senderIsBot ( ) ) ; } public isRoomInvitation ( ) { log . verbose ( 'SuperEvent' , 'isRoomInvitation() for event id: %s' , this . event . event_id ) ; const ret = ! ! ( this . event . type === 'm.room.member' && this . event . content [ 'membership' ] === 'invite' && this . event . state_key ) ; log . silly ( 'SuperEvent' , 'isRoomInvitation() -> %s' , ret ) ; return ret ; } public async acceptRoomInvitation ( ) { log . verbose ( 'SuperEvent' , 'acceptRoomInvitation() for room id: %s' , this . event . room_id ) ; const inviteeMatrixUserId = this . event . state_key ; const matrixRoomId = this . event . room_id ; if ( ! inviteeMatrixUserId ) { throw new Error ( 'no event.state_key' ) ; } const intent = this . appserviceManager . bridge . getIntent ( inviteeMatrixUserId ) ; await intent . join ( matrixRoomId ) ; } }