Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add avatar and nickname when sessionType is notification. #379

Merged
merged 10 commits into from
Oct 19, 2023
Merged
214 changes: 23 additions & 191 deletions internal/conversation_msg/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,204 +81,30 @@ func (c *Conversation) getServerHasReadAndMaxSeqs(ctx context.Context, conversat
return resp.Seqs, nil
}

func (c *Conversation) getHistoryMessageList(ctx context.Context, req sdk.GetHistoryMessageListParams, isReverse bool) ([]*sdk_struct.MsgStruct, error) {
// t := time.Now()
var sourceID string
func (c *Conversation) getAdvancedHistoryMessageList(ctx context.Context, req sdk.GetAdvancedHistoryMessageListParams, isReverse bool) (*sdk.GetAdvancedHistoryMessageListCallback, error) {
t := time.Now()
var messageListCallback sdk.GetAdvancedHistoryMessageListCallback
var conversationID string
var startTime int64
var sessionType int
var list []*model_struct.LocalChatLog
var err error
var messageList sdk_struct.NewMsgList
var msg sdk_struct.MsgStruct
var notStartTime bool
if req.ConversationID != "" {
conversationID = req.ConversationID
lc, err := c.db.GetConversation(ctx, conversationID)
if err != nil {
return nil, err
}
switch lc.ConversationType {
case constant.SingleChatType, constant.NotificationChatType:
sourceID = lc.UserID
case constant.GroupChatType, constant.SuperGroupChatType:
sourceID = lc.GroupID
msg.GroupID = lc.GroupID
}
sessionType = int(lc.ConversationType)
if req.StartClientMsgID == "" {
//startTime = lc.LatestMsgSendTime + TimeOffset
////startTime = utils.GetCurrentTimestampByMill()
notStartTime = true
} else {
msg.SessionType = lc.ConversationType
msg.ClientMsgID = req.StartClientMsgID
m, err := c.db.GetMessage(ctx, conversationID, msg.ClientMsgID)
if err != nil {
return nil, err
}
startTime = m.SendTime
}
} else {
if req.UserID == "" {
newConversationID, newSessionType, err := c.getConversationTypeByGroupID(ctx, req.GroupID)
if err != nil {
return nil, err
}
sourceID = req.GroupID
sessionType = int(newSessionType)
conversationID = newConversationID
msg.GroupID = req.GroupID
msg.SessionType = newSessionType
} else {
sourceID = req.UserID
conversationID = c.getConversationIDBySessionType(sourceID, constant.SingleChatType)
sessionType = constant.SingleChatType
}
if req.StartClientMsgID == "" {
//lc, err := c.db.GetConversation(conversationID)
//if err != nil {
// return nil
//}
//startTime = lc.LatestMsgSendTime + TimeOffset
//startTime = utils.GetCurrentTimestampByMill()
notStartTime = true
} else {
msg.ClientMsgID = req.StartClientMsgID
m, err := c.db.GetMessage(ctx, conversationID, msg.ClientMsgID)
if err != nil {
return nil, err
}
startTime = m.SendTime
}
}
// log.Debug("", "Assembly parameters cost time", time.Since(t))
// t = time.Now()
// log.Info("", "sourceID:", sourceID, "startTime:", startTime, "count:", req.Count, "not start_time", notStartTime)
if notStartTime {
list, err = c.db.GetMessageListNoTime(ctx, conversationID, req.Count, isReverse)
} else {
list, err = c.db.GetMessageList(ctx, conversationID, req.Count, startTime, isReverse)
}
// log.Debug("", "db cost time", time.Since(t))
conversationID = req.ConversationID
lc, err := c.db.GetConversation(ctx, conversationID)
if err != nil {
return nil, err
}
// t = time.Now()
for _, v := range list {
temp := sdk_struct.MsgStruct{}
// tt := time.Now()
temp.ClientMsgID = v.ClientMsgID
temp.ServerMsgID = v.ServerMsgID
temp.CreateTime = v.CreateTime
temp.SendTime = v.SendTime
temp.SessionType = v.SessionType
temp.SendID = v.SendID
temp.RecvID = v.RecvID
temp.MsgFrom = v.MsgFrom
temp.ContentType = v.ContentType
temp.SenderPlatformID = v.SenderPlatformID
temp.SenderNickname = v.SenderNickname
temp.SenderFaceURL = v.SenderFaceURL
temp.Content = v.Content
temp.Seq = v.Seq
temp.IsRead = v.IsRead
temp.Status = v.Status
var attachedInfo sdk_struct.AttachedInfoElem
_ = utils.JsonStringToStruct(v.AttachedInfo, &attachedInfo)
temp.AttachedInfoElem = &attachedInfo
temp.Ex = v.Ex
temp.IsReact = v.IsReact
temp.IsExternalExtensions = v.IsExternalExtensions
err := c.msgHandleByContentType(&temp)
if err != nil {
// log.Error("", "Parsing data error:", err.Error(), temp)
continue
}
// log.Debug("", "internal unmarshal cost time", time.Since(tt))

switch sessionType {
case constant.GroupChatType:
fallthrough
case constant.SuperGroupChatType:
temp.GroupID = temp.RecvID
temp.RecvID = c.loginUserID
}
messageList = append(messageList, &temp)
}
// log.Debug("", "unmarshal cost time", time.Since(t))
// t = time.Now()
if !isReverse {
sort.Sort(messageList)
}
// log.Debug("", "sort cost time", time.Since(t))
return messageList, nil
}

func (c *Conversation) getAdvancedHistoryMessageList2(ctx context.Context, req sdk.GetAdvancedHistoryMessageListParams, isReverse bool) (*sdk.GetAdvancedHistoryMessageListCallback, error) {
t := time.Now()
var messageListCallback sdk.GetAdvancedHistoryMessageListCallback
var sourceID string
var conversationID string
var startTime int64

var sessionType int
var list []*model_struct.LocalChatLog
var err error
var messageList sdk_struct.NewMsgList
var msg sdk_struct.MsgStruct
var notStartTime bool
if req.ConversationID != "" {
conversationID = req.ConversationID
lc, err := c.db.GetConversation(ctx, conversationID)
sessionType = int(lc.ConversationType)
if req.StartClientMsgID == "" {
notStartTime = true
} else {
m, err := c.db.GetMessage(ctx, conversationID, req.StartClientMsgID)
if err != nil {
return nil, err
}
switch lc.ConversationType {
case constant.SingleChatType, constant.NotificationChatType:
sourceID = lc.UserID
case constant.GroupChatType, constant.SuperGroupChatType:
sourceID = lc.GroupID
msg.GroupID = lc.GroupID
}
sessionType = int(lc.ConversationType)
if req.StartClientMsgID == "" {
notStartTime = true
} else {
msg.SessionType = lc.ConversationType
msg.ClientMsgID = req.StartClientMsgID
m, err := c.db.GetMessage(ctx, conversationID, msg.ClientMsgID)
if err != nil {
return nil, err
}
startTime = m.SendTime
}
} else {
if req.UserID == "" {
newConversationID, newSessionType, err := c.getConversationTypeByGroupID(ctx, req.GroupID)
if err != nil {
return nil, err
}
sourceID = req.GroupID
sessionType = int(newSessionType)
conversationID = newConversationID
msg.GroupID = req.GroupID
msg.SessionType = newSessionType
} else {
sourceID = req.UserID
conversationID = c.getConversationIDBySessionType(sourceID, constant.SingleChatType)
sessionType = constant.SingleChatType
}
if req.StartClientMsgID == "" {
notStartTime = true
} else {
msg.ClientMsgID = req.StartClientMsgID
m, err := c.db.GetMessage(ctx, conversationID, msg.ClientMsgID)
if err != nil {
return nil, err
}
startTime = m.SendTime
}
startTime = m.SendTime
}
log.ZDebug(ctx, "Assembly conversation parameters", "cost time", time.Since(t), "conversationID",
conversationID, "startTime:", startTime, "count:", req.Count, "not start_time", notStartTime)
Expand All @@ -288,24 +114,30 @@ func (c *Conversation) getAdvancedHistoryMessageList2(ctx context.Context, req s
} else {
list, err = c.db.GetMessageList(ctx, conversationID, req.Count, startTime, isReverse)
}
log.ZDebug(ctx, "db get messageList", "cost time", time.Since(t), "len", len(list), "err", err, "conversationID", conversationID)
log.ZDebug(ctx, "db get messageList", "cost time", time.Since(t), "len", len(list), "err",
err, "conversationID", conversationID)

if err != nil {
return nil, err
}
rawMessageLength := len(list)
t = time.Now()
if rawMessageLength < req.Count {
maxSeq, minSeq, lostSeqListLength := c.messageBlocksInternalContinuityCheck(ctx, conversationID, notStartTime, isReverse, req.Count, startTime, &list, &messageListCallback)
_ = c.messageBlocksBetweenContinuityCheck(ctx, req.LastMinSeq, maxSeq, conversationID, notStartTime, isReverse, req.Count, startTime, &list, &messageListCallback)
maxSeq, minSeq, lostSeqListLength := c.messageBlocksInternalContinuityCheck(ctx,
conversationID, notStartTime, isReverse, req.Count, startTime, &list, &messageListCallback)
_ = c.messageBlocksBetweenContinuityCheck(ctx, req.LastMinSeq, maxSeq, conversationID,
notStartTime, isReverse, req.Count, startTime, &list, &messageListCallback)
if minSeq == 1 && lostSeqListLength == 0 {
messageListCallback.IsEnd = true
} else {
c.messageBlocksEndContinuityCheck(ctx, minSeq, conversationID, notStartTime, isReverse, req.Count, startTime, &list, &messageListCallback)
c.messageBlocksEndContinuityCheck(ctx, minSeq, conversationID, notStartTime, isReverse,
req.Count, startTime, &list, &messageListCallback)
}
} else {
maxSeq, _, _ := c.messageBlocksInternalContinuityCheck(ctx, conversationID, notStartTime, isReverse, req.Count, startTime, &list, &messageListCallback)
c.messageBlocksBetweenContinuityCheck(ctx, req.LastMinSeq, maxSeq, conversationID, notStartTime, isReverse, req.Count, startTime, &list, &messageListCallback)
maxSeq, _, _ := c.messageBlocksInternalContinuityCheck(ctx, conversationID, notStartTime, isReverse,
req.Count, startTime, &list, &messageListCallback)
c.messageBlocksBetweenContinuityCheck(ctx, req.LastMinSeq, maxSeq, conversationID, notStartTime,
isReverse, req.Count, startTime, &list, &messageListCallback)

}
log.ZDebug(ctx, "pull message", "pull cost time", time.Since(t))
Expand Down
12 changes: 8 additions & 4 deletions internal/conversation_msg/conversation_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,8 @@ func (c *Conversation) addFaceURLAndName(ctx context.Context, lc *model_struct.L
func (c *Conversation) batchAddFaceURLAndName(ctx context.Context, conversations ...*model_struct.LocalConversation) error {
var userIDs, groupIDs []string
for _, conversation := range conversations {
if conversation.ConversationType == constant.SingleChatType {
if conversation.ConversationType == constant.SingleChatType ||
conversation.ConversationType == constant.NotificationChatType {
userIDs = append(userIDs, conversation.UserID)
} else if conversation.ConversationType == constant.SuperGroupChatType {
groupIDs = append(groupIDs, conversation.GroupID)
Expand All @@ -951,19 +952,22 @@ func (c *Conversation) batchAddFaceURLAndName(ctx context.Context, conversations
return err
}
for _, conversation := range conversations {
if conversation.ConversationType == constant.SingleChatType {
if conversation.ConversationType == constant.SingleChatType ||
conversation.ConversationType == constant.NotificationChatType {
if v, ok := users[conversation.UserID]; ok {
conversation.FaceURL = v.FaceURL
conversation.ShowName = v.Nickname
} else {
log.ZWarn(ctx, "user info not found", errors.New("user not found"), "userID", conversation.UserID)
log.ZWarn(ctx, "user info not found", errors.New("user not found"),
"userID", conversation.UserID)
}
} else if conversation.ConversationType == constant.SuperGroupChatType {
if v, ok := groups[conversation.GroupID]; ok {
conversation.FaceURL = v.FaceURL
conversation.ShowName = v.GroupName
} else {
log.ZWarn(ctx, "group info not found", errors.New("group not found"), "groupID", conversation.GroupID)
log.ZWarn(ctx, "group info not found", errors.New("group not found"),
"groupID", conversation.GroupID)
}

}
Expand Down
2 changes: 1 addition & 1 deletion internal/conversation_msg/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *Conversation) clearConversationMsgFromSvr(ctx context.Context, conversa
}

// Delete all messages
func (c *Conversation) deleteAllMessage(ctx context.Context) error {
func (c *Conversation) deleteAllMsgFromLocalAndSvr(ctx context.Context) error {
// Delete the server first (high error rate), then delete it.
err := c.deleteAllMessageFromSvr(ctx)
if err != nil {
Expand Down
33 changes: 11 additions & 22 deletions internal/conversation_msg/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"github.com/openimsdk/openim-sdk-core/v3/internal/file"
"github.com/openimsdk/openim-sdk-core/v3/internal/util"
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/common"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
Expand All @@ -42,7 +41,6 @@ import (

pbConversation "github.com/OpenIMSDK/protocol/conversation"
"github.com/OpenIMSDK/protocol/sdkws"
pbUser "github.com/OpenIMSDK/protocol/user"
"github.com/OpenIMSDK/protocol/wrapperspb"

"github.com/jinzhu/copier"
Expand All @@ -56,14 +54,6 @@ func (c *Conversation) GetConversationListSplit(ctx context.Context, offset, cou
return c.db.GetConversationListSplitDB(ctx, offset, count)
}

func (c *Conversation) SetGlobalRecvMessageOpt(ctx context.Context, opt int) error {
if err := util.ApiPost(ctx, constant.SetGlobalRecvMessageOptRouter, &pbUser.SetGlobalRecvMessageOptReq{UserID: c.loginUserID, GlobalRecvMsgOpt: int32(opt)}, nil); err != nil {
return err
}
c.user.SyncLoginUserInfo(ctx)
return nil
}

func (c *Conversation) HideConversation(ctx context.Context, conversationID string) error {
return c.db.UpdateColumnsConversation(ctx, conversationID, map[string]interface{}{"latest_msg_send_time": 0})
}
Expand Down Expand Up @@ -144,6 +134,13 @@ func (c *Conversation) DeleteAllConversationFromLocal(ctx context.Context) error
}
return nil
}
func (c *Conversation) DeleteConversationFromLocal(ctx context.Context, conversationID string) error {
err := c.db.ResetConversation(ctx, conversationID)
if err != nil {
return err
}
return nil
}

func (c *Conversation) SetConversationDraft(ctx context.Context, conversationID, draftText string) error {
if draftText != "" {
Expand Down Expand Up @@ -895,12 +892,8 @@ func (c *Conversation) FindMessageList(ctx context.Context, req []*sdk_params_ca

}

func (c *Conversation) GetHistoryMessageList(ctx context.Context, req sdk_params_callback.GetHistoryMessageListParams) ([]*sdk_struct.MsgStruct, error) {
return c.getHistoryMessageList(ctx, req, false)
}

func (c *Conversation) GetAdvancedHistoryMessageList(ctx context.Context, req sdk_params_callback.GetAdvancedHistoryMessageListParams) (*sdk_params_callback.GetAdvancedHistoryMessageListCallback, error) {
result, err := c.getAdvancedHistoryMessageList2(ctx, req, false)
result, err := c.getAdvancedHistoryMessageList(ctx, req, false)
if err != nil {
return nil, err
}
Expand All @@ -912,7 +905,7 @@ func (c *Conversation) GetAdvancedHistoryMessageList(ctx context.Context, req sd
}

func (c *Conversation) GetAdvancedHistoryMessageListReverse(ctx context.Context, req sdk_params_callback.GetAdvancedHistoryMessageListParams) (*sdk_params_callback.GetAdvancedHistoryMessageListCallback, error) {
result, err := c.getAdvancedHistoryMessageList2(ctx, req, true)
result, err := c.getAdvancedHistoryMessageList(ctx, req, true)
if err != nil {
return nil, err
}
Expand All @@ -923,10 +916,6 @@ func (c *Conversation) GetAdvancedHistoryMessageListReverse(ctx context.Context,
return result, nil
}

func (c *Conversation) GetHistoryMessageListReverse(ctx context.Context, req sdk_params_callback.GetHistoryMessageListParams) ([]*sdk_struct.MsgStruct, error) {
return c.getHistoryMessageList(ctx, req, true)
}

func (c *Conversation) RevokeMessage(ctx context.Context, conversationID, clientMsgID string) error {
return c.revokeOneMessage(ctx, conversationID, clientMsgID)
}
Expand Down Expand Up @@ -972,8 +961,8 @@ func (c *Conversation) DeleteMessage(ctx context.Context, conversationID string,
return c.deleteMessage(ctx, conversationID, clientMsgID)
}

func (c *Conversation) DeleteAllMessage(ctx context.Context) error {
return c.deleteAllMessage(ctx)
func (c *Conversation) DeleteAllMsgFromLocalAndSvr(ctx context.Context) error {
return c.deleteAllMsgFromLocalAndSvr(ctx)
}

func (c *Conversation) DeleteAllMessageFromLocalStorage(ctx context.Context) error {
Expand Down
Loading
Loading