feat: update workspace profile

This commit is contained in:
Steven
2024-06-03 22:41:51 +08:00
parent 15ca4fe7ac
commit d51d180a29
17 changed files with 332 additions and 282 deletions

View File

@@ -2,20 +2,22 @@ package v1
import (
"context"
"fmt"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
apiv1pb "github.com/yourselfhosted/slash/proto/gen/api/v1"
v1pb "github.com/yourselfhosted/slash/proto/gen/api/v1"
storepb "github.com/yourselfhosted/slash/proto/gen/store"
"github.com/yourselfhosted/slash/store"
)
func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *apiv1pb.GetWorkspaceProfileRequest) (*apiv1pb.GetWorkspaceProfileResponse, error) {
profile := &apiv1pb.WorkspaceProfile{
func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *v1pb.GetWorkspaceProfileRequest) (*v1pb.GetWorkspaceProfileResponse, error) {
profile := &v1pb.WorkspaceProfile{
Mode: s.Profile.Mode,
Version: s.Profile.Version,
Plan: apiv1pb.PlanType_FREE,
Plan: v1pb.PlanType_FREE,
}
// Load subscription plan from license service.
@@ -25,7 +27,7 @@ func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *apiv1pb.GetWo
}
profile.Plan = subscription.Plan
workspaceSetting, err := s.GetWorkspaceSetting(ctx, &apiv1pb.GetWorkspaceSettingRequest{})
workspaceSetting, err := s.GetWorkspaceSetting(ctx, &v1pb.GetWorkspaceSettingRequest{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
}
@@ -36,12 +38,20 @@ func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *apiv1pb.GetWo
profile.CustomScript = setting.GetCustomScript()
profile.FaviconProvider = setting.GetFaviconProvider()
}
return &apiv1pb.GetWorkspaceProfileResponse{
owner, err := s.GetInstanceOwner(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get instance owner: %v", err)
}
if owner != nil {
profile.Owner = fmt.Sprintf("%s%d", UserNamePrefix, owner.Id)
}
return &v1pb.GetWorkspaceProfileResponse{
Profile: profile,
}, nil
}
func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, _ *apiv1pb.GetWorkspaceSettingRequest) (*apiv1pb.GetWorkspaceSettingResponse, error) {
func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, _ *v1pb.GetWorkspaceSettingRequest) (*v1pb.GetWorkspaceSettingResponse, error) {
isAdmin := false
userID, ok := ctx.Value(userIDContextKey).(int32)
if ok {
@@ -57,7 +67,7 @@ func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, _ *apiv1pb.GetWo
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list workspace settings: %v", err)
}
workspaceSetting := &apiv1pb.WorkspaceSetting{
workspaceSetting := &v1pb.WorkspaceSetting{
EnableSignup: true,
}
for _, v := range workspaceSettings {
@@ -70,7 +80,7 @@ func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, _ *apiv1pb.GetWo
} else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_CUSTOM_SCRIPT {
workspaceSetting.CustomScript = v.GetCustomScript()
} else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_DEFAULT_VISIBILITY {
workspaceSetting.DefaultVisibility = apiv1pb.Visibility(v.GetDefaultVisibility())
workspaceSetting.DefaultVisibility = v1pb.Visibility(v.GetDefaultVisibility())
} else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_FAVICON_PROVIDER {
workspaceSetting.FaviconProvider = v.GetFaviconProvider()
} else if isAdmin {
@@ -80,12 +90,12 @@ func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, _ *apiv1pb.GetWo
}
}
}
return &apiv1pb.GetWorkspaceSettingResponse{
return &v1pb.GetWorkspaceSettingResponse{
Setting: workspaceSetting,
}, nil
}
func (s *APIV1Service) UpdateWorkspaceSetting(ctx context.Context, request *apiv1pb.UpdateWorkspaceSettingRequest) (*apiv1pb.UpdateWorkspaceSettingResponse, error) {
func (s *APIV1Service) UpdateWorkspaceSetting(ctx context.Context, request *v1pb.UpdateWorkspaceSettingRequest) (*v1pb.UpdateWorkspaceSettingResponse, error) {
if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "update mask is empty")
}
@@ -159,11 +169,33 @@ func (s *APIV1Service) UpdateWorkspaceSetting(ctx context.Context, request *apiv
}
}
getWorkspaceSettingResponse, err := s.GetWorkspaceSetting(ctx, &apiv1pb.GetWorkspaceSettingRequest{})
getWorkspaceSettingResponse, err := s.GetWorkspaceSetting(ctx, &v1pb.GetWorkspaceSettingRequest{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
}
return &apiv1pb.UpdateWorkspaceSettingResponse{
return &v1pb.UpdateWorkspaceSettingResponse{
Setting: getWorkspaceSettingResponse.Setting,
}, nil
}
var ownerCache *v1pb.User
func (s *APIV1Service) GetInstanceOwner(ctx context.Context) (*v1pb.User, error) {
if ownerCache != nil {
return ownerCache, nil
}
adminRole := store.RoleAdmin
user, err := s.Store.GetUser(ctx, &store.FindUser{
Role: &adminRole,
})
if err != nil {
return nil, errors.Wrapf(err, "failed to find admin")
}
if user == nil {
return nil, nil
}
ownerCache = convertUserFromStore(user)
return ownerCache, nil
}