store

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 16 Imported by: 1 module, 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 store is the Slack plugin's own database, in the state directory the host gives it: the conversations, threads, messages and users it read, the DM segment map and what the extraction keeps per unit.

Index

Constants

View Source
const (
	SlackThread = "thread"
	SlackDM     = "dm"
)

Object kinds of slack_objects.

View Source
const (
	SlackNew      = "new"
	SlackEdited   = "edited"
	SlackDeleted  = "deleted"
	SlackReaction = "reaction"
)

Change kinds of slack_changes.

View Source
const SlackDiscoverCursor = "discover"

SlackDiscoverCursor is the source's discovery cursor.

View Source
const TimeLayout = time.RFC3339Nano

TimeLayout is how the store writes a time.

Variables

View Source
var ErrUnknownSegment = errors.New("unknown segment")

Functions

func Tables

func Tables() []string

Tables are the store's tables, in schema order.

Types

type Change

type Change struct {
	Units []unit.Unit
	All   bool
	// Landed says the commit added a change for the extraction to read.
	Landed bool
}

Change is what one write transaction changed: the units whose reported facts it moved, or every unit when All.

type ChangeRow

type ChangeRow struct {
	Seq                        int64
	Conversation, TS, ThreadTS string
	Kind, EditedTS             string
	At                         time.Time
}

type ConversationRow

type ConversationRow struct {
	ID, Team, Name, Kind string
	IsMember, Archived   bool
	DMUser               string
	HWM                  string
	Admitted             bool
	UpdatedAt            time.Time
}

type DB

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

DB is the plugin's store. Every write runs under one lock, and each commit that changed units is heard by the OnChange listeners, in commit order.

func Open

func Open(ctx context.Context, dir string) (*DB, error)

Open opens dir's store, creating it with the plugin's schema, and imports the import.db the host's store migration exported once.

func OpenDB

func OpenDB(db *sql.DB) *DB

OpenDB is a store over an open database whose schema is current; tests open one in memory.

func (*DB) Close

func (db *DB) Close() error

func (*DB) Extraction

func (db *DB) Extraction() *ExtractionStore

func (*DB) OnChange

func (db *DB) OnChange(f func(Change))

OnChange makes f hear every later commit that changed units.

func (*DB) R

func (db *DB) R() Reader

R reads with one statement.

func (*DB) Read

func (db *DB) Read(ctx context.Context, fn func(tx *sql.Tx) error) error

Read runs fn in one read transaction, so every statement sees one snapshot; fn must not write.

func (*DB) Segments

func (db *DB) Segments() *SegmentStore

Segments is the segment map, whose Apply moves no draft until Rehoming gives it the host's.

func (*DB) Slack

func (db *DB) Slack() *SlackStore

func (*DB) Write

func (db *DB) Write(ctx context.Context, fn func(tx *Tx) error) (Change, error)

Write runs fn in one write transaction. After the commit, the listeners hear what fn touched; an error from fn rolls back and tells nobody.

type DraftHome

type DraftHome interface {
	RehomeDrafts(ctx context.Context, moves []src.UnitMove) (int, error)
}

DraftHome moves drafts between units on the host: the broker's Gates.RehomeDrafts.

type ExtractionHint

type ExtractionHint struct{ Mode, Context, Why string }

type ExtractionRun

type ExtractionRun struct {
	Unit    unit.Unit
	At      time.Time
	Model   string
	CostUSD *float64
	Outcome string
	Error   string
}

type ExtractionSave

type ExtractionSave struct {
	States map[unit.Unit]ExtractionState
	// Hints replace the hints of the items they name.
	Hints map[unit.Unit]map[string]ExtractionHint
}

ExtractionSave is one run's result: the cursors, briefs and hints of its units, in one transaction.

type ExtractionState

type ExtractionState struct {
	LastTS                                        string
	LastSeq                                       int64
	Brief, Verdict, VerdictWhy, ViewerRole, Topic string
	StartedByViewer, ClosurePosted, Watched       bool
	ClosureMarked                                 bool
	At                                            time.Time
}

