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

334 lines
9.6 KiB

3 years ago
package user
import (
chat "Open_IM/internal/rpc/msg"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/controller"
"Open_IM/pkg/common/db/relation"
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/getcdv3"
3 years ago
pbFriend "Open_IM/pkg/proto/friend"
pbGroup "Open_IM/pkg/proto/group"
3 years ago
pbUser "Open_IM/pkg/proto/user"
"Open_IM/pkg/utils"
"context"
"github.com/golang/protobuf/ptypes/wrappers"
3 years ago
"net"
"strconv"
"strings"
3 years ago
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3 years ago
"google.golang.org/grpc"
3 years ago
)
type userServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
controller.UserInterface
3 years ago
}
3 years ago
func NewUserServer(port int) *userServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
u := userServer{
3 years ago
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
3 years ago
}
3 years ago
func (s *userServer) Run() {
log.NewInfo("", "rpc user 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
}
log.NewInfo("", "listen network success, address ", address, listener)
3 years ago
defer listener.Close()
//grpc server
2 years ago
var grpcOpts []grpc.ServerOption
if config.Config.Prometheus.Enable {
2 years ago
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
pbUser.RegisterUserServer(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())
}
}
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10, "")
3 years ago
if err != nil {
log.NewError("", "RegisterEtcd failed ", err.Error(), s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName)
panic(utils.Wrap(err, "register user module rpc to etcd err"))
3 years ago
}
err = srv.Serve(listener)
if err != nil {
log.NewError("", "Serve failed ", err.Error())
3 years ago
return
}
log.NewInfo("", "rpc user success")
3 years ago
}
func (s *userServer) SyncJoinedGroupMemberFaceURL(ctx context.Context, userID string, faceURL string, operationID string, opUserID string) {
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImFriendName)
3 years ago
if err != nil {
return
3 years ago
}
client := pbGroup.NewGroupClient(etcdConn)
newReq := &pbGroup.GetJoinedGroupListReq{FromUserID: userID}
rpcResp, err := client.GetJoinedGroupList(ctx, newReq)
3 years ago
if err != nil {
return
3 years ago
}
for _, group := range rpcResp.Groups {
req := &pbGroup.SetGroupMemberInfoReq{GroupID: group.GroupID, UserID: userID, FaceURL: &wrappers.StringValue{Value: faceURL}}
_, err := client.SetGroupMemberInfo(ctx, req)
3 years ago
if err != nil {
return
3 years ago
}
chat.GroupMemberInfoSetNotification(operationID, opUserID, group.GroupID, userID)
3 years ago
}
3 years ago
}
3 years ago
func (s *userServer) SyncJoinedGroupMemberNickname(ctx context.Context, userID string, newNickname, oldNickname string, operationID string, opUserID string) {
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImFriendName)
3 years ago
if err != nil {
return
3 years ago
}
client := pbGroup.NewGroupClient(etcdConn)
newReq := &pbGroup.GetJoinedGroupListReq{FromUserID: userID}
rpcResp, err := client.GetJoinedGroupList(ctx, newReq)
if err != nil {
return
3 years ago
}
req := pbGroup.GetUserInGroupMembersReq{UserID: userID}
for _, group := range rpcResp.Groups {
req.GroupIDs = append(req.GroupIDs, group.GroupID)
}
resp, err := client.GetUserInGroupMembers(ctx, &req)
3 years ago
if err != nil {
return
}
for _, v := range resp.Members {
if v.Nickname == oldNickname {
req := pbGroup.SetGroupMemberNicknameReq{Nickname: newNickname, GroupID: v.GroupID, UserID: v.UserID}
_, err := client.SetGroupMemberNickname(ctx, &req)
if err != nil {
return
3 years ago
}
chat.GroupMemberInfoSetNotification(operationID, opUserID, v.GroupID, userID)
3 years ago
}
}
}
3 years ago
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 {
2 years ago
n, err := utils.NewDBUser(v).Convert()
if err != nil {
return nil, err
}
resp.UsersInfo = append(resp.UsersInfo, n)
}
return resp, nil
3 years ago
}
func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbUser.UpdateUserInfoReq) (*pbUser.UpdateUserInfoResp, error) {
resp := pbUser.UpdateUserInfoResp{}
err := token_verify.CheckAccessV3(ctx, req.UserInfo.UserID)
if err != nil {
return nil, err
3 years ago
}
oldNickname := ""
if req.UserInfo.Nickname != "" {
u, err := s.Take(ctx, req.UserInfo.UserID)
if err != nil {
return nil, err
}
oldNickname = u.Nickname
3 years ago
}
2 years ago
user, err := utils.NewPBUser(req.UserInfo).Convert()
2 years ago
if err != nil {
return nil, err
3 years ago
}
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
}
3 years ago
client := pbFriend.NewFriendClient(etcdConn)
newReq := &pbFriend.GetFriendListReq{UserID: req.UserInfo.UserID}
2 years ago
rpcResp, err := client.GetFriendList(context.Background(), newReq)
3 years ago
if err != nil {
return nil, err
2 years ago
}
go func() {
for _, v := range rpcResp.FriendInfoList {
2 years ago
chat.FriendInfoUpdatedNotification(utils.OperationID(ctx), req.UserInfo.UserID, v.FriendUser.UserID, utils.OpUserID(ctx))
}
}()
2 years ago
chat.UserInfoUpdatedNotification(utils.OperationID(ctx), utils.OpUserID(ctx), req.UserInfo.UserID)
if req.UserInfo.FaceURL != "" {
2 years ago
s.SyncJoinedGroupMemberFaceURL(ctx, req.UserInfo.UserID, req.UserInfo.FaceURL, utils.OperationID(ctx), utils.OpUserID(ctx))
}
if req.UserInfo.Nickname != "" {
2 years ago
s.SyncJoinedGroupMemberNickname(ctx, req.UserInfo.UserID, req.UserInfo.Nickname, oldNickname, utils.OperationID(ctx), utils.OpUserID(ctx))
}
return &resp, nil
3 years ago
}
3 years ago
func (s *userServer) SetGlobalRecvMessageOpt(ctx context.Context, req *pbUser.SetGlobalRecvMessageOptReq) (*pbUser.SetGlobalRecvMessageOptResp, error) {
resp := pbUser.SetGlobalRecvMessageOptResp{}
m := make(map[string]interface{}, 1)
m["global_recv_msg_opt"] = req.GlobalRecvMsgOpt
err := s.UpdateByMap(ctx, req.UserID, m)
if err != nil {
return nil, err
}
2 years ago
chat.UserInfoUpdatedNotification(utils.OperationID(ctx), req.UserID, req.UserID)
return &resp, nil
}
func (s *userServer) AccountCheck(ctx context.Context, req *pbUser.AccountCheckReq) (*pbUser.AccountCheckResp, error) {
resp := pbUser.AccountCheckResp{}
2 years ago
err := token_verify.CheckManagerUserID(ctx, utils.OpUserID(ctx))
if err != nil {
return nil, err
}
user, err := s.Find(ctx, req.CheckUserIDs)
if err != nil {
return nil, err
}
uidList := make([]string, 0)
for _, v := range user {
uidList = append(uidList, v.UserID)
}
var r []*pbUser.AccountCheckResp_SingleUserStatus
for _, v := range req.CheckUserIDs {
temp := new(pbUser.AccountCheckResp_SingleUserStatus)
temp.UserID = v
if utils.IsContain(v, uidList) {
temp.AccountStatus = constant.Registered
} else {
temp.AccountStatus = constant.UnRegistered
}
r = append(r, temp)
}
return &resp, nil
}
2 years ago
func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pbUser.GetUsersResp, error) {
resp := pbUser.GetUsersResp{}
2 years ago
var err error
2 years ago
if req.UserID != "" {
u, err := s.Take(ctx, req.UserID)
3 years ago
if err != nil {
return nil, err
3 years ago
}
resp.Total = 1
2 years ago
u1, err := utils.NewDBUser(u).Convert()
if err != nil {
return nil, err
}
resp.Users = append(resp.Users, u1)
return &resp, nil
}
if req.UserName != "" {
usersDB, total, err := s.GetByName(ctx, req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber)
2 years ago
if err != nil {
return nil, err
2 years ago
}
resp.Total = int32(total)
for _, v := range usersDB {
2 years ago
u1, err := utils.NewDBUser(v).Convert()
if err != nil {
return nil, err
}
resp.Users = append(resp.Users, u1)
2 years ago
}
return &resp, nil
2 years ago
} else if req.Content != "" {
usersDB, total, err := s.GetByNameAndID(ctx, req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber)
2 years ago
if err != nil {
return nil, err
}
resp.Total = int32(total)
for _, v := range usersDB {
2 years ago
u1, err := utils.NewDBUser(v).Convert()
if err != nil {
return nil, err
}
resp.Users = append(resp.Users, u1)
2 years ago
}
return &resp, nil
}
usersDB, total, err := s.Get(ctx, req.Pagination.ShowNumber, req.Pagination.PageNumber)
if err != nil {
return nil, err
2 years ago
}
resp.Total = int32(total)
for _, userDB := range usersDB {
2 years ago
u, err := utils.NewDBUser(userDB).Convert()
if err != nil {
return nil, err
3 years ago
}
resp.Users = append(resp.Users, u)
3 years ago
}
return &resp, nil
3 years ago
}