organization

pull/455/head
wangchuxiao 3 years ago committed by Xinwei Xiong(cubxxw-openim)
parent 143c0e022c
commit a5455feff4

@ -627,6 +627,19 @@ notification:
openTips: "burn after reading was opened" openTips: "burn after reading was opened"
closeTips: "burn after reading was closed" closeTips: "burn after reading was closed"
###################organization################
joinDepartmentNotification:
conversation:
reliabilityLevel: 3
unreadCount: true
offlinePush:
switch: false
title: "welcome user join department"
desc: "welcome user join department"
ext: "welcome user join department"
defaultTips:
tips: "welcome user join department"
#---------------demo configuration---------------------# #---------------demo configuration---------------------#
#The following configuration items are applied to openIM Demo configuration #The following configuration items are applied to openIM Demo configuration
@ -649,7 +662,8 @@ demo:
senderAuthorizationCode: "gxyausfoevlzbfag" senderAuthorizationCode: "gxyausfoevlzbfag"
smtpAddr: "smtp.qq.com" smtpAddr: "smtp.qq.com"
smtpPort: 25 #需开放此端口 出口方向 smtpPort: 25 #需开放此端口 出口方向
testDepartMentID: 001
rtc: rtc:
port: 11300 port: 11300
address: 127.0.0.1 address: 127.0.0.1

