271 lines
7.1 KiB
Go
271 lines
7.1 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"strings"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
var worldchatkey = "chat:world"
|
|
var unionchatkey = "chat:union"
|
|
var crosschatkey = "chat:cross"
|
|
var systemchatkey = "chat:system"
|
|
|
|
///论坛 数据组件
|
|
type modelChatComp struct {
|
|
modules.MCompModel
|
|
module *Chat
|
|
}
|
|
|
|
//组件初始化接口
|
|
func (this *modelChatComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableChat
|
|
this.MCompModel.Init(service, module, comp, opt)
|
|
this.module = module.(*Chat)
|
|
//创建uid索引
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "ruid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
//查询用户未读消息
|
|
func (this *modelChatComp) QueryUserMsg(uid string) (result []*pb.DBChat, err error) {
|
|
var (
|
|
c *mongo.Cursor
|
|
)
|
|
if c, err = this.DB.Find(core.SqlTable(this.TableName), bson.M{"ruid": uid}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
result = make([]*pb.DBChat, c.RemainingBatchLength())
|
|
n := 0
|
|
for c.Next(context.TODO()) {
|
|
temp := &pb.DBChat{}
|
|
if err = c.Decode(temp); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
result[n] = temp
|
|
n++
|
|
}
|
|
this.DB.DeleteMany(core.SqlTable(this.TableName), bson.M{"ruid": uid}) //清理数据
|
|
return
|
|
}
|
|
|
|
//查询用户未读消息
|
|
func (this *modelChatComp) GetChatQueue(channel pb.ChatChannel, union, group, area int32) (result []*pb.DBChat, err error) {
|
|
var (
|
|
key string
|
|
find bson.M
|
|
c *mongo.Cursor
|
|
n int
|
|
max_chat int32
|
|
readmax_chat int32
|
|
)
|
|
switch channel {
|
|
case pb.ChatChannel_World:
|
|
key = worldchatkey
|
|
find = bson.M{"channel": channel}
|
|
break
|
|
case pb.ChatChannel_Union:
|
|
key = unionchatkey
|
|
find = bson.M{"channel": channel, "unionId": union}
|
|
break
|
|
case pb.ChatChannel_CrossServer:
|
|
key = fmt.Sprintf("%s:%d--%d", crosschatkey, group, area)
|
|
find = bson.M{"channel": channel, "group": group, "areaId": area}
|
|
break
|
|
case pb.ChatChannel_System:
|
|
key = systemchatkey
|
|
find = bson.M{"channel": channel}
|
|
break
|
|
}
|
|
if max_chat, err = this.module.configure.GetChannelRecordMax(); err != nil {
|
|
return
|
|
}
|
|
if readmax_chat, err = this.module.configure.GetChannelReadRecordNum(); err != nil {
|
|
return
|
|
}
|
|
result = make([]*pb.DBChat, 0)
|
|
err = this.GetQueues(key, int(readmax_chat), &result)
|
|
if err == redis.RedisNil {
|
|
//query from mgo
|
|
if c, err = this.DB.Find(core.SqlTable(this.TableName), find); err != nil {
|
|
return
|
|
} else {
|
|
result = make([]*pb.DBChat, c.RemainingBatchLength())
|
|
for c.Next(context.Background()) {
|
|
chat := &pb.DBChat{}
|
|
if err = c.Decode(chat); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
result[n] = chat
|
|
n++
|
|
}
|
|
if len(result) > 0 {
|
|
this.addChatMsg(key, int64(max_chat), result...)
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//添加跨服频道成员
|
|
func (this *modelChatComp) AddCrossChannelMember(session comm.IUserSession) (channel int32, err error) {
|
|
udata := &pb.CacheUser{
|
|
Uid: session.GetUserId(),
|
|
SessionId: session.GetSessionId(),
|
|
ServiceTag: session.GetServiecTag(),
|
|
GatewayServiceId: session.GetGatewayServiceId(),
|
|
Ip: session.GetIP(),
|
|
}
|
|
var (
|
|
count int
|
|
group int32
|
|
maxnum int32
|
|
)
|
|
channel = 1
|
|
if group, err = this.module.configure.GetServiecTagGroup(session.GetServiecTag()); err != nil {
|
|
return
|
|
}
|
|
if maxnum, err = this.module.configure.GetAutoIntoChannelMax(); err != nil {
|
|
return
|
|
}
|
|
for {
|
|
key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, channel)
|
|
if count, err = this.Redis.Hlen(key); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
if int32(count) < maxnum {
|
|
if err = this.Redis.HMSet(key, map[string]interface{}{session.GetUserId(): udata}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
break
|
|
} else {
|
|
channel++
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *modelChatComp) ChanageChannel(session comm.IUserSession, channel int32) (err error, ok bool) {
|
|
udata := &pb.CacheUser{
|
|
Uid: session.GetUserId(),
|
|
SessionId: session.GetSessionId(),
|
|
ServiceTag: session.GetServiecTag(),
|
|
GatewayServiceId: session.GetGatewayServiceId(),
|
|
Ip: session.GetIP(),
|
|
}
|
|
var (
|
|
group int32
|
|
maxnum int32
|
|
)
|
|
if group, err = this.module.configure.GetServiecTagGroup(session.GetServiecTag()); err != nil {
|
|
return
|
|
}
|
|
key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, channel)
|
|
count := 0
|
|
if maxnum, err = this.module.configure.GetChanageChannelMax(); err != nil {
|
|
return
|
|
}
|
|
if count, err = this.Redis.Hlen(key); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
if int32(count) < maxnum {
|
|
if err = this.Redis.HMSet(key, map[string]interface{}{session.GetUserId(): udata}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
ok = true
|
|
}
|
|
return
|
|
}
|
|
|
|
//读取跨服聊天频道下成员
|
|
func (this *modelChatComp) GetCrossChannelMember(group, channel int32) (result []*pb.CacheUser, err error) {
|
|
key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, channel)
|
|
result = make([]*pb.CacheUser, 0)
|
|
if err = this.Redis.HGetAll(key, &result); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//移除频道成员
|
|
func (this *modelChatComp) RemoveCrossChannelMember(session comm.IUserSession) (err error) {
|
|
var (
|
|
result *pb.DBUserExpand
|
|
)
|
|
if result, err = this.module.ModuleUser.GetUserExpand(session.GetUserId()); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
var group int32
|
|
if group, err = this.module.configure.GetServiecTagGroup(session.GetServiecTag()); err != nil {
|
|
return
|
|
}
|
|
key := fmt.Sprintf("%s:%d--%d-member", crosschatkey, group, result.Chatchannel)
|
|
if err = this.Redis.HDel(key, session.GetUserId()); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *modelChatComp) SaveUserMsg(msg *pb.DBChat) (err error) {
|
|
if _, err = this.DB.InsertOne(core.SqlTable(this.TableName), msg); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *modelChatComp) addChatMsg(key string, count int64, msgs ...*pb.DBChat) (err error) {
|
|
var (
|
|
data map[string]*pb.DBChat = make(map[string]*pb.DBChat, len(msgs))
|
|
values []interface{} = make([]interface{}, len(msgs))
|
|
outkey []string
|
|
)
|
|
for i, v := range msgs {
|
|
data[fmt.Sprintf("%s-%s", key, v.Id)] = v
|
|
values[i] = v
|
|
}
|
|
if outkey, err = this.AddQueues(key, count, data); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
// if _, err = this.DB.InsertMany(core.SqlTable(this.TableName), values); err != nil {
|
|
// this.module.Errorf("err:%v", err)
|
|
// return
|
|
// }
|
|
if len(outkey) > 0 {
|
|
delkeys := make([]string, 0)
|
|
for _, v := range outkey {
|
|
temp := strings.Split(v, "_")
|
|
if len(temp) == 2 {
|
|
delkeys = append(delkeys, temp[1])
|
|
}
|
|
}
|
|
if err = this.DeleteModelLogs(this.TableName, "", bson.M{"_id": bson.M{"$in": delkeys}}); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
return
|
|
}
|
|
|
|
}
|
|
return
|
|
}
|