package babble import ( "fmt" "strings" "time" "dk/internal/database" "zombiezen.com/go/sqlite" ) // Babble represents a global chat message in the database type Babble struct { ID int `json:"id"` Posted int64 `json:"posted"` Author string `json:"author"` Babble string `json:"babble"` db *database.DB } // Find retrieves a babble message by ID func Find(db *database.DB, id int) (*Babble, error) { babble := &Babble{db: db} query := "SELECT id, posted, author, babble FROM babble WHERE id = ?" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble.ID = stmt.ColumnInt(0) babble.Posted = stmt.ColumnInt64(1) babble.Author = stmt.ColumnText(2) babble.Babble = stmt.ColumnText(3) return nil }, id) if err != nil { return nil, fmt.Errorf("failed to find babble: %w", err) } if babble.ID == 0 { return nil, fmt.Errorf("babble with ID %d not found", id) } return babble, nil } // All retrieves all babble messages ordered by posted time (newest first) func All(db *database.DB) ([]*Babble, error) { var babbles []*Babble query := "SELECT id, posted, author, babble FROM babble ORDER BY posted DESC, id DESC" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble := &Babble{ ID: stmt.ColumnInt(0), Posted: stmt.ColumnInt64(1), Author: stmt.ColumnText(2), Babble: stmt.ColumnText(3), db: db, } babbles = append(babbles, babble) return nil }) if err != nil { return nil, fmt.Errorf("failed to retrieve all babble: %w", err) } return babbles, nil } // ByAuthor retrieves babble messages by a specific author func ByAuthor(db *database.DB, author string) ([]*Babble, error) { var babbles []*Babble query := "SELECT id, posted, author, babble FROM babble WHERE LOWER(author) = LOWER(?) ORDER BY posted DESC, id DESC" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble := &Babble{ ID: stmt.ColumnInt(0), Posted: stmt.ColumnInt64(1), Author: stmt.ColumnText(2), Babble: stmt.ColumnText(3), db: db, } 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 } // Recent retrieves the most recent babble messages (limited by count) func Recent(db *database.DB, limit int) ([]*Babble, error) { var babbles []*Babble query := "SELECT id, posted, author, babble FROM babble ORDER BY posted DESC, id DESC LIMIT ?" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble := &Babble{ ID: stmt.ColumnInt(0), Posted: stmt.ColumnInt64(1), Author: stmt.ColumnText(2), Babble: stmt.ColumnText(3), db: db, } babbles = append(babbles, babble) return nil }, limit) if err != nil { return nil, fmt.Errorf("failed to retrieve recent babble: %w", err) } return babbles, nil } // Since retrieves babble messages since a specific timestamp func Since(db *database.DB, since int64) ([]*Babble, error) { var babbles []*Babble query := "SELECT id, posted, author, babble FROM babble WHERE posted >= ? ORDER BY posted DESC, id DESC" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble := &Babble{ ID: stmt.ColumnInt(0), Posted: stmt.ColumnInt64(1), Author: stmt.ColumnText(2), Babble: stmt.ColumnText(3), db: db, } 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 } // Between retrieves babble messages between two timestamps (inclusive) func Between(db *database.DB, start, end int64) ([]*Babble, error) { var babbles []*Babble query := "SELECT id, posted, author, babble FROM babble WHERE posted >= ? AND posted <= ? ORDER BY posted DESC, id DESC" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble := &Babble{ ID: stmt.ColumnInt(0), Posted: stmt.ColumnInt64(1), Author: stmt.ColumnText(2), Babble: stmt.ColumnText(3), db: db, } 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 } // Search retrieves babble messages containing the search term (case-insensitive) func Search(db *database.DB, term string) ([]*Babble, error) { var babbles []*Babble query := "SELECT id, posted, author, babble FROM babble WHERE LOWER(babble) LIKE LOWER(?) ORDER BY posted DESC, id DESC" searchTerm := "%" + term + "%" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble := &Babble{ ID: stmt.ColumnInt(0), Posted: stmt.ColumnInt64(1), Author: stmt.ColumnText(2), Babble: stmt.ColumnText(3), db: db, } babbles = append(babbles, babble) return nil }, searchTerm) if err != nil { return nil, fmt.Errorf("failed to search babble: %w", err) } return babbles, nil } // RecentByAuthor retrieves recent messages from a specific author func RecentByAuthor(db *database.DB, author string, limit int) ([]*Babble, error) { var babbles []*Babble query := "SELECT id, posted, author, babble FROM babble WHERE LOWER(author) = LOWER(?) ORDER BY posted DESC, id DESC LIMIT ?" err := db.Query(query, func(stmt *sqlite.Stmt) error { babble := &Babble{ ID: stmt.ColumnInt(0), Posted: stmt.ColumnInt64(1), Author: stmt.ColumnText(2), Babble: stmt.ColumnText(3), db: db, } 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 } // Save updates an existing babble message in the database func (b *Babble) Save() error { if b.ID == 0 { return fmt.Errorf("cannot save babble without ID") } query := `UPDATE babble SET posted = ?, author = ?, babble = ? WHERE id = ?` return b.db.Exec(query, b.Posted, b.Author, b.Babble, b.ID) } // Delete removes the babble message from the database func (b *Babble) Delete() error { if b.ID == 0 { return fmt.Errorf("cannot delete babble without ID") } query := "DELETE FROM babble WHERE id = ?" return b.db.Exec(query, b.ID) } // PostedTime returns the posted timestamp as a time.Time func (b *Babble) PostedTime() time.Time { return time.Unix(b.Posted, 0) } // SetPostedTime sets the posted timestamp from a time.Time func (b *Babble) SetPostedTime(t time.Time) { b.Posted = t.Unix() } // IsRecent returns true if the babble message was posted within the last hour func (b *Babble) IsRecent() bool { return time.Since(b.PostedTime()) < time.Hour } // Age returns how long ago the babble message was posted func (b *Babble) Age() time.Duration { return time.Since(b.PostedTime()) } // IsAuthor 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) } // Preview 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] + "..." } // WordCount 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 } // Length returns the character length of the babble message func (b *Babble) Length() int { return len(b.Babble) } // Contains 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)) } // IsEmpty returns true if the babble message is empty or whitespace-only func (b *Babble) IsEmpty() bool { return strings.TrimSpace(b.Babble) == "" } // IsLongMessage returns true if the message exceeds the typical chat length func (b *Babble) IsLongMessage(threshold int) bool { return b.Length() > threshold } // GetMentions 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 } // HasMention 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 }