canopeas/database/unban.go

50 lines
1.1 KiB
Go

package database
import (
"strconv"
"time"
"go.etcd.io/bbolt"
)
type UnbanRecord struct {
Username string
Expiration time.Time
}
func (u UnbanRecord) ExpirationString() string {
return u.Expiration.Format("01/02/2006 at 03:04:05 pm MST")
}
func (db *Database) ListUnbans() []UnbanRecord {
unbans := make([]UnbanRecord, 0)
_ = db.db.View(func(tx *bbolt.Tx) error {
return tx.Bucket(unbanBucket).ForEach(func(username, unix []byte) error {
unixNum, err := strconv.ParseInt(string(unix), 10, 64)
if err != nil {
return err
}
unbans = append(unbans, UnbanRecord{
Username: string(username),
Expiration: time.Unix(unixNum, 0),
})
return nil
})
})
return unbans
}
func (db *Database) AddUnban(record UnbanRecord) error {
usernameByte := []byte(record.Username)
return db.db.Update(func(tx *bbolt.Tx) error {
return tx.Bucket(unbanBucket).Put(usernameByte, []byte(strconv.FormatInt(record.Expiration.Unix(), 10)))
})
}
func (db *Database) RemoveUnban(username string) error {
usernameByte := []byte(username)
return db.db.Update(func(tx *bbolt.Tx) error {
return tx.Bucket(unbanBucket).Delete(usernameByte)
})
}