package forum import ( "dk/internal/store" "fmt" "sort" "strings" "time" ) // Forum represents a forum post or thread in the game type Forum struct { ID int `json:"id"` Posted int64 `json:"posted"` LastPost int64 `json:"last_post"` Author int `json:"author"` Parent int `json:"parent"` Replies int `json:"replies"` Title string `json:"title"` Content string `json:"content"` } func (f *Forum) Save() error { return GetStore().UpdateWithRebuild(f.ID, f) } func (f *Forum) Delete() error { GetStore().RemoveWithRebuild(f.ID) return nil } // Creates a new Forum with sensible defaults func New() *Forum { now := time.Now().Unix() return &Forum{ Posted: now, LastPost: now, Author: 0, Parent: 0, // Default to thread (not reply) Replies: 0, Title: "", Content: "", } } // Validate checks if forum has valid values func (f *Forum) Validate() error { if strings.TrimSpace(f.Title) == "" { return fmt.Errorf("forum title cannot be empty") } if strings.TrimSpace(f.Content) == "" { return fmt.Errorf("forum content cannot be empty") } if f.Posted <= 0 { return fmt.Errorf("forum Posted timestamp must be positive") } if f.LastPost <= 0 { return fmt.Errorf("forum LastPost timestamp must be positive") } if f.Parent < 0 { return fmt.Errorf("forum Parent cannot be negative") } if f.Replies < 0 { return fmt.Errorf("forum Replies cannot be negative") } return nil } // ForumStore with enhanced BaseStore type ForumStore struct { *store.BaseStore[Forum] } // Global store with singleton pattern var GetStore = store.NewSingleton(func() *ForumStore { fs := &ForumStore{BaseStore: store.NewBaseStore[Forum]()} // Register indices fs.RegisterIndex("byParent", store.BuildIntGroupIndex(func(f *Forum) int { return f.Parent })) fs.RegisterIndex("byAuthor", store.BuildIntGroupIndex(func(f *Forum) int { return f.Author })) fs.RegisterIndex("allByLastPost", store.BuildSortedListIndex(func(a, b *Forum) bool { if a.LastPost != b.LastPost { return a.LastPost > b.LastPost // DESC } return a.ID > b.ID // DESC })) return fs }) // Enhanced CRUD operations func (fs *ForumStore) AddForum(forum *Forum) error { return fs.AddWithRebuild(forum.ID, forum) } func (fs *ForumStore) RemoveForum(id int) { fs.RemoveWithRebuild(id) } func (fs *ForumStore) UpdateForum(forum *Forum) error { return fs.UpdateWithRebuild(forum.ID, forum) } // Data persistence func LoadData(dataPath string) error { fs := GetStore() return fs.BaseStore.LoadData(dataPath) } func SaveData(dataPath string) error { fs := GetStore() return fs.BaseStore.SaveData(dataPath) } // Query functions using enhanced store func Find(id int) (*Forum, error) { fs := GetStore() forum, exists := fs.Find(id) if !exists { return nil, fmt.Errorf("forum post with ID %d not found", id) } return forum, nil } func All() ([]*Forum, error) { fs := GetStore() return fs.AllSorted("allByLastPost"), nil } func Threads() ([]*Forum, error) { fs := GetStore() return fs.FilterByIndex("allByLastPost", func(f *Forum) bool { return f.Parent == 0 }), nil } func ByParent(parentID int) ([]*Forum, error) { fs := GetStore() replies := fs.GroupByIndex("byParent", parentID) // Sort replies chronologically (posted ASC, then ID ASC) if parentID > 0 && len(replies) > 1 { sort.Slice(replies, func(i, j int) bool { if replies[i].Posted != replies[j].Posted { return replies[i].Posted < replies[j].Posted // ASC } return replies[i].ID < replies[j].ID // ASC }) } return replies, nil } func ByAuthor(authorID int) ([]*Forum, error) { fs := GetStore() posts := fs.GroupByIndex("byAuthor", authorID) // Sort by posted DESC, then ID DESC sort.Slice(posts, func(i, j int) bool { if posts[i].Posted != posts[j].Posted { return posts[i].Posted > posts[j].Posted // DESC } return posts[i].ID > posts[j].ID // DESC }) return posts, nil } func Recent(limit int) ([]*Forum, error) { fs := GetStore() all := fs.AllSorted("allByLastPost") if limit > len(all) { limit = len(all) } return all[:limit], nil } func Search(term string) ([]*Forum, error) { fs := GetStore() lowerTerm := strings.ToLower(term) return fs.FilterByIndex("allByLastPost", func(f *Forum) bool { return strings.Contains(strings.ToLower(f.Title), lowerTerm) || strings.Contains(strings.ToLower(f.Content), lowerTerm) }), nil } func Since(since int64) ([]*Forum, error) { fs := GetStore() return fs.FilterByIndex("allByLastPost", func(f *Forum) bool { return f.LastPost >= since }), nil } // Insert with ID assignment func (f *Forum) Insert() error { fs := GetStore() if f.ID == 0 { f.ID = fs.GetNextID() } return fs.AddForum(f) } // Helper methods func (f *Forum) PostedTime() time.Time { return time.Unix(f.Posted, 0) } func (f *Forum) LastPostTime() time.Time { return time.Unix(f.LastPost, 0) } func (f *Forum) SetPostedTime(t time.Time) { f.Posted = t.Unix() } func (f *Forum) SetLastPostTime(t time.Time) { f.LastPost = t.Unix() } func (f *Forum) IsThread() bool { return f.Parent == 0 } func (f *Forum) IsReply() bool { return f.Parent > 0 } func (f *Forum) HasReplies() bool { return f.Replies > 0 } func (f *Forum) IsRecentActivity() bool { return time.Since(f.LastPostTime()) < 24*time.Hour } func (f *Forum) ActivityAge() time.Duration { return time.Since(f.LastPostTime()) } func (f *Forum) PostAge() time.Duration { return time.Since(f.PostedTime()) } func (f *Forum) IsAuthor(userID int) bool { return f.Author == userID } func (f *Forum) Preview(maxLength int) string { if len(f.Content) <= maxLength { return f.Content } if maxLength < 3 { return f.Content[:maxLength] } return f.Content[:maxLength-3] + "..." } func (f *Forum) WordCount() int { if f.Content == "" { return 0 } // Simple word count by splitting on whitespace words := 0 inWord := false for _, char := range f.Content { if char == ' ' || char == '\t' || char == '\n' || char == '\r' { if inWord { words++ inWord = false } } else { inWord = true } } if inWord { words++ } return words } func (f *Forum) Length() int { return len(f.Content) } func (f *Forum) Contains(term string) bool { lowerTerm := strings.ToLower(term) return strings.Contains(strings.ToLower(f.Title), lowerTerm) || strings.Contains(strings.ToLower(f.Content), lowerTerm) } func (f *Forum) UpdateLastPost() { f.LastPost = time.Now().Unix() } func (f *Forum) IncrementReplies() { f.Replies++ } func (f *Forum) DecrementReplies() { if f.Replies > 0 { f.Replies-- } } func (f *Forum) GetReplies() ([]*Forum, error) { return ByParent(f.ID) } func (f *Forum) GetThread() (*Forum, error) { if f.IsThread() { return f, nil } return Find(f.Parent) }