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/admin_cms/admin_cms.go

737 lines
28 KiB

3 years ago
package admin_cms
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
2 years ago
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
3 years ago
"Open_IM/pkg/common/log"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
3 years ago
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbAdminCMS "Open_IM/pkg/proto/admin_cms"
2 years ago
server_api_params "Open_IM/pkg/proto/sdk_ws"
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3 years ago
"Open_IM/pkg/utils"
"context"
2 years ago
"errors"
3 years ago
"net"
"strconv"
"strings"
2 years ago
"sync"
"time"
3 years ago
"google.golang.org/grpc"
2 years ago
"gorm.io/gorm"
3 years ago
)
type adminCMSServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
}
func NewAdminCMSServer(port int) *adminCMSServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
3 years ago
return &adminCMSServer{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImAdminCMSName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
}
func (s *adminCMSServer) Run() {
log.NewInfo("0", "AdminCMS rpc start ")
3 years ago
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(s.rpcPort)
3 years ago
//listener network
3 years ago
listener, err := net.Listen("tcp", address)
3 years ago
if err != nil {
3 years ago
panic("listening err:" + err.Error() + s.rpcRegisterName)
3 years ago
}
3 years ago
log.NewInfo("0", "listen network success, ", address, listener)
3 years ago
defer listener.Close()
2 years ago
var grpcOpts []grpc.ServerOption
if config.Config.Prometheus.Enable {
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
2 years ago
grpcOpts = append(grpcOpts, []grpc.ServerOption{
2 years ago
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
2 years ago
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
2 years ago
}
srv := grpc.NewServer(grpcOpts...)
3 years ago
defer srv.GracefulStop()
//Service registers with etcd
pbAdminCMS.RegisterAdminCMSServer(srv, s)
2 years ago
rpcRegisterIP := config.Config.RpcRegisterIP
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
2 years ago
log.NewInfo("", "rpcRegisterIP ", rpcRegisterIP)
3 years ago
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10)
3 years ago
if err != nil {
log.NewError("0", "RegisterEtcd failed ", err.Error())
panic(utils.Wrap(err, "register admin module rpc to etcd err"))
3 years ago
}
err = srv.Serve(listener)
if err != nil {
log.NewError("0", "Serve failed ", err.Error())
return
}
log.NewInfo("0", "message cms rpc success")
}
func (s *adminCMSServer) AdminLogin(_ context.Context, req *pbAdminCMS.AdminLoginReq) (*pbAdminCMS.AdminLoginResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
2 years ago
resp := &pbAdminCMS.AdminLoginResp{CommonResp: &pbAdminCMS.CommonResp{}}
3 years ago
for i, adminID := range config.Config.Manager.AppManagerUid {
3 years ago
if adminID == req.AdminID && config.Config.Manager.Secrets[i] == req.Secret {
token, expTime, err := token_verify.CreateToken(adminID, constant.SingleChatType)
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "generate token failed", "adminID: ", adminID, err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrTokenUnknown.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
3 years ago
}
2 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "generate token success", "token: ", token, "expTime:", expTime)
3 years ago
resp.Token = token
break
3 years ago
}
}
if resp.Token == "" {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "failed")
2 years ago
resp.CommonResp.ErrCode = constant.ErrTokenUnknown.ErrCode
resp.CommonResp.ErrMsg = constant.ErrTokenMalformed.ErrMsg
return resp, nil
3 years ago
}
2 years ago
admin, err := imdb.GetUserByUserID(req.AdminID)
if err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "failed", req.AdminID, err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrTokenUnknown.ErrCode
2 years ago
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
3 years ago
}
2 years ago
resp.UserName = admin.Nickname
resp.FaceURL = admin.FaceURL
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
3 years ago
return resp, nil
3 years ago
}
2 years ago
2 years ago
func (s *adminCMSServer) GetUserToken(_ context.Context, req *pbAdminCMS.GetUserTokenReq) (*pbAdminCMS.GetUserTokenResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbAdminCMS.GetUserTokenResp{
CommonResp: &pbAdminCMS.CommonResp{},
}
token, expTime, err := token_verify.CreateToken(req.UserID, int(req.PlatformID))
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "generate token failed", "userID: ", req.UserID, req.PlatformID, err.Error())
resp.CommonResp.ErrCode = constant.ErrTokenUnknown.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
resp.Token = token
resp.ExpTime = expTime
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", resp.String())
return resp, nil
}
2 years ago
func (s *adminCMSServer) AddUserRegisterAddFriendIDList(_ context.Context, req *pbAdminCMS.AddUserRegisterAddFriendIDListReq) (*pbAdminCMS.AddUserRegisterAddFriendIDListResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
2 years ago
resp := &pbAdminCMS.AddUserRegisterAddFriendIDListResp{CommonResp: &pbAdminCMS.CommonResp{}}
2 years ago
if err := imdb.AddUserRegisterAddFriendIDList(req.UserIDList...); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserIDList)
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
2 years ago
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", req.String())
return resp, nil
}
func (s *adminCMSServer) ReduceUserRegisterAddFriendIDList(_ context.Context, req *pbAdminCMS.ReduceUserRegisterAddFriendIDListReq) (*pbAdminCMS.ReduceUserRegisterAddFriendIDListResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
2 years ago
resp := &pbAdminCMS.ReduceUserRegisterAddFriendIDListResp{CommonResp: &pbAdminCMS.CommonResp{}}
2 years ago
if req.Operation == 0 {
if err := imdb.ReduceUserRegisterAddFriendIDList(req.UserIDList...); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserIDList)
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
2 years ago
}
} else {
if err := imdb.DeleteAllRegisterAddFriendIDList(); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserIDList)
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
2 years ago
}
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", req.String())
return resp, nil
}
func (s *adminCMSServer) GetUserRegisterAddFriendIDList(_ context.Context, req *pbAdminCMS.GetUserRegisterAddFriendIDListReq) (*pbAdminCMS.GetUserRegisterAddFriendIDListResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
2 years ago
resp := &pbAdminCMS.GetUserRegisterAddFriendIDListResp{CommonResp: &pbAdminCMS.CommonResp{}}
2 years ago
userIDList, err := imdb.GetRegisterAddFriendList(req.Pagination.ShowNumber, req.Pagination.PageNumber)
2 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
2 years ago
}
userList, err := imdb.GetUsersByUserIDList(userIDList)
if err != nil {
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
2 years ago
}
2 years ago
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), userList, userIDList)
resp.Pagination = &server_api_params.ResponsePagination{
CurrentPage: req.Pagination.PageNumber,
ShowNumber: req.Pagination.ShowNumber,
}
2 years ago
utils.CopyStructFields(&resp.UserInfoList, userList)
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", req.String())
return resp, nil
}
2 years ago
func (s *adminCMSServer) GetChatLogs(_ context.Context, req *pbAdminCMS.GetChatLogsReq) (*pbAdminCMS.GetChatLogsResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "GetChatLogs", req.String())
2 years ago
resp := &pbAdminCMS.GetChatLogsResp{CommonResp: &pbAdminCMS.CommonResp{}, Pagination: &server_api_params.ResponsePagination{}}
chatLog := imdb.ChatLog{
2 years ago
Content: req.Content,
ContentType: req.ContentType,
SessionType: req.SessionType,
RecvID: req.RecvID,
SendID: req.SendID,
}
if req.SendTime != "" {
sendTime, err := utils.TimeStringToTime(req.SendTime)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "time string parse error", err.Error())
resp.CommonResp.ErrCode = constant.ErrArgs.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
chatLog.SendTime = sendTime
}
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "chat_log: ", chatLog)
num, chatLogs, err := imdb.GetChatLog(&chatLog, req.Pagination.PageNumber, req.Pagination.ShowNumber, []int32{
constant.Text,
constant.Picture,
constant.Voice,
constant.Video,
constant.File,
constant.AtText,
constant.Merger,
constant.Card,
constant.Location,
constant.Custom,
constant.Revoke,
constant.Quote,
constant.AdvancedText,
constant.AdvancedRevoke,
constant.CustomNotTriggerConversation,
})
2 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetChatLog", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
}
resp.ChatLogsNum = int32(num)
2 years ago
for _, chatLog := range chatLogs {
pbChatLog := &pbAdminCMS.ChatLog{}
utils.CopyStructFields(pbChatLog, chatLog)
pbChatLog.SendTime = chatLog.SendTime.Unix()
pbChatLog.CreateTime = chatLog.CreateTime.Unix()
if chatLog.SenderNickname == "" {
sendUser, err := imdb.GetUserByUserID(chatLog.SendID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserByUserID failed", err.Error())
continue
}
pbChatLog.SenderNickname = sendUser.Nickname
}
switch chatLog.SessionType {
case constant.SingleChatType:
recvUser, err := imdb.GetUserByUserID(chatLog.RecvID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserByUserID failed", err.Error())
continue
}
pbChatLog.SenderNickname = recvUser.Nickname
2 years ago
case constant.GroupChatType, constant.SuperGroupChatType:
2 years ago
group, err := imdb.GetGroupInfoByGroupID(chatLog.RecvID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupById failed")
continue
}
pbChatLog.RecvID = group.GroupID
2 years ago
pbChatLog.GroupName = group.GroupName
2 years ago
}
resp.ChatLogs = append(resp.ChatLogs, pbChatLog)
}
2 years ago
resp.Pagination = &server_api_params.ResponsePagination{
2 years ago
CurrentPage: req.Pagination.PageNumber,
ShowNumber: req.Pagination.ShowNumber,
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp output: ", resp.String())
return resp, nil
}
func (s *adminCMSServer) GetActiveGroup(_ context.Context, req *pbAdminCMS.GetActiveGroupReq) (*pbAdminCMS.GetActiveGroupResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req", req.String())
resp := &pbAdminCMS.GetActiveGroupResp{CommonResp: &pbAdminCMS.CommonResp{}}
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "ParseTimeFromTo failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "time: ", fromTime, toTime)
activeGroups, err := imdb.GetActiveGroups(fromTime, toTime, 12)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetActiveGroups failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
for _, activeGroup := range activeGroups {
resp.Groups = append(resp.Groups,
&pbAdminCMS.GroupResp{
GroupName: activeGroup.Name,
GroupId: activeGroup.Id,
MessageNum: int32(activeGroup.MessageNum),
})
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), resp.String())
return resp, nil
}
func (s *adminCMSServer) GetActiveUser(_ context.Context, req *pbAdminCMS.GetActiveUserReq) (*pbAdminCMS.GetActiveUserResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
2 years ago
resp := &pbAdminCMS.GetActiveUserResp{CommonResp: &pbAdminCMS.CommonResp{}}
2 years ago
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "ParseTimeFromTo failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "time: ", fromTime, toTime)
activeUsers, err := imdb.GetActiveUsers(fromTime, toTime, 12)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetActiveUsers failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
for _, activeUser := range activeUsers {
resp.Users = append(resp.Users,
&pbAdminCMS.UserResp{
2 years ago
UserID: activeUser.ID,
2 years ago
NickName: activeUser.Name,
MessageNum: int32(activeUser.MessageNum),
},
)
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), resp.String())
return resp, nil
}
func ParseTimeFromTo(from, to string) (time.Time, time.Time, error) {
var fromTime time.Time
var toTime time.Time
fromTime, err := utils.TimeStringToTime(from)
if err != nil {
return fromTime, toTime, err
}
toTime, err = utils.TimeStringToTime(to)
if err != nil {
return fromTime, toTime, err
}
return fromTime, toTime, nil
}
func isInOneMonth(from, to time.Time) bool {
return from.Month() == to.Month() && from.Year() == to.Year()
}
func GetRangeDate(from, to time.Time) [][2]time.Time {
interval := to.Sub(from)
var times [][2]time.Time
switch {
// today
case interval == 0:
times = append(times, [2]time.Time{
from, from.Add(time.Hour * 24),
})
// days
case isInOneMonth(from, to):
for i := 0; ; i++ {
fromTime := from.Add(time.Hour * 24 * time.Duration(i))
toTime := from.Add(time.Hour * 24 * time.Duration(i+1))
if toTime.After(to.Add(time.Hour * 24)) {
break
}
times = append(times, [2]time.Time{
fromTime, toTime,
})
}
// month
case !isInOneMonth(from, to):
if to.Sub(from) < time.Hour*24*30 {
for i := 0; ; i++ {
fromTime := from.Add(time.Hour * 24 * time.Duration(i))
toTime := from.Add(time.Hour * 24 * time.Duration(i+1))
if toTime.After(to.Add(time.Hour * 24)) {
break
}
times = append(times, [2]time.Time{
fromTime, toTime,
})
}
} else {
for i := 0; ; i++ {
if i == 0 {
fromTime := from
toTime := getFirstDateOfNextNMonth(fromTime, 1)
times = append(times, [2]time.Time{
fromTime, toTime,
})
} else {
fromTime := getFirstDateOfNextNMonth(from, i)
toTime := getFirstDateOfNextNMonth(fromTime, 1)
if toTime.After(to) {
toTime = to
times = append(times, [2]time.Time{
fromTime, toTime,
})
break
}
times = append(times, [2]time.Time{
fromTime, toTime,
})
}
}
}
}
return times
}
func getFirstDateOfNextNMonth(currentTime time.Time, n int) time.Time {
lastOfMonth := time.Date(currentTime.Year(), currentTime.Month(), 1, 0, 0, 0, 0, currentTime.Location()).AddDate(0, n, 0)
return lastOfMonth
}
func (s *adminCMSServer) GetGroupStatistics(_ context.Context, req *pbAdminCMS.GetGroupStatisticsReq) (*pbAdminCMS.GetGroupStatisticsResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
2 years ago
resp := &pbAdminCMS.GetGroupStatisticsResp{CommonResp: &pbAdminCMS.CommonResp{}}
2 years ago
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupStatistics failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
increaseGroupNum, err := imdb.GetIncreaseGroupNum(fromTime, toTime.Add(time.Hour*24))
2 years ago
2 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseGroupNum failed", err.Error(), fromTime, toTime)
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
totalGroupNum, err := imdb.GetTotalGroupNum()
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
resp.IncreaseGroupNum = increaseGroupNum
resp.TotalGroupNum = totalGroupNum
times := GetRangeDate(fromTime, toTime)
log.NewDebug(req.OperationID, "times:", times)
wg := &sync.WaitGroup{}
resp.IncreaseGroupNumList = make([]*pbAdminCMS.DateNumList, len(times), len(times))
resp.TotalGroupNumList = make([]*pbAdminCMS.DateNumList, len(times), len(times))
wg.Add(len(times))
for i, v := range times {
go func(wg *sync.WaitGroup, index int, v [2]time.Time) {
defer wg.Done()
num, err := imdb.GetIncreaseGroupNum(v[0], v[1])
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
}
resp.IncreaseGroupNumList[index] = &pbAdminCMS.DateNumList{
Date: v[0].String(),
Num: num,
}
num, err = imdb.GetGroupNum(v[1])
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
}
resp.TotalGroupNumList[index] = &pbAdminCMS.DateNumList{
Date: v[0].String(),
Num: num,
}
}(wg, i, v)
}
wg.Wait()
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
return resp, nil
}
func (s *adminCMSServer) GetMessageStatistics(_ context.Context, req *pbAdminCMS.GetMessageStatisticsReq) (*pbAdminCMS.GetMessageStatisticsResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
2 years ago
resp := &pbAdminCMS.GetMessageStatisticsResp{CommonResp: &pbAdminCMS.CommonResp{}}
2 years ago
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "times: ", fromTime, toTime)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "ParseTimeFromTo failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
privateMessageNum, err := imdb.GetPrivateMessageNum(fromTime, toTime.Add(time.Hour*24))
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetPrivateMessageNum failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
groupMessageNum, err := imdb.GetGroupMessageNum(fromTime, toTime.Add(time.Hour*24))
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMessageNum failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), privateMessageNum, groupMessageNum)
resp.PrivateMessageNum = privateMessageNum
resp.GroupMessageNum = groupMessageNum
times := GetRangeDate(fromTime, toTime)
resp.GroupMessageNumList = make([]*pbAdminCMS.DateNumList, len(times), len(times))
resp.PrivateMessageNumList = make([]*pbAdminCMS.DateNumList, len(times), len(times))
wg := &sync.WaitGroup{}
wg.Add(len(times))
for i, v := range times {
go func(wg *sync.WaitGroup, index int, v [2]time.Time) {
defer wg.Done()
num, err := imdb.GetPrivateMessageNum(v[0], v[1])
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
}
resp.PrivateMessageNumList[index] = &pbAdminCMS.DateNumList{
Date: v[0].String(),
Num: num,
}
num, err = imdb.GetGroupMessageNum(v[0], v[1])
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
}
resp.GroupMessageNumList[index] = &pbAdminCMS.DateNumList{
Date: v[0].String(),
Num: num,
}
}(wg, i, v)
}
wg.Wait()
return resp, nil
}
func (s *adminCMSServer) GetUserStatistics(_ context.Context, req *pbAdminCMS.GetUserStatisticsReq) (*pbAdminCMS.GetUserStatisticsResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
2 years ago
resp := &pbAdminCMS.GetUserStatisticsResp{CommonResp: &pbAdminCMS.CommonResp{}}
2 years ago
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "ParseTimeFromTo failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
activeUserNum, err := imdb.GetActiveUserNum(fromTime, toTime.Add(time.Hour*24))
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetActiveUserNum failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
increaseUserNum, err := imdb.GetIncreaseUserNum(fromTime, toTime.Add(time.Hour*24))
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseUserNum failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
totalUserNum, err := imdb.GetTotalUserNum()
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetTotalUserNum failed", err.Error())
2 years ago
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
2 years ago
return resp, nil
2 years ago
}
resp.ActiveUserNum = activeUserNum
resp.TotalUserNum = totalUserNum
resp.IncreaseUserNum = increaseUserNum
times := GetRangeDate(fromTime, toTime)
resp.TotalUserNumList = make([]*pbAdminCMS.DateNumList, len(times), len(times))
resp.ActiveUserNumList = make([]*pbAdminCMS.DateNumList, len(times), len(times))
resp.IncreaseUserNumList = make([]*pbAdminCMS.DateNumList, len(times), len(times))
wg := &sync.WaitGroup{}
wg.Add(len(times))
for i, v := range times {
go func(wg *sync.WaitGroup, index int, v [2]time.Time) {
defer wg.Done()
num, err := imdb.GetActiveUserNum(v[0], v[1])
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
}
resp.ActiveUserNumList[index] = &pbAdminCMS.DateNumList{
Date: v[0].String(),
Num: num,
}
num, err = imdb.GetTotalUserNumByDate(v[1])
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetTotalUserNumByDate", v, err.Error())
}
resp.TotalUserNumList[index] = &pbAdminCMS.DateNumList{
Date: v[0].String(),
Num: num,
}
num, err = imdb.GetIncreaseUserNum(v[0], v[1])
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetIncreaseUserNum", v, err.Error())
}
resp.IncreaseUserNumList[index] = &pbAdminCMS.DateNumList{
Date: v[0].String(),
Num: num,
}
}(wg, i, v)
}
wg.Wait()
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
return resp, nil
}
2 years ago
func (s *adminCMSServer) GetUserFriends(_ context.Context, req *pbAdminCMS.GetUserFriendsReq) (*pbAdminCMS.GetUserFriendsResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbAdminCMS.GetUserFriendsResp{CommonResp: &pbAdminCMS.CommonResp{}, Pagination: &server_api_params.ResponsePagination{CurrentPage: req.Pagination.PageNumber, ShowNumber: req.Pagination.ShowNumber}}
var friendList []*imdb.FriendUser
var err error
if req.FriendUserID != "" {
friend, err := imdb.GetFriendByIDCMS(req.UserID, req.FriendUserID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), err, req.UserID, req.FriendUserID)
2 years ago
return resp, nil
}
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID, req.FriendUserID)
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
friendList = append(friendList, friend)
2 years ago
resp.FriendNums = 1
2 years ago
} else {
2 years ago
var count int64
friendList, count, err = imdb.GetUserFriendsCMS(req.UserID, req.FriendUserName, req.Pagination.PageNumber, req.Pagination.ShowNumber)
2 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID, req.FriendUserName, req.Pagination.PageNumber, req.Pagination.ShowNumber)
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
2 years ago
resp.FriendNums = int32(count)
2 years ago
}
for _, v := range friendList {
friendInfo := &server_api_params.FriendInfo{}
userInfo := &server_api_params.UserInfo{UserID: v.FriendUserID, Nickname: v.Nickname}
utils.CopyStructFields(friendInfo, v)
friendInfo.FriendUser = userInfo
resp.FriendInfoList = append(resp.FriendInfoList, friendInfo)
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
2 years ago
return resp, nil
2 years ago
}
2 years ago
func (s *adminCMSServer) GetUserIDByEmailAndPhoneNumber(_ context.Context, req *pbAdminCMS.GetUserIDByEmailAndPhoneNumberReq) (*pbAdminCMS.GetUserIDByEmailAndPhoneNumberResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbAdminCMS.GetUserIDByEmailAndPhoneNumberResp{CommonResp: &pbAdminCMS.CommonResp{}}
userIDList, err := imdb.GetUserIDsByEmailAndID(req.PhoneNumber, req.Email)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.PhoneNumber, req.Email)
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
resp.UserIDList = userIDList
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
2 years ago
func (s *adminCMSServer) GenerateInvitationCode(_ context.Context, req *pbAdminCMS.GenerateInvitationCodeReq) (*pbAdminCMS.GenerateInvitationCodeResp, error) {
return nil, nil
}
func (s *adminCMSServer) GetInvitationCodes(_ context.Context, req *pbAdminCMS.GetInvitationCodesReq) (*pbAdminCMS.GetInvitationCodesResp, error) {
return nil, nil
}
func (s *adminCMSServer) QueryIPRegister(_ context.Context, req *pbAdminCMS.QueryIPRegisterReq) (*pbAdminCMS.QueryIPRegisterResp, error) {
return nil, nil
}
func (s *adminCMSServer) AddIPLimit(_ context.Context, req *pbAdminCMS.AddIPLimitReq) (*pbAdminCMS.AddIPLimitResp, error) {
return nil, nil
}
func (s *adminCMSServer) RemoveIPLimit(_ context.Context, req *pbAdminCMS.RemoveIPLimitReq) (*pbAdminCMS.RemoveIPLimitResp, error) {
return nil, nil
}
func (s *adminCMSServer) QueryUserIDIPLimitLogin(_ context.Context, req *pbAdminCMS.QueryUserIDIPLimitLoginReq) (*pbAdminCMS.QueryUserIDIPLimitLoginResp, error) {
return nil, nil
}
func (s *adminCMSServer) AddUserIPLimitLogin(_ context.Context, req *pbAdminCMS.AddUserIPLimitLoginReq) (*pbAdminCMS.AddUserIPLimitLoginResp, error) {
return nil, nil
}
func (s *adminCMSServer) RemoveUserIPLimit(_ context.Context, req *pbAdminCMS.RemoveUserIPLimitReq) (*pbAdminCMS.RemoveUserIPLimitResp, error) {
return nil, nil
}
func (s *adminCMSServer) GetClientInitConfig(_ context.Context, req *pbAdminCMS.GetClientInitConfigReq) (*pbAdminCMS.GetClientInitConfigResp, error) {
return nil, nil
}
func (s *adminCMSServer) SetClientInitConfig(_ context.Context, req *pbAdminCMS.SetClientInitConfigReq) (*pbAdminCMS.SetClientInitConfigResp, error) {
return nil, nil
}