-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
2,032 additions
and
498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
"github.com/openimsdk/chat/pkg/common/db/table/admin" | ||
admindb "github.com/openimsdk/chat/pkg/common/db/table/admin" | ||
"github.com/openimsdk/tools/db/mongoutil" | ||
"github.com/openimsdk/tools/db/pagination" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func NewApplication(db *mongo.Database) (admindb.ApplicationInterface, error) { | ||
coll := db.Collection("application") | ||
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{ | ||
{ | ||
Keys: bson.D{ | ||
{Key: "platform", Value: 1}, | ||
{Key: "version", Value: 1}, | ||
}, | ||
Options: options.Index().SetUnique(true), | ||
}, | ||
{ | ||
Keys: bson.D{ | ||
{Key: "latest", Value: -1}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ApplicationMgo{coll: coll}, nil | ||
} | ||
|
||
type ApplicationMgo struct { | ||
coll *mongo.Collection | ||
} | ||
|
||
func (a *ApplicationMgo) sort() any { | ||
return bson.D{{"latest", -1}, {"_id", -1}} | ||
} | ||
|
||
func (a *ApplicationMgo) LatestVersion(ctx context.Context, platform string) (*admin.Application, error) { | ||
return mongoutil.FindOne[*admin.Application](ctx, a.coll, bson.M{"platform": platform}, options.FindOne().SetSort(a.sort())) | ||
} | ||
|
||
func (a *ApplicationMgo) AddVersion(ctx context.Context, val *admin.Application) error { | ||
if val.ID.IsZero() { | ||
val.ID = primitive.NewObjectID() | ||
} | ||
return mongoutil.InsertMany(ctx, a.coll, []*admin.Application{val}) | ||
} | ||
|
||
func (a *ApplicationMgo) UpdateVersion(ctx context.Context, id primitive.ObjectID, update map[string]any) error { | ||
if len(update) == 0 { | ||
return nil | ||
} | ||
return mongoutil.UpdateOne(ctx, a.coll, bson.M{"_id": id}, bson.M{"$set": update}, true) | ||
} | ||
|
||
func (a *ApplicationMgo) DeleteVersion(ctx context.Context, id []primitive.ObjectID) error { | ||
if len(id) == 0 { | ||
return nil | ||
} | ||
return mongoutil.DeleteMany(ctx, a.coll, bson.M{"_id": bson.M{"$in": id}}) | ||
} | ||
|
||
func (a *ApplicationMgo) PageVersion(ctx context.Context, platforms []string, page pagination.Pagination) (int64, []*admin.Application, error) { | ||
filter := bson.M{} | ||
if len(platforms) > 0 { | ||
filter["platform"] = bson.M{"$in": platforms} | ||
} | ||
return mongoutil.FindPage[*admin.Application](ctx, a.coll, filter, page, options.Find().SetSort(a.sort())) | ||
} | ||
|
||
func (a *ApplicationMgo) FindPlatform(ctx context.Context, id []primitive.ObjectID) ([]string, error) { | ||
if len(id) == 0 { | ||
return nil, nil | ||
} | ||
return mongoutil.Find[string](ctx, a.coll, bson.M{"_id": bson.M{"$in": id}}, options.Find().SetProjection(bson.M{"_id": 0, "platform": 1})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
"github.com/openimsdk/tools/db/pagination" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"time" | ||
) | ||
|
||
type Application struct { | ||
ID primitive.ObjectID `bson:"_id"` | ||
Platform string `bson:"platform"` | ||
Hot bool `bson:"hot"` | ||
Version string `bson:"version"` | ||
Url string `bson:"url"` | ||
Text string `bson:"text"` | ||
Force bool `bson:"force"` | ||
Latest bool `bson:"latest"` | ||
CreateTime time.Time `bson:"create_time"` | ||
} | ||
|
||
type ApplicationInterface interface { | ||
LatestVersion(ctx context.Context, platform string) (*Application, error) | ||
AddVersion(ctx context.Context, val *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, []*Application, error) | ||
FindPlatform(ctx context.Context, id []primitive.ObjectID) ([]string, error) | ||
} |
Oops, something went wrong.