membership

package
v0.0.0-...-905e761 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2024 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCheckStaleOptions = &CheckStaleOptions{
	StaleThreshold: time.Hour * 12,
	MaxWorkers:     100,
}

Functions

func CheckStale

func CheckStale(
	ctx context.Context,
	db *ent.Client,
	youtubeConfig *oauth2.Config,
	options *CheckStaleOptions,
) error

func HookMembershipChanges

func HookMembershipChanges(db *ent.Client, handler ChangeHandler)

HookMembershipChanges uses ent hooks to listen to relevant changes to UserMembership ents.

func NewQueuedChangeHandler

func NewQueuedChangeHandler(bufferSize int) (*QueuedChangeHandler, ChangeHandler)

NewQueuedChangeHandler creates a QueuedChangeHandler that puts UserMembership ent IDs in two separate, buffered channels that can be exhausted when "safe" to do so. Use the second argument in HookMembershipChanges.

TODO: notes on how to wrap this properly with a mutex

func PerformCheckBatches

func PerformCheckBatches(
	ctx context.Context,
	db *ent.Client,
	discordConfig, ytConfig *oauth2.Config,
	userDeleteChan chan<- uint64,
	opts *PerformCheckOpts,
) error

func RefreshAllUserGuildEdges

func RefreshAllUserGuildEdges(ctx context.Context, db *ent.Client, discordConfig *oauth2.Config) ([]uint64, int, error)

RefreshAllUserGuildEdges refreshes guild edges for all registered users. Returns a slice of userIDs that could not be refreshed and a count of all users.

func RefreshStaleUserGuildEdges

func RefreshStaleUserGuildEdges(ctx context.Context, db *ent.Client, discordConfig *oauth2.Config, staleThreshold time.Duration) ([]uint64, int, error)

RefreshStaleUserGuildEdgesrefreshes guild edges for all registered users below a freshness threshold. Returns a slice of userIDs that could not be refreshed and a count of all users.

func RefreshUserGuildEdges

func RefreshUserGuildEdges(
	ctx context.Context,
	db *ent.Client,
	token *oauth2.Token,
	userID uint64,
) (added []uint64, removed []uint64, err error)

Refreshes guilds for all registered users.

func SaveMemberships

func SaveMemberships(
	ctx context.Context,
	db *ent.Client,
	userID uint64,
	results *CheckResultSet,
) (err error)

SaveMemberships maintains UserMembership objects and YouTube association info, but not its GuildRole edges. It also sets User.LastCheck to the current time after effecting changes.

Types

type ChangeHandler

type ChangeHandler interface {
	GainedMembership(userMembershipID int)
	LostMembership(userMembershipID int)
	SetChangeReason(reason string)
}

ChangeHandler is implemented by things that react to membership changes.

type CheckForUserOptions

type CheckForUserOptions struct {
	// Specify ChannelIDs to restrict checks to these channels.
	ChannelIDs []string
	// CheckDisabledChannels forces a check on channels that have been disabled *if ChannelIDs is not empty*
	CheckDisabledChannels bool
}

type CheckResult

type CheckResult struct {
	ChannelID string
	Time      time.Time
}

type CheckResultSet

type CheckResultSet struct {
	// Gained contains memberships newly gained.
	Gained []CheckResult `json:",omitempty"`
	// Retained contains memberships that have been kept and re-validated.
	Retained []CheckResult `json:",omitempty"`
	// Lost contains memberships newly lost.
	Lost []CheckResult `json:",omitempty"`
	// Not contains memberships that this user does not have but did not newly lose.
	Not []CheckResult `json:",omitempty"`

	// Disabled contains channel IDs that were skipped due to disabled membership checks.
	DisabledChannels []string

	// YouTubeTokenInvalid notes that check failures are due to token expiry or revocation.
	YouTubeTokenInvalid bool
}

func CheckForUser

func CheckForUser(
	ctx context.Context, db *ent.Client,
	youtubeConfig *oauth2.Config,
	userID uint64,
	options *CheckForUserOptions,
) (results *CheckResultSet, err error)

func (*CheckResultSet) HasResults

func (c *CheckResultSet) HasResults() bool

func (CheckResultSet) IsMember

func (c CheckResultSet) IsMember(channelID string) bool

type CheckStaleOptions

type CheckStaleOptions struct {
	// StaleThreshold is used in a <= comparison to the last stored membership check time.
	StaleThreshold time.Duration
	// UserPredicates overrides predicates that consider a user's memberships stale.
	UserPredicates []predicate.User
	// AdditionalUserPredicates allows specifying additional constraints on user checks.
	AdditionalUserPredicates []predicate.User
	// NoSave skips saving UserMembership edges.
	NoSave bool
	// MaxWorkers sets the maximum amount of users to check and process simultaneously.
	MaxWorkers int
	// The maximum amount of users to check
	TotalLimit int
}

type PerformCheckOpts

type PerformCheckOpts struct {
	BatchSize      int
	StaleThreshold time.Duration
	Enforce        bool
}

func DefaultPerformCheckOptions

func DefaultPerformCheckOptions() *PerformCheckOpts

type QueuedChangeHandler

type QueuedChangeHandler struct {
	// contains filtered or unexported fields
}

QueuedChangeHandler is a wrapper around a ChangeHandler that buffers membership changes to a channel.

(This is meant to be used by the Discord bot to apply its own membership checks.)

func (QueuedChangeHandler) GainedMembership

func (c QueuedChangeHandler) GainedMembership(userMembershipID int)

func (*QueuedChangeHandler) GetGained

func (q *QueuedChangeHandler) GetGained() []int

GetGained() returns a slice of queued-up integers.

func (*QueuedChangeHandler) GetLost

func (q *QueuedChangeHandler) GetLost() []int

GetGained() returns a slice of queued-up integers.

func (QueuedChangeHandler) LostMembership

func (c QueuedChangeHandler) LostMembership(userMembershipID int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL