state

package
v0.0.0-...-229e418 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package state provides persistent state management for the bot.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DMInfo

type DMInfo struct {
	SentAt      time.Time `json:"sent_at"`
	ChannelID   string    `json:"channel_id"`
	MessageID   string    `json:"message_id"`
	MessageText string    `json:"message_text"`
	LastState   string    `json:"last_state"` // PR state when DM was sent/updated
}

DMInfo stores DM message info for updating.

type DailyReportInfo

type DailyReportInfo struct {
	LastSentAt time.Time `json:"last_sent_at"`
	GuildID    string    `json:"guild_id"`
}

DailyReportInfo tracks daily report state for a user.

type FidoStore

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

FidoStore implements Store using fido with CloudRun backend.

Requires these Datastore databases (must be created before use):

  • discordian-threads: PR to Discord thread/message mapping
  • discordian-dms: DM message tracking
  • discordian-dmusers: DM user lists (prURL -> list of user IDs)
  • discordian-reports: Daily report tracking
  • discordian-pending: Pending DM queue
  • discordian-events: Event deduplication (persisted for cross-instance safety)
  • discordian-claims: Distributed claims (persisted for cross-instance coordination)
  • discordian-usermappings: GitHub username to Discord user ID mappings

func NewFidoStore

func NewFidoStore(ctx context.Context, opts ...FidoStoreOption) (*FidoStore, error)

NewFidoStore creates a new fido-backed store. Uses CloudRun backend which auto-detects environment. Use WithThreadStore, WithDMStore, etc. to inject custom stores for testing.

func (*FidoStore) ClaimDM

func (s *FidoStore) ClaimDM(ctx context.Context, userID, prURL string, ttl time.Duration) bool

ClaimDM attempts to claim a DM for sending. Returns true if the claim was successful, false if another instance already claimed it.

func (*FidoStore) ClaimThread

func (s *FidoStore) ClaimThread(ctx context.Context, owner, repo string, number int, channelID string, ttl time.Duration) bool

ClaimThread attempts to claim a thread for creation. Returns true if the claim was successful, false if another instance already claimed it.

func (*FidoStore) Cleanup

func (s *FidoStore) Cleanup(ctx context.Context) error

Cleanup removes expired entries.

func (*FidoStore) Close

func (s *FidoStore) Close() error

Close releases resources.

func (*FidoStore) DMInfo

func (s *FidoStore) DMInfo(ctx context.Context, userID, prURL string) (DMInfo, bool)

DMInfo retrieves DM info for a user/PR.

func (*FidoStore) DailyReportInfo

func (s *FidoStore) DailyReportInfo(ctx context.Context, userID string) (DailyReportInfo, bool)

DailyReportInfo retrieves daily report info for a user.

func (*FidoStore) ListDMUsers

func (s *FidoStore) ListDMUsers(ctx context.Context, prURL string) []string

ListDMUsers returns all user IDs who received DMs for a PR.

func (*FidoStore) ListUserMappings

func (*FidoStore) ListUserMappings(_ context.Context, guildID string) []UserMappingInfo

ListUserMappings returns all user mappings for a guild.

func (*FidoStore) MarkProcessed

func (s *FidoStore) MarkProcessed(ctx context.Context, eventKey string, ttl time.Duration) error

MarkProcessed marks an event as processed.

func (*FidoStore) PendingDMs

func (s *FidoStore) PendingDMs(ctx context.Context, before time.Time) ([]*PendingDM, error)

PendingDMs returns all pending DMs that should be sent before the given time.

func (*FidoStore) QueuePendingDM

func (s *FidoStore) QueuePendingDM(ctx context.Context, dm *PendingDM) error

QueuePendingDM adds a pending DM to the queue.

func (*FidoStore) RemovePendingDM

func (s *FidoStore) RemovePendingDM(ctx context.Context, id string) error

RemovePendingDM removes a pending DM from the queue.

func (*FidoStore) SaveDMInfo

func (s *FidoStore) SaveDMInfo(ctx context.Context, userID, prURL string, info DMInfo) error

SaveDMInfo stores DM info for a user/PR.

func (*FidoStore) SaveDailyReportInfo

func (s *FidoStore) SaveDailyReportInfo(ctx context.Context, userID string, info DailyReportInfo) error

SaveDailyReportInfo stores daily report info for a user.

func (*FidoStore) SaveThread

func (s *FidoStore) SaveThread(ctx context.Context, owner, repo string, number int, channelID string, info ThreadInfo) error

SaveThread stores thread info for a PR.

func (*FidoStore) SaveUserMapping

func (s *FidoStore) SaveUserMapping(ctx context.Context, guildID string, info UserMappingInfo) error

SaveUserMapping stores user mapping info for a GitHub username.

func (*FidoStore) Thread