ExtractionState is one unit's row. Watched is written only by SetWatched; ClosureMarked, a sent draft's unit.closed, never by Save.

Brief is the JSON of the brief the extraction last wrote of the unit; an empty Brief in a Save keeps the one stored.

type ExtractionStore

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

ExtractionStore holds what the Slack extraction job keeps per unit: its cursor and last answer, the hints of each item, the day's spend and a log of its calls.

func (*ExtractionStore) Day

func (s *ExtractionStore) Day(ctx context.Context, day string) (calls int, costUSD float64, err error)

Day is the spend of day ("2006-01-02"): calls made and their summed cost.

func (*ExtractionStore) Hints

Hints are u's item hints by item id.

func (*ExtractionStore) MarkClosed

func (s *ExtractionStore) MarkClosed(ctx context.Context, u unit.Unit, at time.Time) error

MarkClosed records a sent draft's unit.closed on u, which no extraction pass clears.

func (*ExtractionStore) MarkMigrated

func (s *ExtractionStore) MarkMigrated(ctx context.Context, name string, at time.Time) error

MarkMigrated records that the ledger migration name was applied at at.

func (*ExtractionStore) Migrated

func (s *ExtractionStore) Migrated(ctx context.Context, name string) (bool, error)

Migrated says whether the ledger migration name was applied.

func (*ExtractionStore) Record

func (*ExtractionStore) Save

func (*ExtractionStore) SetWatched

func (s *ExtractionStore) SetWatched(ctx context.Context, u unit.Unit, watched bool, at time.Time) error

func (*ExtractionStore) Spend

func (s *ExtractionStore) Spend(ctx context.Context, day string, costUSD float64) error

Spend counts one call of costUSD toward day.

func (*ExtractionStore) States

func (s *ExtractionStore) States(ctx context.Context, units []unit.Unit) (map[unit.Unit]ExtractionState, error)

States are the rows of units; a unit without one is missing from the map.

func (*ExtractionStore) Watched

func (s *ExtractionStore) Watched(ctx context.Context, provider unit.ProviderID) (map[unit.Unit]ExtractionState, error)

Watched are the rows of provider that SetWatched marked.

type FactsRows

type FactsRows struct {
	Team          string
	Conversations []ConversationRow
	Objects       []ObjectFacts
	Users         []UserRow
	Cursor        string // "" keeps the stored one
}

type FileRow

type FileRow struct {
	ID       string `json:"id"`
	Title    string `json:"title,omitempty"`
	Mimetype string `json:"mimetype,omitempty"`
}

type MembersRow added in v0.4.0

type MembersRow struct {
	Conversation string
	Members      []string
	Fetched      time.Time
	Failed       bool
}

MembersRow is a conversation's member user ids as conversations.members read them at Fetched; Failed says the read at Fetched failed, and Members is then empty.

type MessageQuery

type MessageQuery struct {
	TS       string
	Thread   string
	From, To string
	After    string
	Limit    int
	// Before keeps the messages anchored before it; Newest takes the Limit of the newest anchors, returned in ts order.
	Before string
	Newest bool
}

MessageQuery selects a conversation's messages by anchor: Thread names one thread, From and To an inclusive range (To "" open). After and Limit page. TS alone selects the one message at it.

type MessageRow

type MessageRow struct {
	Conversation, TS, ThreadTS, ParentUser string
	User, BotID, AppID, Username           string
	Subtype, Text, EditedTS                string
	Deleted                                bool
	Files                                  []FileRow
	Links, Mentions                        []string
	// Reactions nil was not read and leaves the stored set; non-nil, even
	// empty, is the full set and replaces it.
	Reactions  []ReactionRow
	Permalink  string
	Via        string
	ReceivedAt time.Time
	// Units are what a change of this message notes; not stored.
	Units []unit.Unit
}

func (MessageRow) Anchor

func (m MessageRow) Anchor() string

