package babble import ( "fmt" "strings" "time" "dk/internal/database" "dk/internal/helpers/scanner" "zombiezen.com/go/sqlite" ) // Babble represents a global chat message in the database type Babble struct { database.BaseModel ID int `db:"id" json:"id"` Posted int64 `db:"posted" json:"posted"` Author string `db:"author" json:"author"` Babble string `db:"babble" json:"babble"` } func (b *Babble) GetTableName() string { return "babble" } func (b *Babble) GetID() int { return b.ID } func (b *Babble) SetID(id int) { b.ID = id } func (b *Babble) Set(field string, value any) error { return database.Set(b, field, value) } func (b *Babble) Save() error { return database.Save(b) } func (b *Babble) Delete() error { return database.Delete(b) } // Creates a new Babble with sensible defaults func New() *Babble { return &Babble{ Posted: time.Now().Unix(), Author: "", Babble: "", } } var babbleScanner = scanner.New[Babble]() // Returns the column list for babble queries func babbleColumns() string { return babbleScanner.Columns() } // Populates a Babble struct using the fast scanner func scanBabble(stmt *sqlite.Stmt) *Babble { babble := &Babble{} babbleScanner.Scan(stmt, babble) return babble } // Retrieves a babble message by ID func Find(id int) (*Babble, error) { var babble *Babble query := `SELECT ` + babbleColumns() + ` FROM babble WHERE id = ?` err := database.Query(query, func(stmt *sqlite.Stmt) error { babble = scanBabble(stmt) return nil }, id) if err != nil { return nil, fmt.Errorf("failed to find babble: %w", err) } if babble == nil { return nil, fmt.Errorf("babble with ID %d not found", id) } return babble, nil } // Retrieves all babble messages ordered by posted time (newest first) func All() ([]*Babble, error) { var babbles []*Babble query := `SELECT ` + babbleColumns() + ` FROM babble ORDER BY posted DESC, id DESC` err := database.Query(query, func(stmt *sqlite.Stmt) error { babble := scanBabble(stmt) babbles = append(babbles, babble) return nil }) if err != nil { return nil, fmt.Errorf("failed to retrieve all babble: %w", err) } return babbles, nil } // Retrieves babble messages by a specific author func ByAuthor(author string) ([]*Babble, error) { var babbles []*Babble query := `SELECT ` + babbleColumns() + ` FROM babble WHERE LOWER(author) = LOWER(?) ORDER BY posted DESC, id DESC` err := database.Query(query, func(stmt *sqlite.Stmt) error { babble := scanBabble(stmt) babbles = append(babbles, babble) return nil }, author) if err != nil { return nil, fmt.Errorf("failed to retrieve babble by author: %w", err) } return babbles, nil } // Retrieves the most recent babble messages (limited by count) func Recent(limit int) ([]*Babble, error) { var babbles []*Babble query := `SELECT ` + babbleColumns() + ` FROM babble ORDER BY posted DESC, id DESC LIMIT ?` err := database.Query(query, func(stmt *sqlite.Stmt) error { babble := scanBabble(stmt) babbles = append(babbles, babble) return nil }, limit) if err != nil { return nil, fmt.Errorf("failed to retrieve recent babble: %w", err) } return babbles, nil } // Retrieves babble messages since a specific timestamp func Since(since int64) ([]*Babble, error) { var babbles []*Babble query := `SELECT ` + babbleColumns() + ` FROM babble WHERE posted >= ? ORDER BY posted DESC, id DESC` err := database.Query(query, func(stmt *sqlite.Stmt) error { babble := scanBabble(stmt) babbles = append(babbles, babble) return nil }, since) if err != nil { return nil, fmt.Errorf("failed to retrieve babble since timestamp: %w", err) } return babbles, nil } // Retrieves babble messages between two timestamps (inclusive) func Between(start, end int64) ([]*Babble, error) { var babbles []*Babble query := `SELECT ` + babbleColumns() + ` FROM babble WHERE posted >= ? AND posted <= ? ORDER BY posted DESC, id DESC` err := database.Query(query, func(stmt *sqlite.Stmt) error { babble := scanBabble(stmt) babbles = append(babbles, babble) return nil }, start, end) if err != nil { return nil, fmt.Errorf("failed to retrieve babble between timestamps: %w", err) } return babbles, nil } // Retrieves babble messages containing the search term (case-insensitive) func Search(term string) ([]*Babble, error) { var babbles []*Babble query := `SELECT ` + babbleColumns() + ` FROM babble WHERE LOWER(babble) LIKE LOWER(?) ORDER BY posted DESC, id DESC` searchTerm := "%" + term + "%" err := database.Query(query, func(stmt *sqlite.Stmt) error { babble := scanBabble(stmt) babbles = append(babbles, babble) return nil }, searchTerm) if err != nil { return nil, fmt.Errorf("failed to search babble: %w", err) } return babbles, nil } // Retrieves recent messages from a specific author func RecentByAuthor(author string, limit int) ([]*Babble, error) { var babbles []*Babble query := `SELECT ` + babbleColumns() + ` FROM babble WHERE LOWER(author) = LOWER(?) ORDER BY posted DESC, id DESC LIMIT ?` err := database.Query(query, func(stmt *sqlite.Stmt) error { babble := scanBabble(stmt) babbles = append(babbles, babble) return nil }, author, limit) if err != nil { return nil, fmt.Errorf("failed to retrieve recent babble by author: %w", err) } return babbles, nil } // Saves a new babble to the database and sets the ID func (b *Babble) Insert() error { columns := `posted, author, babble` values := []any{b.Posted, b.Author, b.Babble} return database.Insert(b, columns, values...) } // Returns the posted timestamp as a time.Time func (b *Babble) PostedTime() time.Time { return time.Unix(b.Posted, 0) } // Sets the posted timestamp from a time.Time func (b *Babble) SetPostedTime(t time.Time) { b.Set("Posted", t.Unix()) } // Returns true if the babble message was posted within the last hour func (b *Babble) IsRecent() bool { return time.Since(b.PostedTime()) < time.Hour } // Returns how long ago the babble message was posted func (b *Babble) Age() time.Duration { return time.Since(b.PostedTime()) } // Returns true if the given username is the author of this babble message func (b *Babble) IsAuthor(username string) bool { return strings.EqualFold(b.Author, username) } // Returns a truncated version of the babble for previews func (b *Babble) Preview(maxLength int) string { if len(b.Babble) <= maxLength { return b.Babble } if maxLength < 3 { return b.Babble[:maxLength] } return b.Babble[:maxLength-3] + "..." } // Returns the number of words in the babble message func (b *Babble) WordCount() int { if b.Babble == "" { return 0 } // Simple word count by splitting on whitespace words := 0 inWord := false for _, char := range b.Babble { if char == ' ' || char == '\t' || char == '\n' || char == '\r' { if inWord { words++ inWord = false } } else { inWord = true } } if inWord { words++ } return words } // Returns the character length of the babble message func (b *Babble) Length() int { return len(b.Babble) } // Returns true if the babble message contains the given term (case-insensitive) func (b *Babble) Contains(term string) bool { return strings.Contains(strings.ToLower(b.Babble), strings.ToLower(term)) } // Returns true if the babble message is empty or whitespace-only func (b *Babble) IsEmpty() bool { return strings.TrimSpace(b.Babble) == "" } // Returns true if the message exceeds the typical chat length func (b *Babble) IsLongMessage(threshold int) bool { return b.Length() > threshold } // Returns a slice of usernames mentioned in the message (starting with @) func (b *Babble) GetMentions() []string { words := strings.Fields(b.Babble) var mentions []string for _, word := range words { if strings.HasPrefix(word, "@") && len(word) > 1 { // Clean up punctuation from the end mention := strings.TrimRight(word[1:], ".,!?;:") if mention != "" { mentions = append(mentions, mention) } } } return mentions } // Returns true if the message mentions the given username func (b *Babble) HasMention(username string) bool { mentions := b.GetMentions() for _, mention := range mentions { if strings.EqualFold(mention, username) { return true } } return false }