2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2017-11-06 07:44:08 +00:00
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
//import "log"
|
2020-01-02 21:52:41 +00:00
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"database/sql"
|
2020-01-02 21:52:41 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2020-01-02 21:52:41 +00:00
|
|
|
)
|
2017-11-06 07:44:08 +00:00
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var Rstore ReplyStore
|
2017-11-06 07:44:08 +00:00
|
|
|
|
|
|
|
type ReplyStore interface {
|
2022-02-21 03:32:53 +00:00
|
|
|
Get(id int) (*Reply, error)
|
|
|
|
Each(f func(*Reply) error) error
|
|
|
|
Exists(id int) bool
|
|
|
|
ClearIPs() error
|
|
|
|
Create(t *Topic, content, ip string, uid int) (id int, err error)
|
|
|
|
Count() (count int)
|
|
|
|
CountUser(uid int) (count int)
|
|
|
|
CountMegaUser(uid int) (count int)
|
|
|
|
CountBigUser(uid int) (count int)
|
|
|
|
|
|
|
|
SetCache(cache ReplyCache)
|
|
|
|
GetCache() ReplyCache
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SQLReplyStore struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
cache ReplyCache
|
2019-05-17 08:40:41 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
getAll *sql.Stmt
|
|
|
|
exists *sql.Stmt
|
|
|
|
create *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
countUser *sql.Stmt
|
|
|
|
countWordUser *sql.Stmt
|
2021-04-27 10:00:11 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
clearIPs *sql.Stmt
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
if cache == nil {
|
|
|
|
cache = NewNullReplyCache()
|
|
|
|
}
|
|
|
|
re := "replies"
|
|
|
|
return &SQLReplyStore{
|
|
|
|
cache: cache,
|
|
|
|
get: acc.Select(re).Columns("tid,content,createdBy,createdAt,lastEdit,lastEditBy,ip,likeCount,attachCount,actionType").Where("rid=?").Prepare(),
|
|
|
|
getAll: acc.Select(re).Columns("rid,tid,content,createdBy,createdAt,lastEdit,lastEditBy,ip,likeCount,attachCount,actionType").Prepare(),
|
|
|
|
exists: acc.Exists(re, "rid").Prepare(),
|
|
|
|
create: acc.Insert(re).Columns("tid,content,parsed_content,createdAt,lastUpdated,ip,words,createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(),
|
|
|
|
count: acc.Count(re).Prepare(),
|
|
|
|
countUser: acc.Count(re).Where("createdBy=?").Prepare(),
|
|
|
|
countWordUser: acc.Count(re).Where("createdBy=? AND words>=?").Prepare(),
|
|
|
|
|
|
|
|
clearIPs: acc.Update(re).Set("ip=''").Where("ip!=''").Stmt(),
|
|
|
|
}, acc.FirstError()
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
func (s *SQLReplyStore) Get(id int) (*Reply, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
r, err := s.cache.Get(id)
|
|
|
|
if err == nil {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r = &Reply{ID: id}
|
|
|
|
err = s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP, &r.LikeCount, &r.AttachCount, &r.ActionType)
|
|
|
|
if err == nil {
|
|
|
|
_ = s.cache.Set(r)
|
|
|
|
}
|
|
|
|
return r, err
|
2017-11-06 07:44:08 +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 *SQLReplyStore) eachr(f func(*sql.Rows) error) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
return eachall(s.getAll, f)
|
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 *SQLReplyStore) Each(f func(*Reply) error) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
rows, err := s.getAll.Query()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
r := new(Reply)
|
|
|
|
if err := rows.Scan(&r.ID, &r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP, &r.LikeCount, &r.AttachCount, &r.ActionType); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows.Err()
|
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 *SQLReplyStore) Exists(id int) bool {
|
2022-02-21 03:32:53 +00:00
|
|
|
err := s.exists.QueryRow(id).Scan(&id)
|
|
|
|
if err != nil && err != ErrNoRows {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return err != ErrNoRows
|
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
|
|
|
}
|
|
|
|
|
2021-04-27 10:00:11 +00:00
|
|
|
func (s *SQLReplyStore) ClearIPs() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
_, e := s.clearIPs.Exec()
|
|
|
|
return e
|
2021-04-27 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 07:44:08 +00:00
|
|
|
// TODO: Write a test for this
|
2020-02-26 10:34:38 +00:00
|
|
|
func (s *SQLReplyStore) Create(t *Topic, content, ip string, uid int) (id int, err error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
if Config.DisablePostIP {
|
|
|
|
ip = ""
|
|
|
|
}
|
|
|
|
res, err := s.create.Exec(t.ID, content, ParseMessage(content, t.ParentID, "forums", nil, nil), ip, WordCount(content), uid)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
id = int(lastID)
|
|
|
|
return id, t.AddReply(id, uid)
|
2017-11-06 07:44:08 +00:00
|
|
|
}
|
2019-05-17 08:40:41 +00:00
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
// TODO: Write a test for this
|
|
|
|
// Count returns the total number of topic replies on these forums
|
|
|
|
func (s *SQLReplyStore) Count() (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return Countf(s.count)
|
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 *SQLReplyStore) CountUser(uid int) (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return Countf(s.countUser, uid)
|
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 *SQLReplyStore) CountMegaUser(uid int) (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return Countf(s.countWordUser, uid, SettingBox.Load().(SettingMap)["megapost_min_words"].(int))
|
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 *SQLReplyStore) CountBigUser(uid int) (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return Countf(s.countWordUser, uid, SettingBox.Load().(SettingMap)["bigpost_min_words"].(int))
|
2019-06-01 12:31:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
func (s *SQLReplyStore) SetCache(cache ReplyCache) {
|
2022-02-21 03:32:53 +00:00
|
|
|
s.cache = cache
|
2019-05-17 08:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLReplyStore) GetCache() ReplyCache {
|
2022-02-21 03:32:53 +00:00
|
|
|
return s.cache
|
2019-05-17 08:40:41 +00:00
|
|
|
}
|