The forum list should be updated properly now when a topic is deleted.

This commit is contained in:
Azareal 2019-10-31 18:55:03 +10:00
parent c928c84c95
commit 1ad3e9c9da

View File

@ -10,12 +10,13 @@ import (
"database/sql"
"errors"
"log"
//"fmt"
"sort"
"sync"
"sync/atomic"
"github.com/Azareal/Gosora/query_gen"
qgen "github.com/Azareal/Gosora/query_gen"
)
var forumCreateMutex sync.Mutex
@ -70,6 +71,7 @@ type MemoryForumStore struct {
updateCache *sql.Stmt
addTopics *sql.Stmt
removeTopics *sql.Stmt
lastTopic *sql.Stmt
updateOrder *sql.Stmt
}
@ -87,6 +89,7 @@ func NewMemoryForumStore() (*MemoryForumStore, error) {
updateCache: acc.Update(f).Set("lastTopicID = ?, lastReplyerID = ?").Where("fid = ?").Prepare(),
addTopics: acc.Update(f).Set("topicCount=topicCount+?").Where("fid = ?").Prepare(),
removeTopics: acc.Update(f).Set("topicCount=topicCount-?").Where("fid = ?").Prepare(),
lastTopic: acc.Select("topics").Columns("tid").Where("parentID = ?").Orderby("lastReplyAt DESC, createdAt DESC").Limit("1").Prepare(),
updateOrder: acc.Update(f).Set("order = ?").Where("fid = ?").Prepare(),
}, acc.FirstError()
}
@ -184,7 +187,7 @@ func (s *MemoryForumStore) Get(id int) (*Forum, error) {
}
func (s *MemoryForumStore) BypassGet(id int) (*Forum, error) {
var f = &Forum{ID: id}
f := &Forum{ID: id}
err := s.get.QueryRow(id).Scan(&f.Name, &f.Desc, &f.Tmpl, &f.Active, &f.Order, &f.Preset, &f.ParentID, &f.ParentType, &f.TopicCount, &f.LastTopicID, &f.LastReplyerID)
if err != nil {
return nil, err
@ -308,12 +311,35 @@ func (s *MemoryForumStore) AddTopic(tid int, uid int, fid int) error {
return s.Reload(fid)
}
// TODO: Update the forum cache with the latest topic
// TODO: Make this update more atomic
func (s *MemoryForumStore) RemoveTopic(fid int) error {
_, err := s.removeTopics.Exec(1, fid)
if err != nil {
return err
}
var tid int
err = s.lastTopic.QueryRow(fid).Scan(&tid)
if err == sql.ErrNoRows {
_, err = s.updateCache.Exec(0, 0, fid)
if err != nil {
return err
}
s.Reload(fid)
return nil
}
if err != nil {
return err
}
topic, err := Topics.Get(tid)
if err != nil {
return err
}
_, err = s.updateCache.Exec(tid, topic.CreatedBy, fid)
if err != nil {
return err
}
// TODO: Bypass the database and update this with a lock or an unsafe atomic swap
s.Reload(fid)
return nil