237 lines
5.9 KiB
Go
237 lines
5.9 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/br0xen/boltease"
|
|
)
|
|
|
|
func getDatabase() (*boltease.DB, error) {
|
|
db, err := boltease.Create("aocbot.db", 0644, nil)
|
|
if err != nil {
|
|
return db, err
|
|
}
|
|
// Make sure that we have a 'users' bucket
|
|
db.MkBucketPath([]string{"users"})
|
|
// Make sure that we have a 'channels' bucket
|
|
db.MkBucketPath([]string{"channels"})
|
|
// Make sure that we have a 'aocmembers' bucket
|
|
db.MkBucketPath([]string{"AoCMembers"})
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func saveUser(usr *User) error {
|
|
var err error
|
|
if err = db.OpenDB(); err != nil {
|
|
return err
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
usrPath := []string{"users", usr.ID}
|
|
db.SetValue(usrPath, "Name", usr.Name)
|
|
db.SetBool(usrPath, "Deleted", usr.Deleted)
|
|
db.SetValue(usrPath, "Status", usr.Status)
|
|
db.SetValue(usrPath, "Color", usr.Color)
|
|
db.SetValue(usrPath, "RealName", usr.RealName)
|
|
db.SetValue(usrPath, "TZ", usr.TZ)
|
|
db.SetValue(usrPath, "TZLabel", usr.TZLabel)
|
|
db.SetInt(usrPath, "TZOffset", usr.TZOffset)
|
|
db.SetBool(usrPath, "IsAdmin", usr.IsAdmin)
|
|
db.SetBool(usrPath, "IsOwner", usr.IsOwner)
|
|
db.SetBool(usrPath, "IsPrimaryOwner", usr.IsPrimaryOwner)
|
|
db.SetBool(usrPath, "IsRestricted", usr.IsRestricted)
|
|
db.SetBool(usrPath, "IsUltraRestricted", usr.IsUltraRestricted)
|
|
db.SetBool(usrPath, "IsBot", usr.IsBot)
|
|
db.SetBool(usrPath, "HasFiles", usr.HasFiles)
|
|
db.SetTimestamp(usrPath, "LastUpdated", time.Now())
|
|
return err
|
|
}
|
|
|
|
func getUser(usrId string) (*User, error) {
|
|
usr := new(User)
|
|
var err error
|
|
if err = db.OpenDB(); err != nil {
|
|
return usr, err
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
upath := []string{"users", usrId}
|
|
if usr.Name, err = db.GetValue(upath, "Name"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.Deleted, err = db.GetBool(upath, "Deleted"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.Status, err = db.GetValue(upath, "Status"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.Color, err = db.GetValue(upath, "Color"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.RealName, err = db.GetValue(upath, "RealName"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.TZ, err = db.GetValue(upath, "TZ"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.TZLabel, err = db.GetValue(upath, "TZLabel"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.TZOffset, err = db.GetInt(upath, "TZOffset"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.IsAdmin, err = db.GetBool(upath, "IsAdmin"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.IsOwner, err = db.GetBool(upath, "IsOwner"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.IsPrimaryOwner, err = db.GetBool(upath, "IsPrimaryOwner"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.IsRestricted, err = db.GetBool(upath, "IsRestricted"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.IsUltraRestricted, err = db.GetBool(upath, "IsUltraRestricted"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.IsBot, err = db.GetBool(upath, "IsBot"); err != nil {
|
|
return usr, err
|
|
}
|
|
if usr.HasFiles, err = db.GetBool(upath, "HasFiles"); err != nil {
|
|
return usr, err
|
|
}
|
|
usr.LastUpdated, err = db.GetTimestamp(upath, "LastUpdated")
|
|
|
|
return usr, err
|
|
}
|
|
|
|
func getChannelInfo(chanId string) (*Channel, error) {
|
|
ret := new(Channel)
|
|
var err error
|
|
if err = db.OpenDB(); err != nil {
|
|
return ret, err
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
cPath := []string{"channels", chanId}
|
|
if ret.ID, err = db.GetValue(cPath, "ID"); err != nil {
|
|
return ret, err
|
|
}
|
|
if ret.Name, err = db.GetValue(cPath, "Name"); err != nil {
|
|
return ret, err
|
|
}
|
|
if ret.IsChannel, err = db.GetBool(cPath, "IsChannel"); err != nil {
|
|
return ret, err
|
|
}
|
|
if ret.IsGeneral, err = db.GetBool(cPath, "IsGeneral"); err != nil {
|
|
return ret, err
|
|
}
|
|
if ret.IsMember, err = db.GetBool(cPath, "IsMember"); err != nil {
|
|
return ret, err
|
|
}
|
|
var mbrString string
|
|
if mbrString, err = db.GetValue(cPath, "Members"); err != nil {
|
|
return ret, err
|
|
}
|
|
ret.Members = strings.Split(mbrString, ",")
|
|
return ret, err
|
|
}
|
|
|
|
func saveChannelInfo(c *Channel) error {
|
|
var err error
|
|
if err = db.OpenDB(); err != nil {
|
|
return err
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
cPath := []string{"channels", c.ID}
|
|
db.SetValue(cPath, "ID", c.ID)
|
|
db.SetValue(cPath, "Name", c.Name)
|
|
db.SetBool(cPath, "IsChannel", c.IsChannel)
|
|
db.SetBool(cPath, "IsGeneral", c.IsGeneral)
|
|
db.SetBool(cPath, "IsMember", c.IsMember)
|
|
db.SetValue(cPath, "Members", strings.Join(c.Members, ","))
|
|
return nil
|
|
}
|
|
|
|
func saveAoCUser(m *Member) error {
|
|
var err error
|
|
if err = db.OpenDB(); err != nil {
|
|
return err
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
mPath := []string{"AoCMembers", m.ID}
|
|
db.SetValue(mPath, "ID", m.ID)
|
|
db.SetValue(mPath, "Name", m.Name)
|
|
db.SetInt(mPath, "Stars", m.Stars)
|
|
db.SetTimestamp(mPath, "LastStarTs", m.LastStarTs)
|
|
db.SetValue(mPath, "SlackID", m.SlackID)
|
|
return nil
|
|
}
|
|
|
|
func getAoCUser(mbrId string) (*Member, error) {
|
|
ret := new(Member)
|
|
var err error
|
|
if err = db.OpenDB(); err != nil {
|
|
return ret, err
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
mPath := []string{"AoCMembers", mbrId}
|
|
if ret.ID, err = db.GetValue(mPath, "ID"); err != nil {
|
|
return ret, err
|
|
}
|
|
if ret.Stars, err = db.GetInt(mPath, "Stars"); err != nil {
|
|
return ret, err
|
|
}
|
|
if ret.LastStarTs, err = db.GetTimestamp(mPath, "LastStarTs"); err != nil {
|
|
return ret, err
|
|
}
|
|
if ret.Name, err = db.GetValue(mPath, "Name"); err != nil {
|
|
return ret, err
|
|
}
|
|
ret.SlackID, err = db.GetValue(mPath, "SlackID")
|
|
return ret, err
|
|
}
|
|
|
|
func getAoCUserByName(nm string) (*Member, error) {
|
|
var err error
|
|
if err = db.OpenDB(); err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.CloseDB()
|
|
|
|
mPath := []string{"AoCMembers"}
|
|
var bktList []string
|
|
if bktList, err = db.GetBucketList(mPath); err != nil {
|
|
return nil, err
|
|
}
|
|
var dbNm, dbId string
|
|
for i := range bktList {
|
|
dbNm, err = db.GetValue(append(mPath, bktList[i]), "Name")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if nm == dbNm {
|
|
dbId, err = db.GetValue(append(mPath, bktList[i]), "ID")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return getAoCUser(dbId)
|
|
}
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func getUserSlackIDFromAoCName(nm string) (string, error) {
|
|
m, err := getAoCUserByName(nm)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return m.SlackID, nil
|
|
}
|