2017-11-11 23:34:27 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
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
|
|
|
|
IPAddress string
|
|
|
|
ActorID int
|
|
|
|
DoneAt string
|
|
|
|
}
|
|
|
|
|
2017-11-23 05:37:08 +00:00
|
|
|
type LogStore interface {
|
|
|
|
Create(action string, elementID int, elementType string, ipaddress string, actorID int) (err error)
|
|
|
|
GlobalCount() 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?
|
2017-11-23 05:37:08 +00:00
|
|
|
func (store *SQLModLogStore) Create(action string, elementID int, elementType string, ipaddress string, actorID int) (err error) {
|
|
|
|
_, err = store.create.Exec(action, elementID, elementType, ipaddress, actorID)
|
2017-11-11 23:34:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-23 05:37:08 +00:00
|
|
|
func (store *SQLModLogStore) GlobalCount() (logCount int) {
|
|
|
|
err := store.count.QueryRow().Scan(&logCount)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return logCount
|
|
|
|
}
|
|
|
|
|
2018-05-16 10:46:14 +00:00
|
|
|
func buildLogList(rows *sql.Rows) (logs []LogItem, err error) {
|
|
|
|
for rows.Next() {
|
|
|
|
var log LogItem
|
|
|
|
err := rows.Scan(&log.Action, &log.ElementID, &log.ElementType, &log.IPAddress, &log.ActorID, &log.DoneAt)
|
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
logs = append(logs, log)
|
|
|
|
}
|
|
|
|
return logs, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *SQLModLogStore) GetOffset(offset int, perPage int) (logs []LogItem, err error) {
|
|
|
|
rows, err := store.getOffset.Query(offset, perPage)
|
|
|
|
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?
|
2017-11-23 05:37:08 +00:00
|
|
|
func (store *SQLAdminLogStore) Create(action string, elementID int, elementType string, ipaddress string, actorID int) (err error) {
|
|
|
|
_, err = store.create.Exec(action, elementID, elementType, ipaddress, actorID)
|
2017-11-11 23:34:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-11-23 05:37:08 +00:00
|
|
|
|
|
|
|
func (store *SQLAdminLogStore) GlobalCount() (logCount int) {
|
|
|
|
err := store.count.QueryRow().Scan(&logCount)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return logCount
|
|
|
|
}
|
2018-05-16 10:46:14 +00:00
|
|
|
|
|
|
|
func (store *SQLAdminLogStore) GetOffset(offset int, perPage int) (logs []LogItem, err error) {
|
|
|
|
rows, err := store.getOffset.Query(offset, perPage)
|
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return buildLogList(rows)
|
|
|
|
}
|