Sharing: Optional view count limit #18

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-06-26 12:24:05 +02:00
parent cfd23666a9
commit ff704d56a0
3 changed files with 16 additions and 0 deletions

View File

@ -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
}

View File

@ -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) {

View File

@ -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"`
}