Add ClearIPs() to ProfileReplyStore.

Add ClearIPs() to ReplyStore.
Add ClearLastIPs() to UserStore.
Use Count() in SQLProfileReplyStore.Count()
This commit is contained in:
Azareal 2021-04-27 20:00:11 +10:00
parent 67a968caf2
commit 1b7d6ac724
3 changed files with 48 additions and 22 deletions

View File

@ -11,6 +11,7 @@ var Prstore ProfileReplyStore
type ProfileReplyStore interface { type ProfileReplyStore interface {
Get(id int) (*ProfileReply, error) Get(id int) (*ProfileReply, error)
Exists(id int) bool Exists(id int) bool
ClearIPs() error
Create(profileID int, content string, createdBy int, ip string) (id int, err error) Create(profileID int, content string, createdBy int, ip string) (id int, err error)
Count() (count int) Count() (count int)
} }
@ -22,54 +23,59 @@ type SQLProfileReplyStore struct {
exists *sql.Stmt exists *sql.Stmt
create *sql.Stmt create *sql.Stmt
count *sql.Stmt count *sql.Stmt
clearIPs *sql.Stmt
} }
func NewSQLProfileReplyStore(acc *qgen.Accumulator) (*SQLProfileReplyStore, error) { func NewSQLProfileReplyStore(acc *qgen.Accumulator) (*SQLProfileReplyStore, error) {
ur := "users_replies" ur := "users_replies"
return &SQLProfileReplyStore{ return &SQLProfileReplyStore{
get: acc.Select(ur).Columns("uid, content, createdBy, createdAt, lastEdit, lastEditBy, ip").Where("rid=?").Prepare(), get: acc.Select(ur).Columns("uid,content,createdBy,createdAt,lastEdit,lastEditBy,ip").Where("rid=?").Stmt(),
exists: acc.Exists(ur, "rid").Prepare(), exists: acc.Exists(ur, "rid").Prepare(),
create: acc.Insert(ur).Columns("uid, content, parsed_content, createdAt, createdBy, ip").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(), create: acc.Insert(ur).Columns("uid,content,parsed_content,createdAt,createdBy,ip").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(),
count: acc.Count(ur).Prepare(), count: acc.Count(ur).Stmt(),
clearIPs: acc.Update(ur).Set("ip=''").Where("ip!=''").Stmt(),
}, acc.FirstError() }, acc.FirstError()
} }
func (s *SQLProfileReplyStore) Get(id int) (*ProfileReply, error) { func (s *SQLProfileReplyStore) Get(id int) (*ProfileReply, error) {
r := ProfileReply{ID: id} r := ProfileReply{ID: id}
err := s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP) e := s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP)
return &r, err return &r, e
} }
func (s *SQLProfileReplyStore) Exists(id int) bool { func (s *SQLProfileReplyStore) Exists(id int) bool {
err := s.exists.QueryRow(id).Scan(&id) e := s.exists.QueryRow(id).Scan(&id)
if err != nil && err != ErrNoRows { if e != nil && e != ErrNoRows {
LogError(err) LogError(e)
} }
return err != ErrNoRows return e != ErrNoRows
} }
func (s *SQLProfileReplyStore) Create(profileID int, content string, createdBy int, ip string) (id int, err error) { func (s *SQLProfileReplyStore) ClearIPs() error {
_, e := s.clearIPs.Exec()
return e
}
func (s *SQLProfileReplyStore) Create(profileID int, content string, createdBy int, ip string) (id int, e error) {
if Config.DisablePostIP { if Config.DisablePostIP {
ip = "" ip = ""
} }
res, err := s.create.Exec(profileID, content, ParseMessage(content, 0, "", nil, nil), createdBy, ip) res, e := s.create.Exec(profileID, content, ParseMessage(content, 0, "", nil, nil), createdBy, ip)
if err != nil { if e != nil {
return 0, err return 0, e
} }
lastID, err := res.LastInsertId() lastID, e := res.LastInsertId()
if err != nil { if e != nil {
return 0, err return 0, e
} }
// Should we reload the user? // Should we reload the user?
return int(lastID), err return int(lastID), e
} }
// TODO: Write a test for this // TODO: Write a test for this
// Count returns the total number of topic replies on these forums // Count returns the total number of topic replies on these forums
func (s *SQLProfileReplyStore) Count() (count int) { func (s *SQLProfileReplyStore) Count() (count int) {
err := s.count.QueryRow().Scan(&count) return Count(s.count)
if err != nil {
LogError(err)
}
return count
} }

