parent
68eaa51ebe
commit
66c1b19696
@ -0,0 +1,204 @@
|
||||
/*
|
||||
** description("").
|
||||
** copyright('open-im,www.open-im.io').
|
||||
** author("fg,Gordon@tuoyun.net").
|
||||
** time(2021/9/15 15:23).
|
||||
*/
|
||||
package manage
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/constant"
|
||||
"Open_IM/src/common/log"
|
||||
"Open_IM/src/grpc-etcdv3/getcdv3"
|
||||
pbChat "Open_IM/src/proto/chat"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var validate *validator.Validate
|
||||
|
||||
type paramsManagementSendMsg struct {
|
||||
OperationID string `json:"operationID" binding:"required"`
|
||||
SendID string `json:"sendID" binding:"required"`
|
||||
RecvID string `json:"recvID" binding:"required"`
|
||||
SenderNickName string `json:"senderNickName" `
|
||||
SenderFaceURL string `json:"senderFaceURL" `
|
||||
ForceList []string `json:"forceList" `
|
||||
Content map[string]interface{} `json:"content" binding:"required"`
|
||||
ContentType int32 `json:"contentType" binding:"required"`
|
||||
SessionType int32 `json:"sessionType" binding:"required"`
|
||||
}
|
||||
|
||||
func newUserSendMsgReq(token string, params *paramsManagementSendMsg) *pbChat.UserSendMsgReq {
|
||||
var newContent string
|
||||
switch params.ContentType {
|
||||
case constant.Text:
|
||||
newContent = params.Content["text"].(string)
|
||||
case constant.Picture:
|
||||
fallthrough
|
||||
case constant.Custom:
|
||||
fallthrough
|
||||
case constant.Voice:
|
||||
fallthrough
|
||||
case constant.File:
|
||||
newContent = utils.StructToJsonString(params.Content)
|
||||
default:
|
||||
|
||||
}
|
||||
pbData := pbChat.UserSendMsgReq{
|
||||
ReqIdentifier: constant.WSSendMsg,
|
||||
Token: token,
|
||||
SendID: params.SendID,
|
||||
SenderNickName: params.SenderNickName,
|
||||
SenderFaceURL: params.SenderFaceURL,
|
||||
OperationID: params.OperationID,
|
||||
PlatformID: 0,
|
||||
SessionType: params.SessionType,
|
||||
MsgFrom: constant.UserMsgType,
|
||||
ContentType: params.ContentType,
|
||||
RecvID: params.RecvID,
|
||||
ForceList: params.ForceList,
|
||||
Content: newContent,
|
||||
ClientMsgID: utils.GetMsgID(params.SendID),
|
||||
}
|
||||
return &pbData
|
||||
}
|
||||
func init() {
|
||||
validate = validator.New()
|
||||
}
|
||||
func ManagementSendMsg(c *gin.Context) {
|
||||
var data interface{}
|
||||
params := paramsManagementSendMsg{}
|
||||
if err := c.BindJSON(¶ms); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
log.ErrorByKv("json unmarshal err", c.PostForm("operationID"), "err", err.Error(), "content", c.PostForm("content"))
|
||||
return
|
||||
}
|
||||
switch params.ContentType {
|
||||
case constant.Text:
|
||||
data = TextElem{}
|
||||
case constant.Picture:
|
||||
data = PictureElem{}
|
||||
case constant.Custom:
|
||||
data = CustomElem{}
|
||||
default:
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 404, "errMsg": "contentType err"})
|
||||
log.ErrorByKv("contentType err", c.PostForm("operationID"), "content", c.PostForm("content"))
|
||||
return
|
||||
}
|
||||
if err := mapstructure.WeakDecode(params.Content, &data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": err.Error()})
|
||||
log.ErrorByKv("content to Data struct err", "", "err", err.Error())
|
||||
return
|
||||
} else if err := validate.Struct(data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 403, "errMsg": err.Error()})
|
||||
log.ErrorByKv("data args validate err", "", "err", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
token := c.Request.Header.Get("token")
|
||||
if !utils.VerifyToken(token, config.Config.AppManagerUid) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "token validate err,not authorized", "sendTime": 0, "MsgID": ""})
|
||||
return
|
||||
}
|
||||
|
||||
log.InfoByKv("Ws call success to ManagementSendMsgReq", params.OperationID, "Parameters", params)
|
||||
|
||||
pbData := newUserSendMsgReq(token, ¶ms)
|
||||
log.Info("", "", "api ManagementSendMsg call start..., [data: %s]", pbData.String())
|
||||
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOfflineMessageName)
|
||||
client := pbChat.NewChatClient(etcdConn)
|
||||
|
||||
log.Info("", "", "api ManagementSendMsg call, api call rpc...")
|
||||
|
||||
reply, _ := client.UserSendMsg(context.Background(), pbData)
|
||||
log.Info("", "", "api ManagementSendMsg call end..., [data: %s] [reply: %s]", pbData.String(), reply.String())
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"errCode": reply.ErrCode,
|
||||
"errMsg": reply.ErrMsg,
|
||||
"sendTime": reply.SendTime,
|
||||
"msgID": reply.ClientMsgID,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
type PictureBaseInfo struct {
|
||||
UUID string `mapstructure:"uuid"`
|
||||
Type string `mapstructure:"type" validate:"required"`
|
||||
Size int64 `mapstructure:"size" validate:"required"`
|
||||
Width int32 `mapstructure:"width" validate:"required"`
|
||||
Height int32 `mapstructure:"height" validate:"required"`
|
||||
Url string `mapstructure:"url" validate:"required"`
|
||||
}
|
||||
|
||||
type PictureElem struct {
|
||||
SourcePath string `mapstructure:"sourcePath"`
|
||||
SourcePicture PictureBaseInfo `mapstructure:"sourcePicture" validate:"required"`
|
||||
BigPicture PictureBaseInfo `mapstructure:"bigPicture" `
|
||||
SnapshotPicture PictureBaseInfo `mapstructure:"snapshotPicture"`
|
||||
}
|
||||
type SoundElem struct {
|
||||
UUID string `mapstructure:"uuid"`
|
||||
SoundPath string `mapstructure:"soundPath"`
|
||||
SourceURL string `mapstructure:"sourceUrl"`
|
||||
DataSize int64 `mapstructure:"dataSize"`
|
||||
Duration int64 `mapstructure:"duration"`
|
||||
}
|
||||
type VideoElem struct {
|
||||
VideoPath string `mapstructure:"videoPath"`
|
||||
VideoUUID string `mapstructure:"videoUUID"`
|
||||
VideoURL string `mapstructure:"videoUrl"`
|
||||
VideoType string `mapstructure:"videoType"`
|
||||
VideoSize int64 `mapstructure:"videoSize"`
|
||||
Duration int64 `mapstructure:"duration"`
|
||||
SnapshotPath string `mapstructure:"snapshotPath"`
|
||||
SnapshotUUID string `mapstructure:"snapshotUUID"`
|
||||
SnapshotSize int64 `mapstructure:"snapshotSize"`
|
||||
SnapshotURL string `mapstructure:"snapshotUrl"`
|
||||
SnapshotWidth int32 `mapstructure:"snapshotWidth"`
|
||||
SnapshotHeight int32 `mapstructure:"snapshotHeight"`
|
||||
}
|
||||
type FileElem struct {
|
||||
FilePath string `mapstructure:"filePath"`
|
||||
UUID string `mapstructure:"uuid"`
|
||||
SourceURL string `mapstructure:"sourceUrl"`
|
||||
FileName string `mapstructure:"fileName"`
|
||||
FileSize int64 `mapstructure:"fileSize"`
|
||||
}
|
||||
|
||||
//type MergeElem struct {
|
||||
// Title string `json:"title"`
|
||||
// AbstractList []string `json:"abstractList"`
|
||||
// MultiMessage []*MsgStruct `json:"multiMessage"`
|
||||
//}
|
||||
type AtElem struct {
|
||||
Text string `mapstructure:"text"`
|
||||
AtUserList []string `mapstructure:"atUserList"`
|
||||
IsAtSelf bool `mapstructure:"isAtSelf"`
|
||||
}
|
||||
type LocationElem struct {
|
||||
Description string `mapstructure:"description"`
|
||||
Longitude float64 `mapstructure:"longitude"`
|
||||
Latitude float64 `mapstructure:"latitude"`
|
||||
}
|
||||
type CustomElem struct {
|
||||
Data string `mapstructure:"data" validate:"required"`
|
||||
Description string `mapstructure:"description"`
|
||||
Extension string `mapstructure:"extension"`
|
||||
}
|
||||
type TextElem struct {
|
||||
Text string `mapstructure:"text" validate:"required"`
|
||||
}
|
||||
|
||||
//type QuoteElem struct {
|
||||
// Text string `json:"text"`
|
||||
// QuoteMessage *MsgStruct `json:"quoteMessage"`
|
||||
//}
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
** description("").
|
||||
** copyright('open-im,www.open-im.io').
|
||||
** author("fg,Gordon@tuoyun.net").
|
||||
** time(2021/9/15 10:28).
|
||||
*/
|
||||
package manage
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/log"
|
||||
"Open_IM/src/grpc-etcdv3/getcdv3"
|
||||
pbUser "Open_IM/src/proto/user"
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type paramsDeleteUsers struct {
|
||||
OperationID string `json:"operationID" binding:"required"`
|
||||
DeleteUidList []string `json:"deleteUidList" binding:"required"`
|
||||
}
|
||||
type paramsGetAllUsersUid struct {
|
||||
OperationID string `json:"operationID" binding:"required"`
|
||||
}
|
||||
|
||||
func DeleteUser(c *gin.Context) {
|
||||
params := paramsDeleteUsers{}
|
||||
if err := c.BindJSON(¶ms); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
log.InfoByKv("DeleteUser req come here", params.OperationID, "DeleteUidList", params.DeleteUidList)
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
|
||||
client := pbUser.NewUserClient(etcdConn)
|
||||
//defer etcdConn.Close()
|
||||
|
||||
req := &pbUser.DeleteUsersReq{
|
||||
OperationID: params.OperationID,
|
||||
DeleteUidList: params.DeleteUidList,
|
||||
Token: c.Request.Header.Get("token"),
|
||||
}
|
||||
RpcResp, err := client.DeleteUsers(context.Background(), req)
|
||||
if err != nil {
|
||||
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call delete users rpc server failed"})
|
||||
return
|
||||
}
|
||||
log.InfoByKv("call delete user rpc server is success", params.OperationID, "resp args", RpcResp.String())
|
||||
resp := gin.H{"errCode": RpcResp.CommonResp.ErrorCode, "errMsg": RpcResp.CommonResp.ErrorMsg, "failedUidList": RpcResp.FailedUidList}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
func GetAllUsersUid(c *gin.Context) {
|
||||
params := paramsGetAllUsersUid{}
|
||||
if err := c.BindJSON(¶ms); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
log.InfoByKv("GetAllUsersUid req come here", params.OperationID)
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
|
||||
client := pbUser.NewUserClient(etcdConn)
|
||||
//defer etcdConn.Close()
|
||||
|
||||
req := &pbUser.GetAllUsersUidReq{
|
||||
OperationID: params.OperationID,
|
||||
Token: c.Request.Header.Get("token"),
|
||||
}
|
||||
RpcResp, err := client.GetAllUsersUid(context.Background(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error(), "uidList": []string{}})
|
||||
return
|
||||
}
|
||||
log.InfoByKv("call GetAllUsersUid rpc server is success", params.OperationID, "resp args", RpcResp.String())
|
||||
resp := gin.H{"errCode": RpcResp.CommonResp.ErrorCode, "errMsg": RpcResp.CommonResp.ErrorMsg, "uidList": RpcResp.UidList}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,61 @@
|
||||
/*
|
||||
** description("").
|
||||
** copyright('open-im,www.open-im.io').
|
||||
** author("fg,Gordon@tuoyun.net").
|
||||
** time(2021/9/15 10:28).
|
||||
*/
|
||||
package user
|
||||
|
||||
import (
|
||||
"Open_IM/src/common/config"
|
||||
"Open_IM/src/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/src/common/log"
|
||||
pbUser "Open_IM/src/proto/user"
|
||||
"Open_IM/src/utils"
|
||||
"context"
|
||||
)
|
||||
|
||||
func (s *userServer) DeleteUsers(_ context.Context, req *pbUser.DeleteUsersReq) (*pbUser.DeleteUsersResp, error) {
|
||||
log.InfoByKv("rpc DeleteUsers arrived server", req.OperationID, "args", req.String())
|
||||
var resp pbUser.DeleteUsersResp
|
||||
c, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.ErrorByKv("parse token failed", req.OperationID, "err", err.Error())
|
||||
return &pbUser.DeleteUsersResp{CommonResp: &pbUser.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: err.Error()}, FailedUidList: req.DeleteUidList}, nil
|
||||
}
|
||||
if c.UID != config.Config.AppManagerUid {
|
||||
log.ErrorByKv(" Authentication failed", req.OperationID, "args", c)
|
||||
return &pbUser.DeleteUsersResp{CommonResp: &pbUser.CommonResp{ErrorCode: 401, ErrorMsg: "not authorized"}, FailedUidList: req.DeleteUidList}, nil
|
||||
}
|
||||
for _, uid := range req.DeleteUidList {
|
||||
err = im_mysql_model.UserDelete(uid)
|
||||
if err != nil {
|
||||
resp.CommonResp.ErrorCode = 201
|
||||
resp.CommonResp.ErrorMsg = "some uid deleted failed"
|
||||
resp.FailedUidList = append(resp.FailedUidList, uid)
|
||||
}
|
||||
}
|
||||
return &resp, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *userServer) GetAllUsersUid(_ context.Context, req *pbUser.GetAllUsersUidReq) (*pbUser.GetAllUsersUidResp, error) {
|
||||
log.InfoByKv("rpc GetAllUsersUid arrived server", req.OperationID, "args", req.String())
|
||||
c, err := utils.ParseToken(req.Token)
|
||||
if err != nil {
|
||||
log.InfoByKv("parse token failed", req.OperationID, "err", err.Error())
|
||||
return &pbUser.GetAllUsersUidResp{CommonResp: &pbUser.CommonResp{ErrorCode: config.ErrParseToken.ErrCode, ErrorMsg: err.Error()}}, nil
|
||||
}
|
||||
if c.UID != config.Config.AppManagerUid {
|
||||
log.ErrorByKv(" Authentication failed", req.OperationID, "args", c)
|
||||
return &pbUser.GetAllUsersUidResp{CommonResp: &pbUser.CommonResp{ErrorCode: 401, ErrorMsg: "not authorized"}}, nil
|
||||
}
|
||||
uidList, err := im_mysql_model.SelectAllUID()
|
||||
if err != nil {
|
||||
log.ErrorByKv("db get failed", req.OperationID, "err", err.Error())
|
||||
return &pbUser.GetAllUsersUidResp{CommonResp: &pbUser.CommonResp{ErrorCode: config.ErrMysql.ErrCode, ErrorMsg: err.Error()}}, nil
|
||||
} else {
|
||||
return &pbUser.GetAllUsersUidResp{CommonResp: &pbUser.CommonResp{ErrorCode: 0, ErrorMsg: ""}, UidList: uidList}, nil
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue