From b22bfd7d71ba74fcf3da842013a28ade4315301a Mon Sep 17 00:00:00 2001 From: Redmomn <109732988+Redmomn@users.noreply.github.com> Date: Fri, 29 Nov 2024 18:26:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AD=97=E6=AE=B5=E7=B2=BE?= =?UTF-8?q?=E7=AE=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/entity/group_member.go | 7 +- client/event/group.go | 140 ++++++++++++++------------- client/listener.go | 18 ++-- client/message.go | 6 +- client/operation.go | 2 +- client/packets/oidb/fetch_member.go | 10 +- client/packets/oidb/fetch_members.go | 10 +- message/build.go | 2 +- 8 files changed, 99 insertions(+), 96 deletions(-) diff --git a/client/entity/group_member.go b/client/entity/group_member.go index 073d803..8820a55 100644 --- a/client/entity/group_member.go +++ b/client/entity/group_member.go @@ -9,22 +9,19 @@ const ( ) type GroupMember struct { - Uin uint32 - UID string + User Permission GroupMemberPermission GroupLevel uint32 MemberCard string - MemberName string SpecialTitle string JoinTime uint32 LastMsgTime uint32 ShutUpTime uint32 - Avatar string } func (m *GroupMember) DisplayName() string { if m.MemberCard != "" { return m.MemberCard } - return m.MemberName + return m.Nickname } diff --git a/client/event/group.go b/client/event/group.go index ee1a1b0..521e47e 100644 --- a/client/event/group.go +++ b/client/event/group.go @@ -10,37 +10,37 @@ import ( ) type ( + // GroupEvent 通用群事件 GroupEvent struct { GroupUin uint32 + // 触发事件的主体 详看各事件注释 + UserUin uint32 + UserUID string } + // GroupMemberPermissionChanged 群成员权限变更 GroupMemberPermissionChanged struct { GroupEvent - TargetUin uint32 - TargetUID string - IsAdmin bool + IsAdmin bool } + // GroupNameUpdated 群名变更 GroupNameUpdated struct { - GroupUin uint32 - NewName string - OperatorUin uint32 - OperatorUID string + GroupEvent + NewName string } + // GroupMute 群内禁言事件 user为被禁言的成员 GroupMute struct { GroupEvent - OperatorUID string + OperatorUID string // when TargetUID is empty, mute all members OperatorUin uint32 - TargetUID string // when TargetUID is empty, mute all members - TargetUin uint32 Duration uint32 // Duration == math.MaxUint32 when means mute all } + // GroupRecall 群内消息撤回 user为消息发送者 GroupRecall struct { GroupEvent - AuthorUID string - AuthorUin uint32 OperatorUID string OperatorUin uint32 Sequence uint64 @@ -48,10 +48,9 @@ type ( Random uint32 } + // GroupMemberJoinRequest 加群请求 user为请求加群的成员 GroupMemberJoinRequest struct { GroupEvent - TargetUID string - TargetUin uint32 TargetNick string InvitorUID string InvitorUin uint32 @@ -59,60 +58,57 @@ type ( RequestSeq uint64 } + // GroupMemberIncrease 群成员增加事件 user为新加群的成员 GroupMemberIncrease struct { GroupEvent - MemberUID string - MemberUin uint32 InvitorUID string InvitorUin uint32 JoinType uint32 } + // GroupMemberDecrease 群成员减少事件 user为退群的成员 GroupMemberDecrease struct { GroupEvent - MemberUID string - MemberUin uint32 OperatorUID string OperatorUin uint32 ExitType uint32 } - // GroupDigestEvent 群精华消息 from miraigo + // GroupDigestEvent 群精华消息 user为消息发送者 + // from miraigo GroupDigestEvent struct { - GroupUin uint32 + GroupEvent MessageID uint32 InternalMessageID uint32 OperationType uint32 // 1 -> 设置精华消息, 2 -> 移除精华消息 OperateTime uint32 - SenderUin uint32 OperatorUin uint32 SenderNick string OperatorNick string } - // GroupPokeEvent 群戳一戳事件 from miraigo + // GroupPokeEvent 群戳一戳事件 user为发送者 + // from miraigo GroupPokeEvent struct { - GroupUin uint32 - Sender uint32 + GroupEvent Receiver uint32 Suffix string Action string } + // GroupReactionEvent 群消息表态 user为发送表态的成员 GroupReactionEvent struct { - GroupUin uint32 - TargetSeq uint32 - OperatorUID string - OperatorUin uint32 - IsAdd bool - Code string - Count uint32 + GroupEvent + TargetSeq uint32 + IsAdd bool + Code string + Count uint32 } - // MemberSpecialTitleUpdated 群成员头衔更新事件 from miraigo + // MemberSpecialTitleUpdated 群成员头衔更新事件 + // from miraigo MemberSpecialTitleUpdated struct { - GroupUin uint32 - Uin uint32 + GroupEvent NewTitle string } ) @@ -151,7 +147,7 @@ func (g *GroupDigestEvent) IsSet() bool { } func (g *GroupMemberPermissionChanged) ResolveUin(f func(uid string, groupUin ...uint32) uint32) { - g.TargetUin = f(g.TargetUID, g.GroupUin) + g.UserUin = f(g.UserUID, g.GroupUin) } func ParseGroupMemberPermissionChanged(event *message.GroupAdmin) *GroupMemberPermissionChanged { @@ -167,21 +163,23 @@ func ParseGroupMemberPermissionChanged(event *message.GroupAdmin) *GroupMemberPe return &GroupMemberPermissionChanged{ GroupEvent: GroupEvent{ GroupUin: event.GroupUin, + UserUID: uid, }, - TargetUID: uid, - IsAdmin: isAdmin, + IsAdmin: isAdmin, } } func (g *GroupNameUpdated) ResolveUin(f func(uid string, groupUin ...uint32) uint32) { - g.OperatorUin = f(g.OperatorUID, g.GroupUin) + g.UserUin = f(g.UserUID, g.GroupUin) } func ParseGroupNameUpdatedEvent(event *message.NotifyMessageBody, groupName string) *GroupNameUpdated { return &GroupNameUpdated{ - GroupUin: event.GroupUin, - NewName: groupName, - OperatorUID: event.OperatorUid, + GroupEvent: GroupEvent{ + GroupUin: event.GroupUin, + UserUID: event.OperatorUid, + }, + NewName: groupName, } } @@ -194,9 +192,9 @@ func ParseRequestJoinNotice(event *message.GroupJoin) *GroupMemberJoinRequest { return &GroupMemberJoinRequest{ GroupEvent: GroupEvent{ GroupUin: event.GroupUin, + UserUID: event.TargetUid, }, - TargetUID: event.TargetUid, - Answer: event.RequestField.Unwrap(), + Answer: event.RequestField.Unwrap(), } } @@ -206,8 +204,8 @@ func ParseRequestInvitationNotice(event *message.GroupInvitation) *GroupMemberJo return &GroupMemberJoinRequest{ GroupEvent: GroupEvent{ GroupUin: inn.GroupUin, + UserUID: inn.TargetUid, }, - TargetUID: inn.TargetUid, InvitorUID: inn.InvitorUid, } } @@ -226,15 +224,15 @@ func ParseInviteNotice(event *message.GroupInvite) *GroupInvite { func (g *GroupMemberIncrease) ResolveUin(f func(uid string, groupUin ...uint32) uint32) { g.InvitorUin = f(g.InvitorUID, g.GroupUin) - g.MemberUin = f(g.MemberUID, g.GroupUin) + g.UserUin = f(g.UserUID, g.GroupUin) } func ParseMemberIncreaseEvent(event *message.GroupChange) *GroupMemberIncrease { return &GroupMemberIncrease{ GroupEvent: GroupEvent{ GroupUin: event.GroupUin, + UserUID: event.MemberUid, }, - MemberUID: event.MemberUid, InvitorUID: utils.B2S(event.Operator), JoinType: event.IncreaseType, } @@ -242,15 +240,15 @@ func ParseMemberIncreaseEvent(event *message.GroupChange) *GroupMemberIncrease { func (g *GroupMemberDecrease) ResolveUin(f func(uid string, groupUin ...uint32) uint32) { g.OperatorUin = f(g.OperatorUID, g.GroupUin) - g.MemberUin = f(g.MemberUID, g.GroupUin) + g.UserUin = f(g.UserUID, g.GroupUin) } func ParseMemberDecreaseEvent(event *message.GroupChange) *GroupMemberDecrease { return &GroupMemberDecrease{ GroupEvent: GroupEvent{ GroupUin: event.GroupUin, + UserUID: event.MemberUid, }, - MemberUID: event.MemberUid, OperatorUID: utils.B2S(event.Operator), ExitType: event.DecreaseType, } @@ -258,7 +256,7 @@ func ParseMemberDecreaseEvent(event *message.GroupChange) *GroupMemberDecrease { func (g *GroupRecall) ResolveUin(f func(uid string, groupUin ...uint32) uint32) { g.OperatorUin = f(g.OperatorUID, g.GroupUin) - g.AuthorUin = f(g.AuthorUID, g.GroupUin) + g.UserUin = f(g.UserUID, g.GroupUin) } func ParseGroupRecallEvent(event *message.NotifyMessageBody) *GroupRecall { @@ -266,8 +264,8 @@ func ParseGroupRecallEvent(event *message.NotifyMessageBody) *GroupRecall { result := GroupRecall{ GroupEvent: GroupEvent{ GroupUin: event.GroupUin, + UserUID: info.AuthorUid, }, - AuthorUID: info.AuthorUid, OperatorUID: event.Recall.OperatorUid.Unwrap(), Sequence: info.Sequence, Time: info.Time, @@ -278,28 +276,30 @@ func ParseGroupRecallEvent(event *message.NotifyMessageBody) *GroupRecall { func (g *GroupMute) ResolveUin(f func(uid string, groupUin ...uint32) uint32) { g.OperatorUin = f(g.OperatorUID, g.GroupUin) - g.TargetUin = f(g.TargetUID, g.GroupUin) + g.UserUin = f(g.UserUID, g.GroupUin) } func ParseGroupMuteEvent(event *message.GroupMute) *GroupMute { return &GroupMute{ GroupEvent: GroupEvent{ GroupUin: event.GroupUin, + UserUID: event.Data.State.TargetUid.Unwrap(), }, OperatorUID: event.OperatorUid.Unwrap(), - TargetUID: event.Data.State.TargetUid.Unwrap(), Duration: event.Data.State.Duration, } } func ParseGroupDigestEvent(event *message.NotifyMessageBody) *GroupDigestEvent { return &GroupDigestEvent{ - GroupUin: event.EssenceMessage.GroupUin, + GroupEvent: GroupEvent{ + GroupUin: event.EssenceMessage.GroupUin, + UserUin: event.EssenceMessage.AuthorUin, + }, MessageID: event.EssenceMessage.MsgSequence, InternalMessageID: event.EssenceMessage.Random, OperationType: event.EssenceMessage.SetFlag, OperateTime: event.EssenceMessage.TimeStamp, - SenderUin: event.EssenceMessage.AuthorUin, OperatorUin: event.EssenceMessage.OperatorUin, SenderNick: event.EssenceMessage.AuthorName, OperatorNick: event.EssenceMessage.OperatorName, @@ -309,8 +309,10 @@ func ParseGroupDigestEvent(event *message.NotifyMessageBody) *GroupDigestEvent { func ParseGroupPokeEvent(event *message.NotifyMessageBody, groupUin uint32) *GroupPokeEvent { e := ParsePokeEvent(event.GrayTipInfo) return &GroupPokeEvent{ - GroupUin: groupUin, - Sender: e.Sender, + GroupEvent: GroupEvent{ + GroupUin: groupUin, + UserUin: e.Sender, + }, Receiver: e.Receiver, Suffix: e.Suffix, Action: e.Action, @@ -329,24 +331,28 @@ func ParseGroupMemberSpecialTitleUpdatedEvent(event *message.GroupSpecialTitle, return nil } return &MemberSpecialTitleUpdated{ - GroupUin: groupUin, - Uin: event.TargetUin, + GroupEvent: GroupEvent{ + GroupUin: groupUin, + UserUin: event.TargetUin, + }, NewTitle: medalData.Text, } } func (g *GroupReactionEvent) ResolveUin(f func(uid string, groupUin ...uint32) uint32) { - g.OperatorUin = f(g.OperatorUID, g.GroupUin) + g.UserUin = f(g.UserUID, g.GroupUin) } func ParseGroupReactionEvent(event *message.GroupReaction) *GroupReactionEvent { return &GroupReactionEvent{ - GroupUin: event.GroupUid, - TargetSeq: event.Data.Data.Data.Target.Sequence, - OperatorUID: event.Data.Data.Data.Data.OperatorUid, - IsAdd: event.Data.Data.Data.Data.Type == 1, - Code: event.Data.Data.Data.Data.Code, - Count: event.Data.Data.Data.Data.Count, + GroupEvent: GroupEvent{ + GroupUin: event.GroupUid, + UserUID: event.Data.Data.Data.Data.OperatorUid, + }, + TargetSeq: event.Data.Data.Data.Target.Sequence, + IsAdd: event.Data.Data.Data.Data.Type == 1, + Code: event.Data.Data.Data.Data.Code, + Count: event.Data.Data.Data.Data.Count, } } @@ -356,7 +362,7 @@ func (g *GroupPokeEvent) From() uint32 { func (g *GroupPokeEvent) Content() string { if g.Suffix != "" { - return fmt.Sprintf("%d%s%d的%s", g.Sender, g.Action, g.Receiver, g.Suffix) + return fmt.Sprintf("%d%s%d的%s", g.UserUin, g.Action, g.Receiver, g.Suffix) } - return fmt.Sprintf("%d%s%d", g.Sender, g.Action, g.Receiver) + return fmt.Sprintf("%d%s%d", g.UserUin, g.Action, g.Receiver) } diff --git a/client/listener.go b/client/listener.go index 4212b9b..3cf5f1b 100644 --- a/client/listener.go +++ b/client/listener.go @@ -75,11 +75,11 @@ func decodeOlPushServicePacket(c *QQClient, pkt *network.Packet) (any, error) { } ev := eventConverter.ParseMemberIncreaseEvent(&pb) _ = c.ResolveUin(ev) - if ev.MemberUin == c.Uin { // bot 进群 + if ev.UserUin == c.Uin { // bot 进群 _ = c.RefreshAllGroupsInfo() c.GroupJoinEvent.dispatch(c, ev) } else { - _ = c.RefreshGroupMemberCache(ev.GroupUin, ev.MemberUin) + _ = c.RefreshGroupMemberCache(ev.GroupUin, ev.UserUin) c.GroupMemberJoinEvent.dispatch(c, ev) } return nil, nil @@ -100,7 +100,7 @@ func decodeOlPushServicePacket(c *QQClient, pkt *network.Packet) (any, error) { } ev := eventConverter.ParseMemberDecreaseEvent(&pb) _ = c.ResolveUin(ev) - if ev.MemberUin == c.Uin { + if ev.UserUin == c.Uin { c.GroupLeaveEvent.dispatch(c, ev) } else { c.GroupMemberLeaveEvent.dispatch(c, ev) @@ -114,7 +114,7 @@ func decodeOlPushServicePacket(c *QQClient, pkt *network.Packet) (any, error) { } ev := eventConverter.ParseGroupMemberPermissionChanged(&pb) _ = c.ResolveUin(ev) - _ = c.RefreshGroupMemberCache(ev.GroupUin, ev.TargetUin) + _ = c.RefreshGroupMemberCache(ev.GroupUin, ev.UserUin) c.GroupMemberPermissionChangedEvent.dispatch(c, ev) case 84: // group request join notice pb := message.GroupJoin{} @@ -124,16 +124,16 @@ func decodeOlPushServicePacket(c *QQClient, pkt *network.Packet) (any, error) { } ev := eventConverter.ParseRequestJoinNotice(&pb) _ = c.ResolveUin(ev) - user, _ := c.FetchUserInfo(ev.TargetUID) + user, _ := c.FetchUserInfo(ev.UserUID) if user != nil { - ev.TargetUin = user.Uin + ev.UserUin = user.Uin ev.TargetNick = user.Nickname } commonRequests, reqErr := c.GetGroupSystemMessages(false, 20, ev.GroupUin) filteredRequests, freqErr := c.GetGroupSystemMessages(true, 20, ev.GroupUin) if reqErr == nil && freqErr == nil { for _, request := range append(commonRequests.JoinRequests, filteredRequests.JoinRequests...) { - if request.TargetUID == ev.TargetUID && !request.Checked { + if request.TargetUID == ev.UserUID && !request.Checked { ev.RequestSeq = request.Sequence ev.Answer = request.Comment } @@ -152,9 +152,9 @@ func decodeOlPushServicePacket(c *QQClient, pkt *network.Packet) (any, error) { } ev := eventConverter.ParseRequestInvitationNotice(&pb) _ = c.ResolveUin(ev) - user, _ := c.FetchUserInfo(ev.TargetUID) + user, _ := c.FetchUserInfo(ev.UserUID) if user != nil { - ev.TargetUin = user.Uin + ev.UserUin = user.Uin ev.TargetNick = user.Nickname } c.GroupMemberJoinRequestEvent.dispatch(c, ev) diff --git a/client/message.go b/client/message.go index 8c70625..b1b0c5b 100644 --- a/client/message.go +++ b/client/message.go @@ -202,11 +202,7 @@ func (c *QQClient) preProcessGroupMessage(groupUin uint32, elements []message2.I member := c.GetCachedMemberInfo(elem.TargetUin, groupUin) if member != nil { elem.TargetUID = member.UID - if member.MemberCard != "" { - elem.Display = "@" + member.MemberCard - } else { - elem.Display = "@" + member.MemberName - } + elem.Display = "@" + member.DisplayName() } case *message2.ImageElement: if elem.MsgInfo != nil { diff --git a/client/operation.go b/client/operation.go index ebdcf9a..0536292 100644 --- a/client/operation.go +++ b/client/operation.go @@ -1047,7 +1047,7 @@ func (c *QQClient) FetchEssenceMessage(groupUin uint32) ([]*message2.GroupEssenc Sender: &message2.Sender{ Uin: senderUin, UID: c.GetUID(senderUin, groupUin), - Nickname: senderInfo.MemberName, + Nickname: senderInfo.Nickname, CardName: senderInfo.MemberCard, }, Time: uint32(v.Get("sender_time").Int()), diff --git a/client/packets/oidb/fetch_member.go b/client/packets/oidb/fetch_member.go index 96a1ee6..6fb2227 100644 --- a/client/packets/oidb/fetch_member.go +++ b/client/packets/oidb/fetch_member.go @@ -35,16 +35,18 @@ func ParseFetchMemberResp(data []byte) (*entity.GroupMember, error) { interner := utils.NewStringInterner() member := resp.Member m := &entity.GroupMember{ - Uin: member.Uin.Uin, - UID: interner.Intern(member.Uin.Uid), + User: entity.User{ + Uin: member.Uin.Uin, + UID: interner.Intern(member.Uin.Uid), + Nickname: interner.Intern(member.MemberName), + Avatar: interner.Intern(entity.UserAvatar(member.Uin.Uin)), + }, Permission: entity.GroupMemberPermission(member.Permission), MemberCard: interner.Intern(member.MemberCard.MemberCard.Unwrap()), - MemberName: interner.Intern(member.MemberName), SpecialTitle: interner.Intern(member.SpecialTitle.Unwrap()), JoinTime: member.JoinTimestamp, LastMsgTime: member.LastMsgTimestamp, ShutUpTime: member.ShutUpTimestamp.Unwrap(), - Avatar: interner.Intern(entity.UserAvatar(member.Uin.Uin)), } if member.Level != nil { m.GroupLevel = member.Level.Level diff --git a/client/packets/oidb/fetch_members.go b/client/packets/oidb/fetch_members.go index 83a68ff..12d75a0 100644 --- a/client/packets/oidb/fetch_members.go +++ b/client/packets/oidb/fetch_members.go @@ -38,16 +38,18 @@ func ParseFetchMembersResp(data []byte) ([]*entity.GroupMember, string, error) { for i, member := range resp.Members { // 由于protobuf的优化策略,默认值不会被编码进实际的二进制流中 m := &entity.GroupMember{ - Uin: member.Uin.Uin, - UID: interner.Intern(member.Uin.Uid), + User: entity.User{ + Uin: member.Uin.Uin, + UID: interner.Intern(member.Uin.Uid), + Nickname: interner.Intern(member.MemberName), + Avatar: interner.Intern(entity.UserAvatar(member.Uin.Uin)), + }, Permission: entity.GroupMemberPermission(member.Permission), MemberCard: interner.Intern(member.MemberCard.MemberCard.Unwrap()), - MemberName: interner.Intern(member.MemberName), SpecialTitle: interner.Intern(member.SpecialTitle.Unwrap()), JoinTime: member.JoinTimestamp, LastMsgTime: member.LastMsgTimestamp, ShutUpTime: member.ShutUpTimestamp.Unwrap(), - Avatar: interner.Intern(entity.UserAvatar(member.Uin.Uin)), } if member.Level != nil { m.GroupLevel = member.Level.Level diff --git a/message/build.go b/message/build.go index d75a9a5..a419469 100644 --- a/message/build.go +++ b/message/build.go @@ -176,7 +176,7 @@ func (e *ForwardMessage) BuildElement() []*message.Elem { var metaSource string if len(e.Nodes) > 0 { - isSenderNameExist := make(map[string]bool, 0) + isSenderNameExist := make(map[string]bool) isContainSelf := false isCount := 0 for _, v := range e.Nodes {