engine

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package engine contains the subscription status core.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aggregate

func Aggregate(
	verdicts []domain.SourceVerdict,
	banned bool,
) (domain.EffectiveStatus, domain.AccessDecision)

Aggregate merges source verdicts into an effective access decision.

Types

type AlertStore

type AlertStore interface {
	Create(ctx context.Context, a store.AlertInput) (int64, error)
}

AlertStore is the operational alert surface needed by revocation.

type AuditStore

type AuditStore interface {
	Append(ctx context.Context, e store.AuditEntry) error
}

AuditStore is the audit repository surface needed by the engine.

type Effect

type Effect struct {
	TGID      int64
	Text      string
	ParseMode string
	Plain     bool
}

Effect is a post-commit side effect prepared by the engine.

type Engine

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

Engine applies subscription events and computes access status.

func New

func New(sources []SubscriptionSource, opts ...Option) *Engine

New returns a status engine.

func (*Engine) ApplyObservations

func (e *Engine) ApplyObservations(
	ctx context.Context,
	repos Store,
	tgID int64,
	verdicts []domain.SourceVerdict,
) error

ApplyObservations persists live source observations inside handleTx.

func (*Engine) ApplyRevocation added in v0.1.1

func (e *Engine) ApplyRevocation(
	ctx context.Context,
	repos Store,
	tgID int64,
	plan RevocationPlan,
) ([]Effect, error)

ApplyRevocation runs the APPLY phase of a revocation inside the caller's transaction, using tx-scoped repos only. It performs NO live source, Telegram, or pool access — repos.Members MUST be nil. It persists the observations captured at decide time (atomic with the revoke), then acts on the plan's decision. The caller must hold the same WithUserLock(tgID) that spanned RevocationDecision.

func (*Engine) HandleEvent

func (e *Engine) HandleEvent(
	ctx context.Context,
	repos Store,
	event domain.SubscriptionEvent,
) ([]Effect, error)

HandleEvent applies one normalized subscription event inside handleTx.

func (*Engine) LiveSnapshot

func (e *Engine) LiveSnapshot(
	ctx context.Context,
	repos Store,
	tgID int64,
) (Snapshot, error)

LiveSnapshot collects source verdicts outside handleTx and aggregates them.

func (*Engine) PersistedDecision

func (e *Engine) PersistedDecision(
	ctx context.Context,
	repos Store,
	tgID int64,
) (domain.AccessDecision, error)

PersistedDecision computes access from local state without network probes.

func (*Engine) RecomputeAccess

func (e *Engine) RecomputeAccess(
	ctx context.Context,
	repos Store,
	tgID int64,
) ([]Effect, error)

RecomputeAccess re-evaluates durable local access state after callers have persisted fresh observations.

func (*Engine) RevocationDecision added in v0.1.1

func (e *Engine) RevocationDecision(
	ctx context.Context,
	repos Store,
	tgID int64,
	reason string,
) (RevocationPlan, error)

RevocationDecision runs the DECIDE phase of a revocation: it probes live sources (LiveSnapshot) and, when the aggregated status is inactive, the live Telegram protection status of each eligible grant. It runs entirely on the pool with NO open transaction and performs NO writes. It does NOT take the per-user lock — the caller must already hold WithUserLock(tgID). Feed the returned plan to ApplyRevocation inside a transaction.

func (*Engine) RevokeNow

func (e *Engine) RevokeNow(
	ctx context.Context,
	repos Store,
	tgID int64,
	reason string,
) ([]Effect, error)

RevokeNow safely revokes current bot-admitted grants for a user.

func (*Engine) WithUserLock added in v0.1.1

func (e *Engine) WithUserLock(tgID int64, fn func() error) error

WithUserLock runs fn while holding the per-user serialization lock.

The lock is the same non-reentrant KeyedMutex used by RevokeNow, RecomputeAccess, ApplyObservations, and HandleEvent. Callers that split a revocation across a transaction boundary (decide on the pool, apply in a tx) must hold it across BOTH phases.

Non-reentrant contract: fn MUST NOT call RevokeNow, RecomputeAccess, ApplyObservations, or HandleEvent — they take this same lock and would deadlock. Inside fn use only the non-locking primitives RevocationDecision and ApplyRevocation.

type GrantStore

type GrantStore interface {
	ListEligibleForRevoke(ctx context.Context, tgID int64) ([]domain.AccessGrant, error)
	Revoke(ctx context.Context, tgID int64, resource domain.Resource, reason string) (bool, error)
}

GrantStore is the access grant surface needed by revocation.

type KeyedMutex

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

KeyedMutex serializes work by Telegram user ID.

func NewKeyedMutex

func NewKeyedMutex() *KeyedMutex

NewKeyedMutex returns an empty process-wide keyed mutex.

