Use a like store to abstract some things.

Provisional conversations.
This commit is contained in:
Azareal 2019-06-05 16:00:40 +10:00
parent 084f1bbeca
commit 9c19fc92ca
5 changed files with 127 additions and 22 deletions

61
common/conversations.go Normal file
View File

@ -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
}

57
common/likes.go Normal file
View File

@ -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
}

View File

@ -592,24 +592,12 @@ 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)
eids, err := Likes.BulkExists(likedQueryList[1:], user.ID, "replies")
if err != nil {
return nil, "", err
}
rlist[likedMap[likeRid]].Liked = true
}
err = rows.Err()
if err != nil {
return nil, "", err
for _, eid := range eids {
rlist[likedMap[eid]].Liked = true
}
}

View File

@ -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 {

View File

@ -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 {