Error code standardization

test-errcode
skiffer-git 2 years ago
parent e241c65a9c
commit b162909147

@ -9,6 +9,7 @@ import (
"Open_IM/pkg/common/log" "Open_IM/pkg/common/log"
promePkg "Open_IM/pkg/common/prometheus" promePkg "Open_IM/pkg/common/prometheus"
"Open_IM/pkg/common/token_verify" "Open_IM/pkg/common/token_verify"
"Open_IM/pkg/common/tools"
"Open_IM/pkg/getcdv3" "Open_IM/pkg/getcdv3"
pbConversation "Open_IM/pkg/proto/conversation" pbConversation "Open_IM/pkg/proto/conversation"
pbFriend "Open_IM/pkg/proto/friend" pbFriend "Open_IM/pkg/proto/friend"
@ -138,22 +139,6 @@ func syncPeerUserConversation(conversation *pbConversation.Conversation, operati
return nil return nil
} }
func (s *userServer) GetUsersInfo(ctx context.Context, req *pbUser.GetUsersInfoReq) (*pbUser.GetUsersInfoResp, error) {
resp := &pbUser.GetUsersInfoResp{}
users, err := s.Find(ctx, req.UserIDs)
if err != nil {
return nil, err
}
for _, v := range users {
n, err := utils2.NewDBUser(v).Convert()
if err != nil {
return nil, err
}
resp.UsersInfo = append(resp.UsersInfo, n)
}
return resp, nil
}
func (s *userServer) BatchSetConversations(ctx context.Context, req *pbUser.BatchSetConversationsReq) (*pbUser.BatchSetConversationsResp, error) { func (s *userServer) BatchSetConversations(ctx context.Context, req *pbUser.BatchSetConversationsReq) (*pbUser.BatchSetConversationsResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String()) log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
if req.NotificationType == 0 { if req.NotificationType == 0 {
@ -373,34 +358,87 @@ func (s *userServer) GetAllUserID(_ context.Context, req *pbUser.GetAllUserIDReq
} }
} }
func (s *userServer) AccountCheck(_ context.Context, req *pbUser.AccountCheckReq) (*pbUser.AccountCheckResp, error) { func (s *userServer) SyncJoinedGroupMemberFaceURL(userID string, faceURL string, operationID string, opUserID string) {
log.NewInfo(req.OperationID, "AccountCheck args ", req.String()) joinedGroupIDList, err := rocksCache.GetJoinedGroupIDListFromCache(userID)
if !token_verify.IsManagerUserID(req.OpUserID) { if err != nil {
log.NewError(req.OperationID, "IsManagerUserID false ", req.OpUserID) log.NewWarn(operationID, "GetJoinedGroupIDListByUserID failed ", userID, err.Error())
return &pbUser.AccountCheckResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil return
} }
uidList, err := imdb.SelectSomeUserID(req.CheckUserIDList) for _, groupID := range joinedGroupIDList {
log.NewDebug(req.OperationID, "from db uid list is:", uidList) groupMemberInfo := imdb.GroupMember{UserID: userID, GroupID: groupID, FaceURL: faceURL}
if err := imdb.UpdateGroupMemberInfo(groupMemberInfo); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupMemberInfo)
continue
}
//if err := rocksCache.DelAllGroupMembersInfoFromCache(groupID); err != nil {
// log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID)
// continue
//}
if err := rocksCache.DelGroupMemberInfoFromCache(groupID, userID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID, userID)
continue
}
chat.GroupMemberInfoSetNotification(operationID, opUserID, groupID, userID)
}
}
func (s *userServer) SyncJoinedGroupMemberNickname(userID string, newNickname, oldNickname string, operationID string, opUserID string) {
joinedGroupIDList, err := imdb.GetJoinedGroupIDListByUserID(userID)
if err != nil { if err != nil {
log.NewError(req.OperationID, "SelectSomeUserID failed ", err.Error(), req.CheckUserIDList) log.NewWarn(operationID, "GetJoinedGroupIDListByUserID failed ", userID, err.Error())
return &pbUser.AccountCheckResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil return
} else { }
var r []*pbUser.AccountCheckResp_SingleUserStatus for _, v := range joinedGroupIDList {
for _, v := range req.CheckUserIDList { member, err := imdb.GetGroupMemberInfoByGroupIDAndUserID(v, userID)
temp := new(pbUser.AccountCheckResp_SingleUserStatus) if err != nil {
temp.UserID = v log.NewWarn(operationID, "GetGroupMemberInfoByGroupIDAndUserID failed ", err.Error(), v, userID)
if utils.IsContain(v, uidList) { continue
temp.AccountStatus = constant.Registered }
} else { if member.Nickname == oldNickname {
temp.AccountStatus = constant.UnRegistered groupMemberInfo := imdb.GroupMember{UserID: userID, GroupID: v, Nickname: newNickname}
if err := imdb.UpdateGroupMemberInfo(groupMemberInfo); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupMemberInfo)
continue
} }
r = append(r, temp) //if err := rocksCache.DelAllGroupMembersInfoFromCache(v); err != nil {
// log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), v)
// continue
//}
if err := rocksCache.DelGroupMemberInfoFromCache(v, userID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), v)
}
chat.GroupMemberInfoSetNotification(operationID, opUserID, v, userID)
} }
resp := pbUser.AccountCheckResp{CommonResp: &pbUser.CommonResp{ErrCode: 0, ErrMsg: ""}, ResultList: r}
log.NewInfo(req.OperationID, "AccountCheck rpc return ", resp.String())
return &resp, nil
} }
}
func (s *userServer) AddUser(ctx context.Context, req *pbUser.AddUserReq) (*pbUser.AddUserResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbUser.AddUserResp{CommonResp: &pbUser.CommonResp{}}
err := imdb.AddUser(req.UserInfo.UserID, req.UserInfo.PhoneNumber, req.UserInfo.Nickname, req.UserInfo.Email, req.UserInfo.Gender, req.UserInfo.FaceURL, req.UserInfo.BirthStr)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "AddUser", err.Error(), req.String())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
return resp, nil
}
func (s *userServer) GetUsersInfo(ctx context.Context, req *pbUser.GetUsersInfoReq) (*pbUser.GetUsersInfoResp, error) {
resp := &pbUser.GetUsersInfoResp{}
users, err := s.Find(ctx, req.UserIDs)
if err != nil {
return nil, err
}
for _, v := range users {
n, err := utils2.NewDBUser(v).Convert()
if err != nil {
return nil, err
}
resp.UsersInfo = append(resp.UsersInfo, n)
}
return resp, nil
} }
func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbUser.UpdateUserInfoReq) (*pbUser.UpdateUserInfoResp, error) { func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbUser.UpdateUserInfoReq) (*pbUser.UpdateUserInfoResp, error) {
@ -462,106 +500,65 @@ func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbUser.UpdateUserI
} }
return &pbUser.UpdateUserInfoResp{CommonResp: &pbUser.CommonResp{}}, nil return &pbUser.UpdateUserInfoResp{CommonResp: &pbUser.CommonResp{}}, nil
} }
func (s *userServer) SetGlobalRecvMessageOpt(ctx context.Context, req *pbUser.SetGlobalRecvMessageOptReq) (*pbUser.SetGlobalRecvMessageOptResp, error) {
log.NewInfo(req.OperationID, "SetGlobalRecvMessageOpt args ", req.String())
var user imdb.User func (s *userServer) SetGlobalRecvMessageOpt(ctx context.Context, req *pbUser.SetGlobalRecvMessageOptReq) (*pbUser.SetGlobalRecvMessageOptResp, error) {
user.UserID = req.UserID resp := pbUser.SetGlobalRecvMessageOptResp{}
m := make(map[string]interface{}, 1) m := make(map[string]interface{}, 1)
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), req.GlobalRecvMsgOpt, "set GlobalRecvMsgOpt")
m["global_recv_msg_opt"] = req.GlobalRecvMsgOpt m["global_recv_msg_opt"] = req.GlobalRecvMsgOpt
err := db.DB.SetUserGlobalMsgRecvOpt(user.UserID, req.GlobalRecvMsgOpt) err := s.UpdateByMap(ctx, req.UserID, m)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetGlobalRecvMessageOpt failed ", err.Error(), user)
return &pbUser.SetGlobalRecvMessageOptResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
err = imdb.UpdateUserInfoByMap(user, m)
if err != nil { if err != nil {
log.NewError(req.OperationID, "SetGlobalRecvMessageOpt failed ", err.Error(), user) return nil, err
return &pbUser.SetGlobalRecvMessageOptResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
if err := rocksCache.DelUserInfoFromCache(user.UserID); err != nil {
log.NewError(req.OperationID, "DelUserInfoFromCache failed ", err.Error(), req.String())
return &pbUser.SetGlobalRecvMessageOptResp{CommonResp: &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: err.Error()}}, nil
} }
chat.UserInfoUpdatedNotification(req.OperationID, req.UserID, req.UserID) chat.UserInfoUpdatedNotification(tools.OperationID(ctx), req.UserID, req.UserID)
return &pbUser.SetGlobalRecvMessageOptResp{CommonResp: &pbUser.CommonResp{}}, nil return &resp, nil
} }
func (s *userServer) SyncJoinedGroupMemberFaceURL(userID string, faceURL string, operationID string, opUserID string) { func (s *userServer) AccountCheck(ctx context.Context, req *pbUser.AccountCheckReq) (*pbUser.AccountCheckResp, error) {
joinedGroupIDList, err := rocksCache.GetJoinedGroupIDListFromCache(userID) resp := pbUser.AccountCheckResp{}
err := token_verify.CheckManagerUserID(ctx, tools.OpUserID(ctx))
if err != nil { if err != nil {
log.NewWarn(operationID, "GetJoinedGroupIDListByUserID failed ", userID, err.Error()) return nil, err
return
}
for _, groupID := range joinedGroupIDList {
groupMemberInfo := imdb.GroupMember{UserID: userID, GroupID: groupID, FaceURL: faceURL}
if err := imdb.UpdateGroupMemberInfo(groupMemberInfo); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupMemberInfo)
continue
}
//if err := rocksCache.DelAllGroupMembersInfoFromCache(groupID); err != nil {
// log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID)
// continue
//}
if err := rocksCache.DelGroupMemberInfoFromCache(groupID, userID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID, userID)
continue
}
chat.GroupMemberInfoSetNotification(operationID, opUserID, groupID, userID)
} }
} user, err := s.Find(ctx, req.CheckUserIDs)
func (s *userServer) SyncJoinedGroupMemberNickname(userID string, newNickname, oldNickname string, operationID string, opUserID string) {
joinedGroupIDList, err := imdb.GetJoinedGroupIDListByUserID(userID)
if err != nil { if err != nil {
log.NewWarn(operationID, "GetJoinedGroupIDListByUserID failed ", userID, err.Error()) return nil, err
return
} }
for _, v := range joinedGroupIDList { uidList := make([]string, 0)
member, err := imdb.GetGroupMemberInfoByGroupIDAndUserID(v, userID) for _, v := range user {
if err != nil { uidList = append(uidList, v.UserID)
log.NewWarn(operationID, "GetGroupMemberInfoByGroupIDAndUserID failed ", err.Error(), v, userID) }
continue var r []*pbUser.AccountCheckResp_SingleUserStatus
} for _, v := range req.CheckUserIDs {
if member.Nickname == oldNickname { temp := new(pbUser.AccountCheckResp_SingleUserStatus)
groupMemberInfo := imdb.GroupMember{UserID: userID, GroupID: v, Nickname: newNickname} temp.UserID = v
if err := imdb.UpdateGroupMemberInfo(groupMemberInfo); err != nil { if utils.IsContain(v, uidList) {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupMemberInfo) temp.AccountStatus = constant.Registered
continue } else {
} temp.AccountStatus = constant.UnRegistered
//if err := rocksCache.DelAllGroupMembersInfoFromCache(v); err != nil {
// log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), v)
// continue
//}
if err := rocksCache.DelGroupMemberInfoFromCache(v, userID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), v)
}
chat.GroupMemberInfoSetNotification(operationID, opUserID, v, userID)
} }
r = append(r, temp)
} }
return &resp, nil
} }
func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pbUser.GetUsersResp, error) { func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pbUser.GetUsersResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String()) resp := pbUser.GetUsersResp{}
var usersDB []imdb.User
var err error var err error
resp := &pbUser.GetUsersResp{CommonResp: &pbUser.CommonResp{}, Pagination: &sdkws.ResponsePagination{CurrentPage: req.Pagination.PageNumber, ShowNumber: req.Pagination.ShowNumber}}
if req.UserID != "" { if req.UserID != "" {
userDB, err := imdb.GetUserByUserID(req.UserID) u, err := s.Take(ctx, req.UserID)
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { return nil, err
return resp, nil
}
log.NewError(req.OperationID, utils.GetSelfFuncName(), req.UserID, err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
} }
usersDB = append(usersDB, *userDB) resp.Total = 1
resp.TotalNums = 1 u1, err := utils2.NewDBUser(u).Convert()
} else if req.UserName != "" { if err != nil {
return nil, err
}
resp.Users = append(resp.Users, u1)
return &resp, nil
}
if req.UserName != "" {
usersDB, err = imdb.GetUserByName(req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber) usersDB, err = imdb.GetUserByName(req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber)
if err != nil { if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber, err.Error()) log.NewError(req.OperationID, utils.GetSelfFuncName(), req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber, err.Error())
@ -587,7 +584,9 @@ func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pb
return resp, nil return resp, nil
} }
resp.TotalNums = int32(count) resp.TotalNums = int32(count)
} else { }
else {
usersDB, err = imdb.GetUsers(req.Pagination.ShowNumber, req.Pagination.PageNumber) usersDB, err = imdb.GetUsers(req.Pagination.ShowNumber, req.Pagination.PageNumber)
if err != nil { if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUsers failed", req.Pagination.ShowNumber, req.Pagination.PageNumber, err.Error()) log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUsers failed", req.Pagination.ShowNumber, req.Pagination.PageNumber, err.Error())
@ -631,16 +630,3 @@ func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pb
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String()) log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil return resp, nil
} }
func (s *userServer) AddUser(ctx context.Context, req *pbUser.AddUserReq) (*pbUser.AddUserResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbUser.AddUserResp{CommonResp: &pbUser.CommonResp{}}
err := imdb.AddUser(req.UserInfo.UserID, req.UserInfo.PhoneNumber, req.UserInfo.Nickname, req.UserInfo.Email, req.UserInfo.Gender, req.UserInfo.FaceURL, req.UserInfo.BirthStr)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "AddUser", err.Error(), req.String())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = err.Error()
return resp, nil
}
return resp, nil
}

