2026-10-05 11:36:04 -06:00
|
|
|
package chats
|
|
|
|
|
|
2026-10-06 14:35:42 -06:00
|
|
|
import (
|
2026-10-06 16:04:53 -06:00
|
|
|
"encoding/base64"
|
2026-10-06 14:35:42 -06:00
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
2026-10-06 16:04:53 -06:00
|
|
|
"os"
|
|
|
|
|
"sync"
|
2026-10-06 14:35:42 -06:00
|
|
|
|
|
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
|
"go.destrealm.net/vfs"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-05 11:36:04 -06:00
|
|
|
type Birthday struct {
|
|
|
|
|
}
|
2026-10-05 15:44:58 -06:00
|
|
|
|
2026-10-06 16:04:53 -06:00
|
|
|
type pathMeta struct {
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 14:35:42 -06:00
|
|
|
type ChatMeta struct {
|
2026-10-05 15:44:58 -06:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 14:35:42 -06:00
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-05 15:44:58 -06:00
|
|
|
|
2026-10-06 16:04:53 -06:00
|
|
|
func newChannelConfig(id int) *config {
|
|
|
|
|
conf := &ChatConfig{}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 14:35:42 -06:00
|
|
|
type chats struct {
|
|
|
|
|
ChatMeta []*ChatMeta
|
|
|
|
|
ChatIDMap map[int64]*ChatMeta
|
|
|
|
|
ChatPathMap map[string]*ChatMeta
|
|
|
|
|
Config map[int64]*ChatConfig
|
|
|
|
|
|
|
|
|
|
Channels map[int64]*Channel
|
|
|
|
|
|
|
|
|
|
root vfs.VFS
|
2026-10-06 16:04:53 -06:00
|
|
|
mut sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *chats) Marshal() ([]byte, error) {
|
|
|
|
|
return json.Marshal(c.ChatMeta)
|
2026-10-06 14:35:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2026-10-06 16:04:53 -06:00
|
|
|
c.mut.Lock()
|
|
|
|
|
defer c.mut.Unlock()
|
2026-10-06 14:35:42 -06:00
|
|
|
|
2026-10-06 16:04:53 -06:00
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if ch, ok := c.Channels[id]; ok {
|
|
|
|
|
return ch, true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
ch := &Channel{
|
|
|
|
|
root: c.root,
|
|
|
|
|
store: store.Mutable(),
|
|
|
|
|
config: newChannelConfig(),
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf, err := json.Marshal(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rw := c.root.Mutable()
|
|
|
|
|
fp, err := rw.OpenFile("chats.json", os.O_TRUNC|os.O_WRONLY, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
|
|
if _, err := fp.Write(buf); err != nil {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
2026-10-06 14:35:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 16:04:53 -06:00
|
|
|
meta := &chats{
|
|
|
|
|
mut: sync.RWMutex{},
|
|
|
|
|
}
|
2026-10-06 14:35:42 -06:00
|
|
|
if err := json.Unmarshal(buf, meta); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return meta, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 16:04:53 -06:00
|
|
|
func encodeChannelID(id int64) string {
|
|
|
|
|
pm := &pathMeta{ID: id}
|
|
|
|
|
buf, err := json.Marshal(pm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base64.URLEncoding.EncodeToString(buf)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 14:35:42 -06:00
|
|
|
type Channel struct {
|
|
|
|
|
root vfs.VFS
|
|
|
|
|
store vfs.MutableFS
|
|
|
|
|
config ChatConfig
|
|
|
|
|
meta ChatMeta
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Channel) Name() string {
|
|
|
|
|
return c.meta.Name
|
2026-10-05 15:44:58 -06:00
|
|
|
}
|