Anchor is the ts that places the message: its thread's parent, or its own.

type ObjectFacts

type ObjectFacts struct {
	Object   ObjectRow
	Messages []MessageRow
}

type ObjectRow

type ObjectRow struct {
	Object, Conversation, TS string
	Kind                     string // SlackThread or SlackDM
	Version, LatestReply     string
	ReplyCount               int
	Watched                  bool
	Reasons                  []string
	LastActivityAt           time.Time
	NextCheckAt              time.Time // zero: due now
	SeenAt, AdmittedAt       time.Time
}

type ReactionRow

type ReactionRow struct {
	Name  string
	Users []string
}

type Reader

type Reader interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Reader is what a single read statement needs.

type SaveResult

type SaveResult struct {
	Units    []unit.Unit
	Unitless bool
	Changes  int // change rows written
}

SaveResult is what a Save changed: the units its notes named, and whether a changed message had no unit (a DM range not segmented yet).

type SegmentRow

type SegmentRow struct {
	Team, Conversation, ID, FromTS, ToTS string
	Closed                               bool
}

type SegmentSpan

type SegmentSpan struct{ ID, FromTS, ToTS string }

SegmentSpan is a range Apply writes; ID "" asks for a new segment.

type SegmentStore

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

SegmentStore is the DM segment map: a conversation's ts ranges, each under an id that is never renamed or reused. Ranges are inclusive at both ends; ToTS "" is open.

func (*SegmentStore) Apply

func (s *SegmentStore) Apply(ctx context.Context, provider unit.ProviderID, team, conv string, spans []SegmentSpan, unitOf SegmentUnit, now time.Time) ([]SegmentRow, error)

Apply replaces conv's segments from spans[0].FromTS on: a named id moves to its span, a span without an id adopts the open segment that starts at its FromTS or gets a new id, and an open segment of the replaced range that no span names is closed with its last range. Spans are ascending and disjoint; only the last may be open. A segment that starts before the replaced range and reaches into it must be named. It notes port.TopicSourceEvents on every segment whose range moved, that was minted or that was closed, and returns the rows of spans in order.

func (*SegmentStore) Covering

func (s *SegmentStore) Covering(ctx context.Context, provider unit.ProviderID, conv, ts string) ([]SegmentRow, error)

Covering returns the open segments of conv whose range holds ts.

func (*SegmentStore) Of

func (s *SegmentStore) Of(ctx context.Context, provider unit.ProviderID, conv string) ([]SegmentRow, error)

Of are conv's open segments, ascending.

func (*SegmentStore) Rehoming

func (s *SegmentStore) Rehoming(h DraftHome) *SegmentStore

Rehoming is s moving drafts through h.

func (*SegmentStore) Segment

func (s *SegmentStore) Segment(ctx context.Context, provider unit.ProviderID, conv, id string) (SegmentRow, error)

Segment returns one segment, closed or not; ErrUnknownSegment when conv has no such id.

type SegmentUnit

type SegmentUnit func(team, conv, id string) unit.Unit

SegmentUnit names the unit of a segment, for the notes of Apply.

type SlackStore

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

SlackStore is the Slack source's tables. Only admitted objects have messages; a message outside them moves its conversation's hwm only.

func (*SlackStore) AdmittedConversations

func (s *SlackStore) AdmittedConversations(ctx context.Context) ([]ConversationRow, error)

AdmittedConversations are the conversations with an admitted object, with their hwm, for the catch-up.

func (*SlackStore) Changes

func (s *SlackStore) Changes(ctx context.Context, afterSeq int64, limit int) ([]ChangeRow, error)

Changes are the change rows after afterSeq, ascending, at most limit (0: all).

func (*SlackStore) Conversations

func (s *SlackStore) Conversations(ctx context.Context, ids []string) ([]ConversationRow, error)

func (*SlackStore) Cursor

func (s *SlackStore) Cursor(ctx context.Context, name string) (string, error)

