Skip to content

Commit

Permalink
feat: chat admin config (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
withchao committed Apr 24, 2024
1 parent 7f78020 commit bf781af
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 deletions.
4 changes: 1 addition & 3 deletions config/share.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ openIM:
adminUserID: imAdmin

chatAdmin:
- adminID: chatAdmin
imUserID: imAdmin
nickname: chatAdmin
- "chatAdmin"

#proxyHeader: "X-Forwarded-For"
2 changes: 1 addition & 1 deletion internal/api/admin/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Start(ctx context.Context, index int, config *Config) error {
base := util.Api{
ImUserID: config.Share.OpenIM.AdminUserID,
ProxyHeader: config.Share.ProxyHeader,
ChatAdminUserID: config.Share.ChatAdmin[0].AdminID,
ChatAdminUserID: config.Share.ChatAdmin[0],
}
adminApi := New(chatClient, adminClient, im, &base)
mwApi := chatmw.New(adminClient)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/chat/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Start(ctx context.Context, index int, config *Config) error {
base := util.Api{
ImUserID: config.Share.OpenIM.AdminUserID,
ProxyHeader: config.Share.ProxyHeader,
ChatAdminUserID: config.Share.ChatAdmin[0].AdminID,
ChatAdminUserID: config.Share.ChatAdmin[0],
}
adminApi := New(chatClient, adminClient, im, &base)
mwApi := chatmw.New(adminClient)
Expand Down
14 changes: 7 additions & 7 deletions internal/rpc/admin/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
Expires: time.Duration(config.RpcConfig.TokenPolicy.Expire) * time.Hour * 24,
Secret: config.RpcConfig.Secret,
}
if err := srv.initAdmin(ctx, config.Share.ChatAdmin); err != nil {
if err := srv.initAdmin(ctx, config.Share.ChatAdmin, config.Share.OpenIM.AdminUserID); err != nil {
return err
}
pbadmin.RegisterAdminServer(server, &srv)
Expand All @@ -72,17 +72,17 @@ type adminServer struct {
Token *tokenverify.Token
}

func (o *adminServer) initAdmin(ctx context.Context, users []config.AdminUser) error {
for _, user := range users {
if _, err := o.Database.GetAdmin(ctx, user.AdminID); err == nil {
func (o *adminServer) initAdmin(ctx context.Context, admins []string, imUserID string) error {
for _, account := range admins {
if _, err := o.Database.GetAdmin(ctx, account); err == nil {
continue
} else if !dbutil.IsDBNotFound(err) {
return err
}
sum := md5.Sum([]byte(user.AdminID))
sum := md5.Sum([]byte(account))
a := admin.Admin{
Account: user.AdminID,
UserID: user.IMUserID,
Account: account,
UserID: imUserID,
Password: hex.EncodeToString(sum[:]),
Level: constant.DefaultAdminLevel,
CreateTime: time.Now(),
Expand Down
10 changes: 2 additions & 8 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ type Share struct {
Secret string `mapstructure:"secret"`
AdminUserID string `mapstructure:"adminUserID"`
} `mapstructure:"openIM"`
ChatAdmin []AdminUser `mapstructure:"chatAdmin"`
ProxyHeader string `mapstructure:"proxyHeader"`
}

type AdminUser struct {
AdminID string `mapstructure:"adminID"`
IMUserID string `mapstructure:"imUserID"`
Nickname string `mapstructure:"nickname"`
ChatAdmin []string `mapstructure:"chatAdmin"`
ProxyHeader string `mapstructure:"proxyHeader"`
}

type RpcRegisterName struct {
Expand Down

0 comments on commit bf781af

Please sign in to comment.