Documentation
¶
Overview ¶
Package database provides SQLite and Postgres-protocol access for outtake.
Index ¶
- Constants
- Variables
- type ClipProfile
- type DB
- func (db *DB) AddPendingUser(ctx context.Context, plexUserID int, username string) error
- func (db *DB) ApproveUser(ctx context.Context, plexUserID int) error
- func (db *DB) ClaimOwner(ctx context.Context, plexUserID int, username string) (bool, error)
- func (db *DB) ClearLegacyTokens(ctx context.Context) error
- func (db *DB) ClearSelectedServer(ctx context.Context) error
- func (db *DB) Close() error
- func (db *DB) Conn() *sql.DB
- func (db *DB) DefaultClipProfile(ctx context.Context) (ClipProfile, error)
- func (db *DB) DeleteClip(ctx context.Context, id string) error
- func (db *DB) DeleteClipProfile(ctx context.Context, id string) error
- func (db *DB) GetClip(ctx context.Context, id string) (*clip.Job, error)
- func (db *DB) GetClipProfile(ctx context.Context, id string) (ClipProfile, error)
- func (db *DB) GlobalLimits(ctx context.Context) (quota.Limits, error)
- func (db *DB) HasOwner(ctx context.Context) (bool, error)
- func (db *DB) LegacyToken(ctx context.Context) (string, error)
- func (db *DB) ListClipProfiles(ctx context.Context) ([]ClipProfile, error)
- func (db *DB) ListClips(ctx context.Context) ([]*clip.Job, error)
- func (db *DB) ListClipsForMedia(ctx context.Context, mediaID string) ([]*clip.Job, error)
- func (db *DB) ListPendingClips(ctx context.Context) ([]*clip.Job, error)
- func (db *DB) ListUsers(ctx context.Context) ([]UserRecord, error)
- func (db *DB) RecordRender(ctx context.Context, owner int, at time.Time) error
- func (db *DB) RejectUser(ctx context.Context, plexUserID int) error
- func (db *DB) RemoveUser(ctx context.Context, plexUserID int) error
- func (db *DB) RendersSince(ctx context.Context, owner int, since time.Time) (int, time.Time, error)
- func (db *DB) ResetOwner(ctx context.Context) (bool, error)
- func (db *DB) SaveClip(ctx context.Context, job *clip.Job) error
- func (db *DB) SaveClipProfile(ctx context.Context, profile ClipProfile) error
- func (db *DB) SaveGlobalLimits(ctx context.Context, limits quota.Limits) error
- func (db *DB) SaveSelectedServer(ctx context.Context, server plex.Server) error
- func (db *DB) SaveSetting(ctx context.Context, name, value string) error
- func (db *DB) SaveUserLimits(ctx context.Context, plexUserID int, override quota.Override) error
- func (db *DB) SelectedServer(ctx context.Context) (plex.Server, bool, error)
- func (db *DB) SetDefaultClipProfile(ctx context.Context, id string) error
- func (db *DB) SetUserRole(ctx context.Context, plexUserID int, role string) error
- func (db *DB) Setting(ctx context.Context, name string) (string, bool, error)
- func (db *DB) StorageUsed(ctx context.Context, owner int) (int64, error)
- func (db *DB) SweepRenderLog(ctx context.Context, before time.Time) (int64, error)
- func (db *DB) TouchLogin(ctx context.Context, plexUserID int, username string) error
- func (db *DB) UsageByOwner(ctx context.Context, since time.Time) (map[int]UserUsage, error)
- func (db *DB) UserLimits(ctx context.Context, plexUserID int) (quota.Override, error)
- func (db *DB) UserRole(ctx context.Context, plexUserID int) (string, bool, error)
- func (db *DB) Usernames(ctx context.Context) (map[int]string, error)
- type SessionStore
- func (*SessionStore) Close() error
- func (store *SessionStore) Delete(key string) error
- func (store *SessionStore) DeleteWithContext(ctx context.Context, key string) error
- func (store *SessionStore) Get(key string) ([]byte, error)
- func (store *SessionStore) GetWithContext(ctx context.Context, key string) ([]byte, error)
- func (store *SessionStore) Reset() error
- func (store *SessionStore) ResetWithContext(ctx context.Context) error
- func (store *SessionStore) Set(key string, val []byte, exp time.Duration) error
- func (store *SessionStore) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error
- func (store *SessionStore) Sweep(ctx context.Context) (int64, error)
- type UserRecord
- type UserUsage
Constants ¶
const ( // BackendSQLite is the default libsql/SQLite backend. BackendSQLite = "sqlite" // BackendPostgres is the Postgres-protocol backend. BackendPostgres = "postgres" // BackendPgx is an alias for the Postgres-protocol backend. BackendPgx = "pgx" )
const ( // RoleOwner is the installation's owner. RoleOwner = "owner" // RoleAdmin manages the installation on the owner's behalf. RoleAdmin = "admin" // RoleMember uses the installation. RoleMember = "member" // RolePending is reported for an account an admin has not approved. RolePending = "pending" )
Roles a user is stored or reported with.
Variables ¶
var ( // ErrClipProfileNotFound is returned when a clip profile row does not exist. ErrClipProfileNotFound = errors.New("clip profile not found") // ErrLastClipProfile is returned when deleting the only remaining profile. ErrLastClipProfile = errors.New("cannot delete the last clip profile") // ErrDuplicateClipProfileName is returned when another profile already // has the name, which the name column keeps unique. ErrDuplicateClipProfileName = errors.New("a clip profile with that name already exists") )
var ErrClipNotFound = errors.New("clip not found")
ErrClipNotFound is returned when a clip row does not exist.
var ErrUserNotFound = errors.New("user not found")
ErrUserNotFound is returned for a change to a user that is not stored, or to the owner, whom no change on the users page touches.
Functions ¶
This section is empty.
Types ¶
type ClipProfile ¶
type ClipProfile struct {
ID string
Name string
CRF int
Preset string
AudioKbps int
MaxWidth int
IsDefault bool
// KeepHDR reports whether a video clip rendered with this profile keeps HDR.
KeepHDR bool
// CopyVideo reports whether a video clip rendered with this profile copies
// the source's video when it already fits the profile.
CopyVideo bool
CreatedAt time.Time
UpdatedAt time.Time
}
ClipProfile is a user-managed clip encode profile.
func (ClipProfile) QualityPreset ¶
func (profile ClipProfile) QualityPreset() clip.QualityPreset
QualityPreset returns ffmpeg settings for a stored profile.
Returns:
- preset: The encoder settings this profile describes.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents a database connection.
func New ¶
New creates a new SQLite database instance.
Parameters:
- dbPath: Path to the SQLite database file.
Returns:
- db: A migrated database handle.
- err: Non-nil when the database cannot be opened or migrated.
func NewFromConfig ¶
NewFromConfig constructs the database backend selected by configuration.
Parameters:
- cfg: The loaded configuration naming the backend.
Returns:
- db: A migrated database handle.
- err: Non-nil when the backend is unknown, unconfigured, or cannot be opened.
func (*DB) AddPendingUser ¶ added in v0.4.0
AddPendingUser records a Plex account that waits for an admin's approval.
An account already stored keeps its row.
Parameters:
- ctx: Request scope for the write.
- plexUserID: Plex account id asking to use the installation.
- username: Plex username of that account.
Returns:
- err: Non-nil when the write fails.
func (*DB) ApproveUser ¶ added in v0.4.0
ApproveUser lets a pending account use the installation as a member.
Parameters:
- ctx: Request scope for the write.
- plexUserID: The pending account.
Returns:
- err: ErrUserNotFound when no pending account is stored under the id, otherwise the write failure.
func (*DB) ClaimOwner ¶ added in v0.4.0
ClaimOwner records a Plex account as the owner unless one is stored already.
The unique owner index decides a race between two sign-ins, so exactly one of them reports the claim. The claim also takes over every clip that belongs to nobody, such as the clips of an installation upgraded from a release without owners, in the same transaction.
Parameters:
- ctx: Request scope for the write.
- plexUserID: Plex account id claiming the installation.
- username: Plex username of that account.
Returns:
- claimed: True when this call stored the owner.
- err: Non-nil when the write fails.
func (*DB) ClearLegacyTokens ¶ added in v0.4.0
ClearLegacyTokens deletes every row in plex_tokens.
Parameters:
- ctx: Request scope for the delete.
Returns:
- err: Non-nil when the delete fails.
func (*DB) ClearSelectedServer ¶ added in v0.4.0
ClearSelectedServer forgets the persisted Plex server.
Parameters:
- ctx: Request scope for the delete.
Returns:
- err: Non-nil when the delete fails.
func (*DB) Close ¶
Close closes the database connection.
Returns:
- err: Non-nil when the connection cannot be closed.
func (*DB) Conn ¶
Conn returns the underlying database connection.
Returns:
- conn: The pooled connection this database queries through.
func (*DB) DefaultClipProfile ¶
func (db *DB) DefaultClipProfile(ctx context.Context) (ClipProfile, error)
DefaultClipProfile returns the profile marked default.
A profile is always marked while any is stored, because the last profile cannot be deleted and a delete assigns a new default.
Parameters:
- ctx: Request scope for the read.
Returns:
- profile: The default profile.
- err: ErrClipProfileNotFound when none is marked, or the read failure.
func (*DB) DeleteClip ¶
DeleteClip removes a clip row.
Parameters:
- ctx: Request scope for the write.
- id: Identifier of the clip to remove.
Returns:
- err: Non-nil when the row cannot be deleted.
func (*DB) DeleteClipProfile ¶
DeleteClipProfile removes a profile and keeps a default assigned.
Parameters:
- ctx: Request scope for the write.
- id: Identifier of the profile to remove.
Returns:
- err: ErrLastClipProfile when it is the only profile, otherwise the write failure.
func (*DB) GetClip ¶
GetClip loads a job by ID.
Parameters:
- ctx: Request scope for the read.
- id: Identifier of the job to load.
Returns:
- job: The stored render.
- err: ErrClipNotFound when no row matches, otherwise the read failure.
func (*DB) GetClipProfile ¶
GetClipProfile loads a clip profile by ID.
Parameters:
- ctx: Request scope for the read.
- id: Identifier of the profile to load.
Returns:
- profile: The stored clip profile.
- err: ErrClipProfileNotFound when no row matches, otherwise the read failure.
func (*DB) GlobalLimits ¶ added in v0.4.0
GlobalLimits returns the render limits every member has.
A limit never saved takes its default.
Parameters:
- ctx: Request scope for the reads.
Returns:
- limits: The global limits.
- err: Non-nil when a setting cannot be read or is not a number.
func (*DB) HasOwner ¶ added in v0.4.0
HasOwner reports whether a Plex account has claimed this installation.
Parameters:
- ctx: Request scope for the read.
Returns:
- owned: True when an owner is stored.
- err: Non-nil when the read fails.
func (*DB) LegacyToken ¶ added in v0.4.0
LegacyToken returns the newest token in plex_tokens.
It names the account that may claim an installation without an owner.
Parameters:
- ctx: Request scope for the read.
Returns:
- token: The newest stored token, or empty when none is stored.
- err: Non-nil when the read fails.
func (*DB) ListClipProfiles ¶
func (db *DB) ListClipProfiles(ctx context.Context) ([]ClipProfile, error)
ListClipProfiles returns clip profiles with the default first.
Parameters:
- ctx: Request scope for the read.
Returns:
- profiles: The stored clip profiles.
- err: Non-nil when the rows cannot be read.
func (*DB) ListClips ¶
ListClips returns every job ordered by creation time, newest first.
Parameters:
- ctx: Request scope for the read.
Returns:
- jobs: The stored renders.
- err: Non-nil when the rows cannot be read.
func (*DB) ListClipsForMedia ¶
ListClipsForMedia returns the jobs created from a media item.
Parameters:
- ctx: Request scope for the read.
- mediaID: Identifier of the source media item.
Returns:
- jobs: The renders made from that item.
- err: Non-nil when the rows cannot be read.
func (*DB) ListPendingClips ¶
ListPendingClips returns jobs that still need processing.
Parameters:
- ctx: Request scope for the read.
Returns:
- jobs: The pending and processing renders, oldest first.
- err: Non-nil when the rows cannot be read.
func (*DB) ListUsers ¶ added in v0.4.0
func (db *DB) ListUsers(ctx context.Context) ([]UserRecord, error)
ListUsers returns every stored user, the owner first, then by when each first signed in.
Parameters:
- ctx: Request scope for the read.
Returns:
- users: Every user.
- err: Non-nil when the read fails.
func (*DB) RecordRender ¶ added in v0.4.0
RecordRender logs a render a user started, for the rolling weekly limit.
Parameters:
- ctx: Request scope for the write.
- owner: The user's plex.tv id.
- at: When the render started.
Returns:
- err: Non-nil when the write fails.
func (*DB) RejectUser ¶ added in v0.4.0
RejectUser deletes an account still waiting for approval.
An account approved in the meantime is left alone.
Parameters:
- ctx: Request scope for the write.
- plexUserID: The pending account.
Returns:
- err: ErrUserNotFound when no pending account is stored under the id, otherwise the write failure.
func (*DB) RemoveUser ¶ added in v0.4.0
RemoveUser deletes a user other than the owner.
The user's sessions end on their next request, and their clips are kept.
Parameters:
- ctx: Request scope for the write.
- plexUserID: The user.
Returns:
- err: ErrUserNotFound when no user other than the owner is stored under the id, otherwise the write failure.
func (*DB) RendersSince ¶ added in v0.4.0
func (db *DB) RendersSince( ctx context.Context, owner int, since time.Time, ) (int, time.Time, error)
RendersSince counts the renders a user started since a time.
Parameters:
- ctx: Request scope for the read.
- owner: The user's plex.tv id.
- since: The start of the span counted.
Returns:
- started: How many renders started in the span.
- oldest: When the oldest of them started, zero when none did.
- err: Non-nil when the read fails.
func (*DB) ResetOwner ¶ added in v0.4.0
ResetOwner deletes every user, the selected server, and the rows in plex_tokens, so the next Plex account to sign in claims the installation and picks its own server.
The members and admins go too, because they were admitted to the old owner's server.
Parameters:
- ctx: Request scope for the transaction.
Returns:
- removed: True when an owner was stored.
- err: Non-nil when a delete or the commit fails.
func (*DB) SaveClip ¶
SaveClip inserts or replaces a job.
The owner is written only when the row is inserted, so saving a clip again never moves it to another account.
Parameters:
- ctx: Request scope for the write.
- job: The render to persist.
Returns:
- err: Non-nil when the row cannot be written.
func (*DB) SaveClipProfile ¶
func (db *DB) SaveClipProfile(ctx context.Context, profile ClipProfile) error
SaveClipProfile inserts or replaces a clip profile.
Parameters:
- ctx: Request scope for the write.
- profile: The clip profile to persist.
Returns:
- err: Non-nil when the row cannot be written or the default cannot be assigned.
func (*DB) SaveGlobalLimits ¶ added in v0.4.0
SaveGlobalLimits stores the render limits every member has.
Parameters:
- ctx: Request scope for the writes.
- limits: The global limits, where zero sets no limit.
Returns:
- err: Non-nil when a setting cannot be written.
func (*DB) SaveSelectedServer ¶
SaveSelectedServer upserts the single selected Plex server.
Parameters:
- ctx: Request scope for the write.
- server: The Plex server the user selected.
Returns:
- err: Non-nil when the row cannot be written.
func (*DB) SaveSetting ¶ added in v0.4.0
SaveSetting upserts one value this installation persists about itself.
Parameters:
- ctx: Request scope for the write.
- name: Setting to write.
- value: Value to store.
Returns:
- err: Non-nil when the row cannot be written.
func (*DB) SaveUserLimits ¶ added in v0.4.0
SaveUserLimits stores one user's own render limits.
Parameters:
- ctx: Request scope for the write.
- plexUserID: The user, other than the owner.
- override: The user's limits, where nil takes the global value.
Returns:
- err: ErrUserNotFound when no user other than the owner is stored under the id, otherwise the write failure.
func (*DB) SelectedServer ¶
SelectedServer loads the persisted Plex server, if any.
Parameters:
- ctx: Request scope for the read.
Returns:
- server: The stored server, or the zero value when none is stored.
- found: True when a server row was present.
- err: Non-nil when the read fails.
func (*DB) SetDefaultClipProfile ¶
SetDefaultClipProfile marks one profile as the default.
Parameters:
- ctx: Request scope for the write.
- id: Identifier of the profile to mark.
Returns:
- err: Non-nil when the profile is unknown or the flag cannot be assigned.
func (*DB) SetUserRole ¶ added in v0.4.0
SetUserRole makes an approved member an admin, or an admin a member.
The user must still hold the other role when the row is written, so a change checked against one role never lands on a user whose role has changed since.
Parameters:
- ctx: Request scope for the write.
- plexUserID: The user.
- role: RoleAdmin or RoleMember.
Returns:
- err: ErrUserNotFound when no approved user holding the other role is stored under the id, otherwise the write failure.
func (*DB) Setting ¶ added in v0.4.0
Setting reads one value this installation persisted about itself.
Parameters:
- ctx: Request scope for the read.
- name: Setting to read.
Returns:
- value: The stored value, or empty when none is stored.
- found: True when the setting is stored.
- err: Non-nil when the read fails.
func (*DB) StorageUsed ¶ added in v0.4.0
StorageUsed returns how many bytes a user's clips take.
Parameters:
- ctx: Request scope for the read.
- owner: The user's plex.tv id.
Returns:
- used: The bytes the user's clip files take.
- err: Non-nil when the read fails.
func (*DB) SweepRenderLog ¶ added in v0.4.0
SweepRenderLog deletes the render log rows older than a time.
Only rows the weekly limit no longer counts are deleted, so the log keeps no more than one window of renders.
Parameters:
- ctx: Request scope for the delete.
- before: The start of the span the limit counts.
Returns:
- removed: How many rows were deleted.
- err: Non-nil when the delete fails.
func (*DB) TouchLogin ¶ added in v0.4.0
TouchLogin records a sign-in, refreshing the username Plex reported.
Parameters:
- ctx: Request scope for the write.
- plexUserID: Plex account id that signed in.
- username: Plex username of that account.
Returns:
- err: Non-nil when the write fails.
func (*DB) UsageByOwner ¶ added in v0.4.0
UsageByOwner returns what every user's renders and clips come to, in two grouped reads.
Parameters:
- ctx: Request scope for the reads.
- since: The start of the span renders are counted over.
Returns:
- usage: Each user's usage by plex.tv id, absent for a user with neither renders in the span nor clips.
- err: Non-nil when a read fails.
func (*DB) UserLimits ¶ added in v0.4.0
UserLimits returns one user's own render limits.
Parameters:
- ctx: Request scope for the read.
- plexUserID: The user.
Returns:
- override: The user's limits, all nil for a user not stored.
- err: Non-nil when the read fails.
func (*DB) UserRole ¶ added in v0.4.0
UserRole returns the role of the user behind a Plex account.
Parameters:
- ctx: Request scope for the read.
- plexUserID: Plex account id to look up.
Returns:
- role: The stored role, or empty when no user is stored.
- found: True when the account belongs to a user.
- err: Non-nil when the read fails.
func (*DB) Usernames ¶ added in v0.4.0
Usernames returns every approved user's Plex username by plex.tv id.
An account still waiting for approval is left out, so it neither turns on sharing nor names a removed member who asked again.
Parameters:
- ctx: Request scope for the read.
Returns:
- names: Each approved user's username.
- err: Non-nil when the read fails.
type SessionStore ¶ added in v0.4.0
type SessionStore struct {
// contains filtered or unexported fields
}
SessionStore keeps web sessions in the database.
It satisfies the Fiber storage interface.
func NewSessionStore ¶ added in v0.4.0
func NewSessionStore(db *DB) *SessionStore
NewSessionStore creates a session store backed by the database.
Parameters:
- db: Database the sessions live in.
Returns:
- store: A ready-to-use session store.
func (*SessionStore) Close ¶ added in v0.4.0
func (*SessionStore) Close() error
Close releases nothing, because the database belongs to its owner.
Returns:
- err: Always nil.
func (*SessionStore) Delete ¶ added in v0.4.0
func (store *SessionStore) Delete(key string) error
Delete removes a session.
Parameters:
- key: Session id.
Returns:
- err: Non-nil when the delete fails.
func (*SessionStore) DeleteWithContext ¶ added in v0.4.0
func (store *SessionStore) DeleteWithContext(ctx context.Context, key string) error
DeleteWithContext removes a session.
Parameters:
- ctx: Request scope for the delete.
- key: Session id.
Returns:
- err: Non-nil when the delete fails.
func (*SessionStore) Get ¶ added in v0.4.0
func (store *SessionStore) Get(key string) ([]byte, error)
Get loads a session.
Parameters:
- key: Session id.
Returns:
- data: The stored session, or nil when it is missing or expired.
- err: Non-nil when the read fails.
func (*SessionStore) GetWithContext ¶ added in v0.4.0
GetWithContext loads a session.
Parameters:
- ctx: Request scope for the read.
- key: Session id.
Returns:
- data: The stored session, or nil when it is missing or expired.
- err: Non-nil when the read fails.
func (*SessionStore) Reset ¶ added in v0.4.0
func (store *SessionStore) Reset() error
Reset removes every session.
Returns:
- err: Non-nil when the delete fails.
func (*SessionStore) ResetWithContext ¶ added in v0.4.0
func (store *SessionStore) ResetWithContext(ctx context.Context) error
ResetWithContext removes every session.
Parameters:
- ctx: Request scope for the delete.
Returns:
- err: Non-nil when the delete fails.
func (*SessionStore) Set ¶ added in v0.4.0
Set stores a session.
Parameters:
- key: Session id.
- val: Encoded session.
- exp: Lifetime of the session, zero for none.
Returns:
- err: Non-nil when the write fails.
func (*SessionStore) SetWithContext ¶ added in v0.4.0
func (store *SessionStore) SetWithContext( ctx context.Context, key string, val []byte, exp time.Duration, ) error
SetWithContext stores a session.
Parameters:
- ctx: Request scope for the write.
- key: Session id.
- val: Encoded session.
- exp: Lifetime of the session, zero for none.
Returns:
- err: Non-nil when the write fails.
type UserRecord ¶ added in v0.4.0
type UserRecord struct {
// CreatedAt is when the account first signed in.
CreatedAt time.Time
// LastLoginAt is when it last signed in, zero when it never did.
LastLoginAt time.Time
// Limits are the user's own render limits.
Limits quota.Override
// Username is the Plex username.
Username string
// Role is owner, admin, member, or pending.
Role string
// PlexID is the plex.tv account id.
PlexID int
}
UserRecord is one user as the users page lists them.