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
- Variables
- func Tables() []string
- type Change
- type ChangeRow
- type ConversationRow
- type DB
- func (db *DB) Close() error
- func (db *DB) Extraction() *ExtractionStore
- func (db *DB) OnChange(f func(Change))
- func (db *DB) R() Reader
- func (db *DB) Read(ctx context.Context, fn func(tx *sql.Tx) error) error
- func (db *DB) Segments() *SegmentStore
- func (db *DB) Slack() *SlackStore
- func (db *DB) Write(ctx context.Context, fn func(tx *Tx) error) (Change, error)
- type DraftHome
- type ExtractionHint
- type ExtractionRun
- type ExtractionSave
- type ExtractionState
- type ExtractionStore
- func (s *ExtractionStore) Day(ctx context.Context, day string) (calls int, costUSD float64, err error)
- func (s *ExtractionStore) Hints(ctx context.Context, u unit.Unit) (map[string]ExtractionHint, error)
- func (s *ExtractionStore) MarkClosed(ctx context.Context, u unit.Unit, at time.Time) error
- func (s *ExtractionStore) MarkMigrated(ctx context.Context, name string, at time.Time) error
- func (s *ExtractionStore) Migrated(ctx context.Context, name string) (bool, error)
- func (s *ExtractionStore) Record(ctx context.Context, r ExtractionRun) error
- func (s *ExtractionStore) Save(ctx context.Context, sv ExtractionSave) error
- func (s *ExtractionStore) SetWatched(ctx context.Context, u unit.Unit, watched bool, at time.Time) error
- func (s *ExtractionStore) Spend(ctx context.Context, day string, costUSD float64) error
- func (s *ExtractionStore) States(ctx context.Context, units []unit.Unit) (map[unit.Unit]ExtractionState, error)
- func (s *ExtractionStore) Watched(ctx context.Context, provider unit.ProviderID) (map[unit.Unit]ExtractionState, error)
- type FactsRows
- type FileRow
- type MembersRow
- type MessageQuery
- type MessageRow
- type ObjectFacts
- type ObjectRow
- type ReactionRow
- type Reader
- type SaveResult
- type SegmentRow
- type SegmentSpan
- type SegmentStore
- func (s *SegmentStore) Apply(ctx context.Context, provider unit.ProviderID, team, conv string, ...) ([]SegmentRow, error)
- func (s *SegmentStore) Covering(ctx context.Context, provider unit.ProviderID, conv, ts string) ([]SegmentRow, error)
- func (s *SegmentStore) Of(ctx context.Context, provider unit.ProviderID, conv string) ([]SegmentRow, error)
- func (s *SegmentStore) Rehoming(h DraftHome) *SegmentStore
- func (s *SegmentStore) Segment(ctx context.Context, provider unit.ProviderID, conv, id string) (SegmentRow, error)
- type SegmentUnit
- type SlackStore
- func (s *SlackStore) AdmittedConversations(ctx context.Context) ([]ConversationRow, error)
- func (s *SlackStore) Changes(ctx context.Context, afterSeq int64, limit int) ([]ChangeRow, error)
- func (s *SlackStore) Conversations(ctx context.Context, ids []string) ([]ConversationRow, error)
- func (s *SlackStore) Cursor(ctx context.Context, name string) (string, error)
- func (s *SlackStore) MarkSeen(ctx context.Context, conv, ts string) error
- func (s *SlackStore) Members(ctx context.Context, ids []string) (map[string]MembersRow, error)
- func (s *SlackStore) Messages(ctx context.Context, conv string, q MessageQuery) ([]MessageRow, error)
- func (s *SlackStore) Object(ctx context.Context, object string) (ObjectRow, bool, error)
- func (s *SlackStore) Permalink(ctx context.Context) (string, error)
- func (s *SlackStore) PutConversations(ctx context.Context, cs []ConversationRow) error
- func (s *SlackStore) PutMembers(ctx context.Context, r MembersRow) error
- func (s *SlackStore) PutUsers(ctx context.Context, us []UserRow) error
- func (s *SlackStore) RowObjects(ctx context.Context, since time.Time) ([]ObjectRow, error)
- func (s *SlackStore) Save(ctx context.Context, f FactsRows, now time.Time) (SaveResult, error)
- func (s *SlackStore) Schedule(ctx context.Context, object string, at time.Time) error
- func (s *SlackStore) See(ctx context.Context, objects []string, next map[string]time.Time, ...) error
- func (s *SlackStore) SetCursor(ctx context.Context, name, value string) error
- func (s *SlackStore) SetWatched(ctx context.Context, object string, watched bool, reason string) error
- func (s *SlackStore) UserByEmail(ctx context.Context, email string) (UserRow, bool, error)
- func (s *SlackStore) Users(ctx context.Context, ids []string) ([]UserRow, error)
- func (s *SlackStore) Versions(ctx context.Context, objects []string) (map[string]string, error)
- func (s *SlackStore) Watched(ctx context.Context, now time.Time) ([]ObjectRow, error)
- type Tx
- type UserRow
Constants ¶
const ( SlackThread = "thread" SlackDM = "dm" )
Object kinds of slack_objects.
const ( SlackNew = "new" SlackEdited = "edited" SlackDeleted = "deleted" SlackReaction = "reaction" )
Change kinds of slack_changes.
const SlackDiscoverCursor = "discover"
SlackDiscoverCursor is the source's discovery cursor.
const TimeLayout = time.RFC3339Nano
TimeLayout is how the store writes a time.
Variables ¶
var ErrUnknownSegment = errors.New("unknown segment")
Functions ¶
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 ConversationRow ¶
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 ¶
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 ¶
OpenDB is a store over an open database whose schema is current; tests open one in memory.
func (*DB) Extraction ¶
func (db *DB) Extraction() *ExtractionStore
func (*DB) Read ¶
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
type ExtractionHint ¶
type ExtractionHint struct{ Mode, Context, Why string }
type ExtractionRun ¶
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 ¶
func (s *ExtractionStore) Hints(ctx context.Context, u unit.Unit) (map[string]ExtractionHint, error)
Hints are u's item hints by item id.
func (*ExtractionStore) MarkClosed ¶
MarkClosed records a sent draft's unit.closed on u, which no extraction pass clears.
func (*ExtractionStore) MarkMigrated ¶
MarkMigrated records that the ledger migration name was applied at at.
func (*ExtractionStore) Record ¶
func (s *ExtractionStore) Record(ctx context.Context, r ExtractionRun) error
func (*ExtractionStore) Save ¶
func (s *ExtractionStore) Save(ctx context.Context, sv ExtractionSave) error
func (*ExtractionStore) SetWatched ¶
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 MembersRow ¶ added in v0.4.0
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 ReactionRow ¶
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 ¶
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 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 ¶
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 ¶
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) 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) Permalink ¶
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 ¶
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) 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 ¶
UserByEmail matches the email without case; a live user wins over a deleted one.