Documentation
¶
Index ¶
- Constants
- func New(ctx context.Context, connURL, gid string) (*engine.SQL, error)
- type ApprovedUsers
- type DetectedSpam
- func (ds *DetectedSpam) FindByUserID(ctx context.Context, userID int64) (*DetectedSpamInfo, error)
- func (ds *DetectedSpam) Read(ctx context.Context) ([]DetectedSpamInfo, error)
- func (ds *DetectedSpam) SetAddedToSamplesFlag(ctx context.Context, id int64) error
- func (ds *DetectedSpam) Write(ctx context.Context, entry DetectedSpamInfo, checks []spamcheck.Response) error
- type DetectedSpamInfo
- type Dictionary
- func (d *Dictionary) Add(ctx context.Context, t DictionaryType, data string) error
- func (d *Dictionary) Delete(ctx context.Context, id int64) error
- func (d *Dictionary) Import(ctx context.Context, t DictionaryType, r io.Reader, withCleanup bool) (*DictionaryStats, error)
- func (d *Dictionary) Iterator(ctx context.Context, t DictionaryType) (iter.Seq[string], error)
- func (d *Dictionary) Read(ctx context.Context, t DictionaryType) ([]string, error)
- func (d *Dictionary) Reader(ctx context.Context, t DictionaryType) (io.ReadCloser, error)
- func (d *Dictionary) Stats(ctx context.Context) (*DictionaryStats, error)
- type DictionaryStats
- type DictionaryType
- type Locator
- func (l *Locator) AddMessage(ctx context.Context, msg string, chatID, userID int64, userName string, ...) error
- func (l *Locator) AddSpam(ctx context.Context, userID int64, checks []spamcheck.Response) error
- func (l *Locator) Close(_ context.Context) error
- func (l *Locator) Message(ctx context.Context, msg string) (MsgMeta, bool)
- func (l *Locator) MsgHash(msg string) string
- func (l *Locator) Spam(ctx context.Context, userID int64) (SpamData, bool)
- func (l *Locator) UserIDByName(ctx context.Context, userName string) int64
- func (l *Locator) UserNameByID(ctx context.Context, userID int64) string
- type MsgMeta
- type SampleOrigin
- type SampleType
- type SampleUpdater
- type Samples
- func (s *Samples) Add(ctx context.Context, t SampleType, o SampleOrigin, message string) error
- func (s *Samples) Delete(ctx context.Context, id int64) error
- func (s *Samples) DeleteMessage(ctx context.Context, message string) error
- func (s *Samples) Import(ctx context.Context, t SampleType, o SampleOrigin, r io.Reader, ...) (*SamplesStats, error)
- func (s *Samples) Iterator(ctx context.Context, t SampleType, o SampleOrigin) (iter.Seq[string], error)
- func (s *Samples) Read(ctx context.Context, t SampleType, o SampleOrigin) ([]string, error)
- func (s *Samples) Reader(ctx context.Context, t SampleType, o SampleOrigin) (io.ReadCloser, error)
- func (s *Samples) Stats(ctx context.Context) (*SamplesStats, error)
- type SamplesStats
- type SpamData
Constants ¶
const ( CmdCreateApprovedUsersTable engine.DBCmd = iota + 100 CmdCreateApprovedUsersIndexes CmdAddApprovedUser CmdAddUIDColumn CmdAddGIDColumn )
all approved users queries
const ( CmdCreateDetectedSpamTable engine.DBCmd = iota + 200 CmdCreateDetectedSpamIndexes )
detected spam query commands
const ( CmdCreateDictionaryTable engine.DBCmd = iota + 300 CmdCreateDictionaryIndexes CmdAddDictionaryEntry CmdImportDictionaryEntry )
dictionary-related command constants
const ( CmdCreateLocatorTables engine.DBCmd = iota + 400 CmdCreateLocatorIndexes CmdAddGIDColumnMessages CmdAddGIDColumnSpam CmdAddLocatorMessage CmdAddLocatorSpam )
locator-related command constants
const ( CmdCreateSamplesTable engine.DBCmd = iota + 500 CmdCreateSamplesIndexes CmdAddSample CmdImportSample )
samples-related command constants
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ApprovedUsers ¶
ApprovedUsers is a storage for approved users
func NewApprovedUsers ¶
NewApprovedUsers creates a new ApprovedUsers storage
func (*ApprovedUsers) Delete ¶ added in v1.5.1
func (au *ApprovedUsers) Delete(ctx context.Context, id string) error
Delete removes a user from the approved list
type DetectedSpam ¶ added in v1.7.0
DetectedSpam is a storage for detected spam entries
func NewDetectedSpam ¶ added in v1.7.0
NewDetectedSpam creates a new DetectedSpam storage
func (*DetectedSpam) FindByUserID ¶ added in v1.16.0
func (ds *DetectedSpam) FindByUserID(ctx context.Context, userID int64) (*DetectedSpamInfo, error)
FindByUserID returns the latest detected spam entry for the given user ID
func (*DetectedSpam) Read ¶ added in v1.7.0
func (ds *DetectedSpam) Read(ctx context.Context) ([]DetectedSpamInfo, error)
Read returns the latest detected spam entries, up to maxDetectedSpamEntries
func (*DetectedSpam) SetAddedToSamplesFlag ¶ added in v1.10.0
func (ds *DetectedSpam) SetAddedToSamplesFlag(ctx context.Context, id int64) error
SetAddedToSamplesFlag sets the added flag to true for the detected spam entry with the given id
func (*DetectedSpam) Write ¶ added in v1.7.0
func (ds *DetectedSpam) Write(ctx context.Context, entry DetectedSpamInfo, checks []spamcheck.Response) error
Write adds a new detected spam entry
type DetectedSpamInfo ¶ added in v1.7.0
type DetectedSpamInfo struct { ID int64 `db:"id"` GID string `db:"gid"` Text string `db:"text"` UserID int64 `db:"user_id"` UserName string `db:"user_name"` Timestamp time.Time `db:"timestamp"` Added bool `db:"added"` // added to samples ChecksJSON string `db:"checks"` // store as JSON Checks []spamcheck.Response `db:"-"` // don't store in DB directly }
DetectedSpamInfo represents information about a detected spam entry.
type Dictionary ¶ added in v1.16.0
Dictionary is a storage for stop words/phrases and ignored words
func NewDictionary ¶ added in v1.16.0
NewDictionary creates a new Dictionary storage
func (*Dictionary) Add ¶ added in v1.16.0
func (d *Dictionary) Add(ctx context.Context, t DictionaryType, data string) error
Add adds a stop phrase or ignored word to the dictionary
func (*Dictionary) Delete ¶ added in v1.16.0
func (d *Dictionary) Delete(ctx context.Context, id int64) error
Delete removes an entry from the dictionary by its ID
func (*Dictionary) Import ¶ added in v1.16.0
func (d *Dictionary) Import(ctx context.Context, t DictionaryType, r io.Reader, withCleanup bool) (*DictionaryStats, error)
Import reads phrases from the reader and imports them into the storage. If withCleanup is true removes all entries with the same type before import. Input format is either a single phrase per line or a CSV file with multiple phrases. Import reads phrases from the reader and imports them into the storage.
func (*Dictionary) Iterator ¶ added in v1.16.0
func (d *Dictionary) Iterator(ctx context.Context, t DictionaryType) (iter.Seq[string], error)
Iterator returns an iterator for phrases by type
func (*Dictionary) Read ¶ added in v1.16.0
func (d *Dictionary) Read(ctx context.Context, t DictionaryType) ([]string, error)
Read reads all entries from the dictionary by type
func (*Dictionary) Reader ¶ added in v1.16.0
func (d *Dictionary) Reader(ctx context.Context, t DictionaryType) (io.ReadCloser, error)
Reader returns a reader for phrases by type lock is lacking deliberately because Read is already protected
func (*Dictionary) Stats ¶ added in v1.16.0
func (d *Dictionary) Stats(ctx context.Context) (*DictionaryStats, error)
Stats returns statistics about dictionary entries for the given GID
type DictionaryStats ¶ added in v1.16.0
type DictionaryStats struct { TotalStopPhrases int `db:"stop_phrases_count"` TotalIgnoredWords int `db:"ignored_words_count"` }
DictionaryStats returns statistics about dictionary entries
func (*DictionaryStats) String ¶ added in v1.16.0
func (d *DictionaryStats) String() string
String returns a string representation of the stats
type DictionaryType ¶ added in v1.16.0
type DictionaryType string
DictionaryType represents the type of dictionary entry
const ( DictionaryTypeStopPhrase DictionaryType = "stop_phrase" DictionaryTypeIgnoredWord DictionaryType = "ignored_word" )
enum for dictionary types
func (DictionaryType) String ¶ added in v1.16.0
func (t DictionaryType) String() string
String implements Stringer interface
func (DictionaryType) Validate ¶ added in v1.16.0
func (t DictionaryType) Validate() error
Validate checks if the dictionary type is valid
type Locator ¶ added in v1.3.0
Locator stores messages metadata and spam results for a given ttl period. It is used to locate the message in the chat by its hash and to retrieve spam check results by userID. Useful to match messages from admin chat (only text available) to the original message and to get spam results using UserID.
func NewLocator ¶ added in v1.3.0
func NewLocator(ctx context.Context, ttl time.Duration, minSize int, db *engine.SQL) (*Locator, error)
NewLocator creates new Locator. ttl defines how long to keep messages in db, minSize defines the minimum number of messages to keep
func (*Locator) AddMessage ¶ added in v1.3.0
func (l *Locator) AddMessage(ctx context.Context, msg string, chatID, userID int64, userName string, msgID int) error
AddMessage adds messages to the locator and also cleans up old messages.
func (*Locator) AddSpam ¶ added in v1.3.0
AddSpam adds spam data to the locator and also cleans up old spam data.
func (*Locator) MsgHash ¶ added in v1.3.0
MsgHash returns sha256 hash of a message we use hash to avoid storing potentially long messages and all we need is just match
func (*Locator) Spam ¶ added in v1.3.0
Spam returns message SpamData for given msg within the same gid
func (*Locator) UserIDByName ¶ added in v1.5.0
UserIDByName returns user id by username within the same gid
type MsgMeta ¶ added in v1.3.0
type MsgMeta struct { Time time.Time `db:"time"` ChatID int64 `db:"chat_id"` UserID int64 `db:"user_id"` UserName string `db:"user_name"` MsgID int `db:"msg_id"` }
MsgMeta stores message metadata
type SampleOrigin ¶ added in v1.16.0
type SampleOrigin string
SampleOrigin represents the origin of the sample
const ( SampleOriginPreset SampleOrigin = "preset" SampleOriginUser SampleOrigin = "user" SampleOriginAny SampleOrigin = "any" )
enum for sample origins
func (SampleOrigin) String ¶ added in v1.16.0
func (o SampleOrigin) String() string
String implements Stringer interface
func (SampleOrigin) Validate ¶ added in v1.16.0
func (o SampleOrigin) Validate() error
Validate checks if the sample origin is valid
type SampleType ¶ added in v1.16.0
type SampleType string
SampleType represents the type of the sample
const ( SampleTypeHam SampleType = "ham" SampleTypeSpam SampleType = "spam" )
enum for sample types
func (SampleType) String ¶ added in v1.16.0
func (t SampleType) String() string
String implements Stringer interface
func (SampleType) Validate ¶ added in v1.16.0
func (t SampleType) Validate() error
Validate checks if the sample type is valid
type SampleUpdater ¶ added in v1.16.0
type SampleUpdater struct {
// contains filtered or unexported fields
}
SampleUpdater is an adaptor on top of Samples store, to update dynamic (user's) samples for detector, either ham or spam. The service is used by the detector to update the samples.
func NewSampleUpdater ¶ added in v1.16.0
func NewSampleUpdater(samplesService *Samples, sampleType SampleType, timeout time.Duration) *SampleUpdater
NewSampleUpdater creates a new SampleUpdater instance with the given samples service, sample type and timeout.
func (*SampleUpdater) Append ¶ added in v1.16.0
func (u *SampleUpdater) Append(msg string) error
Append a message to the samples, forcing user origin
func (*SampleUpdater) Reader ¶ added in v1.16.0
func (u *SampleUpdater) Reader() (io.ReadCloser, error)
Reader returns a reader for the samples
func (*SampleUpdater) Remove ¶ added in v1.16.0
func (u *SampleUpdater) Remove(msg string) error
Remove a message from the samples
type Samples ¶ added in v1.16.0
Samples is a storage for samples. It supports both ham and spam, as well as preset samples and user's samples
func NewSamples ¶ added in v1.16.0
NewSamples creates a new Samples storage
func (*Samples) Add ¶ added in v1.16.0
func (s *Samples) Add(ctx context.Context, t SampleType, o SampleOrigin, message string) error
Add adds a sample to the storage. Checks if the sample is already present and skips it if it is.
func (*Samples) DeleteMessage ¶ added in v1.16.0
DeleteMessage removes a sample from the storage by its message
func (*Samples) Import ¶ added in v1.16.0
func (s *Samples) Import(ctx context.Context, t SampleType, o SampleOrigin, r io.Reader, withCleanup bool) (*SamplesStats, error)
Import reads samples from the reader and imports them into the storage. Returns statistics about imported samples. If withCleanup is true removes all samples with the same type and origin before import.
func (*Samples) Iterator ¶ added in v1.16.0
func (s *Samples) Iterator(ctx context.Context, t SampleType, o SampleOrigin) (iter.Seq[string], error)
Iterator returns an iterator for samples by type and origin. Sorts samples by timestamp in descending order, i.e. from the newest to the oldest. The iterator respects context cancellation.
func (*Samples) Read ¶ added in v1.16.0
func (s *Samples) Read(ctx context.Context, t SampleType, o SampleOrigin) ([]string, error)
Read reads samples from storage by type and origin
func (*Samples) Reader ¶ added in v1.16.0
func (s *Samples) Reader(ctx context.Context, t SampleType, o SampleOrigin) (io.ReadCloser, error)
Reader returns a reader for samples by type and origin Sorts samples by timestamp in descending order, i.e. from the newest to the oldest
type SamplesStats ¶ added in v1.16.0
type SamplesStats struct { TotalSpam int `db:"spam_count"` TotalHam int `db:"ham_count"` PresetSpam int `db:"preset_spam_count"` PresetHam int `db:"preset_ham_count"` UserSpam int `db:"user_spam_count"` UserHam int `db:"user_ham_count"` }
SamplesStats returns statistics about samples
func (*SamplesStats) String ¶ added in v1.16.0
func (st *SamplesStats) String() string
String provides a string representation of the statistics