2017-11-11 23:34:27 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2018-12-14 04:08:53 +00:00
|
|
|
"time"
|
2017-11-11 23:34:27 +00:00
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/query_gen"
|
2017-11-11 23:34:27 +00:00
|
|
|
)
|
|
|
|
|
2017-11-23 05:37:08 +00:00
|
|
|
var ModLogs LogStore
|
|
|
|
var AdminLogs LogStore
|
|
|
|
|
2018-05-16 10:46:14 +00:00
|
|
|
type LogItem struct {
|
|
|
|
Action string
|
|
|
|
ElementID int
|
|
|
|
ElementType string
|
2019-08-31 22:34:43 +00:00
|
|
|
IP string
|
2018-05-16 10:46:14 +00:00
|
|
|
ActorID int
|
|
|
|
DoneAt string
|
|
|
|
}
|
|
|
|
|
2017-11-23 05:37:08 +00:00
|
|
|
type LogStore interface {
|
2019-08-31 22:34:43 +00:00
|
|
|
Create(action string, elementID int, elementType string, ip string, actorID int) (err error)
|
2019-06-01 12:31:48 +00:00
|
|
|
Count() int
|
2018-05-16 10:46:14 +00:00
|
|
|
GetOffset(offset int, perPage int) (logs []LogItem, err error)
|
2017-11-11 23:34:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 05:37:08 +00:00
|
|
|
type SQLModLogStore struct {
|
2018-05-16 10:46:14 +00:00
|
|
|
create *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
getOffset *sql.Stmt
|
2017-11-23 05:37:08 +00:00
|
|
|
}
|
2017-11-11 23:34:27 +00:00
|
|
|
|
2018-05-16 10:46:14 +00:00
|
|
|
func NewModLogStore(acc *qgen.Accumulator) (*SQLModLogStore, error) {
|
2017-11-23 05:37:08 +00:00
|
|
|
return &SQLModLogStore{
|
2018-05-16 10:46:14 +00:00
|
|
|
create: acc.Insert("moderation_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
|
|
|
|
count: acc.Count("moderation_logs").Prepare(),
|
|
|
|
getOffset: acc.Select("moderation_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Orderby("doneAt DESC").Limit("?,?").Prepare(),
|
2017-11-23 05:37:08 +00:00
|
|
|
}, acc.FirstError()
|
2017-11-11 23:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Make a store for this?
|
2019-08-31 22:34:43 +00:00
|
|
|
func (s *SQLModLogStore) Create(action string, elementID int, elementType string, ip string, actorID int) (err error) {
|
|
|
|
_, err = s.create.Exec(action, elementID, elementType, ip, actorID)
|
2017-11-11 23:34:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
func (s *SQLModLogStore) Count() (count int) {
|
|
|
|
err := s.count.QueryRow().Scan(&count)
|
2017-11-23 05:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
return count
|
2017-11-23 05:37:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 10:46:14 +00:00
|
|
|
func buildLogList(rows *sql.Rows) (logs []LogItem, err error) {
|
|
|
|
for rows.Next() {
|
2019-08-31 22:34:43 +00:00
|
|
|
var l LogItem
|
2018-12-14 04:08:53 +00:00
|
|
|
var doneAt time.Time
|
2019-08-31 22:34:43 +00:00
|
|
|
err := rows.Scan(&l.Action, &l.ElementID, &l.ElementType, &l.IP, &l.ActorID, &doneAt)
|
2018-05-16 10:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
2019-08-31 22:34:43 +00:00
|
|
|
l.DoneAt = doneAt.Format("2006-01-02 15:04:05")
|
|
|
|
logs = append(logs, l)
|
2018-05-16 10:46:14 +00:00
|
|
|
}
|
|
|
|
return logs, rows.Err()
|
|
|
|
}
|
|
|
|
|
2019-08-31 22:34:43 +00:00
|
|
|
func (s *SQLModLogStore) GetOffset(offset int, perPage int) (logs []LogItem, err error) {
|
|
|
|
rows, err := s.getOffset.Query(offset, perPage)
|
2018-05-16 10:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return buildLogList(rows)
|
|
|
|
}
|
|
|
|
|
2017-11-23 05:37:08 +00:00
|
|
|
type SQLAdminLogStore struct {
|
2018-05-16 10:46:14 +00:00
|
|
|
create *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
getOffset *sql.Stmt
|
2017-11-23 05:37:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 10:46:14 +00:00
|
|
|
func NewAdminLogStore(acc *qgen.Accumulator) (*SQLAdminLogStore, error) {
|
2017-11-23 05:37:08 +00:00
|
|
|
return &SQLAdminLogStore{
|
2018-05-16 10:46:14 +00:00
|
|
|
create: acc.Insert("administration_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
|
|
|
|
count: acc.Count("administration_logs").Prepare(),
|
|
|
|
getOffset: acc.Select("administration_logs").Columns("action, elementID, elementType, ipaddress, actorID, doneAt").Orderby("doneAt DESC").Limit("?,?").Prepare(),
|
2017-11-23 05:37:08 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2017-11-11 23:34:27 +00:00
|
|
|
// TODO: Make a store for this?
|
2019-08-31 22:34:43 +00:00
|
|
|
func (s *SQLAdminLogStore) Create(action string, elementID int, elementType string, ip string, actorID int) (err error) {
|
|
|
|
_, err = s.create.Exec(action, elementID, elementType, ip, actorID)
|
2017-11-11 23:34:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-11-23 05:37:08 +00:00
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
func (s *SQLAdminLogStore) Count() (count int) {
|
|
|
|
err := s.count.QueryRow().Scan(&count)
|
2017-11-23 05:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
return count
|
2017-11-23 05:37:08 +00:00
|
|
|
}
|
2018-05-16 10:46:14 +00:00
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
func (s *SQLAdminLogStore) GetOffset(offset int, perPage int) (logs []LogItem, err error) {
|
|
|
|
rows, err := s.getOffset.Query(offset, perPage)
|
2018-05-16 10:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return buildLogList(rows)
|
|
|
|
}
|