From 9c19fc92ca120330e43f61b454d535b8a34f8535 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 5 Jun 2019 16:00:40 +1000 Subject: [PATCH] Use a like store to abstract some things. Provisional conversations. --- common/conversations.go | 61 +++++++++++++++++++++++++++++++++++++++++ common/likes.go | 57 ++++++++++++++++++++++++++++++++++++++ common/topic.go | 20 +++----------- main.go | 4 +++ routes/panel/debug.go | 7 +---- 5 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 common/conversations.go create mode 100644 common/likes.go diff --git a/common/conversations.go b/common/conversations.go new file mode 100644 index 00000000..10032152 --- /dev/null +++ b/common/conversations.go @@ -0,0 +1,61 @@ +package common + +import "database/sql" +import "github.com/Azareal/Gosora/query_gen" + +/* +conversations +conversations_posts +*/ + +type Conversation struct { + ID int + Participants string +} + +func (co *Conversation) Create() (int, error) { + return 0, sql.ErrNoRows +} + +type ConversationPost struct { +} + +type ConversationStore interface { + Get(id int) (*Conversation, error) + Delete(id int) error + Count() (count int) +} + +type DefaultConversationStore struct { + get *sql.Stmt + delete *sql.Stmt + count *sql.Stmt +} + +func NewDefaultConversationStore(acc *qgen.Accumulator) (*DefaultConversationStore, error) { + return &DefaultConversationStore{ + get: acc.Select("conversations").Columns("participants").Where("cid = ?").Prepare(), + delete: acc.Delete("conversations").Where("cid = ?").Prepare(), + count: acc.Count("conversations").Prepare(), + }, acc.FirstError() +} + +func (s *DefaultConversationStore) Get(id int) (*Conversation, error) { + convo := &Conversation{ID:id} + err := s.get.QueryRow(id).Scan(&convo.Participants) + return nil, err +} + +func (s *DefaultConversationStore) Delete(id int) error { + _, err := s.delete.Exec(id) + return err +} + +// Count returns the total number of topics on these forums +func (s *DefaultConversationStore) Count() (count int) { + err := s.count.QueryRow().Scan(&count) + if err != nil { + LogError(err) + } + return count +} \ No newline at end of file diff --git a/common/likes.go b/common/likes.go new file mode 100644 index 00000000..742b3612 --- /dev/null +++ b/common/likes.go @@ -0,0 +1,57 @@ +package common + +import "database/sql" +import "github.com/Azareal/Gosora/query_gen" + +var Likes LikeStore + +type LikeStore interface { + BulkExists(ids []int, sentBy int, targetType string) ([]int, error) + Count() (count int) +} + +type DefaultLikeStore struct { + count *sql.Stmt +} + +func NewDefaultLikeStore(acc *qgen.Accumulator) (*DefaultLikeStore, error) { + return &DefaultLikeStore{ + count: acc.Count("likes").Prepare(), + }, acc.FirstError() +} + +// TODO: Write a test for this +func (s *DefaultLikeStore) BulkExists(ids []int, sentBy int, targetType string) (eids []int, err error) { + rows, err := qgen.NewAcc().Select("likes").Columns("targetItem").Where("sentBy = ? AND targetType = ?").In("targetItem", ids).Query(sentBy,targetType) + if err == sql.ErrNoRows { + return nil, nil + } else if err != nil { + return nil, err + } + defer rows.Close() + + var id int + for rows.Next() { + err = rows.Scan(&id) + if err != nil { + return nil, err + } + eids = append(eids,id) + } + err = rows.Err() + if err != nil { + return nil, err + } + + return eids, nil +} + +// TODO: Write a test for this +// Count returns the total number of likes globally +func (s *DefaultLikeStore) Count() (count int) { + err := s.count.QueryRow().Scan(&count) + if err != nil { + LogError(err) + } + return count +} \ No newline at end of file diff --git a/common/topic.go b/common/topic.go index 45f7a1ec..3ee4a7c1 100644 --- a/common/topic.go +++ b/common/topic.go @@ -592,25 +592,13 @@ func (topic *TopicUser) Replies(offset int, pFrag int, user *User) (rlist []*Rep // TODO: Add a config setting to disable the liked query for a burst of extra speed if user.Liked > 0 && len(likedQueryList) > 1 /*&& user.LastLiked <= time.Now()*/ { - // TODO: Abstract this - rows, err := qgen.NewAcc().Select("likes").Columns("targetItem").Where("sentBy = ? AND targetType = 'replies'").In("targetItem", likedQueryList[1:]).Query(user.ID) - if err != nil && err != sql.ErrNoRows { - return nil, "", err - } - defer rows.Close() - - var likeRid int - for rows.Next() { - err := rows.Scan(&likeRid) - if err != nil { - return nil, "", err - } - rlist[likedMap[likeRid]].Liked = true - } - err = rows.Err() + eids, err := Likes.BulkExists(likedQueryList[1:], user.ID, "replies") if err != nil { return nil, "", err } + for _, eid := range eids { + rlist[likedMap[eid]].Liked = true + } } if user.Perms.EditReply && len(attachQueryList) > 0 { diff --git a/main.go b/main.go index 1de9775e..ca074f5a 100644 --- a/main.go +++ b/main.go @@ -128,6 +128,10 @@ func storeInit() (err error) { if err != nil { return errors.WithStack(err) } + c.Likes, err = c.NewDefaultLikeStore(acc) + if err != nil { + return errors.WithStack(err) + } err = phrases.InitPhrases(c.Site.Language) if err != nil { diff --git a/routes/panel/debug.go b/routes/panel/debug.go index 62f9b8ad..6695aa47 100644 --- a/routes/panel/debug.go +++ b/routes/panel/debug.go @@ -65,11 +65,6 @@ func Debug(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError { var count = func(tbl string) (int, error) { return qgen.NewAcc().Count(tbl).Total() } - // TODO: Implement a LikeStore and call Count on that instead - likes, err := count("likes") - if err != nil { - return c.InternalError(err,w,r) - } // TODO: Call Count on an attachment store attachs, err := count("attachments") if err != nil { @@ -130,7 +125,7 @@ func Debug(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError { if err != nil { return c.InternalError(err,w,r) } - debugDatabase := c.DebugPageDatabase{c.Topics.Count(),c.Users.Count(),c.Rstore.Count(),c.Prstore.Count(),c.Activity.Count(),likes,attachs,polls,loginLogs,regLogs,modLogs,adminLogs,views,viewsAgents,viewsForums,viewsLangs,viewsReferrers,viewsSystems,postChunks,topicChunks} + debugDatabase := c.DebugPageDatabase{c.Topics.Count(),c.Users.Count(),c.Rstore.Count(),c.Prstore.Count(),c.Activity.Count(),c.Likes.Count(),attachs,polls,loginLogs,regLogs,modLogs,adminLogs,views,viewsAgents,viewsForums,viewsLangs,viewsReferrers,viewsSystems,postChunks,topicChunks} staticSize, err := c.DirSize("./public/") if err != nil {