384 lines
9.5 KiB
Go

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 {
ID int `db:"id" json:"id"`
Posted int64 `db:"posted" json:"posted"`
Author string `db:"author" json:"author"`
Babble string `db:"babble" json:"babble"`
}
// New creates a new Babble with sensible defaults
func New() *Babble {
return &Babble{
Posted: time.Now().Unix(),
Author: "",
Babble: "",
}
}
var babbleScanner = scanner.New[Babble]()
// babbleColumns returns the column list for babble queries
func babbleColumns() string {
return babbleScanner.Columns()
}
// scanBabble populates a Babble struct using the fast scanner
func scanBabble(stmt *sqlite.Stmt) *Babble {
babble := &Babble{}
babbleScanner.Scan(stmt, babble)
return babble
}
// Find 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
}
// All 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
}
// ByAuthor 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
}
// Recent 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
}
// Since 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
}
// Between 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
}
// Search 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
}
// RecentByAuthor 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
}
// 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 database.Exec(query, b.Posted, b.Author, b.Babble, b.ID)
}
// Insert saves a new babble to the database and sets the ID
func (b *Babble) Insert() error {
if b.ID != 0 {
return fmt.Errorf("babble already has ID %d, use Save() to update", b.ID)
}
// Use a transaction to ensure we can get the ID
err := database.Transaction(func(tx *database.Tx) error {
query := `INSERT INTO babble (posted, author, babble) VALUES (?, ?, ?)`
if err := tx.Exec(query, b.Posted, b.Author, b.Babble); err != nil {
return fmt.Errorf("failed to insert babble: %w", err)
}
// Get the last insert ID
var id int
err := tx.Query("SELECT last_insert_rowid()", func(stmt *sqlite.Stmt) error {
id = stmt.ColumnInt(0)
return nil
})
if err != nil {
return fmt.Errorf("failed to get insert ID: %w", err)
}
b.ID = id
return nil
})
return err
}
// 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")
}
return database.Exec("DELETE FROM babble WHERE id = ?", 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
}
// ToMap converts the babble to a map for efficient template rendering
func (b *Babble) ToMap() map[string]any {
return map[string]any{
"ID": b.ID,
"Posted": b.Posted,
"Author": b.Author,
"Babble": b.Babble,
// Computed values
"PostedTime": b.PostedTime(),
"IsRecent": b.IsRecent(),
"Age": b.Age(),
"WordCount": b.WordCount(),
"Length": b.Length(),
"IsEmpty": b.IsEmpty(),
"Mentions": b.GetMentions(),
}
}