301 lines
5.9 KiB
Go
301 lines
5.9 KiB
Go
package datastore
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
"go.destrealm.net/cwrap"
|
|
"go.destrealm.net/vfs"
|
|
)
|
|
|
|
type BirthdaysDataSource struct {
|
|
root vfs.VFS
|
|
config ChatConfig
|
|
}
|
|
|
|
func NewBirthdayDataSource(fs vfs.VFS, conf *ChatConfig) (*BirthdaysDataSource, error) {
|
|
bds := &BirthdaysDataSource{
|
|
root: fs,
|
|
config: *conf,
|
|
}
|
|
|
|
return bds, nil
|
|
}
|
|
|
|
func userToPath(bd *Birthday) (string, error) {
|
|
pth, err := json.Marshal(bd)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return base64.URLEncoding.EncodeToString(pth), nil
|
|
}
|
|
|
|
type Birthday struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Date struct {
|
|
Month int `json:"month,omitempty"`
|
|
Day int `json:"day,omitempty"`
|
|
} `json:"date,omitempty"`
|
|
}
|
|
|
|
type pathMeta struct {
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
type ChatMeta struct {
|
|
ID int64 `json:"id"`
|
|
Path string `json:"path"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type ChatConfig struct {
|
|
ID int64 `toml:"id"`
|
|
TimeZone string `toml:"timezone"`
|
|
Daily struct {
|
|
Cron string `toml:"cron"`
|
|
Rotations struct {
|
|
Path string `toml:"path"`
|
|
Current int `toml:"current"`
|
|
} `toml:"rotations"`
|
|
Skip struct {
|
|
Path string `toml:"path" comment:"Verses that should be skipped by the bot are linked into this directory."`
|
|
} `toml:"skip"`
|
|
UserAdded struct {
|
|
Path string `toml:"path"`
|
|
} `toml:"user_added" comment:"User-supplied verses."`
|
|
} `toml:"daily"`
|
|
Birthdays struct {
|
|
Path string `toml:"path"`
|
|
Announce struct {
|
|
Weekly string `toml:"weekly" comment:"Weekly reports are issued, by default, at 7:30am on Sunday. Birthdays that fall on a the day the weekly report is issued will be announced alongside the weekly report rather than in the daily rotation. If no birthdays are present for this week, the report is skipped.`
|
|
Daily string `toml:"daily"`
|
|
} `toml:"announce"`
|
|
}
|
|
}
|
|
|
|
func newChannelConfig(id int64) *ChatConfig {
|
|
conf := &ChatConfig{}
|
|
|
|
// Defaults.
|
|
conf.ID = id
|
|
conf.TimeZone = "America/Denver"
|
|
conf.Daily.Cron = "inherit"
|
|
|
|
return conf
|
|
}
|
|
|
|
type chats struct {
|
|
ChatMeta []*ChatMeta
|
|
ChatIDMap map[int64]*ChatMeta
|
|
ChatPathMap map[string]*ChatMeta
|
|
Config map[int64]*ChatConfig
|
|
|
|
Channels map[int64]*Channel
|
|
|
|
root vfs.VFS
|
|
mut sync.RWMutex
|
|
}
|
|
|
|
func (c *chats) Marshal() ([]byte, error) {
|
|
return json.Marshal(c.ChatMeta)
|
|
}
|
|
|
|
func (c *chats) Unmarshal(b []byte) error {
|
|
if err := json.Unmarshal(b, &c.ChatMeta); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, chat := range c.ChatMeta {
|
|
c.ChatIDMap[chat.ID] = chat
|
|
c.ChatPathMap[chat.Path] = chat
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *chats) loadChannel(meta ChatMeta) (*Channel, error) {
|
|
if _, err := c.root.Stat(meta.Path); vfs.IsNotDir(err) {
|
|
return nil, err
|
|
}
|
|
|
|
chanfs := c.root.Mutable()
|
|
if err := chanfs.SetRoot(meta.Path); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fp, err := chanfs.Open("chat.toml")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer fp.Close()
|
|
|
|
buf, err := io.ReadAll(fp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var config ChatConfig
|
|
if err := toml.Unmarshal(buf, &config); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Channel{
|
|
root: c.root,
|
|
store: chanfs,
|
|
config: config,
|
|
meta: meta,
|
|
}, nil
|
|
}
|
|
|
|
func (c *chats) LoadChannelByID(id int64) (*Channel, bool) {
|
|
if ch, ok := c.Channels[id]; ok {
|
|
return ch, true
|
|
}
|
|
|
|
if chat, ok := c.ChatIDMap[id]; ok {
|
|
if ch, err := c.loadChannel(*chat); err == nil {
|
|
return ch, true
|
|
}
|
|
} else {
|
|
return nil, false
|
|
}
|
|
|
|
return nil, false
|
|
}
|
|
|
|
func (c *chats) NewChannel(id int64, name string) (*Channel, error) {
|
|
c.mut.Lock()
|
|
defer c.mut.Unlock()
|
|
|
|
var err error
|
|
|
|
if ch, ok := c.Channels[id]; ok {
|
|
return ch, nil
|
|
}
|
|
|
|
meta := &ChatMeta{
|
|
ID: id,
|
|
Path: encodeChannelID(id),
|
|
Name: name,
|
|
}
|
|
c.ChatMeta = append(c.ChatMeta, meta)
|
|
c.ChatIDMap[id] = meta
|
|
c.ChatPathMap[meta.Path] = meta
|
|
|
|
store := c.root.Clone()
|
|
store, err = store.Chroot(meta.Path)
|
|
if err != nil {
|
|
return nil, ErrCreatingChannel.Do(err)
|
|
}
|
|
|
|
ch := &Channel{
|
|
root: c.root,
|
|
store: store.Mutable(),
|
|
config: *newChannelConfig(id),
|
|
meta: *meta,
|
|
}
|
|
if err := ch.Save(); err != nil {
|
|
return nil, ErrCreatingChannel.Do(err)
|
|
}
|
|
|
|
buf, err := json.Marshal(c)
|
|
if err != nil {
|
|
return nil, ErrCreatingChannel.Do(err)
|
|
}
|
|
|
|
rw := c.root.Mutable()
|
|
fp, err := rw.OpenFile("chats.json", os.O_TRUNC|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
return nil, ErrCreatingChannel.Do(err)
|
|
}
|
|
defer fp.Close()
|
|
|
|
if _, err := fp.Write(buf); err != nil {
|
|
return nil, ErrCreatingChannel.Do(err)
|
|
}
|
|
|
|
return ch, nil
|
|
}
|
|
|
|
func LoadChats(fs vfs.VFS) (*chats, error) {
|
|
fp, err := fs.Open("chats.json")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer fp.Close()
|
|
|
|
buf, err := io.ReadAll(fp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
meta := &chats{
|
|
ChatIDMap: make(map[int64]*ChatMeta),
|
|
ChatPathMap: make(map[string]*ChatMeta),
|
|
root: fs,
|
|
mut: sync.RWMutex{},
|
|
}
|
|
if err := json.Unmarshal(buf, &meta.ChatMeta); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, cm := range meta.ChatMeta {
|
|
meta.ChatIDMap[cm.ID] = cm
|
|
meta.ChatPathMap[cm.Path] = cm
|
|
}
|
|
|
|
return meta, nil
|
|
}
|
|
|
|
func encodeChannelID(id int64) string {
|
|
pm := &pathMeta{ID: id}
|
|
buf, err := json.Marshal(pm)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(buf)
|
|
}
|
|
|
|
type Channel struct {
|
|
root vfs.VFS
|
|
store vfs.MutableFS
|
|
config ChatConfig
|
|
meta ChatMeta
|
|
}
|
|
|
|
func (c *Channel) Name() string {
|
|
return c.meta.Name
|
|
}
|
|
|
|
func (c *Channel) Save() error {
|
|
buf := &bytes.Buffer{}
|
|
enc := toml.NewEncoder(cwrap.New(buf))
|
|
enc.ArraysWithOneElementPerLine(true)
|
|
enc.Indentation(" ")
|
|
enc.Order(toml.OrderPreserve)
|
|
enc.PromoteAnonymous(false)
|
|
enc.CompactComments(true)
|
|
enc.QuoteMapKeys(true)
|
|
|
|
if err := enc.Encode(c.config); err != nil {
|
|
return err
|
|
}
|
|
|
|
fp, err := c.store.OpenFile("chat.toml", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer fp.Close()
|
|
|
|
if _, err := fp.Write(buf.Bytes()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|