WIP.
This commit is contained in:
parent
be7607d860
commit
4bc89ed5aa
4 changed files with 134 additions and 22 deletions
|
|
@ -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
|
||||
}
|
||||
26
internal/datastore/chats_test.go
Normal file
26
internal/datastore/chats_test.go
Normal file
|
|
@ -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(".")
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
// Data hive.
|
||||
|
||||
package app
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
|
|
|||
8
internal/datastore/errors.go
Normal file
8
internal/datastore/errors.go
Normal file
|
|
@ -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")
|
||||
Loading…
Reference in a new issue