diff --git a/internal/entity/link.go b/internal/entity/link.go index 035516a5a..c7779e31f 100644 --- a/internal/entity/link.go +++ b/internal/entity/link.go @@ -17,6 +17,7 @@ type Link struct { ShareToken string `gorm:"type:varbinary(255);unique_index:idx_links_uid_token;" json:"ShareToken"` ShareExpires int `json:"ShareExpires" yaml:"ShareExpires,omitempty"` ShareViews uint `json:"ShareViews" yaml:"-"` + MaxViews uint `json:"MaxViews" yaml:"-"` HasPassword bool `json:"HasPassword" yaml:"HasPassword,omitempty"` CanComment bool `json:"CanComment" yaml:"CanComment,omitempty"` CanEdit bool `json:"CanEdit" yaml:"CanEdit,omitempty"` @@ -61,6 +62,10 @@ func (m *Link) Redeem() { } func (m *Link) Expired() bool { + if m.MaxViews > 0 && m.ShareViews >= m.MaxViews { + return true + } + if m.ShareExpires <= 0 { return false } diff --git a/internal/entity/link_test.go b/internal/entity/link_test.go index 2f804d8bc..552b9b7ac 100644 --- a/internal/entity/link_test.go +++ b/internal/entity/link_test.go @@ -33,6 +33,16 @@ func TestLink_Expired(t *testing.T) { link.ShareExpires = oneDay * 8 assert.True(t, link.Expired()) + + link.ShareExpires = oneDay + link.ShareViews = 9 + link.MaxViews = 10 + + assert.False(t, link.Expired()) + + link.Redeem() + + assert.True(t, link.Expired()) } func TestLink_Redeem(t *testing.T) { diff --git a/internal/form/link.go b/internal/form/link.go index bd8932299..9ccf87013 100644 --- a/internal/form/link.go +++ b/internal/form/link.go @@ -5,6 +5,7 @@ type Link struct { Password string `json:"Password"` ShareToken string `json:"ShareToken"` ShareExpires int `json:"ShareExpires"` + MaxViews uint `json:"MaxViews"` CanComment bool `json:"CanComment"` CanEdit bool `json:"CanEdit"` }