storage

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CmdCreateApprovedUsersTable engine.DBCmd = iota + 100
	CmdCreateApprovedUsersIndexes
	CmdAddApprovedUser
	CmdAddUIDColumn
	CmdAddGIDColumn
)

all approved users queries

View Source
const (
	CmdCreateDetectedSpamTable engine.DBCmd = iota + 200
	CmdCreateDetectedSpamIndexes
)

detected spam query commands

View Source
const (
	CmdCreateDictionaryTable engine.DBCmd = iota + 300
	CmdCreateDictionaryIndexes
	CmdAddDictionaryEntry
	CmdImportDictionaryEntry
)

dictionary-related command constants

View Source
const (
	CmdCreateLocatorTables engine.DBCmd = iota + 400
	CmdCreateLocatorIndexes
	CmdAddGIDColumnMessages
	CmdAddGIDColumnSpam
	CmdAddLocatorMessage
	CmdAddLocatorSpam
)

locator-related command constants

View Source
const (
	CmdCreateSamplesTable engine.DBCmd = iota + 500
	CmdCreateSamplesIndexes
	CmdAddSample
	CmdImportSample
)

samples-related command constants

Variables

This section is empty.

Functions

func New added in v1.17.0

func New(ctx context.Context, connURL, gid string) (*engine.SQL, error)

New creates a new store based on the connection URL and the session duration.

Types

type ApprovedUsers

type ApprovedUsers struct {
	*engine.SQL
	engine.RWLocker
}

ApprovedUsers is a storage for approved users

func NewApprovedUsers

func NewApprovedUsers(ctx context.Context, db *engine.SQL) (*ApprovedUsers, error)

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

func (*ApprovedUsers) Read

func (au *ApprovedUsers) Read(ctx context.Context) ([]approved.UserInfo, error)

Read returns a list of all approved users

func (*ApprovedUsers) Write added in v1.6.0

func (au *ApprovedUsers) Write(ctx context.Context, user approved.UserInfo) error

Write adds a user to the approved list

type DetectedSpam added in v1.7.0

type DetectedSpam struct {
	*engine.SQL
	engine.RWLocker
}

DetectedSpam is a storage for detected spam entries

func NewDetectedSpam added in v1.7.0

func NewDetectedSpam(ctx context.Context, db *engine.SQL) (*DetectedSpam, error)

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

type Dictionary struct {
	*engine.SQL
	engine.RWLocker
}

Dictionary is a storage for stop words/phrases and ignored words

func NewDictionary added in v1.16.0

func NewDictionary(ctx context.Context, db *engine.SQL) (*Dictionary, error)

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

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

type Locator struct {
	*engine.SQL

	engine.RWLocker
	// contains filtered or unexported fields
}

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

func (l *Locator) AddSpam(ctx context.Context, userID int64, checks []spamcheck.Response) error

AddSpam adds spam data to the locator and also cleans up old spam data.

func (*Locator) Close added in v1.3.0

func (l *Locator) Close(_ context.Context) error

Close closes the database

func (*Locator) Message added in v1.3.0

func (l *Locator) Message(ctx context.Context, msg string) (MsgMeta, bool)

Message returns message MsgMeta for given msg and gid

func (*Locator) MsgHash added in v1.3.0

func (l *Locator) MsgHash(msg string) string

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

func (l *Locator) Spam(ctx context.Context, userID int64) (SpamData, bool)

Spam returns message SpamData for given msg within the same gid

func (*Locator) UserIDByName added in v1.5.0

func (l *Locator) UserIDByName(ctx context.Context, userName string) int64

UserIDByName returns user id by username within the same gid

func (*Locator) UserNameByID added in v1.5.0

func (l *Locator) UserNameByID(ctx context.Context, userID int64) string

UserNameByID returns username by user id 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

func (MsgMeta) String added in v1.3.0

func (m MsgMeta) String() string

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

type Samples struct {
	*engine.SQL
	engine.RWLocker
}

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

func NewSamples(ctx context.Context, db *engine.SQL) (*Samples, error)

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) Delete added in v1.16.0

func (s *Samples) Delete(ctx context.Context, id int64) error

Delete removes a sample from the storage by its ID

func (*Samples) DeleteMessage added in v1.16.0

func (s *Samples) DeleteMessage(ctx context.Context, message string) error

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

func (*Samples) Stats added in v1.16.0

func (s *Samples) Stats(ctx context.Context) (*SamplesStats, error)

Stats returns statistics about samples

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

type SpamData added in v1.3.0

type SpamData struct {
	Time   time.Time `db:"time"`
	Checks []spamcheck.Response
}

SpamData stores spam data for a given user

func (SpamData) String added in v1.3.0

func (s SpamData) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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