@ -0,0 +1,193 @@
package register
import (
"Open_IM/internal/rpc/msg"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
groupRpc "Open_IM/pkg/proto/group"
organizationRpc "Open_IM/pkg/proto/organization"
commonPb "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
"context"
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"math/rand"
"strings"
"time"
)
func onboardingProcess(operationID, userID, userName string) {
if err := createOrganizationUser(operationID, userID, userName); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "createOrganizationUser failed", err.Error())
}
departmentID := config.Config.Demo.TestDepartMentID
if err := joinTestDepartment(operationID, userID, departmentID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "joinTestDepartment failed", err.Error())
}
groupIDList, err := GetDepartmentGroupIDList(operationID, departmentID)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
}
joinGroups(operationID, userID, userName, groupIDList)
log.NewInfo(operationID, utils.GetSelfFuncName(), "fineshed")
}
func createOrganizationUser(operationID, userID, userName string) error {
defer func() {
log.NewInfo(operationID, utils.GetSelfFuncName(), userID)
}()
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOrganizationName)
client := organizationRpc.NewOrganizationClient(etcdConn)
req := &organizationRpc.CreateOrganizationUserReq{
OrganizationUser: &commonPb.OrganizationUser{
UserID: userID,
Nickname: userName,
EnglishName: randomEnglishName(),
Gender: constant.Male,
CreateTime: uint32(time.Now().Unix()),
},
OperationID: operationID,
OpUserID: userID,
}
if strings.Contains("@", userID) {
req.OrganizationUser.Email = userID
} else {
req.OrganizationUser.Telephone = userID
}
resp, err := client.CreateOrganizationUser(context.Background(), req)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
return err
}
if resp.ErrCode != 0 {
log.NewError(req.OperationID, utils.GetSelfFuncName(), resp)
return errors.New(resp.ErrMsg)
}
return nil
}
func joinTestDepartment(operationID, userID, departmentID string) error {
defer func() {
log.NewInfo(operationID, utils.GetSelfFuncName(), userID)
}()
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOrganizationName)
client := organizationRpc.NewOrganizationClient(etcdConn)
req := &organizationRpc.CreateDepartmentMemberReq{DepartmentMember: &commonPb.DepartmentMember{
UserID: userID,
DepartmentID: departmentID,
Position: randomPosition(),
}}
resp, err := client.CreateDepartmentMember(context.Background(), req)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
return err
}
if resp.ErrCode != 0 {
log.NewError(req.OperationID, utils.GetSelfFuncName(), resp)
return errors.New(resp.ErrMsg)
}
return nil
}
func GetDepartmentGroupIDList(operationID, departmentID string) ([]string, error) {
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOrganizationName)
client := organizationRpc.NewOrganizationClient(etcdConn)
req := organizationRpc.GetDepartmentParentIDListReq{
DepartmentID: departmentID,
OperationID: operationID,
}
resp, err := client.GetDepartmentParentIDList(context.Background(), &req)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), req.String())
return nil, err
}
if resp.ErrCode != 0 {
log.NewError(req.OperationID, utils.GetSelfFuncName(), resp)
return nil, errors.New(resp.ErrMsg)
}
resp.ParentIDList = append(resp.ParentIDList, departmentID)
getDepartmentRelatedGroupIDListReq := organizationRpc.GetDepartmentRelatedGroupIDListReq{OperationID: operationID, DepartmentIDList: resp.ParentIDList}
getDepartmentParentIDListResp, err := client.GetDepartmentRelatedGroupIDList(context.Background(), &getDepartmentRelatedGroupIDListReq)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), getDepartmentRelatedGroupIDListReq.String())
return nil, err
}
if getDepartmentParentIDListResp.ErrCode != 0 {
log.NewError(req.OperationID, utils.GetSelfFuncName(), getDepartmentParentIDListResp)
return nil, errors.New(getDepartmentParentIDListResp.ErrMsg)
}
return getDepartmentParentIDListResp.GroupIDList, nil
}
func joinGroups(operationID, userID, userName string, groupIDList []string) {
defer func() {
log.NewInfo(operationID, utils.GetSelfFuncName(), userID, groupIDList)
}()
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName)
client := groupRpc.NewGroupClient(etcdConn)
for _, groupID := range groupIDList {
req := &groupRpc.InviteUserToGroupReq{
OperationID: operationID,
GroupID: groupID,
Reason: "register auto join",
InvitedUserIDList: []string{userID},
OpUserID: userID,
}
resp, err := client.InviteUserToGroup(context.Background(), req)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), req.String())
continue
}
if resp.ErrCode != 0 {
log.NewError(req.OperationID, utils.GetSelfFuncName(), resp)
continue
}
onboardingProcessNotification(operationID, userID, groupID)
}
}
// welcome user join department notification
func onboardingProcessNotification(operationID, userID, groupID string) {
defer func() {
log.NewInfo(operationID, utils.GetSelfFuncName(), userID, groupID)
}()
var tips commonPb.TipsComm
tips.DefaultTips = config.Config.Notification.JoinDepartmentNotification.DefaultTips.Tips
content, err := proto.Marshal(&tips)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), "proto marshal failed")
return
}
notification := &msg.NotificationMsg{
SendID: userID,
RecvID: groupID,
Content: content,
MsgFrom: constant.UserMsgType,
ContentType: constant.Text,
SessionType: constant.SingleChatType,
OperationID: operationID,
}
// notification user join group
msg.Notification(notification)
}
func randomEnglishName() string {
l := []string{"abandon", "entail", "nebula", "shrink", "accumulate", "etch", "nostalgia", "slide",
"feudal", "adverse", "exploit", "occupy", "solve", "amazing", "fantasy", "orchid", "spiky", "approve", "flap"}
rand.Seed(time.Now().UnixNano())
index := rand.Intn(len(l) - 1)
fmt.Println(index)
return l[index]
}
func randomPosition() string {
l := []string{"Golang工程师", "前端工程师", "后端工程师", "产品经理", "测试开发工程师", "运维开发工程师"}
rand.Seed(time.Now().UnixNano())
index := rand.Intn(len(l) - 1)
fmt.Println(index)
return l[index]
}

@ -84,6 +84,8 @@ func SetPassword(c *gin.Context) {
return return
} }
log.Info(params.OperationID, "end setPassword", account, params.Password) log.Info(params.OperationID, "end setPassword", account, params.Password)
// demo onboarding
onboardingProcess(params.OperationID, account, params.Name)
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMRegisterResp.UserToken}) c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMRegisterResp.UserToken})
return return
} }

