Skip to content

Commit ca240c6

Browse files
committed
fix: searchMessage method has potential NPE bug (openimsdk#3287)
1 parent 8e824c7 commit ca240c6

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

pkg/common/storage/database/mgo/msg.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,12 @@ func (m *MsgMgo) searchMessageIndex(ctx context.Context, filter any, nextID prim
321321
}
322322

323323
func (m *MsgMgo) searchMessage(ctx context.Context, req *msg.SearchMessageReq) (int64, []searchMessageIndex, error) {
324-
filter := bson.M{}
324+
filter := bson.M{
325+
"msgs.msg": bson.M{
326+
"$exists": true,
327+
"$type": "object",
328+
},
329+
}
325330
if req.RecvID != "" {
326331
filter["$or"] = bson.A{
327332
bson.M{"msgs.msg.recv_id": req.RecvID},

pkg/common/storage/database/mgo/msg_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"time"
1010

1111
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
12+
"github.com/openimsdk/protocol/msg"
13+
"github.com/openimsdk/protocol/sdkws"
1214
"github.com/openimsdk/tools/db/mongoutil"
1315
"go.mongodb.org/mongo-driver/bson"
1416
"go.mongodb.org/mongo-driver/mongo"
@@ -148,3 +150,29 @@ func TestName5(t *testing.T) {
148150
// }
149151
// t.Log(seq, sendTime)
150152
//}
153+
154+
func TestSearchMessage(t *testing.T) {
155+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*300)
156+
defer cancel()
157+
cli := Result(mongo.Connect(ctx, options.Client().ApplyURI("mongodb://openIM:openIM123@172.16.8.135:37017/openim_v3?maxPoolSize=100").SetConnectTimeout(5*time.Second)))
158+
159+
msgMongo, err := NewMsgMongo(cli.Database("openim_v3"))
160+
if err != nil {
161+
panic(err)
162+
}
163+
ts := time.Now().Add(-time.Hour * 24 * 5).UnixMilli()
164+
t.Log(ts)
165+
req := &msg.SearchMessageReq{
166+
//SendID: "yjz",
167+
//RecvID: "aibot",
168+
Pagination: &sdkws.RequestPagination{
169+
PageNumber: 1,
170+
ShowNumber: 20,
171+
},
172+
}
173+
count, resp, err := msgMongo.SearchMessage(ctx, req)
174+
if err != nil {
175+
panic(err)
176+
}
177+
t.Log(resp, count)
178+
}

0 commit comments

Comments
 (0)