Cursor is the named cursor's value, "" when unset.

func (*SlackStore) MarkSeen

func (s *SlackStore) MarkSeen(ctx context.Context, conv, ts string) error

MarkSeen moves conv's high-water mark to ts when it is later; it stores nothing of the message.

func (*SlackStore) Members added in v0.4.0

func (s *SlackStore) Members(ctx context.Context, ids []string) (map[string]MembersRow, error)

Members are the stored members of the conversations ids, by conversation; a conversation never read is absent.

func (*SlackStore) Messages

func (s *SlackStore) Messages(ctx context.Context, conv string, q MessageQuery) ([]MessageRow, error)

Messages are conv's messages q selects, ascending ts, with their reactions. Slack timestamps compare as text: every ts has ten digits before the dot.

func (*SlackStore) Object

func (s *SlackStore) Object(ctx context.Context, object string) (ObjectRow, bool, error)

Object is an admitted or watched object.

func (s *SlackStore) Permalink(ctx context.Context) (string, error)

Permalink is a stored message permalink, "" when none is stored: what names the workspace's web host.

func (*SlackStore) PutConversations

func (s *SlackStore) PutConversations(ctx context.Context, cs []ConversationRow) error

PutConversations stores the conversations' metadata; hwm and admitted stay.

func (*SlackStore) PutMembers added in v0.4.0

func (s *SlackStore) PutMembers(ctx context.Context, r MembersRow) error

PutMembers stores r, replacing what its conversation held.

func (*SlackStore) PutUsers

func (s *SlackStore) PutUsers(ctx context.Context, us []UserRow) error

func (*SlackStore) RowObjects

func (s *SlackStore) RowObjects(ctx context.Context, since time.Time) ([]ObjectRow, error)

RowObjects are the objects watched or active at or after since.

func (*SlackStore) Save

func (s *SlackStore) Save(ctx context.Context, f FactsRows, now time.Time) (SaveResult, error)

Save writes the facts in one transaction and notes TopicSourceEvents with the units of every message that changed: new, edited, deleted or with other reactions. A message stored again unchanged writes and notes nothing.

func (*SlackStore) Schedule

func (s *SlackStore) Schedule(ctx context.Context, object string, at time.Time) error

Schedule sets the object's next check.

func (*SlackStore) See

func (s *SlackStore) See(ctx context.Context, objects []string, next map[string]time.Time, now time.Time) error

See stamps every object a pass returned as seen at now, and moves each to its next check in next.

func (*SlackStore) SetCursor

func (s *SlackStore) SetCursor(ctx context.Context, name, value string) error

func (*SlackStore) SetWatched

func (s *SlackStore) SetWatched(ctx context.Context, object string, watched bool, reason string) error

SetWatched flips watched and adds reason to the object's reasons; an object the store lacks is created due now with no version, so the next pass reads it. Its messages stay either way.

func (*SlackStore) UserByEmail

func (s *SlackStore) UserByEmail(ctx context.Context, email string) (UserRow, bool, error)

UserByEmail matches the email without case; a live user wins over a deleted one.

func (*SlackStore) Users

func (s *SlackStore) Users(ctx context.Context, ids []string) ([]UserRow, error)

func (*SlackStore) Versions

func (s *SlackStore) Versions(ctx context.Context, objects []string) (map[string]string, error)

Versions are the stored versions of the objects the store knows.

func (*SlackStore) Watched

func (s *SlackStore) Watched(ctx context.Context, now time.Time) ([]ObjectRow, error)

Watched are the watched objects whose next check is at or before now.

type Tx

type Tx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

Tx is a write transaction that records the units it changed.

func (*Tx) Touch

func (tx *Tx) Touch(units ...unit.Unit)

Touch records that the transaction changed the reported facts of units; none is a change of no unit in particular.

type UserRow

type UserRow struct {
	ID, Name, DisplayName, Email string
	IsBot, Deleted               bool
	FetchedAt                    time.Time
}

Jump to

Keyboard shortcuts

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