@ -10,6 +10,7 @@ import (
"Open_IM/pkg/common/token_verify" "Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3" "Open_IM/pkg/grpc-etcdv3/getcdv3"
"Open_IM/pkg/proto/auth" "Open_IM/pkg/proto/auth"
groupRpc "Open_IM/pkg/proto/group"
rpc "Open_IM/pkg/proto/organization" rpc "Open_IM/pkg/proto/organization"
open_im_sdk "Open_IM/pkg/proto/sdk_ws" open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
@ -99,6 +100,34 @@ func (s *organizationServer) CreateDepartment(ctx context.Context, req *rpc.Crea
utils.CopyStructFields(resp.DepartmentInfo, createdDepartment) utils.CopyStructFields(resp.DepartmentInfo, createdDepartment)
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp) log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID) chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName)
client := groupRpc.NewGroupClient(etcdConn)
createGroupReq := &groupRpc.CreateGroupReq{
InitMemberList: nil,
GroupInfo: &open_im_sdk.GroupInfo{
GroupName: req.DepartmentInfo.Name,
FaceURL: req.DepartmentInfo.FaceURL,
CreateTime: uint32(time.Now().Unix()),
CreatorUserID: req.OpUserID,
GroupType: constant.DepartmentGroup,
},
OperationID: req.OperationID,
OpUserID: req.OpUserID,
}
createGroupResp, err := client.CreateGroup(context.Background(), createGroupReq)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "CreateGroup rpc failed", createGroupReq, err.Error())
resp.ErrCode = constant.ErrDB.ErrCode
resp.ErrMsg = constant.ErrDB.ErrMsg + " createGroup failed " + err.Error()
return resp, nil
}
if createGroupResp.ErrCode != 0 {
log.NewError(req.OperationID, utils.GetSelfFuncName(), resp)
resp.ErrCode = constant.ErrDB.ErrCode
resp.ErrMsg = constant.ErrDB.ErrMsg + " createGroup failed " + createGroupResp.ErrMsg
return resp, nil
}
return resp, nil return resp, nil
} }
@ -296,6 +325,19 @@ func (s *organizationServer) CreateDepartmentMember(ctx context.Context, req *rp
return resp, nil return resp, nil
} }
func (s *organizationServer) GetDepartmentParentIDListReq(_ context.Context, req *rpc.GetDepartmentParentIDListReq) (resp *rpc.GetDepartmentParentIDListResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req.String())
resp = &rpc.GetDepartmentParentIDListResp{}
resp.ParentIDList, err = imdb.GetDepartmentParentIDList(req.DepartmentID)
if err != nil {
resp.ErrMsg = constant.ErrDB.ErrMsg + ": " + err.Error()
resp.ErrCode = constant.ErrDB.ErrCode
return resp, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp:", resp.String())
return resp, nil
}
func (s *organizationServer) GetUserInDepartmentByUserID(userID string, operationID string) (*open_im_sdk.UserInDepartment, error) { func (s *organizationServer) GetUserInDepartmentByUserID(userID string, operationID string) (*open_im_sdk.UserInDepartment, error) {
err, organizationUser := imdb.GetOrganizationUser(userID) err, organizationUser := imdb.GetOrganizationUser(userID)
if err != nil { if err != nil {
@ -427,3 +469,18 @@ func (s *organizationServer) GetDepartmentMember(ctx context.Context, req *rpc.G
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp) log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp)
return &resp, nil return &resp, nil
} }
func (s *organizationServer) GetDepartmentRelatedGroupIDList(ctx context.Context, req rpc.GetDepartmentRelatedGroupIDListReq) (resp *rpc.GetDepartmentRelatedGroupIDListResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &rpc.GetDepartmentRelatedGroupIDListResp{}
groupIDList, err := imdb.GetDepartmentRelatedGroupIDList(req.DepartmentIDList)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
resp.ErrMsg = constant.ErrDB.ErrMsg + " GetDepartMentRelatedGroupIDList failed " + err.Error()
resp.ErrCode = constant.ErrDB.ErrCode
return resp, nil
}
resp.GroupIDList = groupIDList
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}

