Skip to content

Commit

Permalink
feat(event): refactor guild-request/guild-member-request/`guild-m…
Browse files Browse the repository at this point in the history
…ember-removed` events
  • Loading branch information
ilharp committed Sep 14, 2024
1 parent a5994eb commit fe48309
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 29 deletions.
4 changes: 3 additions & 1 deletion packages/engine-chronocat-event/src/globalVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { Group, Profile } from '@chronocat/red'

export const requestMethodMap: Record<string, string> = {}
export const emittedBuddyReqList: string[] = []
export const emittedGroupReqList: string[] = []
export const emittedGuildRequestList: string[] = []
export const emittedGuildMemberRequestList: string[] = []
export const emittedGuildMemberRemovedList: string[] = []

export const sendQueue: string[] = []

Expand Down
77 changes: 66 additions & 11 deletions packages/engine-chronocat-event/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ import type { ChronocatContext } from '@chronocat/shell'
import type { IpcManData } from 'ipcman'
import {
emittedBuddyReqList,
emittedGroupReqList,
emittedGuildMemberRemovedList,
emittedGuildMemberRequestList,
emittedGuildRequestList,
friendMap,
groupMap,
requestMethodMap,
sendQueue,
} from './globalVars'
import {
FriendRequestDispatchMessage,
GuildMemberRemovedDispatchMessage,
GuildMemberRequestDispatchMessage,
GuildRequestDispatchMessage,
MessageCreatedDispatchMessage,
MessageDeletedDispatchMessage,
Expand Down Expand Up @@ -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 (emittedGroupReqList.includes(key)) return
const key = `${x.group.groupCode}:${uin}:${x.seq}`
if (emittedGuildMemberRequestList.includes(key)) return
emittedGuildMemberRequestList.push(key)

emittedGroupReqList.push(key)
// x.actionUser 此时一定为空

ctx.chronocat.emit(new GuildRequestDispatchMessage(x, uin))
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
// }
}
})

return
Expand Down
106 changes: 103 additions & 3 deletions packages/engine-chronocat-event/src/messages.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -40,7 +46,7 @@ export class MessageDeletedDispatchMessage implements SatoriDispatchMessage {

export class GuildRequestDispatchMessage implements SatoriDispatchMessage {
constructor(
private notify: GroupNotify,
private notify: GroupNotifyGuildRequest,
private uin: string,
) {}

Expand Down Expand Up @@ -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`,
},

Expand Down
Loading

0 comments on commit fe48309

Please sign in to comment.