Files
open-im-server/src/common/db/mongoModel.go
T

156 lines
3.5 KiB
Go
Raw Normal View History

2021-05-26 19:17:51 +08:00
package db
import (
"Open_IM/src/common/config"
"Open_IM/src/common/constant"
pbMsg "Open_IM/src/proto/chat"
"errors"
"github.com/golang/protobuf/proto"
"gopkg.in/mgo.v2/bson"
"time"
)
2021-06-28 15:32:26 +08:00
const cChat = "chat"
type MsgInfo struct {
SendTime int64
Msg []byte
}
2021-05-26 19:17:51 +08:00
type UserChat struct {
UID string
2021-06-28 15:32:26 +08:00
Msg []MsgInfo
2021-05-26 19:17:51 +08:00
}
func (d *DataBases) GetUserChat(uid string, seqBegin, seqEnd int64) (SingleMsg []*pbMsg.MsgFormat, GroupMsg []*pbMsg.MsgFormat, MaxSeq int64, MinSeq int64, err error) {
2021-06-28 15:32:26 +08:00
session := d.mgoSession.Clone()
2021-05-26 19:17:51 +08:00
if session == nil {
return nil, nil, MaxSeq, MinSeq, errors.New("session == nil")
}
defer session.Close()
2021-06-28 15:32:26 +08:00
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
2021-05-26 19:17:51 +08:00
sChat := UserChat{}
if err = c.Find(bson.M{"uid": uid}).One(&sChat); err != nil {
return nil, nil, MaxSeq, MinSeq, err
}
pChat := pbMsg.MsgSvrToPushSvrChatMsg{}
for i := 0; i < len(sChat.Msg); i++ {
temp := new(pbMsg.MsgFormat)
2021-06-28 15:32:26 +08:00
if err = proto.Unmarshal(sChat.Msg[i].Msg, &pChat); err != nil {
2021-05-26 19:17:51 +08:00
return nil, nil, MaxSeq, MinSeq, err
}
if pChat.RecvSeq >= seqBegin && pChat.RecvSeq <= seqEnd {
temp.SendID = pChat.SendID
temp.RecvID = pChat.RecvID
temp.MsgFrom = pChat.MsgFrom
temp.Seq = pChat.RecvSeq
temp.ServerMsgID = pChat.MsgID
temp.SendTime = pChat.SendTime
temp.Content = pChat.Content
temp.ContentType = pChat.ContentType
temp.SenderPlatformID = pChat.PlatformID
if pChat.RecvSeq > MaxSeq {
MaxSeq = pChat.RecvSeq
}
if i == 0 {
MinSeq = pChat.RecvSeq
}
if pChat.RecvSeq < MinSeq {
MinSeq = pChat.RecvSeq
}
if pChat.SessionType == constant.SingleChatType {
SingleMsg = append(SingleMsg, temp)
} else {
GroupMsg = append(GroupMsg, temp)
}
}
}
return SingleMsg, GroupMsg, MaxSeq, MinSeq, nil
}
2021-06-28 15:32:26 +08:00
func (d *DataBases) SaveUserChat(uid string, sendTime int64, m proto.Message) error {
session := d.mgoSession.Clone()
2021-05-26 19:17:51 +08:00
if session == nil {
return errors.New("session == nil")
}
defer session.Close()
2021-06-28 15:32:26 +08:00
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
2021-05-26 19:17:51 +08:00
n, err := c.Find(bson.M{"uid": uid}).Count()
if err != nil {
return err
}
2021-06-28 15:32:26 +08:00
sMsg := MsgInfo{}
sMsg.SendTime = sendTime
if sMsg.Msg, err = proto.Marshal(m); err != nil {
return err
}
2021-05-26 19:17:51 +08:00
if n == 0 {
sChat := UserChat{}
sChat.UID = uid
2021-06-28 15:32:26 +08:00
sChat.Msg = append(sChat.Msg, sMsg)
2021-05-26 19:17:51 +08:00
err = c.Insert(&sChat)
if err != nil {
return err
}
} else {
2021-06-28 15:32:26 +08:00
err = c.Update(bson.M{"uid": uid}, bson.M{"$push": bson.M{"msg": sMsg}})
2021-05-26 19:17:51 +08:00
if err != nil {
return err
}
}
return nil
}
2021-06-28 15:32:26 +08:00
func (d *DataBases) DelUserChat(uid string) error {
session := d.mgoSession.Clone()
2021-05-26 19:17:51 +08:00
if session == nil {
2021-06-28 15:32:26 +08:00
return errors.New("session == nil")
2021-05-26 19:17:51 +08:00
}
defer session.Close()
2021-06-28 15:32:26 +08:00
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
2021-05-26 19:17:51 +08:00
2021-06-28 15:32:26 +08:00
delTime := time.Now().Unix() - int64(config.Config.Mongo.DBRetainChatRecords)*24*3600
if err := c.Update(bson.M{"uid": uid}, bson.M{"$pull": bson.M{"msg": bson.M{"sendtime": bson.M{"$lte": delTime}}}}); err != nil {
return err
2021-05-26 19:17:51 +08:00
}
return nil
}
2021-06-28 15:32:26 +08:00
func (d *DataBases) MgoUserCount() (int, error) {
session := d.mgoSession.Clone()
if session == nil {
return 0, errors.New("session == nil")
2021-05-26 19:17:51 +08:00
}
2021-06-28 15:32:26 +08:00
defer session.Close()
2021-05-26 19:17:51 +08:00
2021-06-28 15:32:26 +08:00
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
2021-05-26 19:17:51 +08:00
2021-06-28 15:32:26 +08:00
return c.Find(nil).Count()
}
2021-05-26 19:17:51 +08:00
2021-06-28 15:32:26 +08:00
func (d *DataBases) MgoSkipUID(count int) (string, error) {
session := d.mgoSession.Clone()
if session == nil {
return "", errors.New("session == nil")
2021-05-26 19:17:51 +08:00
}
2021-06-28 15:32:26 +08:00
defer session.Close()
2021-05-26 19:17:51 +08:00
2021-06-28 15:32:26 +08:00
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
sChat := UserChat{}
c.Find(nil).Skip(count).Limit(1).One(&sChat)
return sChat.UID, nil
2021-05-26 19:17:51 +08:00
}