@ -388,6 +388,11 @@ type config struct {
OfflinePush POfflinePush `yaml:"offlinePush"` OfflinePush POfflinePush `yaml:"offlinePush"`
DefaultTips PDefaultTips `yaml:"defaultTips"` DefaultTips PDefaultTips `yaml:"defaultTips"`
} `yaml:"workMomentsNotification"` } `yaml:"workMomentsNotification"`
JoinDepartmentNotification struct {
Conversation PConversation `yaml:"conversation"`
OfflinePush POfflinePush `yaml:"offlinePush"`
DefaultTips PDefaultTips `yaml:"defaultTips"`
} `yaml:"joinDepartmentNotification"`
} }
Demo struct { Demo struct {
Port []int `yaml:"openImDemoPort"` Port []int `yaml:"openImDemoPort"`
@ -406,6 +411,7 @@ type config struct {
SmtpAddr string `yaml:"smtpAddr"` SmtpAddr string `yaml:"smtpAddr"`
SmtpPort int `yaml:"smtpPort"` SmtpPort int `yaml:"smtpPort"`
} }
TestDepartMentID string `yaml:"testDepartMentID"`
} }
Rtc struct { Rtc struct {
Port int `yaml:"port"` Port int `yaml:"port"`

@ -148,6 +148,10 @@ const (
GroupStatusDismissed = 2 GroupStatusDismissed = 2
GroupStatusMuted = 3 GroupStatusMuted = 3
//GroupType
NormalGroup = 0
DepartmentGroup = 1
GroupBaned = 3 GroupBaned = 3
GroupBanPrivateChat = 4 GroupBanPrivateChat = 4

@ -232,6 +232,7 @@ type Department struct {
ParentID string `gorm:"column:parent_id;size:64" json:"parentID" binding:"required"` // "0" or Real parent id ParentID string `gorm:"column:parent_id;size:64" json:"parentID" binding:"required"` // "0" or Real parent id
Order int32 `gorm:"column:order" json:"order" ` // 1, 2, ... Order int32 `gorm:"column:order" json:"order" ` // 1, 2, ...
DepartmentType int32 `gorm:"column:department_type" json:"departmentType"` //1, 2... DepartmentType int32 `gorm:"column:department_type" json:"departmentType"` //1, 2...
RelatedGroupID string `gorm:"column:related_group_id;size:64" json:"relatedGroupID"`
CreateTime time.Time `gorm:"column:create_time" json:"createTime"` CreateTime time.Time `gorm:"column:create_time" json:"createTime"`
Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"` Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"`
} }

@ -59,7 +59,7 @@ func initMysqlDB() {
&GroupMember{}, &GroupMember{},
&GroupRequest{}, &GroupRequest{},
&User{}, &User{},
&Black{}, &ChatLog{}, &Register{}, &Conversation{}, &AppVersion{}) &Black{}, &ChatLog{}, &Register{}, &Conversation{}, &AppVersion{}, &Department{})
db.Set("gorm:table_options", "CHARSET=utf8") db.Set("gorm:table_options", "CHARSET=utf8")
db.Set("gorm:table_options", "collation=utf8_unicode_ci") db.Set("gorm:table_options", "collation=utf8_unicode_ci")

@ -3,6 +3,7 @@ package im_mysql_model
import ( import (
"Open_IM/pkg/common/db" "Open_IM/pkg/common/db"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"github.com/jinzhu/gorm"
"time" "time"
) )
@ -234,5 +235,48 @@ func GetSubDepartmentNum(departmentID string) (error, uint32) {
return utils.Wrap(err, ""), 0 return utils.Wrap(err, ""), 0
} }
return nil, number return nil, number
}
func GetDepartmentRelatedGroupIDList(departmentIDList []string) ([]string, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, utils.Wrap(err, "DefaultGormDB failed")
}
var groupIDList []string
err = dbConn.Table("departments").Where("department_id IN (?) ", departmentIDList).Pluck("related_group_id", &groupIDList).Error
return groupIDList, err
}
func getDepartmentParent(departmentID string, dbConn *gorm.DB) (*db.Department, error) {
var department db.Department
var parentID string
dbConn.LogMode(true)
// select * from departments where department_id = (select parent_id from departments where department_id= zx234fd);
err := dbConn.Table("departments").Where("department_id=?", dbConn.Table("departments").Where("department_id=?", departmentID).Pluck("parent_id", parentID)).Error
return &department, err
}
func GetDepartmentParent(departmentID string, dbConn *gorm.DB, parentIDList []string) (*db.Department, error) {
department, err := getDepartmentParent(departmentID, dbConn)
if err != nil {
return nil, err
}
if department.ParentID != "" {
parentIDList = append(parentIDList, department.ParentID)
_, err = GetDepartmentParent(departmentID, dbConn, parentIDList)
if err != nil {
return nil, nil
}
}
return nil, nil
}
func GetDepartmentParentIDList(departmentID string) ([]string, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var parentIDList []string
_, err = GetDepartmentParent(departmentID, dbConn, parentIDList)
return parentIDList, err
} }

File diff suppressed because it is too large Load Diff

@ -51,6 +51,17 @@ message DeleteDepartmentResp{
string errMsg = 2; string errMsg = 2;
} }
message GetDepartmentParentIDListReq {
string departmentID = 1;
string operationID = 2;
}
message GetDepartmentParentIDListResp {
int32 errCode = 1;
string errMsg = 2;
repeated string parentIDList = 3;
}
message CreateOrganizationUserReq{ message CreateOrganizationUserReq{
server_api_params.OrganizationUser organizationUser = 1; server_api_params.OrganizationUser organizationUser = 1;
@ -148,12 +159,23 @@ message GetDepartmentMemberResp{
repeated server_api_params.UserDepartmentMember userDepartmentMemberList = 3; repeated server_api_params.UserDepartmentMember userDepartmentMemberList = 3;
} }
message GetDepartmentRelatedGroupIDListReq {
string operationID = 1;
repeated string departmentIDList = 2;
}
message GetDepartmentRelatedGroupIDListResp {
int32 errCode = 1;
string errMsg = 2;
repeated string groupIDList = 3;
}
service organization{ service organization{
rpc CreateDepartment(CreateDepartmentReq) returns(CreateDepartmentResp); rpc CreateDepartment(CreateDepartmentReq) returns(CreateDepartmentResp);
rpc UpdateDepartment(UpdateDepartmentReq) returns(UpdateDepartmentResp); rpc UpdateDepartment(UpdateDepartmentReq) returns(UpdateDepartmentResp);
rpc GetSubDepartment(GetSubDepartmentReq) returns(GetSubDepartmentResp); rpc GetSubDepartment(GetSubDepartmentReq) returns(GetSubDepartmentResp);
rpc DeleteDepartment(DeleteDepartmentReq) returns(DeleteDepartmentResp); rpc DeleteDepartment(DeleteDepartmentReq) returns(DeleteDepartmentResp);
rpc GetDepartmentParentIDList(GetDepartmentParentIDListReq) returns(GetDepartmentParentIDListResp);
rpc CreateOrganizationUser(CreateOrganizationUserReq) returns(CreateOrganizationUserResp); rpc CreateOrganizationUser(CreateOrganizationUserReq) returns(CreateOrganizationUserResp);
rpc UpdateOrganizationUser(UpdateOrganizationUserReq) returns(UpdateOrganizationUserResp); rpc UpdateOrganizationUser(UpdateOrganizationUserReq) returns(UpdateOrganizationUserResp);
@ -165,6 +187,7 @@ service organization{
rpc DeleteUserInDepartment(DeleteUserInDepartmentReq) returns(DeleteUserInDepartmentResp); rpc DeleteUserInDepartment(DeleteUserInDepartmentReq) returns(DeleteUserInDepartmentResp);
rpc UpdateUserInDepartment(UpdateUserInDepartmentReq) returns(UpdateUserInDepartmentResp); rpc UpdateUserInDepartment(UpdateUserInDepartmentReq) returns(UpdateUserInDepartmentResp);
rpc GetDepartmentMember(GetDepartmentMemberReq) returns(GetDepartmentMemberResp); rpc GetDepartmentMember(GetDepartmentMemberReq) returns(GetDepartmentMemberResp);
rpc GetDepartmentRelatedGroupIDList(GetDepartmentRelatedGroupIDListReq) returns(GetDepartmentRelatedGroupIDListResp);
} }

Loading…
Cancel
Save