Skip to content

Commit

Permalink
feat: del the AdminList configure and it's init statement (#401)
Browse files Browse the repository at this point in the history
* fix: del the adminList and adminList init statement

* fix: find the error

* fix: fix the AdminList judge condition
  • Loading branch information
luhaoling authored Jan 29, 2024
1 parent 8a0bf54 commit bd3591d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
19 changes: 10 additions & 9 deletions deployments/templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,17 @@ verifyCode:
# proxyHeader: "X-Forwarded-For" # PROXY_HEADER, Header used for extracting the client IP address

# List of admin users
# Attention! This configure is discarded. If you have used him before, configure your own
adminList:
- adminID: admin1
nickname: chat1
imAdmin: openIM123456
- adminID: admin2
nickname: chat2
imAdmin: openIM654321
- adminID: admin3
nickname: chat3
imAdmin: openIMAdmin
- adminID:
nickname:
imAdmin:
- adminID:
nickname:
imAdmin:
- adminID:
nickname:
imAdmin:

# chatAdmin, use for send notification
chatAdmin:
Expand Down
5 changes: 4 additions & 1 deletion pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ func configFieldCopy[T any](local **T, remote T) {
}

func GetDefaultIMAdmin() string {
return Config.AdminList[0].ImAdminID
if Config.AdminList[0].AdminID != "" {
return Config.AdminList[0].ImAdminID
}
return Config.ChatAdmin[0].ImAdminID
}

func GetIMAdmin(chatAdminID string) string {
Expand Down
17 changes: 6 additions & 11 deletions pkg/common/db/model/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,28 @@ func (o *Admin) InitAdmin(ctx context.Context) error {
log.ZInfo(ctx, "Admins are already registered in database", "admin count", count)
return nil
}
if len(config.Config.AdminList) == 0 {
log.ZInfo(ctx, "AdminList is empty", "adminList", config.Config.AdminList)
if len(config.Config.ChatAdmin) == 0 {
log.ZInfo(ctx, "ChatAdmin is empty", "ChatAdmin", config.Config.ChatAdmin)
return nil
}

admins := make([]*admin.Admin, 0, len(config.Config.AdminList)+len(config.Config.ChatAdmin))
o.createAdmins(&admins, config.Config.AdminList, true)
o.createAdmins(&admins, config.Config.ChatAdmin, false)
admins := make([]*admin.Admin, 0, len(config.Config.ChatAdmin))
o.createAdmins(&admins, config.Config.ChatAdmin)

if err := o.db.WithContext(ctx).Create(&admins).Error; err != nil {
return errs.Wrap(err)
}
return nil
}

func (o *Admin) createAdmins(adminList *[]*admin.Admin, registerList []config.Admin, flag bool) {
func (o *Admin) createAdmins(adminList *[]*admin.Admin, registerList []config.Admin) {
// chatAdmin set the level to 50, this account use for send notification.
level := int32(50)
if flag {
level = 100
}
for _, adminChat := range registerList {
table := admin.Admin{
Account: adminChat.AdminID,
UserID: adminChat.ImAdminID,
Password: o.passwordEncryption(adminChat.AdminID),
Level: level,
Level: 100,
CreateTime: time.Now(),
}
if adminChat.NickName != "" {
Expand Down
5 changes: 4 additions & 1 deletion pkg/common/mctx/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ func WithOpUserID(ctx context.Context, opUserID string, userType int) context.Co
}

func WithAdminUser(ctx context.Context) context.Context {
if len(config.Config.AdminList) > 0 {
if config.Config.AdminList[0].AdminID != "" {
ctx = WithOpUserID(ctx, config.Config.AdminList[0].AdminID, constant.AdminUser)
}
if config.Config.AdminList[0].AdminID != "" && len(config.Config.ChatAdmin) > 0 {
ctx = WithOpUserID(ctx, config.Config.ChatAdmin[0].AdminID, constant.AdminUser)
}
return ctx
}

Expand Down

0 comments on commit bd3591d

Please sign in to comment.