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"
|
2020-02-10 10:28:33 +00:00
|
|
|
|
2019-08-28 06:47:54 +00:00
|
|
|
//"log"
|
2019-08-14 10:39:04 +00:00
|
|
|
|
2019-06-16 06:30:58 +00:00
|
|
|
"database/sql"
|
2020-02-10 10:28:33 +00:00
|
|
|
"strconv"
|
2019-06-16 06:30:58 +00:00
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
2019-06-05 06:00:40 +00:00
|
|
|
|
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-08-28 06:47:54 +00:00
|
|
|
has *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-08-21 01:31:13 +00:00
|
|
|
|
|
|
|
getUsers *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 {
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
cpo := "conversations_posts"
|
2019-06-09 03:21:48 +00:00
|
|
|
convoStmts = ConvoStmts{
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
fetchPost: acc.Select(cpo).Columns("cid,body,post,createdBy").Where("pid=?").Prepare(),
|
|
|
|
getPosts: acc.Select(cpo).Columns("pid,body,post,createdBy").Where("cid=?").Limit("?,?").Prepare(),
|
|
|
|
countPosts: acc.Count(cpo).Where("cid=?").Prepare(),
|
|
|
|
edit: acc.Update("conversations").Set("lastReplyBy=?,lastReplyAt=?").Where("cid=?").Prepare(),
|
|
|
|
create: acc.Insert("conversations").Columns("createdAt,lastReplyAt").Fields("UTC_TIMESTAMP(),UTC_TIMESTAMP()").Prepare(),
|
|
|
|
has: acc.Count("conversations_participants").Where("uid=? AND cid=?").Prepare(),
|
2019-07-26 23:13:43 +00:00
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
editPost: acc.Update(cpo).Set("body=?,post=?").Where("pid=?").Prepare(),
|
|
|
|
createPost: acc.Insert(cpo).Columns("cid,body,post,createdBy").Fields("?,?,?,?").Prepare(),
|
|
|
|
deletePost: acc.Delete(cpo).Where("pid=?").Prepare(),
|
2019-08-21 01:31:13 +00:00
|
|
|
|
2020-02-10 10:28:33 +00:00
|
|
|
getUsers: acc.Select("conversations_participants").Columns("uid").Where("cid=?").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-08-21 01:31:13 +00:00
|
|
|
func (co *Conversation) Uids() (ids []int, err error) {
|
|
|
|
rows, err := convoStmts.getUsers.Query(co.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var id int
|
|
|
|
err := rows.Scan(&id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
return ids, rows.Err()
|
|
|
|
}
|
|
|
|
|
2019-08-20 08:44:37 +00:00
|
|
|
func (co *Conversation) Has(uid int) (in bool) {
|
|
|
|
var count int
|
|
|
|
err := convoStmts.has.QueryRow(uid, co.ID).Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count > 0
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-28 06:47:54 +00:00
|
|
|
type ConversationExtra struct {
|
|
|
|
*Conversation
|
|
|
|
Users []*User
|
|
|
|
}
|
|
|
|
|
2019-06-05 06:00:40 +00:00
|
|
|
type ConversationStore interface {
|
|
|
|
Get(id int) (*Conversation, error)
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
GetUser(uid, offset int) (cos []*Conversation, err error)
|
2019-08-28 06:47:54 +00:00
|
|
|
GetUserExtra(uid int, offset int) (cos []*ConversationExtra, err error)
|
2019-07-26 23:13:43 +00:00
|
|
|
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-28 06:47:54 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
getUser *sql.Stmt
|
|
|
|
getUserCount *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
|
|
|
deletePosts *sql.Stmt
|
2019-08-14 10:39:04 +00:00
|
|
|
deleteParticipants *sql.Stmt
|
2019-08-28 06:47:54 +00:00
|
|
|
create *sql.Stmt
|
|
|
|
addParticipant *sql.Stmt
|
|
|
|
count *sql.Stmt
|
2019-06-05 06:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultConversationStore(acc *qgen.Accumulator) (*DefaultConversationStore, error) {
|
2020-02-10 10:28:33 +00:00
|
|
|
co := "conversations"
|
2019-06-05 06:00:40 +00:00
|
|
|
return &DefaultConversationStore{
|
2020-02-10 10:28:33 +00:00
|
|
|
get: acc.Select(co).Columns("createdBy, createdAt, lastReplyBy, lastReplyAt").Where("cid=?").Prepare(),
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
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(),
|
2020-02-10 10:28:33 +00:00
|
|
|
delete: acc.Delete(co).Where("cid=?").Prepare(),
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
deletePosts: acc.Delete("conversations_posts").Where("cid=?").Prepare(),
|
|
|
|
deleteParticipants: acc.Delete("conversations_participants").Where("cid=?").Prepare(),
|
2020-02-10 10:28:33 +00:00
|
|
|
create: acc.Insert(co).Columns("createdBy, createdAt, lastReplyBy, lastReplyAt").Fields("?,UTC_TIMESTAMP(),?,UTC_TIMESTAMP()").Prepare(),
|
|
|
|
addParticipant: acc.Insert("conversations_participants").Columns("uid,cid").Fields("?,?").Prepare(),
|
|
|
|
count: acc.Count(co).Prepare(),
|
2019-06-05 06:00:40 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultConversationStore) Get(id int) (*Conversation, error) {
|
2019-08-28 06:47:54 +00:00
|
|
|
co := &Conversation{ID: id}
|
|
|
|
err := s.get.QueryRow(id).Scan(&co.CreatedBy, &co.CreatedAt, &co.LastReplyBy, &co.LastReplyAt)
|
|
|
|
return co, err
|
2019-07-26 23:13:43 +00:00
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (s *DefaultConversationStore) GetUser(uid, offset int) (cos []*Conversation, err error) {
|
2019-07-26 23:13:43 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-02-10 10:28:33 +00:00
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(cos) == 0 {
|
|
|
|
err = sql.ErrNoRows
|
|
|
|
}
|
|
|
|
return cos, err
|
2019-07-26 23:13:43 +00:00
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*ConversationExtra, err error) {
|
2019-08-28 06:47:54 +00:00
|
|
|
raw, err := s.GetUser(uid, offset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//log.Printf("raw: %+v\n", raw)
|
|
|
|
|
|
|
|
if len(raw) == 1 {
|
|
|
|
//log.Print("r0b2")
|
|
|
|
uids, err := raw[0].Uids()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//log.Println("r1b2")
|
|
|
|
umap, err := Users.BulkGetMap(uids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//log.Println("r2b2")
|
2020-02-10 10:28:33 +00:00
|
|
|
users := make([]*User, len(umap))
|
2019-08-28 06:47:54 +00:00
|
|
|
var i int
|
|
|
|
for _, user := range umap {
|
|
|
|
users[i] = user
|
|
|
|
i++
|
|
|
|
}
|
2020-02-10 10:28:33 +00:00
|
|
|
return []*ConversationExtra{&ConversationExtra{raw[0], users}}, nil
|
2019-08-28 06:47:54 +00:00
|
|
|
}
|
|
|
|
//log.Println("1")
|
|
|
|
|
2020-02-10 10:28:33 +00:00
|
|
|
cmap := make(map[int]*ConversationExtra, len(raw))
|
2019-08-28 06:47:54 +00:00
|
|
|
for _, co := range raw {
|
2020-02-10 10:28:33 +00:00
|
|
|
cmap[co.ID] = &ConversationExtra{co, nil}
|
2019-08-28 06:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add a function for the q stuff
|
|
|
|
var q string
|
2020-02-10 10:28:33 +00:00
|
|
|
idList := make([]interface{}, len(raw))
|
2019-08-28 06:47:54 +00:00
|
|
|
for i, co := range raw {
|
|
|
|
idList[i] = strconv.Itoa(co.ID)
|
|
|
|
q += "?,"
|
|
|
|
}
|
|
|
|
q = q[0 : len(q)-1]
|
|
|
|
|
|
|
|
rows, err := qgen.NewAcc().Select("conversations_participants").Columns("uid,cid").Where("cid IN(" + q + ")").Query(idList...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
//log.Println("2")
|
|
|
|
|
|
|
|
idmap := make(map[int][]int) // cid: []uid
|
|
|
|
puidmap := make(map[int]struct{})
|
|
|
|
for rows.Next() {
|
|
|
|
var uid, cid int
|
|
|
|
err := rows.Scan(&uid, &cid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-10 10:28:33 +00:00
|
|
|
idmap[cid] = append(idmap[cid], uid)
|
2019-08-28 06:47:54 +00:00
|
|
|
puidmap[uid] = struct{}{}
|
|
|
|
}
|
2019-10-26 23:11:09 +00:00
|
|
|
if err = rows.Err(); err != nil {
|
2019-08-28 06:47:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//log.Println("3")
|
|
|
|
//log.Printf("idmap: %+v\n", idmap)
|
|
|
|
//log.Printf("puidmap: %+v\n",puidmap)
|
|
|
|
|
2020-02-10 10:28:33 +00:00
|
|
|
puids := make([]int, len(puidmap))
|
2019-08-28 06:47:54 +00:00
|
|
|
var i int
|
|
|
|
for puid, _ := range puidmap {
|
|
|
|
puids[i] = puid
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
umap, err := Users.BulkGetMap(puids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//log.Println("4")
|
|
|
|
//log.Printf("umap: %+v\n", umap)
|
|
|
|
for cid, uids := range idmap {
|
|
|
|
co := cmap[cid]
|
|
|
|
for _, uid := range uids {
|
2020-02-10 10:28:33 +00:00
|
|
|
co.Users = append(co.Users, umap[uid])
|
2019-08-28 06:47:54 +00:00
|
|
|
}
|
|
|
|
//log.Printf("co.Conversation: %+v\n", co.Conversation)
|
|
|
|
//log.Printf("co.Users: %+v\n", co.Users)
|
|
|
|
cmap[cid] = co
|
|
|
|
}
|
|
|
|
//log.Printf("cmap: %+v\n", cmap)
|
|
|
|
for _, ra := range raw {
|
2020-02-10 10:28:33 +00:00
|
|
|
cos = append(cos, cmap[ra.ID])
|
2019-08-28 06:47:54 +00:00
|
|
|
}
|
|
|
|
//log.Printf("cos: %+v\n", cos)
|
|
|
|
|
|
|
|
return cos, rows.Err()
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:13:43 +00:00
|
|
|
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")
|
|
|
|
}
|
2020-02-10 10:28:33 +00:00
|
|
|
res, err := s.create.Exec(createdBy,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
|
|
|
|
}
|
|
|
|
|
2019-08-28 06:47:54 +00:00
|
|
|
post := &ConversationPost{CID: int(lastID), Body: content, CreatedBy: createdBy}
|
2019-07-26 23:13:43 +00:00
|
|
|
_, 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
|
|
|
}
|