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.
Open-IM-Server/internal/rpc/msg/pull_message.go

160 lines
4.9 KiB

package msg
4 years ago
import (
"context"
"github.com/garyburd/redigo/redis"
4 years ago
commonDB "Open_IM/pkg/common/db"
"Open_IM/pkg/common/log"
3 years ago
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
4 years ago
"sort"
"strings"
pbMsg "Open_IM/pkg/proto/chat"
4 years ago
)
func (rpc *rpcChat) GetMaxAndMinSeq(_ context.Context, in *pbMsg.GetMaxAndMinSeqReq) (*pbMsg.GetMaxAndMinSeqResp, error) {
log.InfoByKv("rpc getMaxAndMinSeq is arriving", in.OperationID, in.String())
4 years ago
//seq, err := model.GetBiggestSeqFromReceive(in.UserID)
maxSeq, err1 := commonDB.DB.GetUserMaxSeq(in.UserID)
minSeq, err2 := commonDB.DB.GetUserMinSeq(in.UserID)
resp := new(pbMsg.GetMaxAndMinSeqResp)
if err1 == nil {
resp.MaxSeq = maxSeq
} else if err1 == redis.ErrNil {
resp.MaxSeq = 0
4 years ago
} else {
log.NewError(in.OperationID, "getMaxSeq from redis error", in.String(), err1.Error())
resp.MaxSeq = -1
resp.ErrCode = 200
resp.ErrMsg = "redis get err"
4 years ago
}
if err2 == nil {
resp.MinSeq = minSeq
} else if err2 == redis.ErrNil {
resp.MinSeq = 0
} else {
log.NewError(in.OperationID, "getMaxSeq from redis error", in.String(), err2.Error())
resp.MinSeq = -1
resp.ErrCode = 201
resp.ErrMsg = "redis get err"
}
return resp, nil
4 years ago
}
func (rpc *rpcChat) PullMessage(_ context.Context, in *open_im_sdk.PullMessageReq) (*open_im_sdk.PullMessageResp, error) {
4 years ago
log.InfoByKv("rpc pullMessage is arriving", in.OperationID, "args", in.String())
resp := new(open_im_sdk.PullMessageResp)
var respSingleMsgFormat []*open_im_sdk.GatherFormat
var respGroupMsgFormat []*open_im_sdk.GatherFormat
SingleMsgFormat, GroupMsgFormat, MaxSeq, MinSeq, err := commonDB.DB.GetMsgBySeqRange(in.UserID, in.SeqBegin, in.SeqEnd)
4 years ago
if err != nil {
log.ErrorByKv("pullMsg data error", in.OperationID, in.String())
resp.ErrCode = 1
resp.ErrMsg = err.Error()
return resp, nil
}
respSingleMsgFormat = singleMsgHandleByUser(SingleMsgFormat, in.UserID)
respGroupMsgFormat = groupMsgHandleByUser(GroupMsgFormat)
return &open_im_sdk.PullMessageResp{
ErrCode: 0,
ErrMsg: "",
MaxSeq: MaxSeq,
MinSeq: MinSeq,
SingleUserMsg: respSingleMsgFormat,
GroupUserMsg: respGroupMsgFormat,
}, nil
}
func (rpc *rpcChat) PullMessageBySeqList(_ context.Context, in *open_im_sdk.PullMessageBySeqListReq) (*open_im_sdk.PullMessageBySeqListResp, error) {
log.NewInfo(in.OperationID, "rpc PullMessageBySeqList is arriving", in.String())
resp := new(open_im_sdk.PullMessageBySeqListResp)
var respSingleMsgFormat []*open_im_sdk.GatherFormat
var respGroupMsgFormat []*open_im_sdk.GatherFormat
SingleMsgFormat, GroupMsgFormat, MaxSeq, MinSeq, err := commonDB.DB.GetMsgBySeqList(in.UserID, in.SeqList)
if err != nil {
log.ErrorByKv("PullMessageBySeqList data error", in.OperationID, in.String())
resp.ErrCode = 1
resp.ErrMsg = err.Error()
return resp, nil
}
respSingleMsgFormat = singleMsgHandleByUser(SingleMsgFormat, in.UserID)
respGroupMsgFormat = groupMsgHandleByUser(GroupMsgFormat)
return &open_im_sdk.PullMessageBySeqListResp{
4 years ago
ErrCode: 0,
ErrMsg: "",
MaxSeq: MaxSeq,
MinSeq: MinSeq,
SingleUserMsg: respSingleMsgFormat,
GroupUserMsg: respGroupMsgFormat,
}, nil
}
func singleMsgHandleByUser(allMsg []*open_im_sdk.MsgData, ownerId string) []*open_im_sdk.GatherFormat {
4 years ago
var userid string
var respMsgFormat []*open_im_sdk.GatherFormat
4 years ago
m := make(map[string]MsgFormats)
//Gather messages in the dimension of users
4 years ago
for _, v := range allMsg {
if v.RecvID != ownerId {
userid = v.RecvID
} else {
userid = v.SendID
}
if value, ok := m[userid]; !ok {
var t MsgFormats
m[userid] = append(t, v)
} else {
m[userid] = append(value, v)
}
}
//Return in pb format
4 years ago
for user, msg := range m {
tempUserMsg := new(open_im_sdk.GatherFormat)
tempUserMsg.Id = user
4 years ago
tempUserMsg.List = msg
sort.Sort(msg)
respMsgFormat = append(respMsgFormat, tempUserMsg)
}
return respMsgFormat
}
func groupMsgHandleByUser(allMsg []*open_im_sdk.MsgData) []*open_im_sdk.GatherFormat {
var respMsgFormat []*open_im_sdk.GatherFormat
4 years ago
m := make(map[string]MsgFormats)
//Gather messages in the dimension of users
4 years ago
for _, v := range allMsg {
//Get group ID
4 years ago
groupID := strings.Split(v.RecvID, " ")[1]
if value, ok := m[groupID]; !ok {
var t MsgFormats
m[groupID] = append(t, v)
} else {
m[groupID] = append(value, v)
}
}
//Return in pb format
4 years ago
for groupID, msg := range m {
tempUserMsg := new(open_im_sdk.GatherFormat)
tempUserMsg.Id = groupID
4 years ago
tempUserMsg.List = msg
sort.Sort(msg)
respMsgFormat = append(respMsgFormat, tempUserMsg)
}
return respMsgFormat
}
type MsgFormats []*open_im_sdk.MsgData
4 years ago
// Implement the sort.Interface interface to get the number of elements method
4 years ago
func (s MsgFormats) Len() int {
return len(s)
}
//Implement the sort.Interface interface comparison element method
4 years ago
func (s MsgFormats) Less(i, j int) bool {
return s[i].SendTime < s[j].SendTime
}
//Implement the sort.Interface interface exchange element method
4 years ago
func (s MsgFormats) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}