Merge remote-tracking branch 'origin/errcode' into errcode

test-errcode
withchao 2 years ago
commit 56a89ad33b

@ -4,9 +4,8 @@ import (
chat "Open_IM/internal/rpc/msg"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
rocksCache "Open_IM/pkg/common/db/rocks_cache"
"Open_IM/pkg/common/db/controller"
"Open_IM/pkg/common/db/relation"
"Open_IM/pkg/common/log"
promePkg "Open_IM/pkg/common/prometheus"
"Open_IM/pkg/common/token_verify"
@ -24,6 +23,7 @@ import (
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
utils2 "Open_IM/internal/utils"
"google.golang.org/grpc"
"gorm.io/gorm"
)
@ -33,16 +33,31 @@ type userServer struct {
rpcRegisterName string
etcdSchema string
etcdAddr []string
controller.UserInterface
}
func NewUserServer(port int) *userServer {
log.NewPrivateLog(constant.LogFileName)
return &userServer{
u := userServer{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImUserName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
//mysql init
var mysql relation.Mysql
var model relation.User
err := mysql.InitConn().AutoMigrateModel(&model)
if err != nil {
panic("db init err:" + err.Error())
}
if mysql.GormConn() != nil {
model.DB = mysql.GormConn()
} else {
panic("db init err:" + "conn is nil")
}
u.UserInterface = controller.NewUserController(model.DB)
return &u
}
func (s *userServer) Run() {
@ -124,25 +139,19 @@ func syncPeerUserConversation(conversation *pbConversation.Conversation, operati
}
func (s *userServer) GetUserInfo(ctx context.Context, req *pbUser.GetUserInfoReq) (*pbUser.GetUserInfoResp, error) {
log.NewInfo(req.OperationID, "GetUserInfo args ", req.String())
var userInfoList []*sdkws.UserInfo
if len(req.UserIDList) > 0 {
for _, userID := range req.UserIDList {
var userInfo sdkws.UserInfo
user, err := rocksCache.GetUserInfoFromCache(userID)
if err != nil {
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), userID)
continue
}
utils.CopyStructFields(&userInfo, user)
userInfo.BirthStr = utils.TimeToString(user.Birth)
userInfoList = append(userInfoList, &userInfo)
resp := &pbUser.GetUserInfoResp{}
users, err := s.Find(ctx, req.UserIDList)
if err != nil {
return nil, err
}
for _, v := range users {
n, err := utils2.NewDBUser(v).Convert()
if err != nil {
return nil, err
}
} else {
return &pbUser.GetUserInfoResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrArgs.ErrCode, ErrMsg: constant.ErrArgs.ErrMsg}}, nil
resp.UserInfoList = append(resp.UserInfoList, n)
}
log.NewInfo(req.OperationID, "GetUserInfo rpc return ", pbUser.GetUserInfoResp{CommonResp: &pbUser.CommonResp{}, UserInfoList: userInfoList})
return &pbUser.GetUserInfoResp{CommonResp: &pbUser.CommonResp{}, UserInfoList: userInfoList}, nil
return resp, nil
}
func (s *userServer) BatchSetConversations(ctx context.Context, req *pbUser.BatchSetConversationsReq) (*pbUser.BatchSetConversationsResp, error) {
@ -395,41 +404,32 @@ func (s *userServer) AccountCheck(_ context.Context, req *pbUser.AccountCheckReq
}
func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbUser.UpdateUserInfoReq) (*pbUser.UpdateUserInfoResp, error) {
log.NewInfo(req.OperationID, "UpdateUserInfo args ", req.String())
if !token_verify.CheckAccess(req.OpUserID, req.UserInfo.UserID) {
log.NewError(req.OperationID, "CheckAccess false ", req.OpUserID, req.UserInfo.UserID)
return &pbUser.UpdateUserInfoResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
resp := pbUser.UpdateUserInfoResp{}
err := token_verify.CheckAccessV3(ctx, req.UserInfo.UserID)
if err != nil {
return nil, err
}
oldNickname := ""
if req.UserInfo.Nickname != "" {
u, err := imdb.GetUserByUserID(req.UserInfo.UserID)
if err == nil {
oldNickname = u.Nickname
}
}
var user imdb.User
utils.CopyStructFields(&user, req.UserInfo)
if req.UserInfo.BirthStr != "" {
time, err := utils.TimeStringToTime(req.UserInfo.BirthStr)
u, err := s.Take(ctx, req.UserInfo.UserID)
if err != nil {
log.NewError(req.OperationID, "TimeStringToTime failed ", err.Error(), req.UserInfo.BirthStr)
return &pbUser.UpdateUserInfoResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrArgs.ErrCode, ErrMsg: "TimeStringToTime failed:" + err.Error()}}, nil
return nil, err
}
user.Birth = time
oldNickname = u.Nickname
}
err := imdb.UpdateUserInfo(user)
user, err := utils2.NewPBUser(req.UserInfo).Convert()
if err != nil {
log.NewError(req.OperationID, "UpdateUserInfo failed ", err.Error(), user)
return &pbUser.UpdateUserInfoResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
return nil, err
}
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName, req.OperationID)
if etcdConn == nil {
errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(req.OperationID, errMsg)
return &pbUser.UpdateUserInfoResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrInternal.ErrCode, ErrMsg: errMsg}}, nil
err = s.Update(ctx, []*relation.User{user})
if err != nil {
return nil, err
}
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImFriendName)
if err != nil {
return nil, err
}
client := pbFriend.NewFriendClient(etcdConn)
@ -644,84 +644,3 @@ func (s *userServer) AddUser(ctx context.Context, req *pbUser.AddUserReq) (*pbUs
}
return resp, nil
}
func (s *userServer) BlockUser(ctx context.Context, req *pbUser.BlockUserReq) (*pbUser.BlockUserResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbUser.BlockUserResp{CommonResp: &pbUser.CommonResp{}}
err := imdb.BlockUser(req.UserID, req.EndDisableTime)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "BlockUser", err.Error(), req.UserID, req.EndDisableTime)
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
func (s *userServer) UnBlockUser(ctx context.Context, req *pbUser.UnBlockUserReq) (*pbUser.UnBlockUserResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbUser.UnBlockUserResp{CommonResp: &pbUser.CommonResp{}}
err := imdb.UnBlockUser(req.UserID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "unBlockUser", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
func (s *userServer) GetBlockUsers(ctx context.Context, req *pbUser.GetBlockUsersReq) (resp *pbUser.GetBlockUsersResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbUser.GetBlockUsersResp{CommonResp: &pbUser.CommonResp{}, Pagination: &sdkws.ResponsePagination{ShowNumber: req.Pagination.ShowNumber, CurrentPage: req.Pagination.PageNumber}}
var blockUsers []imdb.BlockUserInfo
if req.UserID != "" {
blockUser, err := imdb.GetBlockUserByID(req.UserID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp, nil
}
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID)
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
blockUsers = append(blockUsers, blockUser)
resp.UserNums = 1
} else {
blockUsers, err = imdb.GetBlockUsers(req.Pagination.ShowNumber, req.Pagination.PageNumber)
if err != nil {
log.Error(req.OperationID, utils.GetSelfFuncName(), "GetBlockUsers", err.Error(), req.Pagination.ShowNumber, req.Pagination.PageNumber)
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
nums, err := imdb.GetBlockUsersNumCount()
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetBlockUsersNumCount failed", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
resp.UserNums = nums
}
for _, v := range blockUsers {
resp.BlockUsers = append(resp.BlockUsers, &pbUser.BlockUser{
UserInfo: &sdkws.UserInfo{
FaceURL: v.User.FaceURL,
Nickname: v.User.Nickname,
UserID: v.User.UserID,
PhoneNumber: v.User.PhoneNumber,
Email: v.User.Email,
Gender: v.User.Gender,
},
BeginDisableTime: (v.BeginDisableTime).String(),
EndDisableTime: (v.EndDisableTime).String(),
})
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
return resp, nil
}

@ -1,24 +1,69 @@
package controller
import (
"Open_IM/pkg/common/db/mysql"
"Open_IM/pkg/common/db/relation"
"context"
"gorm.io/gorm"
)
type UserModel struct {
db *relation.User
type UserInterface interface {
Find(ctx context.Context, userIDs []string) (users []*relation.User, err error)
Create(ctx context.Context, users []*relation.User) error
Take(ctx context.Context, userID string) (user *relation.User, err error)
Update(ctx context.Context, users []*relation.User) (err error)
}
func NewGroupUser(ctx context.Context) *UserModel {
var userModel UserModel
userModel.db = relation.NewUserDB()
return &userModel
type UserController struct {
database UserDatabaseInterface
}
func (u *UserModel) Find(ctx context.Context, userIDs []string) (users []*relation.User, err error) {
return u.db.Find(ctx, userIDs)
func (u *UserController) Find(ctx context.Context, userIDs []string) (users []*relation.User, err error) {
return u.database.Find(ctx, userIDs)
}
func (u *UserController) Create(ctx context.Context, users []*relation.User) error {
return u.database.Create(ctx, users)
}
func (u *UserController) Take(ctx context.Context, userID string) (user *relation.User, err error) {
return u.database.Take(ctx, userID)
}
func (u *UserController) Update(ctx context.Context, users []*relation.User) (err error) {
return u.database.Update(ctx, users)
}
func NewUserController(db *gorm.DB) UserInterface {
controller := &UserController{database: newUserDatabase(db)}
return controller
}
type UserDatabaseInterface interface {
Find(ctx context.Context, userIDs []string) (users []*relation.User, err error)
Create(ctx context.Context, users []*relation.User) error
Take(ctx context.Context, userID string) (user *relation.User, err error)
Update(ctx context.Context, users []*relation.User) (err error)
}
func (u *UserModel) Create(ctx context.Context, users []*relation.User) error {
return u.db.Create(ctx, users)
type UserDatabase struct {
sqlDB *relation.User
}
func newUserDatabase(db *gorm.DB) UserDatabaseInterface {
sqlDB := relation.NewUserDB(db)
database := &UserDatabase{
sqlDB: sqlDB,
}
return database
}
func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*relation.User, err error) {
return u.sqlDB.Find(ctx, userIDs)
}
func (u *UserDatabase) Create(ctx context.Context, users []*relation.User) error {
return u.sqlDB.Create(ctx, users)
}
func (u *UserDatabase) Take(ctx context.Context, userID string) (user *relation.User, err error) {
return u.sqlDB.Take(ctx, userID)
}
func (u *UserDatabase) Update(ctx context.Context, users []*relation.User) (err error) {
return u.sqlDB.Update(ctx, users)
}

@ -25,9 +25,9 @@ type User struct {
DB *gorm.DB `gorm:"-" json:"-"`
}
func NewUserDB() *User {
func NewUserDB(db *gorm.DB) *User {
var user User
user.DB = initMysqlDB(&user)
user.DB = db
return &user
}

@ -4,66 +4,48 @@ import "Open-IM-Server/pkg/proto/conversation/conversation.proto";
option go_package = "Open_IM/pkg/proto/user;user";
package user;
message CommonResp{
int32 errCode = 1;
string errMsg = 2;
}
message GetAllUserIDReq{
string opUserID = 1;
string operationID = 2;
server_api_params.RequestPagination pagination = 1;
}
message GetAllUserIDResp{
CommonResp CommonResp = 1;
int32 total = 1;
repeated string UserIDList = 2;
}
message AccountCheckReq{
repeated string CheckUserIDList = 1;
string OpUserID = 2;
string OperationID = 3;
repeated string checkUserIDs = 1;
}
message AccountCheckResp{
CommonResp commonResp = 1;
message SingleUserStatus {
string userID = 1;
string accountStatus = 2;
}
repeated SingleUserStatus ResultList = 2;
repeated SingleUserStatus results = 1;
}
message GetUserInfoReq{
repeated string userIDList = 1;
string OpUserID = 2;
string OperationID = 3;
message GetUsersInfoReq{
repeated string userIDs = 1;
}
message GetUserInfoResp{
CommonResp commonResp = 1;
repeated server_api_params.UserInfo UserInfoList = 3;
message GetUsersInfoResp{
repeated server_api_params.UserInfo usersInfo = 1;
}
message UpdateUserInfoReq{
server_api_params.UserInfo UserInfo = 1;
string OpUserID = 2;
string operationID = 3;
server_api_params.UserInfo userInfo = 1;
}
message UpdateUserInfoResp{
CommonResp commonResp = 1;
}
message SetGlobalRecvMessageOptReq{
string userID = 1;
string operationID = 2;
int32 globalRecvMsgOpt = 3;
}
message SetGlobalRecvMessageOptResp{
CommonResp commonResp = 1;
}
message SetConversationReq{
@ -73,7 +55,7 @@ message SetConversationReq{
}
message SetConversationResp{
CommonResp commonResp = 1;
}
message SetRecvMsgOptReq {
@ -85,7 +67,7 @@ message SetRecvMsgOptReq {
}
message SetRecvMsgOptResp {
CommonResp commonResp = 1;
}
message GetConversationReq{
@ -95,7 +77,6 @@ message GetConversationReq{
}
message GetConversationResp{
CommonResp commonResp = 1;
conversation.Conversation Conversation = 2;
}
@ -106,7 +87,6 @@ message GetConversationsReq{
}
message GetConversationsResp{
CommonResp commonResp = 1;
repeated conversation.Conversation Conversations = 2;
}
@ -116,7 +96,6 @@ message GetAllConversationsReq{
}
message GetAllConversationsResp{
CommonResp commonResp = 1;
repeated conversation.Conversation Conversations = 2;
}
@ -128,30 +107,21 @@ message BatchSetConversationsReq{
}
message BatchSetConversationsResp{
CommonResp commonResp = 1;
repeated string Success = 2;
repeated string Failed = 3;
}
message GetUsersReq {
string operationID = 1;
server_api_params.RequestPagination pagination = 2;
string userName = 3;
string userID = 4;
string content = 5;
}
message CmsUser {
server_api_params.UserInfo user = 1;
bool isBlock = 2;
}
message GetUsersResp{
CommonResp commonResp = 1;
repeated CmsUser userList = 2;
server_api_params.ResponsePagination Pagination = 3;
int32 totalNums = 4;
int32 total = 1;
repeated server_api_params.UserInfo users = 2;
}
message AddUserReq{
@ -160,7 +130,6 @@ message AddUserReq{
}
message AddUserResp{
CommonResp CommonResp = 1;
}
@ -199,7 +168,6 @@ message BlockUser {
}
message GetBlockUsersResp{
CommonResp CommonResp = 1;
repeated BlockUser BlockUsers = 2;
server_api_params.ResponsePagination Pagination = 3;
int32 UserNums = 4;
@ -207,26 +175,15 @@ message GetBlockUsersResp{
service user {
rpc GetUserInfo(GetUserInfoReq) returns(GetUserInfoResp);
//
rpc GetUsersInfo(GetUsersInfoReq) returns(GetUsersInfoResp);
//
rpc UpdateUserInfo(UpdateUserInfoReq) returns(UpdateUserInfoResp);
//
rpc SetGlobalRecvMessageOpt(SetGlobalRecvMessageOptReq) returns(SetGlobalRecvMessageOptResp);
rpc GetAllUserID(GetAllUserIDReq)returns(GetAllUserIDResp);
//userID
rpc AccountCheck(AccountCheckReq)returns(AccountCheckResp);
rpc GetConversation(GetConversationReq)returns(GetConversationResp);
rpc GetAllConversations(GetAllConversationsReq)returns(GetAllConversationsResp);
rpc GetConversations(GetConversationsReq)returns(GetConversationsResp);
rpc BatchSetConversations(BatchSetConversationsReq)returns(BatchSetConversationsResp);
rpc SetConversation(SetConversationReq)returns(SetConversationResp);
rpc SetRecvMsgOpt(SetRecvMsgOptReq)returns(SetRecvMsgOptResp);
//userID
rpc GetUsers(GetUsersReq) returns (GetUsersResp);
rpc AddUser(AddUserReq) returns (AddUserResp);
rpc BlockUser(BlockUserReq) returns (BlockUserResp);
rpc UnBlockUser(UnBlockUserReq) returns (UnBlockUserResp);
rpc GetBlockUsers(GetBlockUsersReq) returns (GetBlockUsersResp);
}

Loading…
Cancel
Save