Documentation
¶
Index ¶
- Constants
- Variables
- type EmailUnsubscribe
- type EmailVerification
- type Notification
- type NotificationStats
- type RefreshToken
- type User
- type UserOAuthLink
- type UserRepo
- func (r *UserRepo) CheckUserExists(ctx context.Context, name string, role model.Role) (bool, error)
- func (r *UserRepo) ClaimNotification(ctx context.Context, owner string) (Notification, error)
- func (r *UserRepo) CompleteNotification(ctx context.Context, n Notification, messageID string) error
- func (r *UserRepo) CreateEmailUnsubscribe(ctx context.Context, unsubscribe EmailUnsubscribe) error
- func (r *UserRepo) CreateEmailVerification(ctx context.Context, vrf EmailVerification) error
- func (r *UserRepo) CreateNotification(ctx context.Context, n Notification) error
- func (r *UserRepo) CreateRefreshToken(ctx context.Context, refreshToken RefreshToken) error
- func (r *UserRepo) CreateUser(ctx context.Context, user User) error
- func (r *UserRepo) CreateUserOAuthLink(ctx context.Context, link UserOAuthLink) error
- func (r *UserRepo) DeleteExpiredRefreshTokens(ctx context.Context) error
- func (r *UserRepo) DeleteRefreshToken(ctx context.Context, tokenHash string) error
- func (r *UserRepo) DeleteRefreshTokensByUserID(ctx context.Context, userID string) error
- func (r *UserRepo) DeleteUser(ctx context.Context, userID string) error
- func (r *UserRepo) ExpireNotifications(ctx context.Context) error
- func (r *UserRepo) FinishNotification(ctx context.Context, n Notification, status, reason string) error
- func (r *UserRepo) GetEmailVerificationByUserID(ctx context.Context, userID string) (EmailVerification, error)
- func (r *UserRepo) GetRefreshTokenByHash(ctx context.Context, tokenHash string) (RefreshToken, error)
- func (r *UserRepo) GetUserByEmail(ctx context.Context, email string) (User, error)
- func (r *UserRepo) GetUserByID(ctx context.Context, userID string) (user User, err error)
- func (r *UserRepo) GetUserByOAuthLink(ctx context.Context, provider string, oauthID string) (User, error)
- func (r *UserRepo) GetUserByUsername(ctx context.Context, username string) (user User, err error)
- func (r *UserRepo) GetVerificationState(ctx context.Context, userID, email string) (VerificationState, error)
- func (r *UserRepo) HasOAuthLink(ctx context.Context, userID string) (bool, error)
- func (r *UserRepo) IsEmailUnsubscribed(ctx context.Context, email string) (bool, error)
- func (r *UserRepo) NotificationEligible(ctx context.Context, n Notification, email string) (bool, error)
- func (r *UserRepo) NotificationStats(ctx context.Context) (NotificationStats, error)
- func (r *UserRepo) PurgeNotifications(ctx context.Context) error
- func (r *UserRepo) RetryNotification(ctx context.Context, n Notification, delay time.Duration, reason string) error
- func (r *UserRepo) RunWithTx(ctx context.Context, f func(context.Context) error) error
- func (r *UserRepo) SetEmailVerificationUsed(ctx context.Context, id string, verified bool) error
- func (r *UserRepo) SetUnsubscribeToken(ctx context.Context, id string, token string) error
- func (r *UserRepo) SetUserEmailVerified(ctx context.Context, userID string) error
- func (r *UserRepo) UpdateUser(ctx context.Context, user User) error
- type VerificationState
Constants ¶
const NotificationLease = 60 * time.Second
NotificationLease exceeds the worker attempt deadline to allow crash recovery
Variables ¶
var ( // ErrNotFound is used when a record is not found ErrNotFound = errors.New("not found") // ErrUserExists is used when username/email already exists ErrUserExists = errors.New("user already exists") // ErrOAuthLinkExists is used when a user already has an OAuth link for the same provider ErrOAuthLinkExists = errors.New("oauth link already exists for this provider") )
var ErrLeaseLost = errors.New("notification lease lost")
ErrLeaseLost means the claim has expired or another worker owns it
Functions ¶
This section is empty.
Types ¶
type EmailUnsubscribe ¶
EmailUnsubscribe represents an email unsubscribe record
func NewEmailUnsubscribe ¶
func NewEmailUnsubscribe(email string) EmailUnsubscribe
NewEmailUnsubscribe creates a new email unsubscribe record
type EmailVerification ¶
type EmailVerification struct {
ID string `db:"id"`
UserID string `db:"user_id"`
CodeHash sql.NullString `db:"verification_code"`
MessageID sql.NullString `db:"message_id"`
UnsubscribeToken sql.NullString `db:"unsubscribe_token"`
DateCreated time.Time `db:"date_created"`
}
EmailVerification represents an email verification record
func NewEmailVerification ¶
func NewEmailVerification(userID, codeHash, unsubscribeToken string, createdAt time.Time) EmailVerification
NewEmailVerification creates a new email verification record
func (*EmailVerification) IsExpired ¶
func (ev *EmailVerification) IsExpired() bool
IsExpired checks if the verification code has expired
type Notification ¶ added in v1.2.0
type Notification struct {
ID string `db:"id"`
ReferenceID string `db:"reference_id"`
Kind string `db:"kind"`
Payload []byte `db:"payload"`
Attempts int `db:"attempts"`
LockedBy string `db:"locked_by"`
LockedAt time.Time `db:"locked_at"`
ExpiresAt time.Time `db:"expires_at"`
CreatedAt time.Time `db:"created_at"`
Reclaimed bool `db:"reclaimed"`
}
Notification contains the JSON payload and the exact lease returned by a claim
type NotificationStats ¶ added in v1.2.0
NotificationStats describes the shared queue, not one worker process
type RefreshToken ¶
type RefreshToken struct {
ID string `db:"id"`
UserID string `db:"user_id"`
TokenHash string `db:"token_hash"`
ExpiresAt time.Time `db:"expires_at"`
DateCreated time.Time `db:"date_created"`
}
RefreshToken represents a refresh token
func NewRefreshToken ¶
func NewRefreshToken(userID, tokenHash string, expiresAt time.Time) RefreshToken
NewRefreshToken creates a new refresh token
func (*RefreshToken) IsExpired ¶
func (rt *RefreshToken) IsExpired() bool
IsExpired checks if the refresh token has expired
type User ¶
type User struct {
ID string `db:"id"`
Username string `db:"username"`
DisplayName string `db:"name"`
Email sql.NullString `db:"email"`
EmailVerified bool `db:"email_verified"`
PasswordHash []byte `db:"password_hash"`
Role model.Role `db:"role"`
DateCreated time.Time `db:"date_created"`
DateUpdated sql.NullTime `db:"date_updated"`
}
User represents a user
type UserOAuthLink ¶
type UserOAuthLink struct {
UserID string `db:"user_id"`
OAuthProvider string `db:"oauth_provider"`
OAuthID string `db:"oauth_id"`
}
UserOAuthLink represents a link between a user and an OAuth provider
func NewUserOAuthLink ¶
func NewUserOAuthLink(userID, provider, oauthID string) UserOAuthLink
NewUserOAuthLink creates a new user OAuth link
type UserRepo ¶
type UserRepo struct {
// contains filtered or unexported fields
}
UserRepo manages API for user access
func NewUserRepo ¶
NewUserRepo constructs a user Repo
func (*UserRepo) CheckUserExists ¶
CheckUserExists checks whether user with provided name and role exists
func (*UserRepo) ClaimNotification ¶ added in v1.2.0
ClaimNotification commits its short row lock before the provider is contacted
func (*UserRepo) CompleteNotification ¶ added in v1.2.0
func (r *UserRepo) CompleteNotification(ctx context.Context, n Notification, messageID string) error
CompleteNotification acknowledges delivery and records the provider message ID in one atomic statement
func (*UserRepo) CreateEmailUnsubscribe ¶
func (r *UserRepo) CreateEmailUnsubscribe(ctx context.Context, unsubscribe EmailUnsubscribe) error
CreateEmailUnsubscribe creates a new email unsubscribe record
func (*UserRepo) CreateEmailVerification ¶
func (r *UserRepo) CreateEmailVerification(ctx context.Context, vrf EmailVerification) error
CreateEmailVerification creates a new email verification record
func (*UserRepo) CreateNotification ¶ added in v1.2.0
func (r *UserRepo) CreateNotification(ctx context.Context, n Notification) error
CreateNotification participates in the caller's transaction without querying the referenced entity
func (*UserRepo) CreateRefreshToken ¶
func (r *UserRepo) CreateRefreshToken(ctx context.Context, refreshToken RefreshToken) error
CreateRefreshToken inserts a new refresh token into the database
func (*UserRepo) CreateUser ¶
CreateUser inserts a new user into the database
func (*UserRepo) CreateUserOAuthLink ¶
func (r *UserRepo) CreateUserOAuthLink(ctx context.Context, link UserOAuthLink) error
CreateUserOAuthLink inserts a new user OAuth link, ignoring duplicates for the same provider+oauth_id pair. Returns ErrOAuthLinkExists if the user already has a link for the given provider
func (*UserRepo) DeleteExpiredRefreshTokens ¶
DeleteExpiredRefreshTokens deletes all expired refresh tokens
func (*UserRepo) DeleteRefreshToken ¶
DeleteRefreshToken deletes a refresh token by token hash string
func (*UserRepo) DeleteRefreshTokensByUserID ¶
DeleteRefreshTokensByUserID deletes all refresh tokens for a user
func (*UserRepo) DeleteUser ¶
DeleteUser deletes a user by user id
func (*UserRepo) ExpireNotifications ¶ added in v1.2.0
ExpireNotifications ignores retry eligibility, including long provider Retry-After delays
func (*UserRepo) FinishNotification ¶ added in v1.2.0
func (r *UserRepo) FinishNotification(ctx context.Context, n Notification, status, reason string) error
FinishNotification clears the payload after a terminal delivery decision
func (*UserRepo) GetEmailVerificationByUserID ¶
func (r *UserRepo) GetEmailVerificationByUserID(ctx context.Context, userID string) (EmailVerification, error)
GetEmailVerificationByUserID gets email verification by user ID (most recent unused)
func (*UserRepo) GetRefreshTokenByHash ¶
func (r *UserRepo) GetRefreshTokenByHash(ctx context.Context, tokenHash string) (RefreshToken, error)
GetRefreshTokenByHash retrieves a refresh token by token hash string with row-level lock
func (*UserRepo) GetUserByEmail ¶
GetUserByEmail gets user by email address
func (*UserRepo) GetUserByID ¶
GetUserByID returns user by id
func (*UserRepo) GetUserByOAuthLink ¶
func (r *UserRepo) GetUserByOAuthLink(ctx context.Context, provider string, oauthID string) (User, error)
GetUserByOAuthLink returns a user by oauth provider and oauth_id via user_oauth_links table
func (*UserRepo) GetUserByUsername ¶
GetUserByUsername returns user by username
func (*UserRepo) GetVerificationState ¶ added in v1.2.0
func (r *UserRepo) GetVerificationState(ctx context.Context, userID, email string) (VerificationState, error)
GetVerificationState returns the database time, whether email is unsubscribed and the user's latest unused code, locking that code. Call it after locking the user row so the latest code is read from a fresh snapshot
func (*UserRepo) HasOAuthLink ¶
HasOAuthLink checks if a user has any OAuth link
func (*UserRepo) IsEmailUnsubscribed ¶
IsEmailUnsubscribed checks if an email address is unsubscribed
func (*UserRepo) NotificationEligible ¶ added in v1.2.0
func (r *UserRepo) NotificationEligible(ctx context.Context, n Notification, email string) (bool, error)
NotificationEligible reports whether a still-owned claim targets a pending code for an unverified, subscribed publisher at email. It runs just before sending, without holding locks over HTTP
func (*UserRepo) NotificationStats ¶ added in v1.2.0
func (r *UserRepo) NotificationStats(ctx context.Context) (NotificationStats, error)
NotificationStats returns queue depth and age for monitoring
func (*UserRepo) PurgeNotifications ¶ added in v1.2.0
PurgeNotifications removes a bounded batch of old terminal metadata
func (*UserRepo) RetryNotification ¶ added in v1.2.0
func (r *UserRepo) RetryNotification(ctx context.Context, n Notification, delay time.Duration, reason string) error
RetryNotification releases an owned lease and persists the next attempt time
func (*UserRepo) SetEmailVerificationUsed ¶
SetEmailVerificationUsed sets email verification as used by clearing code hash and optionally setting verified_at
func (*UserRepo) SetUnsubscribeToken ¶
SetUnsubscribeToken sets the unsubscribe token for an email verification record
func (*UserRepo) SetUserEmailVerified ¶
SetUserEmailVerified sets user email as verified
type VerificationState ¶ added in v1.2.0
type VerificationState struct {
Now time.Time `db:"now"`
Unsubscribed bool `db:"unsubscribed"`
LatestID sql.NullString `db:"latest_id"`
LatestCreated sql.NullTime `db:"latest_created"`
}
VerificationState holds what queueing a verification code depends on