299 lines
6.7 KiB
Go
299 lines
6.7 KiB
Go
package news
|
|
|
|
import (
|
|
"dk/internal/database"
|
|
"dk/internal/helpers/scanner"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"zombiezen.com/go/sqlite"
|
|
)
|
|
|
|
// News represents a news post in the database
|
|
type News struct {
|
|
database.BaseModel
|
|
|
|
ID int `db:"id" json:"id"`
|
|
Author int `db:"author" json:"author"`
|
|
Posted int64 `db:"posted" json:"posted"`
|
|
Content string `db:"content" json:"content"`
|
|
}
|
|
|
|
func (n *News) GetTableName() string {
|
|
return "news"
|
|
}
|
|
|
|
func (n *News) GetID() int {
|
|
return n.ID
|
|
}
|
|
|
|
func (n *News) SetID(id int) {
|
|
n.ID = id
|
|
}
|
|
|
|
func (n *News) Set(field string, value any) error {
|
|
return database.Set(n, field, value)
|
|
}
|
|
|
|
func (n *News) Save() error {
|
|
return database.Save(n)
|
|
}
|
|
|
|
func (n *News) Delete() error {
|
|
return database.Delete(n)
|
|
}
|
|
|
|
// Creates a new News with sensible defaults
|
|
func New() *News {
|
|
return &News{
|
|
Author: 0, // No author by default
|
|
Posted: time.Now().Unix(), // Current time
|
|
Content: "", // Empty content
|
|
}
|
|
}
|
|
|
|
var newsScanner = scanner.New[News]()
|
|
|
|
// Returns the column list for news queries
|
|
func newsColumns() string {
|
|
return newsScanner.Columns()
|
|
}
|
|
|
|
// Populates a News struct using the fast scanner
|
|
func scanNews(stmt *sqlite.Stmt) *News {
|
|
news := &News{}
|
|
newsScanner.Scan(stmt, news)
|
|
return news
|
|
}
|
|
|
|
// Retrieves a news post by ID
|
|
func Find(id int) (*News, error) {
|
|
var news *News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE id = ?`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news = scanNews(stmt)
|
|
return nil
|
|
}, id)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to find news: %w", err)
|
|
}
|
|
|
|
if news == nil {
|
|
return nil, fmt.Errorf("news with ID %d not found", id)
|
|
}
|
|
|
|
return news, nil
|
|
}
|
|
|
|
// Retrieves all news posts ordered by posted date (newest first)
|
|
func All() ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve all news: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Retrieves news posts by a specific author
|
|
func ByAuthor(authorID int) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE author = ? ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, authorID)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve news by author: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Retrieves the most recent news posts (limited by count)
|
|
func Recent(limit int) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news ORDER BY posted DESC, id DESC LIMIT ?`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, limit)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve recent news: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Retrieves news posts since a specific timestamp
|
|
func Since(since int64) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE posted >= ? ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, since)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve news since timestamp: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Retrieves news posts between two timestamps (inclusive)
|
|
func Between(start, end int64) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE posted >= ? AND posted <= ? ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, start, end)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve news between timestamps: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Saves a new news post to the database and sets the ID
|
|
func (n *News) Insert() error {
|
|
columns := `author, posted, content`
|
|
values := []any{n.Author, n.Posted, n.Content}
|
|
return database.Insert(n, columns, values...)
|
|
}
|
|
|
|
// Returns the posted timestamp as a time.Time
|
|
func (n *News) PostedTime() time.Time {
|
|
return time.Unix(n.Posted, 0)
|
|
}
|
|
|
|
// Sets the posted timestamp from a time.Time
|
|
func (n *News) SetPostedTime(t time.Time) {
|
|
n.Set("Posted", t.Unix())
|
|
}
|
|
|
|
// Returns true if the news post was made within the last 24 hours
|
|
func (n *News) IsRecent() bool {
|
|
return time.Since(n.PostedTime()) < 24*time.Hour
|
|
}
|
|
|
|
// Returns how long ago the news post was made
|
|
func (n *News) Age() time.Duration {
|
|
return time.Since(n.PostedTime())
|
|
}
|
|
|
|
// Converts a time.Time to a human-readable date string
|
|
func (n *News) ReadableTime() string {
|
|
return n.PostedTime().Format("Jan 2, 2006 3:04 PM")
|
|
}
|
|
|
|
// Returns true if the given user ID is the author of this news post
|
|
func (n *News) IsAuthor(userID int) bool {
|
|
return n.Author == userID
|
|
}
|
|
|
|
// Returns a truncated version of the content for previews
|
|
func (n *News) Preview(maxLength int) string {
|
|
if len(n.Content) <= maxLength {
|
|
return n.Content
|
|
}
|
|
|
|
if maxLength < 3 {
|
|
return n.Content[:maxLength]
|
|
}
|
|
|
|
return n.Content[:maxLength-3] + "..."
|
|
}
|
|
|
|
// Returns the number of words in the content
|
|
func (n *News) WordCount() int {
|
|
if n.Content == "" {
|
|
return 0
|
|
}
|
|
|
|
// Simple word count by splitting on spaces
|
|
words := 0
|
|
inWord := false
|
|
|
|
for _, char := range n.Content {
|
|
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 content
|
|
func (n *News) Length() int {
|
|
return len(n.Content)
|
|
}
|
|
|
|
// Returns true if the content contains the given term (case-insensitive)
|
|
func (n *News) Contains(term string) bool {
|
|
return strings.Contains(strings.ToLower(n.Content), strings.ToLower(term))
|
|
}
|
|
|
|
// Returns true if the content is empty or whitespace-only
|
|
func (n *News) IsEmpty() bool {
|
|
return strings.TrimSpace(n.Content) == ""
|
|
}
|
|
|
|
// Retrieves news posts containing the search term in content
|
|
func Search(term string) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE LOWER(content) LIKE LOWER(?) ORDER BY posted DESC, id DESC`
|
|
searchTerm := "%" + term + "%"
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, searchTerm)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to search news: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|