From e3411adb3d65a8a0db89e56895584ba0790c5e3b Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Thu, 11 Nov 2021 19:25:59 +0800 Subject: [PATCH] feat: GuildChannelUpdatedEvent --- client/entities.go | 8 ++++++++ client/events.go | 16 ++++++++++++++++ client/guild.go | 4 ++++ client/guild_eventflow.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/client/entities.go b/client/entities.go index 3583ef0f..bfbd5c65 100644 --- a/client/entities.go +++ b/client/entities.go @@ -230,6 +230,14 @@ type ( CurrentReactions []*message.GuildMessageEmojiReaction } + GuildChannelUpdatedEvent struct { + OperatorId uint64 + GuildId uint64 + ChannelId uint64 + OldChannelInfo *ChannelInfo + NewChannelInfo *ChannelInfo + } + OcrResponse struct { Texts []*TextDetection `json:"texts"` Language string `json:"language"` diff --git a/client/events.go b/client/events.go index 105a6b49..72ab18d6 100644 --- a/client/events.go +++ b/client/events.go @@ -16,6 +16,7 @@ type eventHandlers struct { selfGroupMessageHandlers []func(*QQClient, *message.GroupMessage) guildChannelMessageHandlers []func(*QQClient, *message.GuildChannelMessage) guildMessageReactionsUpdatedHandlers []func(*QQClient, *GuildMessageReactionsUpdatedEvent) + guildChannelUpdatedHandlers []func(*QQClient, *GuildChannelUpdatedEvent) groupMuteEventHandlers []func(*QQClient, *GroupMuteEvent) groupRecalledHandlers []func(*QQClient, *GroupMessageRecalledEvent) friendRecalledHandlers []func(*QQClient, *FriendMessageRecalledEvent) @@ -78,6 +79,10 @@ func (s *GuildService) OnGuildMessageReactionsUpdated(f func(*QQClient, *GuildMe s.c.eventHandlers.guildMessageReactionsUpdatedHandlers = append(s.c.eventHandlers.guildMessageReactionsUpdatedHandlers, f) } +func (s *GuildService) OnGuildChannelUpdated(f func(*QQClient, *GuildChannelUpdatedEvent)) { + s.c.eventHandlers.guildChannelUpdatedHandlers = append(s.c.eventHandlers.guildChannelUpdatedHandlers, f) +} + func (c *QQClient) OnGroupMuted(f func(*QQClient, *GroupMuteEvent)) { c.eventHandlers.groupMuteEventHandlers = append(c.eventHandlers.groupMuteEventHandlers, f) } @@ -262,6 +267,17 @@ func (c *QQClient) dispatchGuildMessageReactionsUpdatedEvent(e *GuildMessageReac } } +func (c *QQClient) dispatchGuildChannelUpdatedEvent(e *GuildChannelUpdatedEvent) { + if e == nil { + return + } + for _, f := range c.eventHandlers.guildChannelUpdatedHandlers { + cover(func() { + f(c, e) + }) + } +} + func (c *QQClient) dispatchGroupMuteEvent(e *GroupMuteEvent) { if e == nil { return diff --git a/client/guild.go b/client/guild.go index 04bbc1f5..a3a05c20 100644 --- a/client/guild.go +++ b/client/guild.go @@ -2,6 +2,7 @@ package client import ( "fmt" + "time" "github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/client/pb/channel" @@ -76,6 +77,8 @@ type ( ChannelType ChannelType AtAllSeq uint64 Meta *ChannelMeta + + fetchTime int64 } ChannelMeta struct { @@ -381,6 +384,7 @@ func convertChannelInfo(info *channel.GuildChannelInfo) *ChannelInfo { NotifyType: uint32(info.GetFinalNotifyType()), ChannelType: ChannelType(info.GetChannelType()), Meta: meta, + fetchTime: time.Now().Unix(), } } diff --git a/client/guild_eventflow.go b/client/guild_eventflow.go index 98401c22..7515d198 100644 --- a/client/guild_eventflow.go +++ b/client/guild_eventflow.go @@ -1,6 +1,7 @@ package client import ( + "sync" "time" "github.com/Mrs4s/MiraiGo/client/pb/channel" @@ -14,6 +15,10 @@ func init() { decoders["MsgPush.PushGroupProMsg"] = decodeGuildEventFlowPacket } +var ( + updateChanLock sync.Mutex +) + func decodeGuildEventFlowPacket(c *QQClient, _ *incomingPacketInfo, payload []byte) (interface{}, error) { push := new(channel.MsgOnlinePush) if err := proto.Unmarshal(payload, push); err != nil { @@ -59,6 +64,34 @@ func decodeGuildEventFlowPacket(c *QQClient, _ *incomingPacketInfo, payload []by c.Error("failed to unmarshal guild channel event body: %v", err) continue } + if eventBody.ChangeChanInfo != nil { + updateChanLock.Lock() + defer updateChanLock.Unlock() + guild := c.GuildService.FindGuild(m.Head.RoutingHead.GetGuildId()) + oldInfo := guild.FindChannel(eventBody.ChangeChanInfo.GetChanId()) + if time.Now().Unix()-oldInfo.fetchTime <= 2 { + continue + } + newInfo, err := c.GuildService.FetchChannelInfo(m.Head.RoutingHead.GetGuildId(), eventBody.ChangeChanInfo.GetChanId()) + if err != nil { + c.Error("error to decode channel info updated event: fetch channel info failed: %v", err) + continue + } + for i := range guild.Channels { + if guild.Channels[i].ChannelId == newInfo.ChannelId { + guild.Channels[i] = newInfo + break + } + } + c.dispatchGuildChannelUpdatedEvent(&GuildChannelUpdatedEvent{ + OperatorId: m.Head.RoutingHead.GetFromTinyid(), + GuildId: m.Head.RoutingHead.GetGuildId(), + ChannelId: eventBody.ChangeChanInfo.GetChanId(), + OldChannelInfo: oldInfo, + NewChannelInfo: newInfo, + }) + continue + } if eventBody.UpdateMsg != nil { if eventBody.UpdateMsg.GetEventType() == 1 || eventBody.UpdateMsg.GetEventType() == 2 { // todo: 撤回消息 continue