diff --git a/common/profile_reply_store.go b/common/profile_reply_store.go index 0565accd..ebcacc5e 100644 --- a/common/profile_reply_store.go +++ b/common/profile_reply_store.go @@ -11,6 +11,7 @@ var Prstore ProfileReplyStore type ProfileReplyStore interface { Get(id int) (*ProfileReply, error) Exists(id int) bool + ClearIPs() error Create(profileID int, content string, createdBy int, ip string) (id int, err error) Count() (count int) } @@ -22,54 +23,59 @@ type SQLProfileReplyStore struct { exists *sql.Stmt create *sql.Stmt count *sql.Stmt + + clearIPs *sql.Stmt } func NewSQLProfileReplyStore(acc *qgen.Accumulator) (*SQLProfileReplyStore, error) { ur := "users_replies" 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(), - create: acc.Insert(ur).Columns("uid, content, parsed_content, createdAt, createdBy, ip").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(), - count: acc.Count(ur).Prepare(), + create: acc.Insert(ur).Columns("uid,content,parsed_content,createdAt,createdBy,ip").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(), + count: acc.Count(ur).Stmt(), + + clearIPs: acc.Update(ur).Set("ip=''").Where("ip!=''").Stmt(), }, acc.FirstError() } func (s *SQLProfileReplyStore) Get(id int) (*ProfileReply, error) { r := ProfileReply{ID: id} - err := s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP) - return &r, err + e := s.get.QueryRow(id).Scan(&r.ParentID, &r.Content, &r.CreatedBy, &r.CreatedAt, &r.LastEdit, &r.LastEditBy, &r.IP) + return &r, e } func (s *SQLProfileReplyStore) Exists(id int) bool { - err := s.exists.QueryRow(id).Scan(&id) - if err != nil && err != ErrNoRows { - LogError(err) + e := s.exists.QueryRow(id).Scan(&id) + if e != nil && e != ErrNoRows { + 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 { ip = "" } - res, err := s.create.Exec(profileID, content, ParseMessage(content, 0, "", nil, nil), createdBy, ip) - if err != nil { - return 0, err + res, e := s.create.Exec(profileID, content, ParseMessage(content, 0, "", nil, nil), createdBy, ip) + if e != nil { + return 0, e } - lastID, err := res.LastInsertId() - if err != nil { - return 0, err + lastID, e := res.LastInsertId() + if e != nil { + return 0, e } // Should we reload the user? - return int(lastID), err + return int(lastID), e } // TODO: Write a test for this // Count returns the total number of topic replies on these forums func (s *SQLProfileReplyStore) Count() (count int) { - err := s.count.QueryRow().Scan(&count) - if err != nil { - LogError(err) - } - return count + return Count(s.count) } diff --git a/common/reply_store.go b/common/reply_store.go index 819eee90..d68f3fd5 100644 --- a/common/reply_store.go +++ b/common/reply_store.go @@ -13,6 +13,7 @@ type ReplyStore interface { 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) @@ -33,6 +34,8 @@ type SQLReplyStore struct { count *sql.Stmt countUser *sql.Stmt countWordUser *sql.Stmt + + clearIPs *sql.Stmt } 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(), 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() } @@ -96,6 +101,11 @@ func (s *SQLReplyStore) Exists(id int) bool { return err != ErrNoRows } +func (s *SQLReplyStore) ClearIPs() error { + _, e := s.clearIPs.Exec() + return e +} + // TODO: Write a test for this func (s *SQLReplyStore) Create(t *Topic, content, ip string, uid int) (id int, err error) { if Config.DisablePostIP { diff --git a/common/user_store.go b/common/user_store.go index 02bc4a50..66c4bfce 100644 --- a/common/user_store.go +++ b/common/user_store.go @@ -30,6 +30,7 @@ type UserStore interface { //BulkGet(ids []int) ([]*User, error) BulkGetMap(ids []int) (map[int]*User, error) BypassGet(id int) (*User, error) + ClearLastIPs() error Create(name, password, email string, group int, active bool) (int, error) Reload(id int) error Count() int @@ -53,6 +54,8 @@ type DefaultUserStore struct { count *sql.Stmt countSearch *sql.Stmt + + clearIPs *sql.Stmt } // NewDefaultUserStore gives you a new instance of DefaultUserStore @@ -79,6 +82,8 @@ func NewDefaultUserStore(cache UserCache) (*DefaultUserStore, error) { count: acc.Count(u).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() } @@ -412,6 +417,11 @@ func (s *DefaultUserStore) Exists(id int) bool { return err != ErrNoRows } +func (s *DefaultUserStore) ClearLastIPs() error { + _, e := s.clearIPs.Exec() + return e +} + // TODO: Change active to a bool? // TODO: Use unique keys for the usernames func (s *DefaultUserStore) Create(name, password, email string, group int, active bool) (int, error) {