func (s *FidoStore) Thread(ctx context.Context, owner, repo string, number int, channelID string) (ThreadInfo, bool)

Thread retrieves thread info for a PR.

func (*FidoStore) UserMapping

func (s *FidoStore) UserMapping(ctx context.Context, guildID, gitHubUsername string) (UserMappingInfo, bool)

UserMapping retrieves user mapping info for a GitHub username in a guild.

func (*FidoStore) WasProcessed

func (s *FidoStore) WasProcessed(ctx context.Context, eventKey string) bool

WasProcessed checks if an event was already processed.

type FidoStoreOption

type FidoStoreOption func(*fidoStoreOptions)

FidoStoreOption configures a FidoStore.

func WithClaimStore

func WithClaimStore(s fido.Store[string, time.Time]) FidoStoreOption

WithClaimStore sets a custom store for claim data.

func WithDMStore

func WithDMStore(s fido.Store[string, DMInfo]) FidoStoreOption

WithDMStore sets a custom store for DM data.

func WithDMUserStore

func WithDMUserStore(s fido.Store[string, dmUserList]) FidoStoreOption

WithDMUserStore sets a custom store for DM user list data.

func WithEventStore

func WithEventStore(s fido.Store[string, time.Time]) FidoStoreOption

WithEventStore sets a custom store for event deduplication data.

func WithPendingStore

func WithPendingStore(s fido.Store[string, pendingDMQueue]) FidoStoreOption

WithPendingStore sets a custom store for pending DM data.

func WithReportStore

func WithReportStore(s fido.Store[string, DailyReportInfo]) FidoStoreOption

WithReportStore sets a custom store for daily report data.

func WithThreadStore

func WithThreadStore(s fido.Store[string, ThreadInfo]) FidoStoreOption

WithThreadStore sets a custom store for thread data.

func WithUserMappingStore

func WithUserMappingStore(s fido.Store[string, UserMappingInfo]) FidoStoreOption

WithUserMappingStore sets a custom store for user mapping data.

type MemoryStore

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

MemoryStore provides an in-memory implementation of Store.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory store.

func (*MemoryStore) ClaimDM

func (s *MemoryStore) ClaimDM(ctx context.Context, userID, prURL string, ttl time.Duration) bool

ClaimDM attempts to claim a DM for sending. Returns true if the claim was successful, false if another goroutine already claimed it.

func (*MemoryStore) ClaimThread

func (s *MemoryStore) ClaimThread(ctx context.Context, owner, repo string, number int, channelID string, ttl time.Duration) bool

ClaimThread attempts to claim a thread for creation. Returns true if the claim was successful, false if another goroutine already claimed it.

func (*MemoryStore) Cleanup

func (s *MemoryStore) Cleanup(ctx context.Context) error

Cleanup removes old entries from the store.

func (*MemoryStore) Close

func (*MemoryStore) Close() error

Close closes the store (no-op for memory store).

func (*MemoryStore) DMInfo

func (s *MemoryStore) DMInfo(ctx context.Context, userID, prURL string) (DMInfo, bool)

DMInfo returns DM info for a user/PR combination.

func (*MemoryStore) DailyReportInfo

func (s *MemoryStore) DailyReportInfo(_ context.Context, userID string) (DailyReportInfo, bool)

DailyReportInfo returns daily report info for a user.

func (*MemoryStore) ListDMUsers

func (s *MemoryStore) ListDMUsers(_ context.Context, prURL string) []string

ListDMUsers returns all user IDs who received DMs for a PR.

func (*MemoryStore) ListUserMappings

func (s *MemoryStore) ListUserMappings(ctx context.Context, guildID string) []UserMappingInfo

ListUserMappings returns all user mappings for a guild.

func (*MemoryStore) MarkProcessed

func (s *MemoryStore) MarkProcessed(_ context.Context, eventKey string, _ time.Duration) error

MarkProcessed marks an event as processed.

func (*MemoryStore) PendingDMs

func (s *MemoryStore) PendingDMs(ctx context.Context, before time.Time) ([]*PendingDM, error)

PendingDMs returns DMs scheduled before the given time.

func (*MemoryStore) QueuePendingDM

func (s *MemoryStore) QueuePendingDM(ctx context.Context, dm *PendingDM) error

QueuePendingDM adds a DM to the pending queue.

func (*MemoryStore) RemovePendingDM

func (s *MemoryStore) RemovePendingDM(_ context.Context, id string) error

RemovePendingDM removes a DM from the queue.

func (*MemoryStore) SaveDMInfo

func (s *MemoryStore) SaveDMInfo(_ context.Context, userID, prURL string, info DMInfo) error

SaveDMInfo saves DM info.

func (*MemoryStore) SaveDailyReportInfo

func (s *MemoryStore) SaveDailyReportInfo(_ context.Context, userID string, info DailyReportInfo) error

