diff --git a/internal/chats/types.go b/internal/datastore/chats.go similarity index 54% rename from internal/chats/types.go rename to internal/datastore/chats.go index 1b372ee..8fb5945 100644 --- a/internal/chats/types.go +++ b/internal/datastore/chats.go @@ -1,6 +1,7 @@ -package chats +package datastore import ( + "bytes" "encoding/base64" "encoding/json" "io" @@ -8,10 +9,39 @@ import ( "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 { @@ -28,26 +58,36 @@ type ChatConfig struct { ID int64 `toml:"id"` TimeZone string `toml:"timezone"` Daily struct { - Path string `toml:"path"` - Current int `toml:"current"` - Skip struct { - Path string `toml:"path"` + 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"` + } `toml:"user_added" comment:"User-supplied verses."` } `toml:"daily"` Birthdays struct { Path string `toml:"path"` Announce struct { - Weekly string `toml:"weekly"` + 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 int) *config { +func newChannelConfig(id int64) *ChatConfig { conf := &ChatConfig{} + + // Defaults. + conf.ID = id + conf.TimeZone = "America/Denver" + conf.Daily.Cron = "inherit" + + return conf } type chats struct { @@ -129,14 +169,14 @@ func (c *chats) LoadChannelByID(id int64) (*Channel, bool) { return nil, false } -func (c *chats) NewChannel(id int64, name string) (*Channel, bool) { +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, true + return ch, nil } meta := &ChatMeta{ @@ -151,32 +191,36 @@ func (c *chats) NewChannel(id int64, name string) (*Channel, bool) { store := c.root.Clone() store, err = store.Chroot(meta.Path) if err != nil { - return nil, false + return nil, ErrCreatingChannel.Do(err) } - } ch := &Channel{ - root: c.root, - store: store.Mutable(), - config: newChannelConfig(), - + 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, false + 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, false + return nil, ErrCreatingChannel.Do(err) } defer fp.Close() if _, err := fp.Write(buf); err != nil { - return nil, false + return nil, ErrCreatingChannel.Do(err) } + + return ch, nil } func LoadChats(fs vfs.VFS) (*chats, error) { @@ -192,11 +236,18 @@ func LoadChats(fs vfs.VFS) (*chats, error) { } meta := &chats{ - mut: sync.RWMutex{}, + ChatIDMap: make(map[int64]*ChatMeta), + ChatPathMap: make(map[string]*ChatMeta), + root: fs, + mut: sync.RWMutex{}, } - if err := json.Unmarshal(buf, meta); err != nil { + 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 } @@ -221,3 +272,30 @@ type Channel struct { 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 +} diff --git a/internal/datastore/chats_test.go b/internal/datastore/chats_test.go new file mode 100644 index 0000000..1c43a84 --- /dev/null +++ b/internal/datastore/chats_test.go @@ -0,0 +1,26 @@ +package datastore + +import ( + "fmt" + "testing" + + "go.destrealm.net/vfs" +) + +func Test_LoadChats(t *testing.T) { + fs, _ := vfs.NewVFS("../../data/chats") + c, err := LoadChats(fs) + if err != nil { + t.Error(err) + } + fmt.Println("chats:", c) + fmt.Println(c.ChatIDMap) + + fmt.Println(c.root) + for k, _ := range c.ChatIDMap { + ch, ok := c.LoadChannelByID(k) + fmt.Println(ch, ok) + } + + t.Error(".") +} diff --git a/internal/datastore/datastore.go b/internal/datastore/datastore.go index 31bb856..ae180fd 100644 --- a/internal/datastore/datastore.go +++ b/internal/datastore/datastore.go @@ -1,6 +1,6 @@ // Data hive. -package app +package datastore import ( "sync" diff --git a/internal/datastore/errors.go b/internal/datastore/errors.go new file mode 100644 index 0000000..4aeab09 --- /dev/null +++ b/internal/datastore/errors.go @@ -0,0 +1,8 @@ +package datastore + +import ( + "go.destrealm.net/errors" +) + +var ErrChannelDoesNotExist = errors.NewError("the requested channel does not exist") +var ErrCreatingChannel = errors.NewError("error creating channel")