From f06284b5e243179eb41fdf16b2fe295eb263994c Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Wed, 29 Mar 2023 18:14:15 +0800 Subject: [PATCH] update --- internal/logic/app/message_app.go | 7 ++++--- .../logic/domain/friend/friend_service.go | 19 ++++++++++++++++--- internal/logic/domain/group/model/group.go | 18 +++++++++++++++--- .../logic/domain/message/repo/message_repo.go | 15 +++++++++++++-- .../domain/message/service/message_service.go | 15 +++++---------- internal/logic/proxy/message_proxy.go | 3 ++- pkg/proto/connect.ext.proto | 1 + 7 files changed, 56 insertions(+), 22 deletions(-) diff --git a/internal/logic/app/message_app.go b/internal/logic/app/message_app.go index d9190eb..f34909b 100644 --- a/internal/logic/app/message_app.go +++ b/internal/logic/app/message_app.go @@ -4,6 +4,7 @@ import ( "context" "gim/internal/logic/domain/message/service" "gim/pkg/pb" + "time" "google.golang.org/protobuf/proto" ) @@ -18,8 +19,8 @@ func (*messageApp) SendToUser(ctx context.Context, sender *pb.Sender, toUserId i } // RecallMessageSendToUser 撤回消息发送给用户 -func (*messageApp) RecallMessageSendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.RecallMessageReq, isRecallMessageUser bool) (int64, error) { - return service.MessageService.RecallMessageSendToUser(ctx, sender, toUserId, req, isRecallMessageUser) +func (*messageApp) RecallMessageSendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.RecallMessageReq, sendTime time.Time) (int64, error) { + return service.MessageService.RecallMessageSendToUser(ctx, sender, toUserId, req, sendTime) } // SendRedPackageToUser 发送红包给用户 @@ -79,7 +80,7 @@ func (s *messageApp) RecallMessage(ctx context.Context, sender *pb.Sender, req * if sender.SenderType == pb.SenderType_ST_USER { return FriendApp.RecallMessageSendToFriend(ctx, sender, req) } else { - return s.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, true) + //return s.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, true) } // 消息接收者是群组 case pb.ReceiverType_RT_GROUP: diff --git a/internal/logic/domain/friend/friend_service.go b/internal/logic/domain/friend/friend_service.go index fb74fbd..fb0c43a 100644 --- a/internal/logic/domain/friend/friend_service.go +++ b/internal/logic/domain/friend/friend_service.go @@ -4,11 +4,13 @@ import ( "context" svc "gim/internal/business/comm/service" "gim/internal/business/comm/utils" + "gim/internal/logic/domain/message/repo" "gim/internal/logic/proxy" "gim/pkg/gerrors" "gim/pkg/grpclib" "gim/pkg/pb" "gim/pkg/rpc" + "google.golang.org/protobuf/proto" "time" ) @@ -225,19 +227,30 @@ func (*friendService) RecallMessageSendToFriend(ctx context.Context, sender *pb. if friend == nil || friend.Status != FriendStatusAgree { return 0, gerrors.ErrNotIsFriend } - utils.FilePutContents("RecallMessageSendToFriend", utils.SerializeStr(map[string]interface{}{ "send": sender, "req": req, })) + + //查询到对应seq的消息 + msg := &pb.RECALL{} + err = proto.Unmarshal(req.MessageContent, msg) + if err != nil { + return 0, err + } + message, err := repo.MessageRepo.GetMessage(sender.SenderId, msg.RecallSeq) + if err != nil { + return 0, err + } + // 发给发送者 - seq, err := proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, true) + seq, err := proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, message.SendTime) if err != nil { return 0, err } // 发给接收者 - _, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, false) + _, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, message.SendTime) if err != nil { return 0, err } diff --git a/internal/logic/domain/group/model/group.go b/internal/logic/domain/group/model/group.go index 0d33ff0..440e52d 100644 --- a/internal/logic/domain/group/model/group.go +++ b/internal/logic/domain/group/model/group.go @@ -2,6 +2,7 @@ package model import ( "context" + "gim/internal/logic/domain/message/repo" "gim/internal/logic/proxy" "gim/pkg/gerrors" "gim/pkg/grpclib" @@ -167,12 +168,23 @@ func (g *Group) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *p logger.Sugar.Error(ctx, sender.SenderId, req.ReceiverId, "不在群组内") return 0, gerrors.ErrNotInGroup } + var err error + + //查询到对应seq的消息 + msg := &pb.RECALL{} + err = proto.Unmarshal(req.MessageContent, msg) + if err != nil { + return 0, err + } + message, err := repo.MessageRepo.GetMessage(sender.SenderId, msg.RecallSeq) + if err != nil { + return 0, err + } // 如果发送者是用户,将消息发送给发送者,获取用户seq var userSeq int64 - var err error if sender.SenderType == pb.SenderType_ST_USER { - userSeq, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, true) + userSeq, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, message.SendTime) if err != nil { return 0, err } @@ -186,7 +198,7 @@ func (g *Group) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *p if sender.SenderType == pb.SenderType_ST_USER && user.UserId == sender.SenderId { continue } - _, err := proxy.MessageProxy.RecallMessageSendToUser(grpclib.NewAndCopyRequestId(ctx), sender, user.UserId, req, false) + _, err := proxy.MessageProxy.RecallMessageSendToUser(grpclib.NewAndCopyRequestId(ctx), sender, user.UserId, req, message.SendTime) if err != nil { return } diff --git a/internal/logic/domain/message/repo/message_repo.go b/internal/logic/domain/message/repo/message_repo.go index 103d412..8d17a2a 100644 --- a/internal/logic/domain/message/repo/message_repo.go +++ b/internal/logic/domain/message/repo/message_repo.go @@ -5,6 +5,7 @@ import ( "gim/internal/logic/domain/message/model" "gim/pkg/db" "gim/pkg/gerrors" + "time" ) const messageTableNum = 1 @@ -48,9 +49,19 @@ func (d *messageRepo) ListBySeq(userId, seq, limit int64) ([]model.Message, bool return messages, count > limit, nil } +// GetMessage 获取消息状态 +func (d *messageRepo) GetMessage(senderId, seq int64) (*model.Message, error) { + var message model.Message + if err := db.DB.Where("sender_id = ? and seq = ?", senderId, seq). + First(&message).Error; err != nil { + return nil, gerrors.WrapError(err) + } + return &message, nil +} + // UpdateStatus 更新消息状态 -func (d *messageRepo) UpdateStatus(senderId, seq int64, status int) (int64, error) { - db := db.DB.Model(&model.Message{}).Where("sender_id = ? and seq = ?", senderId, seq). +func (d *messageRepo) UpdateStatus(senderId int64, sendTime time.Time, status int) (int64, error) { + db := db.DB.Model(&model.Message{}).Where("sender_id = ? and send_time = ?", senderId, sendTime). Update("status", status) if db.Error != nil { return 0, gerrors.WrapError(db.Error) diff --git a/internal/logic/domain/message/service/message_service.go b/internal/logic/domain/message/service/message_service.go index 5273eb4..be7e4c6 100644 --- a/internal/logic/domain/message/service/message_service.go +++ b/internal/logic/domain/message/service/message_service.go @@ -257,7 +257,7 @@ func (*messageService) SendToUser(ctx context.Context, sender *pb.Sender, toUser } // RecallMessageSendToUser 撤回消息用户 -func (*messageService) RecallMessageSendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.RecallMessageReq, isRecallMessageUser bool) (int64, error) { +func (*messageService) RecallMessageSendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.RecallMessageReq, sendTime time.Time) (int64, error) { masterId, _ := grpclib.GetCtxMasterId(ctx) logger.Logger.Debug("SendToUser", zap.String("master_id", masterId), @@ -268,15 +268,10 @@ func (*messageService) RecallMessageSendToUser(ctx context.Context, sender *pb.S err error ) - //1、解析消息体内容,content值为撤回消息的req - msg := &pb.RECALL{} - err = proto.Unmarshal(req.MessageContent, msg) - if isRecallMessageUser { - //2、改变消息状态 - _, err = repo.MessageRepo.UpdateStatus(sender.SenderId, msg.RecallSeq, int(pb.MessageStatus_MS_RECALL)) - if err != nil { - return 0, err - } + //1、改变消息状态 + _, err = repo.MessageRepo.UpdateStatus(sender.SenderId, sendTime, int(pb.MessageStatus_MS_RECALL)) + if err != nil { + return 0, err } //3、发送一条新的消息 diff --git a/internal/logic/proxy/message_proxy.go b/internal/logic/proxy/message_proxy.go index 57c7973..1fa86d1 100644 --- a/internal/logic/proxy/message_proxy.go +++ b/internal/logic/proxy/message_proxy.go @@ -3,6 +3,7 @@ package proxy import ( "context" "gim/pkg/pb" + "time" "google.golang.org/protobuf/proto" ) @@ -11,7 +12,7 @@ var MessageProxy messageProxy type messageProxy interface { SendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.SendMessageReq) (int64, error) - RecallMessageSendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.RecallMessageReq, isRecallMessageUser bool) (int64, error) + RecallMessageSendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.RecallMessageReq, sendTime time.Time) (int64, error) SendRedPackageToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.SendRedPacketReq) (int64, error) PushToUser(ctx context.Context, userId int64, code pb.PushCode, message proto.Message, isPersist bool) error } diff --git a/pkg/proto/connect.ext.proto b/pkg/proto/connect.ext.proto index e8562f5..50b1efc 100644 --- a/pkg/proto/connect.ext.proto +++ b/pkg/proto/connect.ext.proto @@ -146,6 +146,7 @@ message RED_PACKAGE { repeated float received_user_amount = 9; // 已领取用户金额 repeated string received_user_nickname = 10; // 已领取用户昵称 RedPacketStatusType red_packet_status_type = 11; // 领取状态 + int64 red_packet_id = 12; // 红包id } /************************************消息体定义结束************************************/