SaveDailyReportInfo saves daily report info for a user.

func (*MemoryStore) SaveThread

func (s *MemoryStore) SaveThread(ctx context.Context, owner, repo string, number int, channelID string, info ThreadInfo) error

SaveThread saves thread info for a PR.

func (*MemoryStore) SaveUserMapping

func (s *MemoryStore) SaveUserMapping(ctx context.Context, guildID string, info UserMappingInfo) error

SaveUserMapping saves user mapping info for a GitHub username.

func (*MemoryStore) Stats

func (s *MemoryStore) Stats() StoreStats

Stats returns current store statistics.

func (*MemoryStore) Thread

func (s *MemoryStore) Thread(ctx context.Context, owner, repo string, number int, channelID string) (ThreadInfo, bool)

Thread returns thread info for a PR in a channel.

func (*MemoryStore) UserMapping

func (s *MemoryStore) UserMapping(ctx context.Context, guildID, gitHubUsername string) (UserMappingInfo, bool)

UserMapping returns user mapping info for a GitHub username in a guild.

func (*MemoryStore) WasProcessed

func (s *MemoryStore) WasProcessed(ctx context.Context, eventKey string) bool

WasProcessed checks if an event was already processed.

type PendingDM

type PendingDM struct {
	SendAt      time.Time `json:"send_at"`
	CreatedAt   time.Time `json:"created_at"`
	ExpiresAt   time.Time `json:"expires_at"`
	ID          string    `json:"id"`
	UserID      string    `json:"user_id"`
	PRURL       string    `json:"pr_url"`
	MessageText string    `json:"message_text"`
	GuildID     string    `json:"guild_id"`
	Org         string    `json:"org"`
	RetryCount  int       `json:"retry_count"`
}

PendingDM represents a scheduled DM notification.

type Store

type Store interface {
	// Thread/post tracking - maps PR to Discord thread/message
	Thread(ctx context.Context, owner, repo string, number int, channelID string) (ThreadInfo, bool)
	SaveThread(ctx context.Context, owner, repo string, number int, channelID string, info ThreadInfo) error

	// Distributed claim mechanism to prevent duplicate thread/message creation across instances
	// Returns true if claim was successful, false if another instance already claimed it
	ClaimThread(ctx context.Context, owner, repo string, number int, channelID string, ttl time.Duration) bool

	// DM tracking
	DMInfo(ctx context.Context, userID, prURL string) (DMInfo, bool)
	SaveDMInfo(ctx context.Context, userID, prURL string, info DMInfo) error
	ListDMUsers(ctx context.Context, prURL string) []string // Returns all user IDs who received DMs for this PR

	// Distributed claim mechanism for DMs
	ClaimDM(ctx context.Context, userID, prURL string, ttl time.Duration) bool

	// Event deduplication
	WasProcessed(ctx context.Context, eventKey string) bool
	MarkProcessed(ctx context.Context, eventKey string, ttl time.Duration) error

	// Pending DM queue
	QueuePendingDM(ctx context.Context, dm *PendingDM) error
	PendingDMs(ctx context.Context, before time.Time) ([]*PendingDM, error)
	RemovePendingDM(ctx context.Context, id string) error

	// Daily report tracking
	DailyReportInfo(ctx context.Context, userID string) (DailyReportInfo, bool)
	SaveDailyReportInfo(ctx context.Context, userID string, info DailyReportInfo) error

	// User mapping tracking (GitHub username <-> Discord user ID)
	UserMapping(ctx context.Context, guildID, gitHubUsername string) (UserMappingInfo, bool)
	SaveUserMapping(ctx context.Context, guildID string, info UserMappingInfo) error
	ListUserMappings(ctx context.Context, guildID string) []UserMappingInfo

	// Lifecycle
	Cleanup(ctx context.Context) error
	Close() error
}

Store provides persistent state operations.

type StoreStats

type StoreStats struct {
	Threads int
	DMs     int
	Events  int
	Pending int
}

StoreStats contains store statistics.

type ThreadInfo

type ThreadInfo struct {
	UpdatedAt   time.Time `json:"updated_at"`
	ThreadID    string    `json:"thread_id"`
	MessageID   string    `json:"message_id"`
	ChannelID   string    `json:"channel_id"`
	ChannelType string    `json:"channel_type"` // "forum" or "text"
	LastState   string    `json:"last_state"`
	MessageText string    `json:"message_text"`
}

ThreadInfo stores Discord thread/message info for a PR.

type UserMappingInfo

type UserMappingInfo struct {
	CreatedAt      time.Time `json:"created_at"`
	GitHubUsername string    `json:"github_username"`
	DiscordUserID  string    `json:"discord_user_id"`
	GuildID        string    `json:"guild_id"`
}

UserMappingInfo stores explicit GitHub-Discord user mappings.

Jump to

Keyboard shortcuts

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