chore: tweak plans

This commit is contained in:
Steven
2024-08-16 21:39:14 +08:00
parent 0be4d8c906
commit faa6fcf31c
10 changed files with 163 additions and 100 deletions

View File

@ -108,15 +108,14 @@ func (s *APIV1Service) CreateCollection(ctx context.Context, request *v1pb.Creat
return nil, status.Errorf(codes.InvalidArgument, "name and title are required")
}
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedAccounts) {
collections, err := s.Store.ListCollections(ctx, &store.FindCollection{
VisibilityList: []store.Visibility{store.VisibilityWorkspace, store.VisibilityPublic},
})
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedCollections) {
collections, err := s.Store.ListCollections(ctx, &store.FindCollection{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get collection list, err: %v", err)
}
if len(collections) >= 5 {
return nil, status.Errorf(codes.PermissionDenied, "Maximum number of collections reached")
collectionsLimit := int(s.LicenseService.GetSubscription().CollectionsLimit)
if len(collections) >= collectionsLimit {
return nil, status.Errorf(codes.PermissionDenied, "Maximum number of collections %d reached", collectionsLimit)
}
}

View File

@ -132,6 +132,17 @@ func (s *APIV1Service) CreateShortcut(ctx context.Context, request *v1pb.CreateS
return nil, status.Errorf(codes.InvalidArgument, "name and link are required")
}
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedShortcuts) {
shortcuts, err := s.Store.ListShortcuts(ctx, &store.FindShortcut{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get shortcut list, err: %v", err)
}
shortcutsLimit := int(s.LicenseService.GetSubscription().ShortcutsLimit)
if len(shortcuts) >= shortcutsLimit {
return nil, status.Errorf(codes.PermissionDenied, "Maximum number of shortcuts %d reached", shortcutsLimit)
}
}
user, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "failed to get current user: %v", err)

View File

@ -18,6 +18,8 @@ const (
// FeatureTypeUnlimitedAccounts allows the user to create unlimited accounts.
FeatureTypeUnlimitedAccounts FeatureType = "ysh.slash.unlimited-accounts"
// FeatureTypeUnlimitedShortcuts allows the user to create unlimited shortcuts.
FeatureTypeUnlimitedShortcuts FeatureType = "ysh.slash.unlimited-shortcuts"
// FeatureTypeUnlimitedAccounts allows the user to create unlimited collections.
FeatureTypeUnlimitedCollections FeatureType = "ysh.slash.unlimited-collections"
@ -36,6 +38,7 @@ var FeatureMatrix = map[FeatureType][3]bool{
FeatureTypeSSO: {false, false, false},
FeatureTypeAdvancedAnalytics: {false, false, false},
FeatureTypeUnlimitedAccounts: {false, true, false},
FeatureTypeUnlimitedShortcuts: {false, true, true},
FeatureTypeUnlimitedCollections: {false, true, true},
FeatureTypeCustomeBranding: {false, true, true},
}
@ -58,6 +61,8 @@ func validateFeatureString(feature string) (FeatureType, bool) {
return FeatureTypeAdvancedAnalytics, true
case "ysh.slash.unlimited-accounts":
return FeatureTypeUnlimitedAccounts, true
case "ysh.slash.unlimited-shortcuts":
return FeatureTypeUnlimitedShortcuts, true
case "ysh.slash.unlimited-collections":
return FeatureTypeUnlimitedCollections, true
case "ysh.slash.custom-branding":

View File

@ -30,12 +30,9 @@ type LicenseService struct {
// NewLicenseService creates a new LicenseService.
func NewLicenseService(profile *profile.Profile, store *store.Store) *LicenseService {
return &LicenseService{
Profile: profile,
Store: store,
cachedSubscription: &v1pb.Subscription{
Plan: v1pb.PlanType_FREE,
Seats: 5,
},
Profile: profile,
Store: store,
cachedSubscription: getSubscriptionForFreePlan(),
}
}
@ -47,10 +44,7 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) (*v1pb.Subscripti
return nil, errors.Wrap(err, "failed to get workspace setting")
}
subscription := &v1pb.Subscription{
Plan: v1pb.PlanType_FREE,
Seats: 5,
}
subscription := getSubscriptionForFreePlan()
licenseKey := ""
if workspaceSettingGeneral != nil {
licenseKey = workspaceSettingGeneral.GetGeneral().LicenseKey
@ -183,12 +177,8 @@ func validateLicenseKey(licenseKey string) (*ValidateResult, error) {
}
if validateResponse.Valid {
result := &ValidateResult{
Plan: v1pb.PlanType_PRO,
Features: []FeatureType{
FeatureTypeUnlimitedAccounts,
FeatureTypeUnlimitedCollections,
FeatureTypeCustomeBranding,
},
Plan: v1pb.PlanType_PRO,
Features: getDefaultFeatures(v1pb.PlanType_PRO),
}
if validateResponse.LicenseKey.ExpiresAt != nil && *validateResponse.LicenseKey.ExpiresAt != "" {
expiresTime, err := time.Parse(time.RFC3339Nano, *validateResponse.LicenseKey.ExpiresAt)
@ -230,3 +220,13 @@ func parseLicenseKey(licenseKey string) (*Claims, error) {
}
return claims, nil
}
func getSubscriptionForFreePlan() *v1pb.Subscription {
return &v1pb.Subscription{
Plan: v1pb.PlanType_FREE,
Seats: 5,
ShortcutsLimit: 100,
CollectionsLimit: 5,
Features: []string{},
}
}