From d55929cf12411837af5990ee07efd09f094b5bec Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Mon, 13 Dec 2021 20:53:55 +0800 Subject: [PATCH] feat: PullGuildChannelMessage --- client/guild_eventflow.go | 30 ++------------------ client/guild_msg.go | 60 +++++++++++++++++++++++++++++++++++++-- message/message_guild.go | 1 + 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/client/guild_eventflow.go b/client/guild_eventflow.go index 33e07ceb..20bf48b8 100644 --- a/client/guild_eventflow.go +++ b/client/guild_eventflow.go @@ -10,7 +10,6 @@ import ( "github.com/Mrs4s/MiraiGo/client/pb/channel" "github.com/Mrs4s/MiraiGo/client/pb/msg" - "github.com/Mrs4s/MiraiGo/internal/packets" "github.com/Mrs4s/MiraiGo/internal/proto" ) @@ -92,7 +91,7 @@ func decodeGuildEventFlowPacket(c *QQClient, _ *incomingPacketInfo, payload []by // todo: direct message decode continue } - if cm := c.parseGuildChannelMessage(m); cm != nil { + if cm := c.GuildService.parseGuildChannelMessage(m); cm != nil { c.dispatchGuildChannelMessage(cm) } } @@ -206,7 +205,7 @@ func (c *QQClient) processGuildEventBody(m *channel.ChannelMsgContent, eventBody return } if eventBody.UpdateMsg.GetEventType() == 4 { // 消息贴表情更新 (包含添加或删除) - t, err := c.GuildService.pullRoamMsgByEventFlow(m.Head.RoutingHead.GetGuildId(), m.Head.RoutingHead.GetChannelId(), eventBody.UpdateMsg.GetMsgSeq(), eventBody.UpdateMsg.GetMsgSeq(), eventBody.UpdateMsg.GetEventVersion()-1) + t, err := c.GuildService.pullChannelMessages(m.Head.RoutingHead.GetGuildId(), m.Head.RoutingHead.GetChannelId(), eventBody.UpdateMsg.GetMsgSeq(), eventBody.UpdateMsg.GetMsgSeq(), eventBody.UpdateMsg.GetEventVersion()-1, false) if err != nil || len(t) == 0 { c.Error("process guild event flow error: pull eventMsg message error: %v", err) return @@ -236,28 +235,3 @@ func (c *QQClient) processGuildEventBody(m *channel.ChannelMsgContent, eventBody } } } - -func (s *GuildService) pullRoamMsgByEventFlow(guildId, channelId, beginSeq, endSeq, eventVersion uint64) ([]*channel.ChannelMsgContent, error) { - payload, _ := proto.Marshal(&channel.ChannelMsgReq{ - ChannelParam: &channel.ChannelParam{ - GuildId: &guildId, - ChannelId: &channelId, - BeginSeq: &beginSeq, - EndSeq: &endSeq, - Version: []uint64{eventVersion}, - }, - WithVersionFlag: proto.Uint32(1), - DirectMessageFlag: proto.Uint32(0), - }) - seq := s.c.nextSeq() - packet := packets.BuildUniPacket(s.c.Uin, seq, "trpc.group_pro.synclogic.SyncLogic.GetChannelMsg", 1, s.c.OutGoingPacketSessionId, []byte{}, s.c.sigInfo.d2Key, payload) - rsp, err := s.c.sendAndWaitDynamic(seq, packet) - if err != nil { - return nil, errors.Wrap(err, "send packet error") - } - msgRsp := new(channel.ChannelMsgRsp) - if err = proto.Unmarshal(rsp, msgRsp); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal protobuf message") - } - return msgRsp.ChannelMsg.Msgs, nil -} diff --git a/client/guild_msg.go b/client/guild_msg.go index a6b6b200..7e6bc9aa 100644 --- a/client/guild_msg.go +++ b/client/guild_msg.go @@ -168,6 +168,62 @@ ok: }, nil } +func (s *GuildService) PullGuildChannelMessage(guildId, channelId, beginSeq, endSeq uint64) (r []*message.GuildChannelMessage, e error) { + contents, err := s.pullChannelMessages(guildId, channelId, beginSeq, endSeq, 0, false) + if err != nil { + return nil, errors.Wrap(err, "pull channel message error") + } + for _, c := range contents { + if cm := s.parseGuildChannelMessage(c); cm != nil { + cm.Reactions = decodeGuildMessageEmojiReactions(c) + r = append(r, cm) + } + } + if len(r) == 0 { + return nil, errors.New("message not found") + } + return +} + +func (s *GuildService) pullChannelMessages(guildId, channelId, beginSeq, endSeq, eventVersion uint64, direct bool) ([]*channel.ChannelMsgContent, error) { + param := &channel.ChannelParam{ + GuildId: &guildId, + ChannelId: &channelId, + BeginSeq: &beginSeq, + EndSeq: &endSeq, + } + if eventVersion != 0 { + param.Version = []uint64{eventVersion} + } + + payload, _ := proto.Marshal(&channel.ChannelMsgReq{ + ChannelParam: param, + WithVersionFlag: proto.Uint32(func() uint32 { + if eventVersion != 0 { + return 1 + } + return 0 + }()), + DirectMessageFlag: proto.Uint32(func() uint32 { + if direct { + return 1 + } + return 0 + }()), + }) + seq := s.c.nextSeq() + packet := packets.BuildUniPacket(s.c.Uin, seq, "trpc.group_pro.synclogic.SyncLogic.GetChannelMsg", 1, s.c.OutGoingPacketSessionId, []byte{}, s.c.sigInfo.d2Key, payload) + rsp, err := s.c.sendAndWaitDynamic(seq, packet) + if err != nil { + return nil, errors.Wrap(err, "send packet error") + } + msgRsp := new(channel.ChannelMsgRsp) + if err = proto.Unmarshal(rsp, msgRsp); err != nil { + return nil, errors.Wrap(err, "failed to unmarshal protobuf message") + } + return msgRsp.ChannelMsg.Msgs, nil +} + func (c *QQClient) buildGuildImageStorePacket(guildId, channelId uint64, hash []byte, size uint64) (uint16, []byte) { seq := c.nextSeq() payload, _ := proto.Marshal(&cmd0x388.D388ReqBody{ @@ -264,8 +320,8 @@ func decodeGuildImageStoreResponse(_ *QQClient, _ *incomingPacketInfo, payload [ }, nil } -func (c *QQClient) parseGuildChannelMessage(msg *channel.ChannelMsgContent) *message.GuildChannelMessage { - guild := c.GuildService.FindGuild(msg.Head.RoutingHead.GetGuildId()) +func (s *GuildService) parseGuildChannelMessage(msg *channel.ChannelMsgContent) *message.GuildChannelMessage { + guild := s.FindGuild(msg.Head.RoutingHead.GetGuildId()) if guild == nil { return nil // todo: sync guild info } diff --git a/message/message_guild.go b/message/message_guild.go index 943910d0..ede327b8 100644 --- a/message/message_guild.go +++ b/message/message_guild.go @@ -9,6 +9,7 @@ type ( Time int64 Sender *GuildSender Elements []IMessageElement + Reactions []*GuildMessageEmojiReaction // only available for pulling messages } GuildMessageEmojiReaction struct {