Skip to content

Commit

Permalink
Merge branch 'main' into wf/mile
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et authored Nov 25, 2024
2 parents 050fd05 + a67ab1c commit 49b157f
Show file tree
Hide file tree
Showing 13 changed files with 2,066 additions and 531 deletions.
30 changes: 25 additions & 5 deletions internal/api/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (o *Api) AdminUpdateInfo(c *gin.Context) {
}

imAdminUserID := o.GetDefaultIMAdminUserID()
imToken, err := o.imApiCaller.GetAdminToken(c, imAdminUserID)
imToken, err := o.imApiCaller.GetAdminTokenCache(c, imAdminUserID)
if err != nil {
log.ZError(c, "AdminUpdateInfo ImAdminTokenWithDefaultAdmin", err, "imAdminUserID", imAdminUserID)
return
Expand Down Expand Up @@ -193,7 +193,7 @@ func (o *Api) AddDefaultGroup(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -241,7 +241,7 @@ func (o *Api) SearchDefaultGroup(c *gin.Context) {
Groups: make([]*sdkws.GroupInfo, 0, len(searchResp.GroupIDs)),
}
if len(searchResp.GroupIDs) > 0 {
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -323,7 +323,7 @@ func (o *Api) BlockUser(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -382,7 +382,7 @@ func (o *Api) NewUserCount(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -566,3 +566,23 @@ func (o *Api) SetAllowRegister(c *gin.Context) {
func (o *Api) GetAllowRegister(c *gin.Context) {
a2r.Call(chat.ChatClient.GetAllowRegister, o.chatClient, c)
}

func (o *Api) LatestApplicationVersion(c *gin.Context) {
a2r.Call(admin.AdminClient.LatestApplicationVersion, o.adminClient, c)
}

func (o *Api) PageApplicationVersion(c *gin.Context) {
a2r.Call(admin.AdminClient.PageApplicationVersion, o.adminClient, c)
}

func (o *Api) AddApplicationVersion(c *gin.Context) {
a2r.Call(admin.AdminClient.AddApplicationVersion, o.adminClient, c)
}

func (o *Api) UpdateApplicationVersion(c *gin.Context) {
a2r.Call(admin.AdminClient.UpdateApplicationVersion, o.adminClient, c)
}

func (o *Api) DeleteApplicationVersion(c *gin.Context) {
a2r.Call(admin.AdminClient.DeleteApplicationVersion, o.adminClient, c)
}
7 changes: 7 additions & 0 deletions internal/api/admin/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,11 @@ func SetAdminRoute(router gin.IRouter, admin *Api, mw *chatmw.MW) {
statistic := router.Group("/statistic", mw.CheckAdmin)
statistic.POST("/new_user_count", admin.NewUserCount)
statistic.POST("/login_user_count", admin.LoginUserCount)

applicationGroup := router.Group("application")
applicationGroup.POST("/add_version", mw.CheckAdmin, admin.AddApplicationVersion)
applicationGroup.POST("/update_version", mw.CheckAdmin, admin.UpdateApplicationVersion)
applicationGroup.POST("/delete_version", mw.CheckAdmin, admin.DeleteApplicationVersion)
applicationGroup.POST("/latest_version", admin.LatestApplicationVersion)
applicationGroup.POST("/page_versions", admin.PageApplicationVersion)
}
24 changes: 10 additions & 14 deletions internal/api/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/openimsdk/chat/pkg/common/apistruct"
"github.com/openimsdk/chat/pkg/common/constant"
"github.com/openimsdk/chat/pkg/common/imapi"
"github.com/openimsdk/chat/pkg/common/mctx"
"github.com/openimsdk/chat/pkg/protocol/admin"
Expand Down Expand Up @@ -235,20 +234,9 @@ func (o *Api) UpdateUserInfo(c *gin.Context) {
apiresp.GinError(c, err)
return
}
opUserType, err := mctx.GetUserType(c)
if err != nil {
apiresp.GinError(c, err)
return
}

var imToken string
if opUserType == constant.NormalUser {
imToken, err = o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
} else if opUserType == constant.AdminUser {
imToken, err = o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
} else {
apiresp.GinError(c, errs.ErrArgs.WrapMsg("opUserType unknown"))
return
}
imToken, err = o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -360,3 +348,11 @@ func (o *Api) SearchFriend(c *gin.Context) {
}
apiresp.GinSuccess(c, resp)
}

func (o *Api) LatestApplicationVersion(c *gin.Context) {
a2r.Call(admin.AdminClient.LatestApplicationVersion, o.adminClient, c)
}

func (o *Api) PageApplicationVersion(c *gin.Context) {
a2r.Call(admin.AdminClient.PageApplicationVersion, o.adminClient, c)
}
4 changes: 4 additions & 0 deletions internal/api/chat/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,9 @@ func SetChatRoute(router gin.IRouter, chat *Api, mw *chatmw.MW) {

router.Group("/client_config").POST("/get", chat.GetClientConfig) // Get client initialization configuration

applicationGroup := router.Group("application")
applicationGroup.POST("/latest_version", chat.LatestApplicationVersion)
applicationGroup.POST("/page_versions", chat.PageApplicationVersion)

router.Group("/callback").POST("/open_im", chat.OpenIMCallback) // Callback
}
128 changes: 128 additions & 0 deletions internal/rpc/admin/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package admin

import (
"context"
admindb "github.com/openimsdk/chat/pkg/common/db/table/admin"
"github.com/openimsdk/chat/pkg/common/mctx"
"github.com/openimsdk/chat/pkg/protocol/admin"
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/utils/datautil"
"github.com/redis/go-redis/v9"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"time"
)

func IsNotFound(err error) bool {
switch errs.Unwrap(err) {
case redis.Nil, mongo.ErrNoDocuments:
return true
default:
return false
}
}

func (o *adminServer) db2pbApplication(val *admindb.Application) *admin.ApplicationVersion {
return &admin.ApplicationVersion{
Id: val.ID.Hex(),
Platform: val.Platform,
Version: val.Version,
Url: val.Url,
Text: val.Text,
Force: val.Force,
Latest: val.Latest,
Hot: val.Hot,
CreateTime: val.CreateTime.UnixMilli(),
}
}

func (o *adminServer) LatestApplicationVersion(ctx context.Context, req *admin.LatestApplicationVersionReq) (*admin.LatestApplicationVersionResp, error) {
res, err := o.Database.LatestVersion(ctx, req.Platform)
if err == nil {
return &admin.LatestApplicationVersionResp{Version: o.db2pbApplication(res)}, nil
} else if IsNotFound(err) {
return &admin.LatestApplicationVersionResp{}, nil
} else {
return nil, err
}
}

func (o *adminServer) AddApplicationVersion(ctx context.Context, req *admin.AddApplicationVersionReq) (*admin.AddApplicationVersionResp, error) {
if _, err := mctx.CheckAdmin(ctx); err != nil {
return nil, err
}
val := &admindb.Application{
ID: primitive.NewObjectID(),
Platform: req.Platform,
Version: req.Version,
Url: req.Url,
Text: req.Text,
Force: req.Force,
Latest: req.Latest,
Hot: req.Hot,
CreateTime: time.Now(),
}
if err := o.Database.AddVersion(ctx, val); err != nil {
return nil, err
}
return &admin.AddApplicationVersionResp{}, nil
}

func (o *adminServer) UpdateApplicationVersion(ctx context.Context, req *admin.UpdateApplicationVersionReq) (*admin.UpdateApplicationVersionResp, error) {
if _, err := mctx.CheckAdmin(ctx); err != nil {
return nil, err
}
oid, err := primitive.ObjectIDFromHex(req.Id)
if err != nil {
return nil, errs.ErrArgs.WrapMsg("invalid id " + err.Error())
}
update := make(map[string]any)
putUpdate(update, "platform", req.Platform)
putUpdate(update, "version", req.Version)
putUpdate(update, "url", req.Url)
putUpdate(update, "text", req.Text)
putUpdate(update, "force", req.Force)
putUpdate(update, "latest", req.Latest)
putUpdate(update, "hot", req.Hot)
if err := o.Database.UpdateVersion(ctx, oid, update); err != nil {
return nil, err
}
return &admin.UpdateApplicationVersionResp{}, nil
}

func (o *adminServer) DeleteApplicationVersion(ctx context.Context, req *admin.DeleteApplicationVersionReq) (*admin.DeleteApplicationVersionResp, error) {
if _, err := mctx.CheckAdmin(ctx); err != nil {
return nil, err
}
ids := make([]primitive.ObjectID, 0, len(req.Id))
for _, id := range req.Id {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, errs.ErrArgs.WrapMsg("invalid id " + err.Error())
}
ids = append(ids, oid)
}
if err := o.Database.DeleteVersion(ctx, ids); err != nil {
return nil, err
}
return &admin.DeleteApplicationVersionResp{}, nil
}

func (o *adminServer) PageApplicationVersion(ctx context.Context, req *admin.PageApplicationVersionReq) (*admin.PageApplicationVersionResp, error) {
total, res, err := o.Database.PageVersion(ctx, req.Platform, req.Pagination)
if err != nil {
return nil, err
}
return &admin.PageApplicationVersionResp{
Total: total,
Versions: datautil.Slice(res, o.db2pbApplication),
}, nil
}

func putUpdate[T any](update map[string]any, name string, val interface{ GetValuePtr() *T }) {
ptrVal := val.GetValuePtr()
if ptrVal == nil {
return
}
update[name] = *ptrVal
}
32 changes: 32 additions & 0 deletions pkg/common/db/database/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package database

import (
"context"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"

"github.com/openimsdk/chat/pkg/common/db/cache"
Expand Down Expand Up @@ -78,6 +79,11 @@ type AdminDatabaseInterface interface {
CacheToken(ctx context.Context, userID string, token string, expire time.Duration) error
GetTokens(ctx context.Context, userID string) (map[string]int32, error)
DeleteToken(ctx context.Context, userID string) error
LatestVersion(ctx context.Context, platform string) (*admindb.Application, error)
AddVersion(ctx context.Context, val *admindb.Application) error
UpdateVersion(ctx context.Context, id primitive.ObjectID, update map[string]any) error
DeleteVersion(ctx context.Context, id []primitive.ObjectID) error
PageVersion(ctx context.Context, platforms []string, page pagination.Pagination) (int64, []*admindb.Application, error)
}

func NewAdminDatabase(cli *mongoutil.Client, rdb redis.UniversalClient) (AdminDatabaseInterface, error) {
Expand Down Expand Up @@ -117,6 +123,10 @@ func NewAdminDatabase(cli *mongoutil.Client, rdb redis.UniversalClient) (AdminDa
if err != nil {
return nil, err
}
application, err := admin.NewApplication(cli.GetDB())
if err != nil {
return nil, err
}
return &AdminDatabase{
tx: cli.GetTx(),
admin: a,
Expand All @@ -128,6 +138,7 @@ func NewAdminDatabase(cli *mongoutil.Client, rdb redis.UniversalClient) (AdminDa
registerAddGroup: registerAddGroup,
applet: applet,
clientConfig: clientConfig,
application: application,
cache: cache.NewTokenInterface(rdb),
}, nil
}
Expand All @@ -143,6 +154,7 @@ type AdminDatabase struct {
registerAddGroup admindb.RegisterAddGroupInterface
applet admindb.AppletInterface
clientConfig admindb.ClientConfigInterface
application admindb.ApplicationInterface
cache cache.TokenInterface
}

Expand Down Expand Up @@ -347,3 +359,23 @@ func (o *AdminDatabase) GetTokens(ctx context.Context, userID string) (map[strin
func (o *AdminDatabase) DeleteToken(ctx context.Context, userID string) error {
return o.cache.DeleteTokenByUid(ctx, userID)
}

func (o *AdminDatabase) LatestVersion(ctx context.Context, platform string) (*admindb.Application, error) {
return o.application.LatestVersion(ctx, platform)
}

func (o *AdminDatabase) AddVersion(ctx context.Context, val *admindb.Application) error {
return o.application.AddVersion(ctx, val)
}

func (o *AdminDatabase) UpdateVersion(ctx context.Context, id primitive.ObjectID, update map[string]any) error {
return o.application.UpdateVersion(ctx, id, update)
}

func (o *AdminDatabase) DeleteVersion(ctx context.Context, id []primitive.ObjectID) error {
return o.application.DeleteVersion(ctx, id)
}

func (o *AdminDatabase) PageVersion(ctx context.Context, platforms []string, page pagination.Pagination) (int64, []*admindb.Application, error) {
return o.application.PageVersion(ctx, platforms, page)
}
Loading

0 comments on commit 49b157f

Please sign in to comment.