2016-12-06 04:20:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-12-06 23:31:22 +00:00
|
|
|
"strings"
|
2016-12-06 04:20:14 +00:00
|
|
|
"time"
|
|
|
|
|
2017-09-26 16:24:17 +00:00
|
|
|
"github.com/br0xen/boltease"
|
2016-12-06 04:20:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getDatabase() (*boltease.DB, error) {
|
|
|
|
db, err := boltease.Create("aocbot.db", 0644, nil)
|
|
|
|
if err != nil {
|
2016-12-06 23:31:22 +00:00
|
|
|
return db, err
|
2016-12-06 04:20:14 +00:00
|
|
|
}
|
|
|
|
// Make sure that we have a 'users' bucket
|
|
|
|
db.MkBucketPath([]string{"users"})
|
|
|
|
// Make sure that we have a 'channels' bucket
|
|
|
|
db.MkBucketPath([]string{"channels"})
|
2016-12-06 23:31:22 +00:00
|
|
|
// Make sure that we have a 'aocmembers' bucket
|
|
|
|
db.MkBucketPath([]string{"AoCMembers"})
|
2016-12-06 04:20:14 +00:00
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2016-12-06 23:31:22 +00:00
|
|
|
func saveUser(usr *User) error {
|
2016-12-06 04:20:14 +00:00
|
|
|
var err error
|
|
|
|
if err = db.OpenDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.CloseDB()
|
|
|
|
|
2016-12-06 23:31:22 +00:00
|
|
|
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())
|
2016-12-06 04:20:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-06 23:31:22 +00:00
|
|
|
func getUser(usrId string) (*User, error) {
|
2016-12-06 04:20:14 +00:00
|
|
|
usr := new(User)
|
|
|
|
var err error
|
|
|
|
if err = db.OpenDB(); err != nil {
|
|
|
|
return usr, err
|
|
|
|
}
|
|
|
|
defer db.CloseDB()
|
|
|
|
|
2016-12-06 23:31:22 +00:00
|
|
|
upath := []string{"users", usrId}
|
|
|
|
if usr.Name, err = db.GetValue(upath, "Name"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.Deleted, err = db.GetBool(upath, "Deleted"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.Status, err = db.GetValue(upath, "Status"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.Color, err = db.GetValue(upath, "Color"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.RealName, err = db.GetValue(upath, "RealName"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.TZ, err = db.GetValue(upath, "TZ"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.TZLabel, err = db.GetValue(upath, "TZLabel"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.TZOffset, err = db.GetInt(upath, "TZOffset"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.IsAdmin, err = db.GetBool(upath, "IsAdmin"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.IsOwner, err = db.GetBool(upath, "IsOwner"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.IsPrimaryOwner, err = db.GetBool(upath, "IsPrimaryOwner"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.IsRestricted, err = db.GetBool(upath, "IsRestricted"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.IsUltraRestricted, err = db.GetBool(upath, "IsUltraRestricted"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.IsBot, err = db.GetBool(upath, "IsBot"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
if usr.HasFiles, err = db.GetBool(upath, "HasFiles"); err != nil {
|
2016-12-06 04:20:14 +00:00
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
usr.LastUpdated, err = db.GetTimestamp(upath, "LastUpdated")
|
2016-12-06 04:20:14 +00:00
|
|
|
|
|
|
|
return usr, err
|
|
|
|
}
|
2016-12-06 23:31:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-09-26 16:24:17 +00:00
|
|
|
|
|
|
|
func getUserSlackIDFromAoCName(nm string) (string, error) {
|
|
|
|
m, err := getAoCUserByName(nm)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return m.SlackID, nil
|
|
|
|
}
|