Work in progress implementation:
Added API (preliminary, mostly for decoupling and separation of concerns) Chats are *mostly* implemented as far as metadata goes. Datastore confusion persists.
This commit is contained in:
parent
b53eca4fb9
commit
2996867c80
4 changed files with 228 additions and 7 deletions
9
internal/app/api/bible.go
Normal file
9
internal/app/api/bible.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"go.destrealm.net/vfs"
|
||||
)
|
||||
|
||||
type Translation interface {
|
||||
SetSource(vfs.VFS)
|
||||
}
|
||||
72
internal/app/api/verses.go
Normal file
72
internal/app/api/verses.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"go.destrealm.net/vfs"
|
||||
)
|
||||
|
||||
type BaseVerse interface {
|
||||
// FullReference returns the full verse reference for the current verse
|
||||
// (e.g. Matthew 5:20).
|
||||
FullReference() string
|
||||
|
||||
// IncludeVerseNum enables or disables the display of verse numbers.
|
||||
IncludeVerseNum(bool)
|
||||
|
||||
// SetFormatter sets the formatter to use for this verse. Any struct will
|
||||
// work as long as it implements VerseFormatter.
|
||||
SetFormatter(VerseFormatter)
|
||||
|
||||
// ShortRef returns a shortened (and standardized) reference for the current
|
||||
// verse (e.g. "mat 5:20"). Truncated book names are always lowercase.
|
||||
ShortRef() string
|
||||
|
||||
// SkipLineBreaks enables or disables skipping linebreaks.
|
||||
SkipLineBreaks(bool)
|
||||
|
||||
// Returns the verse as a string.
|
||||
String() string
|
||||
|
||||
// UseMarkdown enables or disables the use of Markdown to format verse
|
||||
// output. Not all formatters may support Markdown.
|
||||
UseMarkdown(bool)
|
||||
}
|
||||
|
||||
type VerseFormatter interface {
|
||||
Format(string) string
|
||||
}
|
||||
|
||||
type Verse interface {
|
||||
BaseVerse
|
||||
}
|
||||
|
||||
type VerseReader interface {
|
||||
// AddSource includes additional sources for random verse lookup.
|
||||
// System-level sources are always included.
|
||||
AddSource(vfs.VFS)
|
||||
|
||||
// Read a verse or verses from the designated translation. In most
|
||||
// implementations, this is a pass-through to the underlying translation
|
||||
// reader.
|
||||
Read(ref string) VerseSet
|
||||
|
||||
// SetRotationSource sets the writeable location for verse rotation.
|
||||
SetRotationSource(vfs.VFS)
|
||||
|
||||
// SetTranslation configures the default translation to use.
|
||||
SetTranslation(Translation)
|
||||
}
|
||||
|
||||
type VerseSet interface {
|
||||
BaseVerse
|
||||
|
||||
// Set returns the boundary set for the current verses.
|
||||
Set() VerseBounds
|
||||
|
||||
// Verses returns a list of verses in this set.
|
||||
Verses() []Verse
|
||||
}
|
||||
|
||||
type VerseBounds interface {
|
||||
First() int
|
||||
Last() int
|
||||
}
|
||||
|
|
@ -1,15 +1,149 @@
|
|||
package chats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
"go.destrealm.net/vfs"
|
||||
)
|
||||
|
||||
type Birthday struct {
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
type ChatMeta struct {
|
||||
ID int64 `json:"id"`
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Chats []*Chat
|
||||
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"`
|
||||
} `toml:"skip"`
|
||||
UserAdded struct {
|
||||
Path string `toml:"path"`
|
||||
} `toml:"user_added"`
|
||||
} `toml:"daily"`
|
||||
Birthdays struct {
|
||||
Path string `toml:"path"`
|
||||
Announce struct {
|
||||
Weekly string `toml:"weekly"`
|
||||
Daily string `toml:"daily"`
|
||||
} `toml:"announce"`
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
type chats struct {
|
||||
ChatMeta []*ChatMeta
|
||||
ChatIDMap map[int64]*ChatMeta
|
||||
ChatPathMap map[string]*ChatMeta
|
||||
Config map[int64]*ChatConfig
|
||||
|
||||
Channels map[int64]*Channel
|
||||
|
||||
root vfs.VFS
|
||||
}
|
||||
|
||||
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, bool) {
|
||||
|
||||
}
|
||||
|
||||
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{}
|
||||
if err := json.Unmarshal(buf, meta); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
type Channel struct {
|
||||
root vfs.VFS
|
||||
store vfs.MutableFS
|
||||
config ChatConfig
|
||||
meta ChatMeta
|
||||
}
|
||||
|
||||
func (c *Channel) Name() string {
|
||||
return c.meta.Name
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// Data hive.
|
||||
|
||||
package datastore
|
||||
package app
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
|
@ -8,6 +8,12 @@ import (
|
|||
"go.destrealm.net/vfs"
|
||||
)
|
||||
|
||||
const (
|
||||
DATASTORE_CHATS = "./data/chats"
|
||||
DATASTORE_TRANSLATIONS = "./data/translations"
|
||||
DATASTORE_VERSES = "./data/verses"
|
||||
)
|
||||
|
||||
type datastore struct {
|
||||
storage *storage
|
||||
mut *sync.RWMutex
|
||||
|
|
@ -19,17 +25,17 @@ func newDataStore(root vfs.VFS) (ds *datastore, err error) {
|
|||
storage: &storage{},
|
||||
}
|
||||
|
||||
if chats, err := root.Chroot("./data/chats"); err != nil {
|
||||
if chats, err := root.Chroot(DATASTORE_CHATS); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
ds.storage.chats = chats.Mutable()
|
||||
}
|
||||
if ds.storage.translations, err = root.Chroot("./data/translations"); err == nil {
|
||||
if ds.storage.translations, err = root.Chroot(DATASTORE_TRANSLATIONS); err == nil {
|
||||
ds.storage.translations.SetReadOnly(true)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
if ds.storage.verses, err = root.Chroot("./data/verses"); err == nil {
|
||||
if ds.storage.verses, err = root.Chroot(DATASTORE_VERSES); err == nil {
|
||||
ds.storage.verses.SetReadOnly(true)
|
||||
} else {
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in a new issue