View File

@ -13,6 +13,7 @@ type ReplyStore interface {
Get(id int) (*Reply, error) Get(id int) (*Reply, error)
Each(f func(*Reply) error) error Each(f func(*Reply) error) error
Exists(id int) bool Exists(id int) bool
ClearIPs() error
Create(t *Topic, content, ip string, uid int) (id int, err error) Create(t *Topic, content, ip string, uid int) (id int, err error)
Count() (count int) Count() (count int)
CountUser(uid int) (count int) CountUser(uid int) (count int)
@ -33,6 +34,8 @@ type SQLReplyStore struct {
count *sql.Stmt count *sql.Stmt
countUser *sql.Stmt countUser *sql.Stmt
countWordUser *sql.Stmt countWordUser *sql.Stmt
clearIPs *sql.Stmt
} }
func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore, error) { func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore, error) {
@ -49,6 +52,8 @@ func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore,
count: acc.Count(re).Prepare(), count: acc.Count(re).Prepare(),
countUser: acc.Count(re).Where("createdBy=?").Prepare(), countUser: acc.Count(re).Where("createdBy=?").Prepare(),
countWordUser: acc.Count(re).Where("createdBy=? AND words>=?").Prepare(), countWordUser: acc.Count(re).Where("createdBy=? AND words>=?").Prepare(),
clearIPs: acc.Update(re).Set("ip=''").Where("ip!=''").Stmt(),
}, acc.FirstError() }, acc.FirstError()
} }
@ -96,6 +101,11 @@ func (s *SQLReplyStore) Exists(id int) bool {
return err != ErrNoRows return err != ErrNoRows
} }
func (s *SQLReplyStore) ClearIPs() error {
_, e := s.clearIPs.Exec()
return e
}
// TODO: Write a test for this // TODO: Write a test for this
func (s *SQLReplyStore) Create(t *Topic, content, ip string, uid int) (id int, err error) { func (s *SQLReplyStore) Create(t *Topic, content, ip string, uid int) (id int, err error) {
if Config.DisablePostIP { if Config.DisablePostIP {

View File

@ -30,6 +30,7 @@ type UserStore interface {
//BulkGet(ids []int) ([]*User, error) //BulkGet(ids []int) ([]*User, error)
BulkGetMap(ids []int) (map[int]*User, error) BulkGetMap(ids []int) (map[int]*User, error)
BypassGet(id int) (*User, error) BypassGet(id int) (*User, error)
ClearLastIPs() error
Create(name, password, email string, group int, active bool) (int, error) Create(name, password, email string, group int, active bool) (int, error)
Reload(id int) error Reload(id int) error
Count() int Count() int
@ -53,6 +54,8 @@ type DefaultUserStore struct {
count *sql.Stmt count *sql.Stmt
countSearch *sql.Stmt countSearch *sql.Stmt
clearIPs *sql.Stmt
} }
// NewDefaultUserStore gives you a new instance of DefaultUserStore // NewDefaultUserStore gives you a new instance of DefaultUserStore
@ -79,6 +82,8 @@ func NewDefaultUserStore(cache UserCache) (*DefaultUserStore, error) {
count: acc.Count(u).Prepare(), count: acc.Count(u).Prepare(),
countSearch: acc.Count(u).Where("(name=? OR ?='') AND (email=? OR ?='') AND (group=? OR ?=0)").Prepare(), countSearch: acc.Count(u).Where("(name=? OR ?='') AND (email=? OR ?='') AND (group=? OR ?=0)").Prepare(),
clearIPs: acc.Update(u).Set("last_ip=''").Where("last_ip!=''").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
@ -412,6 +417,11 @@ func (s *DefaultUserStore) Exists(id int) bool {
return err != ErrNoRows return err != ErrNoRows
} }
func (s *DefaultUserStore) ClearLastIPs() error {
_, e := s.clearIPs.Exec()
return e
}
// TODO: Change active to a bool? // TODO: Change active to a bool?
// TODO: Use unique keys for the usernames // TODO: Use unique keys for the usernames
func (s *DefaultUserStore) Create(name, password, email string, group int, active bool) (int, error) { func (s *DefaultUserStore) Create(name, password, email string, group int, active bool) (int, error) {