Skip to content

Commit

Permalink
fix: update the latest message when group member's changed. (#752)
Browse files Browse the repository at this point in the history
Signed-off-by: Gordon <[email protected]>
  • Loading branch information
FGadvancer authored and OpenIM-Robot committed Oct 25, 2024
1 parent 00c2c26 commit 8ff7f5a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 98 deletions.
170 changes: 85 additions & 85 deletions internal/conversation_msg/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions internal/conversation_msg/read_drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions internal/relation/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 10 additions & 11 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ff7f5a

Please sign in to comment.