diff --git a/internal/conversation_msg/notification.go b/internal/conversation_msg/notification.go index 0ffed6a79..96aa1c874 100644 --- a/internal/conversation_msg/notification.go +++ b/internal/conversation_msg/notification.go @@ -203,7 +203,6 @@ func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) { lc := node.Args.(model_struct.LocalConversation) oc, err := c.db.GetConversation(ctx, lc.ConversationID) if err == nil { - // log.Info("this is old conversation", *oc) if lc.LatestMsgSendTime >= oc.LatestMsgSendTime || c.getConversationLatestMsgClientID(lc.LatestMsg) == c.getConversationLatestMsgClientID(oc.LatestMsg) { // The session update of asynchronous messages is subject to the latest sending time err := c.db.UpdateColumnsConversation(ctx, node.ConID, map[string]interface{}{"latest_msg_send_time": lc.LatestMsgSendTime, "latest_msg": lc.LatestMsg}) if err != nil { @@ -269,7 +268,7 @@ func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) { } c.doUpdateConversation(common.Cmd2Value{Value: common.UpdateConNode{ConID: lc.ConversationID, Action: constant.ConChange, Args: []string{lc.ConversationID}}}) - case constant.UpdateLatestMessageChange: + case constant.UpdateLatestMessageReadState: conversationID := node.ConID var latestMsg sdk_struct.MsgStruct l, err := c.db.GetConversation(ctx, conversationID) @@ -288,6 +287,43 @@ func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) { } } } + case constant.UpdateLatestMessageFaceUrlAndNickName: + args := node.Args.(common.UpdateMessageInfo) + switch args.SessionType { + case constant.ReadGroupChatType: + conversationID := c.getConversationIDBySessionType(args.GroupID, constant.ReadGroupChatType) + lc, err := c.db.GetConversation(ctx, conversationID) + if err != nil { + log.ZWarn(ctx, "getConversation err", err) + return + } + var latestMsg sdk_struct.MsgStruct + err = json.Unmarshal([]byte(lc.LatestMsg), &latestMsg) + if err != nil { + log.ZError(ctx, "latestMsg,Unmarshal err", err) + } else { + //If the sender of the latest message in the conversation + //happens to be a member of the group whose status has changed, + //then update the sender's avatar and nickname for the latest message. + if latestMsg.SendID == args.UserID { + latestMsg.SenderFaceURL = args.FaceURL + latestMsg.SenderNickname = args.Nickname + newLatestMessage := utils.StructToJsonString(latestMsg) + lc.LatestMsg = newLatestMessage + err = c.db.UpdateColumnsConversation(ctx, conversationID, map[string]interface{}{"latest_msg": newLatestMessage}) + if err != nil { + log.ZError(ctx, "updateConversationLatestMsgModel err", err) + } else { + var cList []*model_struct.LocalConversation + cList = append(cList, lc) + data := utils.StructToJsonStringDefault(cList) + c.ConversationListener().OnConversationChanged(data) + } + + } + } + } + case constant.ConChange: conversationIDs := node.Args.([]string) conversations, err := c.db.GetMultipleConversationDB(ctx, conversationIDs) @@ -325,43 +361,59 @@ func (c *Conversation) doUpdateConversation(c2v common.Cmd2Value) { log.ZDebug(ctx, "NewConversation", "cidList", cidList) c.ConversationListener().OnNewConversation(cidList) - case constant.ConversationLatestMsgHasRead: - hasReadMsgList := node.Args.(map[string][]string) - var result []*model_struct.LocalConversation - var latestMsg sdk_struct.MsgStruct - var lc model_struct.LocalConversation - for conversationID, msgIDList := range hasReadMsgList { - LocalConversation, err := c.db.GetConversation(ctx, conversationID) - if err != nil { - log.ZWarn(ctx, "get conversation err", err, "conversationID", conversationID) - continue - } - err = utils.JsonStringToStruct(LocalConversation.LatestMsg, &latestMsg) - if err != nil { - log.ZWarn(ctx, "JsonStringToStruct err", err, "conversationID", conversationID) - continue - } - if utils.IsContain(latestMsg.ClientMsgID, msgIDList) { - latestMsg.IsRead = true - lc.ConversationID = conversationID - lc.LatestMsg = utils.StructToJsonString(latestMsg) - LocalConversation.LatestMsg = utils.StructToJsonString(latestMsg) - err := c.db.UpdateConversation(ctx, &lc) + } +} + +func (c *Conversation) doUpdateMessage(c2v common.Cmd2Value) { + node := c2v.Value.(common.UpdateMessageNode) + ctx := c2v.Ctx + switch node.Action { + case constant.UpdateMsgFaceUrlAndNickName: + args := node.Args.(common.UpdateMessageInfo) + switch args.SessionType { + case constant.SingleChatType: + if args.UserID == c.loginUserID { + conversationIDList, err := c.db.GetAllSingleConversationIDList(ctx) if err != nil { - log.ZWarn(ctx, "UpdateConversation err", err) - continue + log.ZError(ctx, "GetAllSingleConversationIDList err", err) + return } else { - result = append(result, LocalConversation) + log.ZDebug(ctx, "get single conversationID list", "conversationIDList", conversationIDList) + for _, conversationID := range conversationIDList { + err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) + if err != nil { + log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) + continue + } + } + } + } else { + conversationID := c.getConversationIDBySessionType(args.UserID, constant.SingleChatType) + err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) + if err != nil { + log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) + } + } + case constant.ReadGroupChatType: + conversationID := c.getConversationIDBySessionType(args.GroupID, constant.ReadGroupChatType) + err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) + if err != nil { + log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) + } + case constant.NotificationChatType: + conversationID := c.getConversationIDBySessionType(args.UserID, constant.NotificationChatType) + err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) + if err != nil { + log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) + } + default: + log.ZError(ctx, "not support sessionType", nil, "args", args) + return } - if result != nil { - log.ZDebug(ctx, "getMultipleConversationModel success", "result", result) - c.ConversationListener().OnNewConversation(utils.StructToJsonString(result)) - } - case constant.SyncConversation: - } + } func (c *Conversation) syncData(c2v common.Cmd2Value) { @@ -433,58 +485,6 @@ func executeSyncFunction(ctx context.Context, fn func(c context.Context) error, } } -func (c *Conversation) doUpdateMessage(c2v common.Cmd2Value) { - node := c2v.Value.(common.UpdateMessageNode) - ctx := c2v.Ctx - switch node.Action { - case constant.UpdateMsgFaceUrlAndNickName: - args := node.Args.(common.UpdateMessageInfo) - switch args.SessionType { - case constant.SingleChatType: - if args.UserID == c.loginUserID { - conversationIDList, err := c.db.GetAllSingleConversationIDList(ctx) - if err != nil { - log.ZError(ctx, "GetAllSingleConversationIDList err", err) - return - } else { - log.ZDebug(ctx, "get single conversationID list", "conversationIDList", conversationIDList) - for _, conversationID := range conversationIDList { - err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) - if err != nil { - log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) - continue - } - } - - } - } else { - conversationID := c.getConversationIDBySessionType(args.UserID, constant.SingleChatType) - err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) - if err != nil { - log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) - } - - } - case constant.ReadGroupChatType: - conversationID := c.getConversationIDBySessionType(args.GroupID, constant.ReadGroupChatType) - err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) - if err != nil { - log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) - } - case constant.NotificationChatType: - conversationID := c.getConversationIDBySessionType(args.UserID, constant.NotificationChatType) - err := c.db.UpdateMsgSenderFaceURLAndSenderNickname(ctx, conversationID, args.UserID, args.FaceURL, args.Nickname) - if err != nil { - log.ZError(ctx, "UpdateMsgSenderFaceURLAndSenderNickname err", err) - } - default: - log.ZError(ctx, "not support sessionType", nil, "args", args) - return - } - } - -} - func (c *Conversation) DoConversationChangedNotification(ctx context.Context, msg *sdkws.MsgData) error { c.conversationSyncMutex.Lock() defer c.conversationSyncMutex.Unlock() diff --git a/internal/conversation_msg/read_drawing.go b/internal/conversation_msg/read_drawing.go index d9abf2e0b..b015461cb 100644 --- a/internal/conversation_msg/read_drawing.go +++ b/internal/conversation_msg/read_drawing.go @@ -162,7 +162,7 @@ func (c *Conversation) getAsReadMsgMapAndList(ctx context.Context, func (c *Conversation) unreadChangeTrigger(ctx context.Context, conversationID string, latestMsgIsRead bool) { if latestMsgIsRead { c.doUpdateConversation(common.Cmd2Value{Value: common.UpdateConNode{ConID: conversationID, - Action: constant.UpdateLatestMessageChange, Args: []string{conversationID}}, Ctx: ctx}) + Action: constant.UpdateLatestMessageReadState, Args: []string{conversationID}}, Ctx: ctx}) } c.doUpdateConversation(common.Cmd2Value{Value: common.UpdateConNode{ConID: conversationID, Action: constant.ConChange, Args: []string{conversationID}}, Ctx: ctx}) @@ -210,7 +210,7 @@ func (c *Conversation) doUnreadCount(ctx context.Context, conversation *model_st } if (!latestMsg.IsRead) && datautil.Contain(latestMsg.Seq, seqs...) { c.doUpdateConversation(common.Cmd2Value{Value: common.UpdateConNode{ConID: conversation.ConversationID, - Action: constant.UpdateLatestMessageChange, Args: []string{conversation.ConversationID}}, Ctx: ctx}) + Action: constant.UpdateLatestMessageReadState, Args: []string{conversation.ConversationID}}, Ctx: ctx}) } } else { if err := c.db.UpdateColumnsConversation(ctx, conversation.ConversationID, map[string]interface{}{"unread_count": 0}); err != nil { diff --git a/internal/group/group.go b/internal/group/group.go index e19ade5a2..36d7248ad 100644 --- a/internal/group/group.go +++ b/internal/group/group.go @@ -187,6 +187,10 @@ func (g *Group) initSyncer() { Nickname: server.Nickname, GroupID: server.GroupID, }, }, g.conversationCh) + _ = common.TriggerCmdUpdateConversation(ctx, common.UpdateConNode{Action: constant.UpdateLatestMessageFaceUrlAndNickName, Args: common.UpdateMessageInfo{ + SessionType: constant.ReadGroupChatType, UserID: server.UserID, FaceURL: server.FaceURL, + Nickname: server.Nickname, GroupID: server.GroupID, + }}, g.conversationCh) } } return nil diff --git a/internal/relation/relation.go b/internal/relation/relation.go index 1974965c0..a05869a25 100644 --- a/internal/relation/relation.go +++ b/internal/relation/relation.go @@ -62,6 +62,27 @@ func (r *Relation) initSyncer() { switch state { case syncer.Insert: r.friendshipListener.OnFriendAdded(*server) + if server.Remark != "" { + server.Nickname = server.Remark + } + _ = common.TriggerCmdUpdateConversation(ctx, common.UpdateConNode{ + Action: constant.UpdateConFaceUrlAndNickName, + Args: common.SourceIDAndSessionType{ + SourceID: server.FriendUserID, + SessionType: constant.SingleChatType, + FaceURL: server.FaceURL, + Nickname: server.Nickname, + }, + }, r.conversationCh) + _ = common.TriggerCmdUpdateMessage(ctx, common.UpdateMessageNode{ + Action: constant.UpdateMsgFaceUrlAndNickName, + Args: common.UpdateMessageInfo{ + SessionType: constant.SingleChatType, + UserID: server.FriendUserID, + FaceURL: server.FaceURL, + Nickname: server.Nickname, + }, + }, r.conversationCh) case syncer.Delete: log.ZDebug(ctx, "syncer OnFriendDeleted", "local", local) r.friendshipListener.OnFriendDeleted(*local) diff --git a/pkg/constant/constant.go b/pkg/constant/constant.go index 580dde808..3b62a6141 100644 --- a/pkg/constant/constant.go +++ b/pkg/constant/constant.go @@ -178,17 +178,16 @@ const ( ) const ( - AddConOrUpLatMsg = 1 - TotalUnreadMessageChanged = 2 - UpdateConFaceUrlAndNickName = 3 - UpdateLatestMessageChange = 4 - ConChange = 5 - NewCon = 6 - ConChangeDirect = 7 - NewConDirect = 8 - ConversationLatestMsgHasRead = 9 - UpdateMsgFaceUrlAndNickName = 10 - SyncConversation = 11 + AddConOrUpLatMsg = 1 + TotalUnreadMessageChanged = 2 + UpdateConFaceUrlAndNickName = 3 + UpdateLatestMessageReadState = 4 + UpdateLatestMessageFaceUrlAndNickName = 5 + ConChange = 6 + NewCon = 7 + ConChangeDirect = 8 + NewConDirect = 9 + UpdateMsgFaceUrlAndNickName = 10 HasRead = 1 NotRead = 0