2021-07-24 19:44:26 +00:00
|
|
|
package domain
|
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
2022-03-28 21:05:01 +00:00
|
|
|
"time"
|
2021-10-12 14:18:40 +00:00
|
|
|
|
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2021-08-08 18:19:47 +00:00
|
|
|
)
|
2021-07-24 19:44:26 +00:00
|
|
|
|
2022-03-28 21:05:01 +00:00
|
|
|
const (
|
2023-03-16 14:34:15 +00:00
|
|
|
NotificationCheckInterval = 60 * time.Second // time between notification checks
|
2022-11-10 17:05:57 +00:00
|
|
|
NotificationCheckTimeout = 5 * time.Minute // time before we give up an account check lock
|
|
|
|
StuckNotificationCheckInterval = 2 * time.Minute // time between stuck notification checks
|
|
|
|
StaleTokenThreshold = 2 * time.Hour // time an oauth token has to be expired for to be stale
|
2022-03-28 21:05:01 +00:00
|
|
|
)
|
|
|
|
|
2021-07-24 19:44:26 +00:00
|
|
|
// Account represents an account we need to periodically check in the notifications worker.
|
|
|
|
type Account struct {
|
|
|
|
ID int64
|
|
|
|
|
|
|
|
// Reddit information
|
2022-03-28 21:05:01 +00:00
|
|
|
Username string
|
|
|
|
AccountID string
|
|
|
|
AccessToken string
|
|
|
|
RefreshToken string
|
|
|
|
TokenExpiresAt time.Time
|
2022-11-13 14:08:55 +00:00
|
|
|
Development bool
|
2021-07-24 19:44:26 +00:00
|
|
|
|
|
|
|
// Tracking how far behind we are
|
2022-03-28 21:05:01 +00:00
|
|
|
LastMessageID string
|
|
|
|
NextNotificationCheckAt time.Time
|
|
|
|
NextStuckNotificationCheckAt time.Time
|
|
|
|
CheckCount int64
|
2021-07-24 19:44:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
func (acct *Account) NormalizedUsername() string {
|
|
|
|
return strings.ToLower(acct.Username)
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:18:40 +00:00
|
|
|
func (acct *Account) Validate() error {
|
|
|
|
return validation.ValidateStruct(acct,
|
|
|
|
validation.Field(&acct.Username, validation.Required, validation.Length(3, 32)),
|
2021-10-14 03:50:34 +00:00
|
|
|
validation.Field(&acct.AccountID, validation.Required, validation.Length(4, 9)),
|
2021-10-12 14:18:40 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-24 19:44:26 +00:00
|
|
|
// AccountRepository represents the account's repository contract
|
|
|
|
type AccountRepository interface {
|
|
|
|
GetByID(ctx context.Context, id int64) (Account, error)
|
|
|
|
GetByRedditID(ctx context.Context, id string) (Account, error)
|
2021-08-08 18:19:47 +00:00
|
|
|
GetByAPNSToken(ctx context.Context, token string) ([]Account, error)
|
2021-07-24 19:44:26 +00:00
|
|
|
|
2021-07-26 16:34:26 +00:00
|
|
|
CreateOrUpdate(ctx context.Context, acc *Account) error
|
2021-07-24 20:17:54 +00:00
|
|
|
Update(ctx context.Context, acc *Account) error
|
|
|
|
Create(ctx context.Context, acc *Account) error
|
2021-07-24 19:44:26 +00:00
|
|
|
Delete(ctx context.Context, id int64) error
|
2021-07-27 14:05:50 +00:00
|
|
|
Associate(ctx context.Context, acc *Account, dev *Device) error
|
2021-08-08 18:19:47 +00:00
|
|
|
Disassociate(ctx context.Context, acc *Account, dev *Device) error
|
2021-07-24 19:44:26 +00:00
|
|
|
|
2021-08-14 15:59:13 +00:00
|
|
|
PruneOrphaned(ctx context.Context) (int64, error)
|
2022-03-28 21:05:01 +00:00
|
|
|
PruneStale(ctx context.Context, expiry time.Time) (int64, error)
|
2021-07-24 19:44:26 +00:00
|
|
|
}
|