From 1156b04cc66ac4bfd205414eadfb0d9881df0b3e Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong (cubxxw)" <3293172751nss@gmail.com> Date: Mon, 4 Mar 2024 11:50:18 +0800 Subject: [PATCH] feat: optimize openim reset code --- internal/api/route.go | 8 +- pkg/common/db/unrelation/msg.go | 138 -------------------------------- 2 files changed, 4 insertions(+), 142 deletions(-) diff --git a/internal/api/route.go b/internal/api/route.go index 576b319f9..a6d73b683 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -251,26 +251,23 @@ func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc { } m, err := dataBase.GetTokensWithoutError(c, claims.UserID, claims.PlatformID) if err != nil { - log.ZWarn(c, "cache get token error", errs.ErrTokenNotExist.Wrap()) apiresp.GinError(c, errs.ErrTokenNotExist.Wrap()) c.Abort() return } if len(m) == 0 { - log.ZWarn(c, "cache do not exist token error", errs.ErrTokenNotExist.Wrap()) apiresp.GinError(c, errs.ErrTokenNotExist.Wrap()) c.Abort() + return } if v, ok := m[token]; ok { switch v { case constant.NormalToken: case constant.KickedToken: - log.ZWarn(c, "cache kicked token error", errs.ErrTokenKicked.Wrap()) apiresp.GinError(c, errs.ErrTokenKicked.Wrap()) c.Abort() return default: - log.ZWarn(c, "cache unknown token error", errs.ErrTokenUnknown.Wrap()) apiresp.GinError(c, errs.ErrTokenUnknown.Wrap()) c.Abort() return @@ -280,6 +277,9 @@ func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc { c.Abort() return } + c.Set(constant.OpUserPlatform, constant.PlatformIDToName(claims.PlatformID)) + c.Set(constant.OpUserID, claims.UserID) + c.Next() } } } diff --git a/pkg/common/db/unrelation/msg.go b/pkg/common/db/unrelation/msg.go index 31d9ff8fc..fe015b694 100644 --- a/pkg/common/db/unrelation/msg.go +++ b/pkg/common/db/unrelation/msg.go @@ -1061,141 +1061,3 @@ func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessa return total, msgs, nil } -func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*table.MsgInfoModel, error) { - var pipe mongo.Pipeline - condition := bson.A{} - if req.SendTime != "" { - condition = append(condition, bson.M{"$eq": bson.A{bson.M{"$dateToString": bson.M{"format": "%Y-%m-%d", "date": bson.M{"$toDate": "$$item.msg.send_time"}}}, req.SendTime}}) - } - if req.MsgType != 0 { - condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.content_type", req.MsgType}}) - } - if req.SessionType != 0 { - condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.session_type", req.SessionType}}) - } - if req.RecvID != "" { - condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.recv_id", "regex": req.RecvID}}) - } - if req.SendID != "" { - condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.send_id", "regex": req.SendID}}) - } - - or := bson.A{ - bson.M{ - "doc_id": bson.M{ - "$regex": "^si_", - "$options": "i", - }, - }, - } - or = append(or, - bson.M{ - "doc_id": bson.M{ - "$regex": "^g_", - "$options": "i", - }, - }, - bson.M{ - "doc_id": bson.M{ - "$regex": "^sg_", - "$options": "i", - }, - }, - ) - - pipe = mongo.Pipeline{ - { - {"$match", bson.D{ - { - "$or", or, - }, - }}, - }, - { - {"$project", bson.D{ - { - "msgs", bson.D{ - { - "$filter", bson.D{ - {"input", "$msgs"}, - {"as", "item"}, - { - "cond", bson.D{ - {"$and", condition}, - }, - }, - }, - }, - }, - }, - {"doc_id", 1}, - }}, - }, - { - {"$unwind", bson.M{"path": "$msgs"}}, - }, - { - {"$sort", bson.M{"msgs.msg.send_time": -1}}, - }, - } - cursor, err := m.MsgCollection.Aggregate(ctx, pipe) - if err != nil { - return 0, nil, err - } - type docModel struct { - DocID string `bson:"doc_id"` - Msg *table.MsgInfoModel `bson:"msgs"` - } - var msgsDocs []docModel - err = cursor.All(ctx, &msgsDocs) - if err != nil { - return 0, nil, errs.Wrap(err, "cursor.All msgsDocs") - } - log.ZDebug(ctx, "query mongoDB", "result", msgsDocs) - msgs := make([]*table.MsgInfoModel, 0) - for index := range msgsDocs { - msgInfo := msgsDocs[index].Msg - if msgInfo == nil || msgInfo.Msg == nil { - continue - } - if msgInfo.Revoke != nil { - revokeContent := sdkws.MessageRevokedContent{ - RevokerID: msgInfo.Revoke.UserID, - RevokerRole: msgInfo.Revoke.Role, - ClientMsgID: msgInfo.Msg.ClientMsgID, - RevokerNickname: msgInfo.Revoke.Nickname, - RevokeTime: msgInfo.Revoke.Time, - SourceMessageSendTime: msgInfo.Msg.SendTime, - SourceMessageSendID: msgInfo.Msg.SendID, - SourceMessageSenderNickname: msgInfo.Msg.SenderNickname, - SessionType: msgInfo.Msg.SessionType, - Seq: msgInfo.Msg.Seq, - Ex: msgInfo.Msg.Ex, - } - data, err := json.Marshal(&revokeContent) - if err != nil { - return 0, nil, errs.Wrap(err, "json.Marshal revokeContent") - } - elem := sdkws.NotificationElem{ - Detail: string(data), - } - content, err := json.Marshal(&elem) - if err != nil { - return 0, nil, errs.Wrap(err, "json.Marshal elem") - } - msgInfo.Msg.ContentType = constant.MsgRevokeNotification - msgInfo.Msg.Content = string(content) - } - msgs = append(msgs, msgInfo) - } - start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber - n := int32(len(msgs)) - if start >= n { - return n, []*table.MsgInfoModel{}, nil - } else if start+req.Pagination.ShowNumber < n { - msgs = msgs[start : start+req.Pagination.ShowNumber] - } else { - msgs = msgs[start:] - } - return n, msgs, nil -}