From 992f76df0ee500a0377523b0780d3a85f2275755 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Wed, 23 Feb 2022 12:39:42 +0800 Subject: [PATCH] mark c2c add check --- internal/conversation_msg/conversation.go | 21 ++++++++++++++----- .../open_im_sdk_conversation_msg.go | 2 +- pkg/db/chat_log_model.go | 13 +++++++++++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/conversation_msg/conversation.go b/internal/conversation_msg/conversation.go index 05d2241ee..f1e57ff40 100644 --- a/internal/conversation_msg/conversation.go +++ b/internal/conversation_msg/conversation.go @@ -299,12 +299,23 @@ func (c *Conversation) typingStatusUpdate(callback open_im_sdk_callback.Base, re } -func (c *Conversation) markC2CMessageAsRead(callback open_im_sdk_callback.Base, msgIDList sdk.MarkC2CMessageAsReadParams, sourceMsgIDList, userID, operationID string) { +func (c *Conversation) markC2CMessageAsRead(callback open_im_sdk_callback.Base, msgIDList sdk.MarkC2CMessageAsReadParams, userID, operationID string) { var localMessage db.LocalChatLog + var newMessageIDList []string + messages, err := c.db.GetMultipleMessage(msgIDList) + common.CheckDBErrCallback(callback, err, operationID) + for _, v := range messages { + if v.IsRead == false && v.ContentType < constant.NotificationBegin { + newMessageIDList = append(newMessageIDList, v.ClientMsgID) + } + } + if len(newMessageIDList) == 0 { + common.CheckAnyErrCallback(callback, 201, errors.New("message has been marked read"), operationID) + } conversationID := utils.GetConversationIDBySessionType(userID, constant.SingleChatType) s := sdk_struct.MsgStruct{} c.initBasicInfo(&s, constant.UserMsgType, constant.HasReadReceipt, operationID) - s.Content = sourceMsgIDList + s.Content = utils.StructToJsonString(newMessageIDList) options := make(map[string]bool, 5) utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, false) utils.SetSwitchFromOptions(options, constant.IsUnreadCount, false) @@ -315,13 +326,13 @@ func (c *Conversation) markC2CMessageAsRead(callback open_im_sdk_callback.Base, s.SendTime = resp.SendTime s.Status = constant.MsgStatusFiltered msgStructToLocalChatLog(&localMessage, &s) - err := c.db.InsertMessage(&localMessage) + err = c.db.InsertMessage(&localMessage) if err != nil { log.Error(operationID, "inset into chat log err", localMessage, s, err.Error()) } - err2 := c.db.UpdateMessageHasRead(userID, msgIDList) + err2 := c.db.UpdateMessageHasRead(userID, newMessageIDList) if err2 != nil { - log.Error(operationID, "update message has read error", msgIDList, userID, err2.Error()) + log.Error(operationID, "update message has read error", newMessageIDList, userID, err2.Error()) } _ = common.TriggerCmdUpdateConversation(common.UpdateConNode{ConID: conversationID, Action: constant.UpdateLatestMessageChange}, c.ch) //_ = common.TriggerCmdUpdateConversation(common.UpdateConNode{ConID: conversationID, Action: constant.ConChange, Args: []string{conversationID}}, c.ch) diff --git a/internal/conversation_msg/open_im_sdk_conversation_msg.go b/internal/conversation_msg/open_im_sdk_conversation_msg.go index 38502f0ff..dcaf13060 100644 --- a/internal/conversation_msg/open_im_sdk_conversation_msg.go +++ b/internal/conversation_msg/open_im_sdk_conversation_msg.go @@ -917,7 +917,7 @@ func (c *Conversation) MarkC2CMessageAsRead(callback open_im_sdk_callback.Base, callback.OnSuccess(sdk_params_callback.MarkC2CMessageAsReadCallback) return } - c.markC2CMessageAsRead(callback, unmarshalParams, msgIDList, userID, operationID) + c.markC2CMessageAsRead(callback, unmarshalParams, userID, operationID) callback.OnSuccess(sdk_params_callback.MarkC2CMessageAsReadCallback) log.NewInfo(operationID, "MarkC2CMessageAsRead callback: ", sdk_params_callback.MarkC2CMessageAsReadCallback) }() diff --git a/pkg/db/chat_log_model.go b/pkg/db/chat_log_model.go index c5a0f1ce8..7e7cb4af0 100644 --- a/pkg/db/chat_log_model.go +++ b/pkg/db/chat_log_model.go @@ -187,12 +187,23 @@ func (d *DataBase) GetSendingMessageList() (result []*LocalChatLog, err error) { func (d *DataBase) UpdateMessageHasRead(sendID string, msgIDList []string) error { d.mRWMutex.Lock() defer d.mRWMutex.Unlock() - t := d.conn.Model(LocalChatLog{}).Debug().Where("send_id=? And is_read=? AND session_type=? AND client_msg_id in ?", sendID, constant.NotRead, constant.SingleChatType, msgIDList).Update("is_read", constant.HasRead) + t := d.conn.Model(LocalChatLog{}).Debug().Where("send_id=? AND session_type=? AND client_msg_id in ?", sendID, constant.SingleChatType, msgIDList).Update("is_read", constant.HasRead) if t.RowsAffected == 0 { return utils.Wrap(errors.New("RowsAffected == 0"), "no update") } return utils.Wrap(t.Error, "UpdateMessageStatusBySourceID failed") } +func (d *DataBase) GetMultipleMessage(conversationIDList []string) (result []*LocalChatLog, err error) { + d.mRWMutex.Lock() + defer d.mRWMutex.Unlock() + var messageList []LocalChatLog + err = utils.Wrap(d.conn.Where("client_msg_id IN ?", conversationIDList).Find(&messageList).Error, "GetMultipleMessage failed") + for _, v := range messageList { + v1 := v + result = append(result, &v1) + } + return result, err +} func (d *DataBase) GetNormalMsgSeq() (uint32, error) { d.mRWMutex.Lock()