func (*KeyedMutex) Lock

func (m *KeyedMutex) Lock(key int64) func()

Lock locks one key and returns an unlock function.

type MemberChecker

type MemberChecker interface {
	GetChatMember(
		ctx context.Context,
		resource domain.Resource,
		tgID int64,
	) (*models.ChatMember, error)
}

MemberChecker reads live Telegram membership for protection checks.

type Option

type Option func(*Engine)

Option configures Engine.

func WithClock

func WithClock(now func() time.Time) Option

WithClock replaces time.Now for tests.

func WithRevocationConfig

func WithRevocationConfig(cfg RevocationConfig) Option

WithRevocationConfig sets expiry-mode behavior for recomputeAccess.

type OutboxStore

type OutboxStore interface {
	Enqueue(
		ctx context.Context,
		input store.AccessActionInput,
	) (domain.AccessAction, bool, error)
}

OutboxStore is the durable Telegram action surface needed by revocation.

type PlannedRevoke added in v0.1.1

type PlannedRevoke struct {
	Resource  domain.Resource
	UpdatedAt time.Time // identity token: the grant's updated_at at decide time
	Protected bool      // live isProtected verdict (creator/administrator)
}

PlannedRevoke is one revoke-eligible grant resolved during the decide phase.

type RevocationConfig

type RevocationConfig struct {
	ExpiryMode  string
	GracePeriod time.Duration
}

RevocationConfig controls expiry behavior.

type RevocationPlan added in v0.1.1

type RevocationPlan struct {
	TGID     int64
	Reason   string
	Decision domain.AccessDecision
	Verdicts []domain.SourceVerdict

	// Revokes is the per-grant revocation plan captured at decide time: for each
	// eligible grant, its resource, an identity token (UpdatedAt) proving which
	// grant instance was evaluated, and the live protection verdict. Apply
	// revokes a grant only when a currently-eligible grant matches this identity
	// and is unprotected; any grant that changed or appeared since decide is
	// skipped and the pending revocation is retried next pass.
	Revokes []PlannedRevoke
}

RevocationPlan is the output of the DECIDE phase of a revocation. It carries no live/pool/network handles, so ApplyRevocation can consume it inside a transaction. Produced by RevocationDecision (outside any tx, on the pool), applied by ApplyRevocation (inside a tx, tx-scoped repos only). The caller holds WithUserLock across both phases.

type RevocationStore

type RevocationStore interface {
	CreateIfAbsent(ctx context.Context, p domain.PendingRevocation) (bool, error)
	Get(ctx context.Context, tgID int64) (domain.PendingRevocation, bool, error)
	Delete(ctx context.Context, tgID int64) error
	MarkNotified(ctx context.Context, tgID int64) error
}

RevocationStore is the pending-revocation surface needed by the engine.

type Snapshot

type Snapshot struct {
	TGID     int64
	Verdicts []domain.SourceVerdict
	Decision domain.AccessDecision
}

Snapshot is a live, pre-transaction status snapshot.

type Store

type Store struct {
	Users         UserStore
	Subscriptions SubscriptionStore
	Audit         AuditStore
	Revocations   RevocationStore
	Whitelist     WhitelistStore
	Grants        GrantStore
	Outbox        OutboxStore
	Alerts        AlertStore
	Members       MemberChecker

	// OperatorLog, when set, receives durable operator events emitted in the
	// caller's transaction. It is optional: a nil writer disables the feed.
	OperatorLog *operatorlog.Writer
}

Store is the narrow repository bundle used by engine operations.

type SubscriptionSource

type SubscriptionSource interface {
	Platform() domain.Platform
	Verdict(ctx context.Context, tgID int64) (domain.SourceVerdict, error)
}

SubscriptionSource is the consumer-side source interface.

type SubscriptionStore

type SubscriptionStore interface {
	UpsertActive(ctx context.Context, s domain.Subscription) (int64, error)
	ExpireActive(
		ctx context.Context,
		tgID int64,
		platform domain.Platform,
		endedAt time.Time,
		signal string,
	) (bool, error)
	GetActive(
		ctx context.Context,
		tgID int64,
		platform domain.Platform,
	) (domain.Subscription, bool, error)
	ListActiveByUser(ctx context.Context, tgID int64) ([]domain.Subscription, error)
}

SubscriptionStore is the subscription repository surface needed by the engine.

type UserStore

type UserStore interface {
	Upsert(ctx context.Context, u domain.User) error
	Get(ctx context.Context, tgID int64) (domain.User, error)
}

UserStore is the user repository surface needed by the engine.

type WhitelistStore

type WhitelistStore interface {
	Has(ctx context.Context, tgID int64) (bool, error)
}

WhitelistStore is the whitelist repository surface needed by the engine.

Jump to

Keyboard shortcuts

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