2019-06-05 06:00:40 +00:00
|
|
|
package common
|
|
|
|
|
2019-06-16 06:30:58 +00:00
|
|
|
import (
|
2019-07-26 23:13:43 +00:00
|
|
|
"errors"
|
2019-08-14 10:39:04 +00:00
|
|
|
"time"
|
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
//"strconv"
|
2019-06-16 06:30:58 +00:00
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
2019-06-05 06:00:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
conversations
|
|
|
|
conversations_posts
|
|
|
|
*/
|
2019-07-26 23:13:43 +00:00
|
|
|
var Convos ConversationStore
|
2019-06-09 03:21:48 +00:00
|
|
|
var convoStmts ConvoStmts
|
|
|
|
|
|
|
|
type ConvoStmts struct {
|
2019-08-14 10:39:04 +00:00
|
|
|
fetchPost *sql.Stmt
|
|
|
|
getPosts *sql.Stmt
|
2019-07-26 23:13:43 +00:00
|
|
|
countPosts *sql.Stmt
|
2019-08-14 10:39:04 +00:00
|
|
|
edit *sql.Stmt
|
|
|
|
create *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
2019-06-16 06:30:58 +00:00
|
|
|
|
2019-08-14 10:39:04 +00:00
|
|
|
editPost *sql.Stmt
|
2019-06-16 06:30:58 +00:00
|
|
|
createPost *sql.Stmt
|
2019-08-14 10:39:04 +00:00
|
|
|
deletePost *sql.Stmt
|
2019-06-09 03:21:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:39:04 +00:00
|
|
|
func init() {
|
2019-06-16 06:30:58 +00:00
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
2019-06-09 03:21:48 +00:00
|
|
|
convoStmts = ConvoStmts{
|
2019-08-14 10:39:04 +00:00
|
|
|
fetchPost: acc.Select("conversations_posts").Columns("cid, body, post, createdBy").Where("pid = ?").Prepare(),
|
|
|
|
getPosts: acc.Select("conversations_posts").Columns("pid, body, post, createdBy").Where("cid = ?").Limit("?,?").Prepare(),
|
2019-07-26 23:13:43 +00:00
|
|
|
countPosts: acc.Count("conversations_posts").Where("cid = ?").Prepare(),
|
2019-08-14 10:39:04 +00:00
|
|
|
edit: acc.Update("conversations").Set("lastReplyBy = ?, lastReplyAt = ?").Where("cid = ?").Prepare(),
|
|
|
|
create: acc.Insert("conversations").Columns("createdAt, lastReplyAt").Fields("UTC_TIMESTAMP(),UTC_TIMESTAMP()").Prepare(),
|
2019-07-26 23:13:43 +00:00
|
|
|
|
2019-08-14 10:39:04 +00:00
|
|
|
editPost: acc.Update("conversations_posts").Set("body = ?, post = ?").Where("cid = ?").Prepare(),
|
2019-07-26 23:13:43 +00:00
|
|
|
createPost: acc.Insert("conversations_posts").Columns("cid, body, post, createdBy").Fields("?,?,?,?").Prepare(),
|
2019-08-14 10:39:04 +00:00
|
|
|
deletePost: acc.Delete("conversations_posts").Where("pid = ?").Prepare(),
|
2019-06-09 03:21:48 +00:00
|
|
|
}
|
|
|
|
return acc.FirstError()
|
2019-06-16 06:30:58 +00:00
|
|
|
})
|
2019-08-14 10:39:04 +00:00
|
|
|
}
|
2019-06-09 03:21:48 +00:00
|
|
|
|
2019-06-05 06:00:40 +00:00
|
|
|
type Conversation struct {
|
2019-08-14 10:39:04 +00:00
|
|
|
ID int
|
|
|
|
CreatedBy int
|
|
|
|
CreatedAt time.Time
|
2019-07-26 23:13:43 +00:00
|
|
|
LastReplyBy int
|
2019-06-16 06:30:58 +00:00
|
|
|
LastReplyAt time.Time
|
|
|
|
}
|
|
|
|
|
2019-08-14 10:39:04 +00:00
|
|
|
func (co *Conversation) Posts(offset, itemsPerPage int) (posts []*ConversationPost, err error) {
|
|
|
|
rows, err := convoStmts.getPosts.Query(co.ID, offset, itemsPerPage)
|
2019-06-16 06:30:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
2019-08-14 10:39:04 +00:00
|
|
|
p := &ConversationPost{CID: co.ID}
|
|
|
|
err := rows.Scan(&p.ID, &p.Body, &p.Post, &p.CreatedBy)
|
2019-06-16 06:30:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-14 10:39:04 +00:00
|
|
|
p, err = ConvoPostProcess.OnLoad(p)
|
2019-06-16 06:30:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-14 10:39:04 +00:00
|
|
|
posts = append(posts, p)
|
2019-06-16 06:30:58 +00:00
|
|
|
}
|
2019-08-14 10:39:04 +00:00
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
return posts, rows.Err()
|
2019-06-09 03:21:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
func (co *Conversation) PostsCount() (count int) {
|
|
|
|
err := convoStmts.countPosts.QueryRow(co.ID).Scan(&count)
|
2019-06-09 03:21:48 +00:00
|
|
|
if err != nil {
|
2019-07-26 23:13:43 +00:00
|
|
|
LogError(err)
|
2019-06-09 03:21:48 +00:00
|
|
|
}
|
2019-07-26 23:13:43 +00:00
|
|
|
return count
|
2019-06-16 06:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
func (co *Conversation) Update() error {
|
2019-08-14 10:39:04 +00:00
|
|
|
_, err := convoStmts.edit.Exec(co.CreatedAt, co.LastReplyBy, co.LastReplyAt, co.ID)
|
2019-06-16 06:30:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
func (co *Conversation) Create() (int, error) {
|
2019-08-14 10:39:04 +00:00
|
|
|
res, err := convoStmts.create.Exec()
|
2019-06-16 06:30:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
return int(lastID), err
|
2019-06-05 06:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConversationStore interface {
|
|
|
|
Get(id int) (*Conversation, error)
|
2019-07-26 23:13:43 +00:00
|
|
|
GetUser(uid int, offset int) (cos []*Conversation, err error)
|
|
|
|
GetUserCount(uid int) (count int)
|
2019-06-05 06:00:40 +00:00
|
|
|
Delete(id int) error
|
|
|
|
Count() (count int)
|
2019-07-26 23:13:43 +00:00
|
|
|
Create(content string, createdBy int, participants []int) (int, error)
|
2019-06-05 06:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultConversationStore struct {
|
2019-08-14 10:39:04 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
getUser *sql.Stmt
|
|
|
|
getUserCount *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
|
|
|
deletePosts *sql.Stmt
|
|
|
|
deleteParticipants *sql.Stmt
|
|
|
|
create *sql.Stmt
|
2019-07-26 23:13:43 +00:00
|
|
|
addParticipant *sql.Stmt
|
2019-08-14 10:39:04 +00:00
|
|
|
count *sql.Stmt
|
2019-06-05 06:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultConversationStore(acc *qgen.Accumulator) (*DefaultConversationStore, error) {
|
|
|
|
return &DefaultConversationStore{
|
2019-08-14 10:39:04 +00:00
|
|
|
get: acc.Select("conversations").Columns("createdBy, createdAt, lastReplyBy, lastReplyAt").Where("cid = ?").Prepare(),
|
|
|
|
getUser: acc.SimpleInnerJoin("conversations_participants AS cp", "conversations AS c", "cp.cid, c.createdBy, c.createdAt, c.lastReplyBy, c.lastReplyAt", "cp.cid = c.cid", "cp.uid = ?", "c.lastReplyAt DESC, c.createdAt DESC, c.cid DESC", "?,?"),
|
|
|
|
getUserCount: acc.Count("conversations_participants").Where("uid = ?").Prepare(),
|
|
|
|
delete: acc.Delete("conversations").Where("cid = ?").Prepare(),
|
|
|
|
deletePosts: acc.Delete("conversations_posts").Where("cid = ?").Prepare(),
|
|
|
|
deleteParticipants: acc.Delete("conversations_participants").Where("cid = ?").Prepare(),
|
|
|
|
create: acc.Insert("conversations").Columns("createdBy, createdAt, lastReplyAt").Fields("?,UTC_TIMESTAMP(),UTC_TIMESTAMP()").Prepare(),
|
2019-07-26 23:13:43 +00:00
|
|
|
addParticipant: acc.Insert("conversations_participants").Columns("uid, cid").Fields("?,?").Prepare(),
|
2019-08-14 10:39:04 +00:00
|
|
|
count: acc.Count("conversations").Prepare(),
|
2019-06-05 06:00:40 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultConversationStore) Get(id int) (*Conversation, error) {
|
2019-06-16 06:30:58 +00:00
|
|
|
convo := &Conversation{ID: id}
|
2019-08-14 10:39:04 +00:00
|
|
|
err := s.get.QueryRow(id).Scan(&convo.CreatedBy, &convo.CreatedAt, &convo.LastReplyBy, &convo.LastReplyAt)
|
2019-07-26 23:13:43 +00:00
|
|
|
return convo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultConversationStore) GetUser(uid int, offset int) (cos []*Conversation, err error) {
|
|
|
|
rows, err := s.getUser.Query(uid, offset, Config.ItemsPerPage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
co := &Conversation{}
|
2019-08-14 10:39:04 +00:00
|
|
|
err := rows.Scan(&co.ID, &co.CreatedBy, &co.CreatedAt, &co.LastReplyBy, &co.LastReplyAt)
|
2019-07-26 23:13:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cos = append(cos, co)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cos, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultConversationStore) GetUserCount(uid int) (count int) {
|
|
|
|
err := s.getUserCount.QueryRow(uid).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
2019-06-05 06:00:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
// TODO: Use a foreign key or transaction
|
2019-06-05 06:00:40 +00:00
|
|
|
func (s *DefaultConversationStore) Delete(id int) error {
|
|
|
|
_, err := s.delete.Exec(id)
|
2019-07-26 23:13:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = s.deletePosts.Exec(id)
|
2019-08-14 10:39:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = s.deleteParticipants.Exec(id)
|
2019-06-05 06:00:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
func (s *DefaultConversationStore) Create(content string, createdBy int, participants []int) (int, error) {
|
|
|
|
if len(participants) == 0 {
|
|
|
|
return 0, errors.New("no participants set")
|
|
|
|
}
|
2019-08-14 10:39:04 +00:00
|
|
|
res, err := s.create.Exec(createdBy)
|
2019-07-26 23:13:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
post := &ConversationPost{}
|
|
|
|
post.CID = int(lastID)
|
|
|
|
post.Body = content
|
|
|
|
post.CreatedBy = createdBy
|
|
|
|
_, err = post.Create()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range participants {
|
2019-08-14 10:39:04 +00:00
|
|
|
_, err := s.addParticipant.Exec(p, lastID)
|
2019-07-26 23:13:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 10:39:04 +00:00
|
|
|
_, err = s.addParticipant.Exec(createdBy, lastID)
|
2019-07-26 23:13:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(lastID), err
|
|
|
|
}
|
|
|
|
|
2019-06-05 06:00:40 +00:00
|
|
|
// 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
|
2019-08-14 10:39:04 +00:00
|
|
|
}
|