From fe48309be07a78f3e9d6a838e7b81ceb598c259b Mon Sep 17 00:00:00 2001 From: Il Harper Date: Sat, 14 Sep 2024 18:21:54 +0800 Subject: [PATCH] feat(event): refactor `guild-request`/`guild-member-request`/`guild-member-removed` events --- .../engine-chronocat-event/src/globalVars.ts | 4 +- .../engine-chronocat-event/src/handler.ts | 81 ++++++-- .../engine-chronocat-event/src/messages.ts | 106 +++++++++- packages/red/src/redIpcEntity.ts | 185 +++++++++++++++--- 4 files changed, 336 insertions(+), 40 deletions(-) diff --git a/packages/engine-chronocat-event/src/globalVars.ts b/packages/engine-chronocat-event/src/globalVars.ts index ecd9239..8ee287a 100644 --- a/packages/engine-chronocat-event/src/globalVars.ts +++ b/packages/engine-chronocat-event/src/globalVars.ts @@ -2,7 +2,9 @@ import type { Group, Profile } from '@chronocat/red' export const requestMethodMap: Record = {} export const emittedBuddyReqList: string[] = [] -export const emittedGroupReqList: string[] = [] +export const emittedGuildRequestList: string[] = [] +export const emittedGuildMemberRequestList: string[] = [] +export const emittedGuildMemberRemovedList: string[] = [] export const sendQueue: string[] = [] diff --git a/packages/engine-chronocat-event/src/handler.ts b/packages/engine-chronocat-event/src/handler.ts index 1abf63c..c6e7879 100644 --- a/packages/engine-chronocat-event/src/handler.ts +++ b/packages/engine-chronocat-event/src/handler.ts @@ -23,7 +23,9 @@ import type { ChronocatContext } from '@chronocat/shell' import type { IpcManData } from 'ipcman' import { emittedBuddyReqList, - emittedGroupReqList, + emittedGuildMemberRemovedList, + emittedGuildMemberRequestList, + emittedGuildRequestList, friendMap, groupMap, requestMethodMap, @@ -31,6 +33,8 @@ import { } from './globalVars' import { FriendRequestDispatchMessage, + GuildMemberRemovedDispatchMessage, + GuildMemberRequestDispatchMessage, GuildRequestDispatchMessage, MessageCreatedDispatchMessage, MessageDeletedDispatchMessage, @@ -219,20 +223,71 @@ const dispatcher = async ( // eslint-disable-next-line @typescript-eslint/no-misused-promises notifies.forEach(async (x) => { - if ((x.type !== 1 && x.type !== 7) || x.status !== 1) return + switch (x.type) { + case 1: { + // guild-request + if (x.status !== 1) return - const uin = await ctx.chronocat.uix.getUin2(x.user1.uid) // 此时用户刚刚申请入群,不在群里,不能带 group 场景 - if (!uin) { - ctx.chronocat.l.error('内部错误', { code: 2152 }) - return + const uin = await ctx.chronocat.uix.getUin2(x.user2.uid) + if (!uin) { + ctx.chronocat.l.error('内部错误', { code: 2152 }) + return + } + + const key = `${x.group.groupCode}:${uin}:${x.seq}` + if (emittedGuildRequestList.includes(key)) return + emittedGuildRequestList.push(key) + + ctx.chronocat.emit(new GuildRequestDispatchMessage(x, uin)) + + return + } + + case 7: { + // guild-member-request + if (x.status !== 1) return + + const uin = await ctx.chronocat.uix.getUin2(x.user1.uid) + if (!uin) { + ctx.chronocat.l.error('内部错误', { code: 2152 }) + return + } + + const key = `${x.group.groupCode}:${uin}:${x.seq}` + if (emittedGuildMemberRequestList.includes(key)) return + emittedGuildMemberRequestList.push(key) + + // x.actionUser 此时一定为空 + + ctx.chronocat.emit(new GuildMemberRequestDispatchMessage(x, uin)) + + return + } + + case 11: { + // guild-member-removed + + const uin = await ctx.chronocat.uix.getUin2(x.user1.uid) + if (!uin) { + ctx.chronocat.l.error('内部错误', { code: 2152 }) + return + } + + const key = `${x.group.groupCode}:${uin}:${x.seq}` + if (emittedGuildMemberRemovedList.includes(key)) return + emittedGuildMemberRemovedList.push(key) + + // x.actionUser 此时一定为空 + + ctx.chronocat.emit(new GuildMemberRemovedDispatchMessage(x, uin)) + + return + } + + // case 8: { + // return + // } } - - const key = `${x.group.groupCode}:${uin}:${x.seq}` - if (emittedGroupReqList.includes(key)) return - - emittedGroupReqList.push(key) - - ctx.chronocat.emit(new GuildRequestDispatchMessage(x, uin)) }) return diff --git a/packages/engine-chronocat-event/src/messages.ts b/packages/engine-chronocat-event/src/messages.ts index c9a3122..c21bba7 100644 --- a/packages/engine-chronocat-event/src/messages.ts +++ b/packages/engine-chronocat-event/src/messages.ts @@ -1,4 +1,10 @@ -import type { BuddyReq, GroupNotify, RedMessage } from '@chronocat/red' +import type { + BuddyReq, + GroupNotifyGuildMemberRemoved, + GroupNotifyGuildMemberRequest, + GroupNotifyGuildRequest, + RedMessage, +} from '@chronocat/red' import type { ChronocatContext, ChronocatLogCurrentConfig, @@ -40,7 +46,7 @@ export class MessageDeletedDispatchMessage implements SatoriDispatchMessage { export class GuildRequestDispatchMessage implements SatoriDispatchMessage { constructor( - private notify: GroupNotify, + private notify: GroupNotifyGuildRequest, private uin: string, ) {} @@ -69,7 +75,101 @@ export class GuildRequestDispatchMessage implements SatoriDispatchMessage { user: { id: `${this.uin}`, - name: this.notify.user1.nickName, + name: this.notify.user2.nickName, + avatar: `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${this.uin}&spec=640`, + }, + + message: { + id: undefined as unknown as string, + content: this.notify.postscript, + }, + } + + return [event] + } +} + +export class GuildMemberRequestDispatchMessage + implements SatoriDispatchMessage +{ + constructor( + private notify: GroupNotifyGuildMemberRequest, + private uin: string, + ) {} + + type = 'satori' as const + + toSatori = async ( + ctx: ChronocatContext, + _config: O.Intersect< + ChronocatLogCurrentConfig, + ChronocatSatoriEventsConfig + >, + ) => { + const event: Event = { + id: undefined as unknown as number, + type: 'guild-member-request', + + platform: ctx.chronocat.platform, + self_id: undefined as unknown as string, + timestamp: new Date().getTime(), + + guild: { + id: this.notify.group.groupCode, + name: this.notify.group.groupName, + avatar: `https://p.qlogo.cn/gh/${this.notify.group.groupCode}/${this.notify.group.groupCode}/640`, + }, + + user: { + id: `${this.uin}`, + name: this.notify.user2.nickName, + avatar: `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${this.uin}&spec=640`, + }, + + message: { + id: undefined as unknown as string, + content: this.notify.postscript, + }, + } + + return [event] + } +} + +export class GuildMemberRemovedDispatchMessage + implements SatoriDispatchMessage +{ + constructor( + private notify: GroupNotifyGuildMemberRemoved, + private uin: string, + ) {} + + type = 'satori' as const + + toSatori = async ( + ctx: ChronocatContext, + _config: O.Intersect< + ChronocatLogCurrentConfig, + ChronocatSatoriEventsConfig + >, + ) => { + const event: Event = { + id: undefined as unknown as number, + type: 'guild-member-removed', + + platform: ctx.chronocat.platform, + self_id: undefined as unknown as string, + timestamp: new Date().getTime(), + + guild: { + id: this.notify.group.groupCode, + name: this.notify.group.groupName, + avatar: `https://p.qlogo.cn/gh/${this.notify.group.groupCode}/${this.notify.group.groupCode}/640`, + }, + + user: { + id: `${this.uin}`, + name: this.notify.user2.nickName, avatar: `http://thirdqq.qlogo.cn/headimg_dl?dst_uin=${this.uin}&spec=640`, }, diff --git a/packages/red/src/redIpcEntity.ts b/packages/red/src/redIpcEntity.ts index f1d8b81..6143267 100644 --- a/packages/red/src/redIpcEntity.ts +++ b/packages/red/src/redIpcEntity.ts @@ -216,39 +216,30 @@ export interface OnGroupSingleScreenNotifies { notifies: GroupNotify[] } -export interface GroupNotify { +export type GroupNotify = + | GroupNotifyGuildRequest + | GroupNotifyGuildMemberRequest + | GroupNotifyGuildMemberRemoved + | GroupNotifyGuildSetAdmin + +export interface GroupNotifyBase { seq: string // '1716209619000000' - type: number // 7 申请入群,11 已退群 - - status: number // 0 无需操作,1 未操作,2 已操作 - group: { groupCode: string groupName: string } - user1: { - uid: string - nickName: string - } - - user2: { - uid: string - nickName: string - } - - actionUser: { - uid: string - nickName: string - } - - actionTime: string // '0' + actionTime: string // '0',status 变 2/3 以后有值 invitationExt: { srcType: number // 0 groupCode: string // '0' - waitStatus: number // 0 + + /** + * 2:管理员已同意 + */ + waitStatus: 0 | 2 } /** @@ -256,7 +247,155 @@ export interface GroupNotify { */ postscript: string - repeatSeqs: [] + repeatSeqs: string[] warningTips: string // '该账号存在风险,请谨慎操作' } + +export interface GroupNotifyGuildRequest extends GroupNotifyBase { + /** + * guild-request 事件 + */ + type: 1 + + /** + * 1:等待操作 + * 2:已同意 + * 3:已拒绝 + */ + status: 1 | 2 | 3 + + /** + * 自身,可忽略该字段 + */ + user1: { + uid: string + nickName: string + } + + /** + * 邀请自身进群的人 + */ + user2: { + uid: string + nickName: string + } + + /** + * 自身,可忽略该字段 + */ + actionUser: { + uid: string + nickName: string + } +} + +export interface GroupNotifyGuildMemberRequest extends GroupNotifyBase { + /** + * guild-member-request 事件 + */ + type: 7 + + /** + * 1:等待操作 + * 2:已同意 + * 3:已拒绝 + */ + status: 1 | 2 | 3 + + /** + * 申请进群的人 + */ + user1: { + uid: string + nickName: string + } + + /** + * 值始终为空 + */ + user2: { + uid: '' + nickName: '' + } + + /** + * 处理的管理员 + */ + actionUser: { + uid: string + nickName: string + } +} + +export interface GroupNotifyGuildMemberRemoved extends GroupNotifyBase { + /** + * guild-member-removed 事件 + */ + type: 11 + + /** + * 无需任何操作 + */ + status: 0 + + /** + * 退群的用户 + */ + user1: { + uid: string + nickName: string + } + + /** + * 值始终为空 + */ + user2: { + uid: '' + nickName: '' + } + + /** + * 值始终为空 + */ + actionUser: { + uid: '' + nickName: '' + } +} + +export interface GroupNotifyGuildSetAdmin extends GroupNotifyBase { + /** + * 群主将某群员设置为管理员 + */ + type: 8 + + /** + * 无需任何操作 + */ + status: 0 + + /** + * 自身,可忽略该字段 + */ + user1: { + uid: string + nickName: string + } + + /** + * 将自身设置为管理员的人(群主) + */ + user2: { + uid: string + nickName: string + } + + /** + * 值始终为空 + */ + actionUser: { + uid: '' + nickName: '' + } +}