package forum import ( "dk/internal/store" "fmt" "sort" "strings" "sync" "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 { forumStore := GetStore() forumStore.UpdateForum(f) return nil } func (f *Forum) Delete() error { forumStore := GetStore() forumStore.RemoveForum(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 provides in-memory storage with O(1) lookups and forum-specific indices type ForumStore struct { *store.BaseStore[Forum] // Embedded generic store byParent map[int][]int // Parent -> []ID byAuthor map[int][]int // Author -> []ID threadsOnly []int // Parent=0 IDs sorted by last_post DESC, id DESC allByLastPost []int // All IDs sorted by last_post DESC, id DESC mu sync.RWMutex // Protects indices } // Global in-memory store var forumStore *ForumStore var storeOnce sync.Once // Initialize the in-memory store func initStore() { forumStore = &ForumStore{ BaseStore: store.NewBaseStore[Forum](), byParent: make(map[int][]int), byAuthor: make(map[int][]int), threadsOnly: make([]int, 0), allByLastPost: make([]int, 0), } } // GetStore returns the global forum store func GetStore() *ForumStore { storeOnce.Do(initStore) return forumStore } // AddForum adds a forum post to the in-memory store and updates all indices func (fs *ForumStore) AddForum(forum *Forum) { fs.mu.Lock() defer fs.mu.Unlock() // Validate forum if err := forum.Validate(); err != nil { return } // Add to base store fs.Add(forum.ID, forum) // Rebuild indices fs.rebuildIndicesUnsafe() } // RemoveForum removes a forum post from the store and updates indices func (fs *ForumStore) RemoveForum(id int) { fs.mu.Lock() defer fs.mu.Unlock() // Remove from base store fs.Remove(id) // Rebuild indices fs.rebuildIndicesUnsafe() } // UpdateForum updates a forum post efficiently func (fs *ForumStore) UpdateForum(forum *Forum) { fs.mu.Lock() defer fs.mu.Unlock() // Validate forum if err := forum.Validate(); err != nil { return } // Update base store fs.Add(forum.ID, forum) // Rebuild indices fs.rebuildIndicesUnsafe() } // LoadData loads forum data from JSON file, or starts with empty store func LoadData(dataPath string) error { fs := GetStore() // Load from base store, which handles JSON loading if err := fs.BaseStore.LoadData(dataPath); err != nil { return err } // Rebuild indices from loaded data fs.rebuildIndices() return nil } // SaveData saves forum data to JSON file func SaveData(dataPath string) error { fs := GetStore() return fs.BaseStore.SaveData(dataPath) } // rebuildIndicesUnsafe rebuilds all indices from base store data (caller must hold lock) func (fs *ForumStore) rebuildIndicesUnsafe() { // Clear indices fs.byParent = make(map[int][]int) fs.byAuthor = make(map[int][]int) fs.threadsOnly = make([]int, 0) fs.allByLastPost = make([]int, 0) // Collect all forum posts and build indices allForums := fs.GetAll() for id, forum := range allForums { // Parent index fs.byParent[forum.Parent] = append(fs.byParent[forum.Parent], id) // Author index fs.byAuthor[forum.Author] = append(fs.byAuthor[forum.Author], id) // Threads only (parent = 0) if forum.Parent == 0 { fs.threadsOnly = append(fs.threadsOnly, id) } // All posts fs.allByLastPost = append(fs.allByLastPost, id) } // Sort allByLastPost by last_post DESC, then ID DESC sort.Slice(fs.allByLastPost, func(i, j int) bool { forumI, _ := fs.GetByID(fs.allByLastPost[i]) forumJ, _ := fs.GetByID(fs.allByLastPost[j]) if forumI.LastPost != forumJ.LastPost { return forumI.LastPost > forumJ.LastPost // DESC } return fs.allByLastPost[i] > fs.allByLastPost[j] // DESC }) // Sort threadsOnly by last_post DESC, then ID DESC sort.Slice(fs.threadsOnly, func(i, j int) bool { forumI, _ := fs.GetByID(fs.threadsOnly[i]) forumJ, _ := fs.GetByID(fs.threadsOnly[j]) if forumI.LastPost != forumJ.LastPost { return forumI.LastPost > forumJ.LastPost // DESC } return fs.threadsOnly[i] > fs.threadsOnly[j] // DESC }) // Sort byParent replies by posted ASC, then ID ASC for parent := range fs.byParent { if parent > 0 { // Only sort replies, not threads sort.Slice(fs.byParent[parent], func(i, j int) bool { forumI, _ := fs.GetByID(fs.byParent[parent][i]) forumJ, _ := fs.GetByID(fs.byParent[parent][j]) if forumI.Posted != forumJ.Posted { return forumI.Posted < forumJ.Posted // ASC } return fs.byParent[parent][i] < fs.byParent[parent][j] // ASC }) } } // Sort byAuthor by posted DESC, then ID DESC for author := range fs.byAuthor { sort.Slice(fs.byAuthor[author], func(i, j int) bool { forumI, _ := fs.GetByID(fs.byAuthor[author][i]) forumJ, _ := fs.GetByID(fs.byAuthor[author][j]) if forumI.Posted != forumJ.Posted { return forumI.Posted > forumJ.Posted // DESC } return fs.byAuthor[author][i] > fs.byAuthor[author][j] // DESC }) } } // rebuildIndices rebuilds all forum-specific indices from base store data func (fs *ForumStore) rebuildIndices() { fs.mu.Lock() defer fs.mu.Unlock() fs.rebuildIndicesUnsafe() } // Retrieves a forum post by ID func Find(id int) (*Forum, error) { fs := GetStore() forum, exists := fs.GetByID(id) if !exists { return nil, fmt.Errorf("forum post with ID %d not found", id) } return forum, nil } // Retrieves all forum posts ordered by last post time (most recent first) func All() ([]*Forum, error) { fs := GetStore() fs.mu.RLock() defer fs.mu.RUnlock() result := make([]*Forum, 0, len(fs.allByLastPost)) for _, id := range fs.allByLastPost { if forum, exists := fs.GetByID(id); exists { result = append(result, forum) } } return result, nil } // Retrieves all top-level forum threads (parent = 0) func Threads() ([]*Forum, error) { fs := GetStore() fs.mu.RLock() defer fs.mu.RUnlock() result := make([]*Forum, 0, len(fs.threadsOnly)) for _, id := range fs.threadsOnly { if forum, exists := fs.GetByID(id); exists { result = append(result, forum) } } return result, nil } // Retrieves all replies to a specific thread/post func ByParent(parentID int) ([]*Forum, error) { fs := GetStore() fs.mu.RLock() defer fs.mu.RUnlock() ids, exists := fs.byParent[parentID] if !exists { return []*Forum{}, nil } result := make([]*Forum, 0, len(ids)) for _, id := range ids { if forum, exists := fs.GetByID(id); exists { result = append(result, forum) } } return result, nil } // Retrieves forum posts by a specific author func ByAuthor(authorID int) ([]*Forum, error) { fs := GetStore() fs.mu.RLock() defer fs.mu.RUnlock() ids, exists := fs.byAuthor[authorID] if !exists { return []*Forum{}, nil } result := make([]*Forum, 0, len(ids)) for _, id := range ids { if forum, exists := fs.GetByID(id); exists { result = append(result, forum) } } return result, nil } // Retrieves the most recent forum activity (limited by count) func Recent(limit int) ([]*Forum, error) { fs := GetStore() fs.mu.RLock() defer fs.mu.RUnlock() if limit > len(fs.allByLastPost) { limit = len(fs.allByLastPost) } result := make([]*Forum, 0, limit) for i := 0; i < limit; i++ { if forum, exists := fs.GetByID(fs.allByLastPost[i]); exists { result = append(result, forum) } } return result, nil } // Retrieves forum posts containing the search term in title or content func Search(term string) ([]*Forum, error) { fs := GetStore() fs.mu.RLock() defer fs.mu.RUnlock() var result []*Forum lowerTerm := strings.ToLower(term) for _, id := range fs.allByLastPost { if forum, exists := fs.GetByID(id); exists { if strings.Contains(strings.ToLower(forum.Title), lowerTerm) || strings.Contains(strings.ToLower(forum.Content), lowerTerm) { result = append(result, forum) } } } return result, nil } // Retrieves forum posts with activity since a specific timestamp func Since(since int64) ([]*Forum, error) { fs := GetStore() fs.mu.RLock() defer fs.mu.RUnlock() var result []*Forum for _, id := range fs.allByLastPost { if forum, exists := fs.GetByID(id); exists && forum.LastPost >= since { result = append(result, forum) } } return result, nil } // Saves a new forum post to the in-memory store and sets the ID func (f *Forum) Insert() error { fs := GetStore() // Validate before insertion if err := f.Validate(); err != nil { return fmt.Errorf("validation failed: %w", err) } // Assign new ID if not set if f.ID == 0 { f.ID = fs.GetNextID() } // Add to store fs.AddForum(f) return nil } // Returns the posted timestamp as a time.Time func (f *Forum) PostedTime() time.Time { return time.Unix(f.Posted, 0) } // Returns the last post timestamp as a time.Time func (f *Forum) LastPostTime() time.Time { return time.Unix(f.LastPost, 0) } // Sets the posted timestamp from a time.Time func (f *Forum) SetPostedTime(t time.Time) { f.Posted = t.Unix() } // Sets the last post timestamp from a time.Time func (f *Forum) SetLastPostTime(t time.Time) { f.LastPost = t.Unix() } // Returns true if this is a top-level thread (parent = 0) func (f *Forum) IsThread() bool { return f.Parent == 0 } // Returns true if this is a reply to another post (parent > 0) func (f *Forum) IsReply() bool { return f.Parent > 0 } // Returns true if this post has replies func (f *Forum) HasReplies() bool { return f.Replies > 0 } // Returns true if there has been activity within the last 24 hours func (f *Forum) IsRecentActivity() bool { return time.Since(f.LastPostTime()) < 24*time.Hour } // Returns how long ago the last activity occurred func (f *Forum) ActivityAge() time.Duration { return time.Since(f.LastPostTime()) } // Returns how long ago the post was originally made func (f *Forum) PostAge() time.Duration { return time.Since(f.PostedTime()) } // Returns true if the given user ID is the author of this post func (f *Forum) IsAuthor(userID int) bool { return f.Author == userID } // Returns a truncated version of the content for previews 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] + "..." } // Returns the number of words in the content 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 } // Returns the character length of the content func (f *Forum) Length() int { return len(f.Content) } // Returns true if the title or content contains the given term (case-insensitive) 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) } // Updates the last_post timestamp to current time func (f *Forum) UpdateLastPost() { f.LastPost = time.Now().Unix() } // Increments the reply count func (f *Forum) IncrementReplies() { f.Replies++ } // Decrements the reply count (minimum 0) func (f *Forum) DecrementReplies() { if f.Replies > 0 { f.Replies-- } } // Retrieves all direct replies to this post func (f *Forum) GetReplies() ([]*Forum, error) { return ByParent(f.ID) } // Retrieves the parent thread (if this is a reply) or returns self (if this is a thread) func (f *Forum) GetThread() (*Forum, error) { if f.IsThread() { return f, nil } return Find(f.Parent) }