fix: SearchMessage (#2289)

pull/2327/head
chao 7 months ago committed by GitHub
parent fe399d72f2
commit fcfc7da4d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -267,58 +267,80 @@ func (m *MsgMgo) MarkSingleChatMsgsAsRead(ctx context.Context, userID string, do
} }
func (m *MsgMgo) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*relation.MsgInfoModel, error) { func (m *MsgMgo) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*relation.MsgInfoModel, error) {
var pipe mongo.Pipeline where := make(bson.A, 0, 6)
condition := bson.A{} if req.RecvID != "" {
if req.SendTime != "" { where = append(where, bson.M{"msgs.msg.recv_id": req.RecvID})
// Changed to keyed fields for bson.M to avoid govet errors }
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.SendID != "" {
where = append(where, bson.M{"msgs.msg.send_id": req.SendID})
} }
if req.ContentType != 0 { if req.ContentType != 0 {
condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.content_type", req.ContentType}}) where = append(where, bson.M{"msgs.msg.content_type": req.ContentType})
} }
if req.SessionType != 0 { if req.SessionType != 0 {
condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.session_type", req.SessionType}}) where = append(where, bson.M{"msgs.msg.session_type": req.SessionType})
} }
if req.RecvID != "" { if req.SendTime != "" {
condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.recv_id", "regex": req.RecvID}}) sendTime, err := time.Parse(time.DateOnly, req.SendTime)
if err != nil {
return 0, nil, errs.ErrArgs.WrapMsg("invalid sendTime", "req", req.SendTime, "format", time.DateOnly, "cause", err.Error())
}
where = append(where,
bson.M{
"msgs.msg.send_time": bson.M{
"$gte": sendTime.UnixMilli(),
},
},
bson.M{
"msgs.msg.send_time": bson.M{
"$lt": sendTime.Add(time.Hour * 24).UnixMilli(),
},
},
)
} }
if req.SendID != "" { pipeline := bson.A{
condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.send_id", "regex": req.SendID}}) bson.M{
"$unwind": "$msgs",
},
} }
if len(where) > 0 {
or := bson.A{ pipeline = append(pipeline, bson.M{
bson.M{"doc_id": bson.M{"$regex": "^si_", "$options": "i"}}, "$match": bson.M{"$and": where},
bson.M{"doc_id": bson.M{"$regex": "^g_", "$options": "i"}}, })
bson.M{"doc_id": bson.M{"$regex": "^sg_", "$options": "i"}},
} }
pipeline = append(pipeline,
// Use bson.D with keyed fields to specify the order explicitly bson.M{
pipe = mongo.Pipeline{ "$project": bson.M{
{{"$match", bson.D{{Key: "$or", Value: or}}}}, "_id": 0,
{{"$project", bson.D{ "msg": "$msgs.msg",
{Key: "msgs", Value: bson.D{ },
{Key: "$filter", Value: bson.D{ },
{Key: "input", Value: "$msgs"}, bson.M{
{Key: "as", Value: "item"}, "$count": "count",
{Key: "cond", Value: bson.D{{Key: "$and", Value: condition}}}, },
}}, )
}}, count, err := mongoutil.Aggregate[int32](ctx, m.coll, pipeline)
{Key: "doc_id", Value: 1}, if err != nil {
}}}, return 0, nil, err
{{"$unwind", bson.M{"path": "$msgs"}}},
{{"$sort", bson.M{"msgs.msg.send_time": -1}}},
} }
type docModel struct { if len(count) == 0 || count[0] == 0 {
DocID string `bson:"doc_id"` return 0, nil, nil
Msg *relation.MsgInfoModel `bson:"msgs"`
} }
msgsDocs, err := mongoutil.Aggregate[*docModel](ctx, m.coll, pipe) pipeline = pipeline[:len(pipeline)-1]
pipeline = append(pipeline,
bson.M{
"$skip": (req.Pagination.GetPageNumber() - 1) * req.Pagination.GetShowNumber(),
},
bson.M{
"$limit": req.Pagination.GetShowNumber(),
},
)
msgs, err := mongoutil.Aggregate[*relation.MsgInfoModel](ctx, m.coll, pipeline)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
msgs := make([]*relation.MsgInfoModel, 0) for i := range msgs {
for _, doc := range msgsDocs { msgInfo := msgs[i]
msgInfo := doc.Msg
if msgInfo == nil || msgInfo.Msg == nil { if msgInfo == nil || msgInfo.Msg == nil {
continue continue
} }
@ -350,17 +372,17 @@ func (m *MsgMgo) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (
} }
msgs = append(msgs, msgInfo) msgs = append(msgs, msgInfo)
} }
start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber //start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber
n := int32(len(msgs)) //n := int32(len(msgs))
if start >= n { //if start >= n {
return n, []*relation.MsgInfoModel{}, nil // return n, []*relation.MsgInfoModel{}, nil
} //}
if start+req.Pagination.ShowNumber < n { //if start+req.Pagination.ShowNumber < n {
msgs = msgs[start : start+req.Pagination.ShowNumber] // msgs = msgs[start : start+req.Pagination.ShowNumber]
} else { //} else {
msgs = msgs[start:] // msgs = msgs[start:]
} //}
return n, msgs, nil return count[0], msgs, nil
} }
func (m *MsgMgo) RangeUserSendCount(ctx context.Context, start time.Time, end time.Time, group bool, ase bool, pageNumber int32, showNumber int32) (msgCount int64, userCount int64, users []*relation.UserCount, dateCount map[string]int64, err error) { func (m *MsgMgo) RangeUserSendCount(ctx context.Context, start time.Time, end time.Time, group bool, ase bool, pageNumber int32, showNumber int32) (msgCount int64, userCount int64, users []*relation.UserCount, dateCount map[string]int64, err error) {

Loading…
Cancel
Save