You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
5.3 KiB
234 lines
5.3 KiB
4 years ago
|
package db
|
||
|
|
||
|
import (
|
||
3 years ago
|
pbMsg "Open_IM/pkg/proto/chat"
|
||
3 years ago
|
"Open_IM/pkg/common/config"
|
||
|
"Open_IM/pkg/common/constant"
|
||
4 years ago
|
"errors"
|
||
|
"github.com/golang/protobuf/proto"
|
||
|
"gopkg.in/mgo.v2/bson"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
3 years ago
|
const cChat = "chat"
|
||
3 years ago
|
const cGroup = "group"
|
||
3 years ago
|
|
||
|
type MsgInfo struct {
|
||
|
SendTime int64
|
||
|
Msg []byte
|
||
|
}
|
||
|
|
||
4 years ago
|
type UserChat struct {
|
||
|
UID string
|
||
3 years ago
|
Msg []MsgInfo
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
type GroupMember struct {
|
||
|
GroupID string
|
||
|
UIDList []string
|
||
|
}
|
||
|
|
||
4 years ago
|
func (d *DataBases) GetUserChat(uid string, seqBegin, seqEnd int64) (SingleMsg []*pbMsg.MsgFormat, GroupMsg []*pbMsg.MsgFormat, MaxSeq int64, MinSeq int64, err error) {
|
||
3 years ago
|
count := 0
|
||
3 years ago
|
session := d.mgoSession.Clone()
|
||
4 years ago
|
if session == nil {
|
||
|
return nil, nil, MaxSeq, MinSeq, errors.New("session == nil")
|
||
|
}
|
||
|
defer session.Close()
|
||
|
|
||
3 years ago
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||
4 years ago
|
|
||
|
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)
|
||
3 years ago
|
if err = proto.Unmarshal(sChat.Msg[i].Msg, &pChat); err != nil {
|
||
4 years ago
|
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
|
||
3 years ago
|
temp.ClientMsgID = pChat.ClientMsgID
|
||
|
temp.SenderFaceURL = pChat.SenderFaceURL
|
||
|
temp.SenderNickName = pChat.SenderNickName
|
||
4 years ago
|
if pChat.RecvSeq > MaxSeq {
|
||
|
MaxSeq = pChat.RecvSeq
|
||
|
}
|
||
3 years ago
|
if count == 0 {
|
||
4 years ago
|
MinSeq = pChat.RecvSeq
|
||
|
}
|
||
|
if pChat.RecvSeq < MinSeq {
|
||
|
MinSeq = pChat.RecvSeq
|
||
|
}
|
||
|
if pChat.SessionType == constant.SingleChatType {
|
||
|
SingleMsg = append(SingleMsg, temp)
|
||
|
} else {
|
||
|
GroupMsg = append(GroupMsg, temp)
|
||
|
}
|
||
3 years ago
|
count++
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return SingleMsg, GroupMsg, MaxSeq, MinSeq, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func (d *DataBases) SaveUserChat(uid string, sendTime int64, m proto.Message) error {
|
||
|
|
||
|
session := d.mgoSession.Clone()
|
||
4 years ago
|
if session == nil {
|
||
|
return errors.New("session == nil")
|
||
|
}
|
||
|
defer session.Close()
|
||
|
|
||
3 years ago
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||
4 years ago
|
|
||
|
n, err := c.Find(bson.M{"uid": uid}).Count()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
sMsg := MsgInfo{}
|
||
|
sMsg.SendTime = sendTime
|
||
|
if sMsg.Msg, err = proto.Marshal(m); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
4 years ago
|
if n == 0 {
|
||
|
sChat := UserChat{}
|
||
|
sChat.UID = uid
|
||
3 years ago
|
sChat.Msg = append(sChat.Msg, sMsg)
|
||
4 years ago
|
err = c.Insert(&sChat)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
3 years ago
|
err = c.Update(bson.M{"uid": uid}, bson.M{"$push": bson.M{"msg": sMsg}})
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func (d *DataBases) DelUserChat(uid string) error {
|
||
|
session := d.mgoSession.Clone()
|
||
4 years ago
|
if session == nil {
|
||
3 years ago
|
return errors.New("session == nil")
|
||
4 years ago
|
}
|
||
|
defer session.Close()
|
||
|
|
||
3 years ago
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||
4 years ago
|
|
||
3 years ago
|
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
|
||
4 years ago
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func (d *DataBases) MgoUserCount() (int, error) {
|
||
|
session := d.mgoSession.Clone()
|
||
|
if session == nil {
|
||
|
return 0, errors.New("session == nil")
|
||
4 years ago
|
}
|
||
3 years ago
|
defer session.Close()
|
||
4 years ago
|
|
||
3 years ago
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||
4 years ago
|
|
||
3 years ago
|
return c.Find(nil).Count()
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
func (d *DataBases) MgoSkipUID(count int) (string, error) {
|
||
|
session := d.mgoSession.Clone()
|
||
|
if session == nil {
|
||
|
return "", errors.New("session == nil")
|
||
4 years ago
|
}
|
||
3 years ago
|
defer session.Close()
|
||
4 years ago
|
|
||
3 years ago
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
|
||
|
|
||
|
sChat := UserChat{}
|
||
|
c.Find(nil).Skip(count).Limit(1).One(&sChat)
|
||
|
return sChat.UID, nil
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
|
func (d *DataBases) GetGroupMember(groupID string) []string {
|
||
|
groupInfo := GroupMember{}
|
||
|
groupInfo.GroupID = groupID
|
||
|
groupInfo.UIDList = make([]string, 0)
|
||
|
|
||
|
session := d.mgoSession.Clone()
|
||
|
if session == nil {
|
||
|
return groupInfo.UIDList
|
||
|
}
|
||
|
defer session.Close()
|
||
|
|
||
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
|
||
|
|
||
|
if err := c.Find(bson.M{"groupid": groupInfo.GroupID}).One(&groupInfo); err != nil {
|
||
|
return groupInfo.UIDList
|
||
|
}
|
||
|
|
||
|
return groupInfo.UIDList
|
||
|
}
|
||
|
|
||
|
func (d *DataBases) AddGroupMember(groupID, uid string) error {
|
||
|
session := d.mgoSession.Clone()
|
||
|
if session == nil {
|
||
|
return errors.New("session == nil")
|
||
|
}
|
||
|
defer session.Close()
|
||
|
|
||
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
|
||
|
|
||
|
n, err := c.Find(bson.M{"groupid": groupID}).Count()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if n == 0 {
|
||
|
groupInfo := GroupMember{}
|
||
|
groupInfo.GroupID = groupID
|
||
|
groupInfo.UIDList = append(groupInfo.UIDList, uid)
|
||
|
err = c.Insert(&groupInfo)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
err = c.Update(bson.M{"groupid": groupID}, bson.M{"$addToSet": bson.M{"uidlist": uid}})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (d *DataBases) DelGroupMember(groupID, uid string) error {
|
||
|
session := d.mgoSession.Clone()
|
||
|
if session == nil {
|
||
|
return errors.New("session == nil")
|
||
|
}
|
||
|
defer session.Close()
|
||
|
|
||
|
c := session.DB(config.Config.Mongo.DBDatabase).C(cGroup)
|
||
|
|
||
|
if err := c.Update(bson.M{"groupid": groupID}, bson.M{"$pull": bson.M{"uidlist": uid}}); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|