style: add format

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
This commit is contained in:
Xinwei Xiong(cubxxw-openim)
2023-07-03 16:29:22 +08:00
parent 187ee9a375
commit 166ddb1e34
170 changed files with 4816 additions and 1279 deletions
+77 -14
View File
@@ -5,12 +5,13 @@ import (
"errors"
"fmt"
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
)
type ExtendMsgSetMongoDriver struct {
@@ -27,7 +28,11 @@ func (e *ExtendMsgSetMongoDriver) CreateExtendMsgSet(ctx context.Context, set *u
return err
}
func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(ctx context.Context, ID string, opts *unRelationTb.GetAllExtendMsgSetOpts) (sets []*unRelationTb.ExtendMsgSetModel, err error) {
func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(
ctx context.Context,
ID string,
opts *unRelationTb.GetAllExtendMsgSetOpts,
) (sets []*unRelationTb.ExtendMsgSetModel, err error) {
regex := fmt.Sprintf("^%s", ID)
var findOpts *options.FindOptions
if opts != nil {
@@ -47,11 +52,23 @@ func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(ctx context.Context, ID str
return sets, nil
}
func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, conversationID string, sessionType int32, maxMsgUpdateTime int64) (*unRelationTb.ExtendMsgSetModel, error) {
func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(
ctx context.Context,
conversationID string,
sessionType int32,
maxMsgUpdateTime int64,
) (*unRelationTb.ExtendMsgSetModel, error) {
var err error
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{"extend_msgs": 0})
findOpts := options.Find().
SetLimit(1).
SetSkip(0).
SetSort(bson.M{"source_id": -1}).
SetProjection(bson.M{"extend_msgs": 0})
// update newest
find := bson.M{"source_id": primitive.Regex{Pattern: fmt.Sprintf("^%s", conversationID)}, "session_type": sessionType}
find := bson.M{
"source_id": primitive.Regex{Pattern: fmt.Sprintf("^%s", conversationID)},
"session_type": sessionType,
}
if maxMsgUpdateTime > 0 {
find["max_msg_update_time"] = maxMsgUpdateTime
}
@@ -70,7 +87,12 @@ func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, conversat
}
// first modify msg
func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(ctx context.Context, conversationID string, sessionType int32, msg *unRelationTb.ExtendMsgModel) error {
func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(
ctx context.Context,
conversationID string,
sessionType int32,
msg *unRelationTb.ExtendMsgModel,
) error {
set, err := e.GetExtendMsgSet(ctx, conversationID, sessionType, 0)
if err != nil {
return utils.Wrap(err, "")
@@ -95,7 +117,14 @@ func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(ctx context.Context, conversat
}
// insert or update
func (e *ExtendMsgSetMongoDriver) InsertOrUpdateReactionExtendMsgSet(ctx context.Context, conversationID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*unRelationTb.KeyValueModel) error {
func (e *ExtendMsgSetMongoDriver) InsertOrUpdateReactionExtendMsgSet(
ctx context.Context,
conversationID string,
sessionType int32,
clientMsgID string,
msgFirstModifyTime int64,
reactionExtensionList map[string]*unRelationTb.KeyValueModel,
) error {
var updateBson = bson.M{}
for _, v := range reactionExtensionList {
updateBson[fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, v.TypeKey)] = v
@@ -111,12 +140,24 @@ func (e *ExtendMsgSetMongoDriver) InsertOrUpdateReactionExtendMsgSet(ctx context
if set == nil {
return errors.New(fmt.Sprintf("conversationID %s has no set", conversationID))
}
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"source_id": set.ConversationID, "session_type": sessionType}, bson.M{"$set": updateBson}, opt)
_, err = e.ExtendMsgSetCollection.UpdateOne(
ctx,
bson.M{"source_id": set.ConversationID, "session_type": sessionType},
bson.M{"$set": updateBson},
opt,
)
return utils.Wrap(err, "")
}
// delete TypeKey
func (e *ExtendMsgSetMongoDriver) DeleteReactionExtendMsgSet(ctx context.Context, conversationID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*unRelationTb.KeyValueModel) error {
func (e *ExtendMsgSetMongoDriver) DeleteReactionExtendMsgSet(
ctx context.Context,
conversationID string,
sessionType int32,
clientMsgID string,
msgFirstModifyTime int64,
reactionExtensionList map[string]*unRelationTb.KeyValueModel,
) error {
var updateBson = bson.M{}
for _, v := range reactionExtensionList {
updateBson[fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, v.TypeKey)] = ""
@@ -128,14 +169,36 @@ func (e *ExtendMsgSetMongoDriver) DeleteReactionExtendMsgSet(ctx context.Context
if set == nil {
return errors.New(fmt.Sprintf("conversationID %s has no set", conversationID))
}
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"source_id": set.ConversationID, "session_type": sessionType}, bson.M{"$unset": updateBson})
_, err = e.ExtendMsgSetCollection.UpdateOne(
ctx,
bson.M{"source_id": set.ConversationID, "session_type": sessionType},
bson.M{"$unset": updateBson},
)
return err
}
func (e *ExtendMsgSetMongoDriver) TakeExtendMsg(ctx context.Context, conversationID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) (extendMsg *unRelationTb.ExtendMsgModel, err error) {
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{fmt.Sprintf("extend_msgs.%s", clientMsgID): 1})
func (e *ExtendMsgSetMongoDriver) TakeExtendMsg(
ctx context.Context,
conversationID string,
sessionType int32,
clientMsgID string,
maxMsgUpdateTime int64,
) (extendMsg *unRelationTb.ExtendMsgModel, err error) {
findOpts := options.Find().
SetLimit(1).
SetSkip(0).
SetSort(bson.M{"source_id": -1}).
SetProjection(bson.M{fmt.Sprintf("extend_msgs.%s", clientMsgID): 1})
regex := fmt.Sprintf("^%s", conversationID)
result, err := e.ExtendMsgSetCollection.Find(ctx, bson.M{"source_id": primitive.Regex{Pattern: regex}, "session_type": sessionType, "max_msg_update_time": bson.M{"$lte": maxMsgUpdateTime}}, findOpts)
result, err := e.ExtendMsgSetCollection.Find(
ctx,
bson.M{
"source_id": primitive.Regex{Pattern: regex},
"session_type": sessionType,
"max_msg_update_time": bson.M{"$lte": maxMsgUpdateTime},
},
findOpts,
)
if err != nil {
return nil, utils.Wrap(err, "")
}
+6 -4
View File
@@ -6,14 +6,15 @@ import (
"strings"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type Mongo struct {
@@ -24,7 +25,8 @@ func NewMongo() (*Mongo, error) {
specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound)
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
if config.Config.Mongo.Uri != "" {
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
// example:
// mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
uri = config.Config.Mongo.Uri
} else {
//mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
+60 -14
View File
@@ -8,15 +8,16 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
table "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"google.golang.org/protobuf/proto"
table "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
)
var ErrMsgListNotExist = errors.New("user not have msg in mongoDB")
@@ -32,7 +33,8 @@ func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface {
}
func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []table.MsgInfoModel) error {
return m.MsgCollection.FindOneAndUpdate(ctx, bson.M{"doc_id": docID}, bson.M{"$push": bson.M{"msgs": bson.M{"$each": msgsToMongo}}}).Err()
return m.MsgCollection.FindOneAndUpdate(ctx, bson.M{"doc_id": docID}, bson.M{"$push": bson.M{"msgs": bson.M{"$each": msgsToMongo}}}).
Err()
}
func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error {
@@ -40,7 +42,13 @@ func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) e
return err
}
func (m *MsgMongoDriver) UpdateMsg(ctx context.Context, docID string, index int64, key string, value any) (*mongo.UpdateResult, error) {
func (m *MsgMongoDriver) UpdateMsg(
ctx context.Context,
docID string,
index int64,
key string,
value any,
) (*mongo.UpdateResult, error) {
var field string
if key == "" {
field = fmt.Sprintf("msgs.%d", index)
@@ -57,7 +65,13 @@ func (m *MsgMongoDriver) UpdateMsg(ctx context.Context, docID string, index int6
}
// PushUnique value must slice
func (m *MsgMongoDriver) PushUnique(ctx context.Context, docID string, index int64, key string, value any) (*mongo.UpdateResult, error) {
func (m *MsgMongoDriver) PushUnique(
ctx context.Context,
docID string,
index int64,
key string,
value any,
) (*mongo.UpdateResult, error) {
var field string
if key == "" {
field = fmt.Sprintf("msgs.%d", index)
@@ -78,20 +92,34 @@ func (m *MsgMongoDriver) PushUnique(ctx context.Context, docID string, index int
}
func (m *MsgMongoDriver) UpdateMsgContent(ctx context.Context, docID string, index int64, msg []byte) error {
_, err := m.MsgCollection.UpdateOne(ctx, bson.M{"doc_id": docID}, bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", index): msg}})
_, err := m.MsgCollection.UpdateOne(
ctx,
bson.M{"doc_id": docID},
bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", index): msg}},
)
if err != nil {
return utils.Wrap(err, "")
}
return nil
}
func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc(ctx context.Context, docID string, msg *sdkws.MsgData, seqIndex int, status int32) error {
func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc(
ctx context.Context,
docID string,
msg *sdkws.MsgData,
seqIndex int,
status int32,
) error {
msg.Status = status
bytes, err := proto.Marshal(msg)
if err != nil {
return utils.Wrap(err, "")
}
_, err = m.MsgCollection.UpdateOne(ctx, bson.M{"doc_id": docID}, bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", seqIndex): bytes}})
_, err = m.MsgCollection.UpdateOne(
ctx,
bson.M{"doc_id": docID},
bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", seqIndex): bytes}},
)
if err != nil {
return utils.Wrap(err, "")
}
@@ -104,12 +132,20 @@ func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*tab
return doc, err
}
func (m *MsgMongoDriver) GetMsgDocModelByIndex(ctx context.Context, conversationID string, index, sort int64) (*table.MsgDocModel, error) {
func (m *MsgMongoDriver) GetMsgDocModelByIndex(
ctx context.Context,
conversationID string,
index, sort int64,
) (*table.MsgDocModel, error) {
if sort != 1 && sort != -1 {
return nil, errs.ErrArgs.Wrap("mongo sort must be 1 or -1")
}
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"doc_id": sort})
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": primitive.Regex{Pattern: fmt.Sprintf("^%s:", conversationID)}}, findOpts)
cursor, err := m.MsgCollection.Find(
ctx,
bson.M{"doc_id": primitive.Regex{Pattern: fmt.Sprintf("^%s:", conversationID)}},
findOpts,
)
if err != nil {
return nil, utils.Wrap(err, "")
}
@@ -180,7 +216,12 @@ func (m *MsgMongoDriver) DeleteDocs(ctx context.Context, docIDs []string) error
return err
}
func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(ctx context.Context, userID string, docID string, seqs []int64) (msgs []*table.MsgInfoModel, err error) {
func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(
ctx context.Context,
userID string,
docID string,
seqs []int64,
) (msgs []*table.MsgInfoModel, err error) {
indexs := make([]int64, 0, len(seqs))
for _, seq := range seqs {
indexs = append(indexs, m.model.GetMsgIndex(seq))
@@ -286,7 +327,12 @@ func (m *MsgMongoDriver) IsExistDocID(ctx context.Context, docID string) (bool,
return count > 0, nil
}
func (m *MsgMongoDriver) MarkSingleChatMsgsAsRead(ctx context.Context, userID string, docID string, indexes []int64) error {
func (m *MsgMongoDriver) MarkSingleChatMsgsAsRead(
ctx context.Context,
userID string,
docID string,
indexes []int64,
) error {
updates := []mongo.WriteModel{}
for _, index := range indexes {
filter := bson.M{
+48 -13
View File
@@ -3,15 +3,19 @@ package unrelation
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
)
func NewSuperGroupMongoDriver(database *mongo.Database) unrelation.SuperGroupModelInterface {
return &SuperGroupMongoDriver{superGroupCollection: database.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: database.Collection(unrelation.CUserToSuperGroup)}
return &SuperGroupMongoDriver{
superGroupCollection: database.Collection(unrelation.CSuperGroup),
userToSuperGroupCollection: database.Collection(unrelation.CUserToSuperGroup),
}
}
type SuperGroupMongoDriver struct {
@@ -28,9 +32,14 @@ func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID st
return err
}
for _, userID := range initMemberIDs {
_, err = s.userToSuperGroupCollection.UpdateOne(ctx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, &options.UpdateOptions{
Upsert: utils.ToPtr(true),
})
_, err = s.userToSuperGroupCollection.UpdateOne(
ctx,
bson.M{"user_id": userID},
bson.M{"$addToSet": bson.M{"group_id_list": groupID}},
&options.UpdateOptions{
Upsert: utils.ToPtr(true),
},
)
if err != nil {
return err
}
@@ -38,14 +47,20 @@ func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID st
return nil
}
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string) (group *unrelation.SuperGroupModel, err error) {
func (s *SuperGroupMongoDriver) TakeSuperGroup(
ctx context.Context,
groupID string,
) (group *unrelation.SuperGroupModel, err error) {
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
return nil, utils.Wrap(err, "")
}
return group, nil
}
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string) (groups []*unrelation.SuperGroupModel, err error) {
func (s *SuperGroupMongoDriver) FindSuperGroup(
ctx context.Context,
groupIDs []string,
) (groups []*unrelation.SuperGroupModel, err error) {
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
"$in": groupIDs,
}})
@@ -60,7 +75,11 @@ func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []s
}
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
_, err := s.superGroupCollection.UpdateOne(ctx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
_, err := s.superGroupCollection.UpdateOne(
ctx,
bson.M{"group_id": groupID},
bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}},
)
if err != nil {
return err
}
@@ -69,7 +88,12 @@ func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID
Upsert: &upsert,
}
for _, userID := range userIDs {
_, err = s.userToSuperGroupCollection.UpdateOne(ctx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
_, err = s.userToSuperGroupCollection.UpdateOne(
ctx,
bson.M{"user_id": userID},
bson.M{"$addToSet": bson.M{"group_id_list": groupID}},
opts,
)
if err != nil {
return utils.Wrap(err, "transaction failed")
}
@@ -78,7 +102,11 @@ func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID
}
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
_, err := s.superGroupCollection.UpdateOne(ctx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
_, err := s.superGroupCollection.UpdateOne(
ctx,
bson.M{"group_id": groupID},
bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}},
)
if err != nil {
return err
}
@@ -89,7 +117,10 @@ func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, g
return nil
}
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string) (*unrelation.UserToSuperGroupModel, error) {
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(
ctx context.Context,
userID string,
) (*unrelation.UserToSuperGroupModel, error) {
var user unrelation.UserToSuperGroupModel
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
return &user, utils.Wrap(err, "")
@@ -107,6 +138,10 @@ func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID st
}
func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string) error {
_, err := s.userToSuperGroupCollection.UpdateOne(ctx, bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
_, err := s.userToSuperGroupCollection.UpdateOne(
ctx,
bson.M{"user_id": bson.M{"$in": userIDs}},
bson.M{"$pull": bson.M{"group_id_list": groupID}},
)
return utils.Wrap(err, "")
}