2020-04-03 18:08:49 +02:00
|
|
|
package query
|
|
|
|
|
2020-04-04 13:53:52 +02:00
|
|
|
import (
|
|
|
|
"time"
|
2020-04-03 18:08:49 +02:00
|
|
|
|
2020-04-04 13:53:52 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FileShares returns up to 100 file shares for a given account id and status.
|
2020-04-03 18:08:49 +02:00
|
|
|
func (q *Query) FileShares(accountId uint, status string) (result []entity.FileShare, err error) {
|
|
|
|
s := q.db.Where(&entity.FileShare{})
|
|
|
|
|
|
|
|
if accountId > 0 {
|
|
|
|
s = s.Where("account_id = ?", accountId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != "" {
|
|
|
|
s = s.Where("status = ?", status)
|
|
|
|
}
|
|
|
|
|
|
|
|
s = s.Order("created_at ASC")
|
|
|
|
s = s.Limit(100).Offset(0)
|
|
|
|
|
|
|
|
s = s.Preload("File")
|
|
|
|
|
|
|
|
if err := s.Find(&result).Error; err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2020-04-04 13:53:52 +02:00
|
|
|
|
|
|
|
// ExpiredFileShares returns up to 100 expired file shares for a given account.
|
|
|
|
func (q *Query) ExpiredFileShares(account entity.Account) (result []entity.FileShare, err error) {
|
|
|
|
if account.ShareExpires <= 0 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := q.db.Where(&entity.FileShare{})
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
exp := time.Now().Add(time.Duration(-1*account.ShareExpires) * time.Second)
|
2020-04-04 13:53:52 +02:00
|
|
|
|
|
|
|
s = s.Where("account_id = ?", account.ID)
|
|
|
|
s = s.Where("status = ?", entity.FileShareShared)
|
|
|
|
s = s.Where("updated_at < ?", exp)
|
|
|
|
|
|
|
|
s = s.Order("updated_at ASC")
|
|
|
|
s = s.Limit(100).Offset(0)
|
|
|
|
|
|
|
|
s = s.Preload("File")
|
|
|
|
|
|
|
|
if err := s.Find(&result).Error; err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|