Files
spsays-bff/internal/model/limits.go
T
2026-06-23 14:21:45 +05:30

52 lines
1.7 KiB
Go

package model
// Plan-default save limits (-1 = unlimited). Per-user override columns on the
// users row take precedence when non-nil; admins set them from the panel.
const (
planPremium = "premium"
defaultFreeBookmarkLimit = 1
defaultPremiumBookmarkLimit = 5
defaultFreeCollectionLimit = 5 // per visibility; premium = unlimited
)
// UserLimits are the effective per-resource caps in force for a user (-1 means
// unlimited). Surfaced on the self user payload so the app can render
// "X / Y used" and gate create actions without hardcoding the numbers.
type UserLimits struct {
Bookmarks int `json:"bookmarks"`
PublicCollections int `json:"public_collections"`
PrivateCollections int `json:"private_collections"`
}
// ResolveLimits computes the effective caps from a plan plus optional per-user
// overrides (nil = use the plan default). Shared by the self payload and the
// admin user listing so both agree on what's in force.
func ResolveLimits(plan string, bookmark, publicCollection, privateCollection *int) UserLimits {
premium := plan == planPremium
bm := defaultFreeBookmarkLimit
if premium {
bm = defaultPremiumBookmarkLimit
}
pub, priv := defaultFreeCollectionLimit, defaultFreeCollectionLimit
if premium {
pub, priv = -1, -1
}
if bookmark != nil {
bm = *bookmark
}
if publicCollection != nil {
pub = *publicCollection
}
if privateCollection != nil {
priv = *privateCollection
}
return UserLimits{Bookmarks: bm, PublicCollections: pub, PrivateCollections: priv}
}
// EffectiveLimits resolves this user's caps (plan default + any overrides).
func (u *User) EffectiveLimits() UserLimits {
return ResolveLimits(u.Plan, u.BookmarkLimit, u.PublicCollectionLimit, u.PrivateCollectionLimit)
}