@ -11,6 +11,8 @@ type UserInterface interface {
Create(ctx context.Context, users []*relation.User) error Create(ctx context.Context, users []*relation.User) error
Take(ctx context.Context, userID string) (user *relation.User, err error) Take(ctx context.Context, userID string) (user *relation.User, err error)
Update(ctx context.Context, users []*relation.User) (err error) Update(ctx context.Context, users []*relation.User) (err error)
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation.User, err error)
} }
type UserController struct { type UserController struct {
@ -29,6 +31,12 @@ func (u *UserController) Take(ctx context.Context, userID string) (user *relatio
func (u *UserController) Update(ctx context.Context, users []*relation.User) (err error) { func (u *UserController) Update(ctx context.Context, users []*relation.User) (err error) {
return u.database.Update(ctx, users) return u.database.Update(ctx, users)
} }
func (u *UserController) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
return u.database.UpdateByMap(ctx, userID, args)
}
func (u *UserController) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation.User, err error) {
return u.database.GetByName(ctx, userName, showNumber, pageNumber)
}
func NewUserController(db *gorm.DB) UserInterface { func NewUserController(db *gorm.DB) UserInterface {
controller := &UserController{database: newUserDatabase(db)} controller := &UserController{database: newUserDatabase(db)}
@ -40,6 +48,8 @@ type UserDatabaseInterface interface {
Create(ctx context.Context, users []*relation.User) error Create(ctx context.Context, users []*relation.User) error
Take(ctx context.Context, userID string) (user *relation.User, err error) Take(ctx context.Context, userID string) (user *relation.User, err error)
Update(ctx context.Context, users []*relation.User) (err error) Update(ctx context.Context, users []*relation.User) (err error)
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation.User, err error)
} }
type UserDatabase struct { type UserDatabase struct {
@ -67,3 +77,9 @@ func (u *UserDatabase) Take(ctx context.Context, userID string) (user *relation.
func (u *UserDatabase) Update(ctx context.Context, users []*relation.User) (err error) { func (u *UserDatabase) Update(ctx context.Context, users []*relation.User) (err error) {
return u.sqlDB.Update(ctx, users) return u.sqlDB.Update(ctx, users)
} }
func (u *UserDatabase) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
return u.sqlDB.UpdateByMap(ctx, userID, args)
}
func (u *UserDatabase) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation.User, err error) {
return u.sqlDB.GetByName(ctx, userName, showNumber, pageNumber)
}

@ -4,6 +4,7 @@ import (
"Open_IM/pkg/common/trace_log" "Open_IM/pkg/common/trace_log"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"context" "context"
"fmt"
"gorm.io/gorm" "gorm.io/gorm"
"time" "time"
) )
@ -69,3 +70,11 @@ func (u *User) Take(ctx context.Context, userID string) (user *User, err error)
err = utils.Wrap(u.DB.Where("user_id = ?", userID).Take(&user).Error, "") err = utils.Wrap(u.DB.Where("user_id = ?", userID).Take(&user).Error, "")
return user, err return user, err
} }
func (u *User) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*User, err error) {
defer func() {
trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userName", userName, "showNumber", showNumber, "pageNumber", pageNumber, "users", users)
}()
err = u.DB.Where(" name like ?", fmt.Sprintf("%%%s%%", userName)).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
return users, utils.Wrap(err, "")
}

Loading…
Cancel
Save