Types partial rework.
This commit is contained in:
parent
2d4ba6b788
commit
1b30c32d33
1 changed files with 75 additions and 1 deletions
|
|
@ -1,8 +1,11 @@
|
|||
package chats
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
"go.destrealm.net/vfs"
|
||||
|
|
@ -11,6 +14,10 @@ import (
|
|||
type Birthday struct {
|
||||
}
|
||||
|
||||
type pathMeta struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
type ChatMeta struct {
|
||||
ID int64 `json:"id"`
|
||||
Path string `json:"path"`
|
||||
|
|
@ -39,6 +46,10 @@ type ChatConfig struct {
|
|||
}
|
||||
}
|
||||
|
||||
func newChannelConfig(id int) *config {
|
||||
conf := &ChatConfig{}
|
||||
}
|
||||
|
||||
type chats struct {
|
||||
ChatMeta []*ChatMeta
|
||||
ChatIDMap map[int64]*ChatMeta
|
||||
|
|
@ -48,6 +59,11 @@ type chats struct {
|
|||
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 {
|
||||
|
|
@ -114,7 +130,53 @@ func (c *chats) LoadChannelByID(id int64) (*Channel, bool) {
|
|||
}
|
||||
|
||||
func (c *chats) NewChannel(id int64, name string) (*Channel, bool) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func LoadChats(fs vfs.VFS) (*chats, error) {
|
||||
|
|
@ -129,7 +191,9 @@ func LoadChats(fs vfs.VFS) (*chats, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
meta := &chats{}
|
||||
meta := &chats{
|
||||
mut: sync.RWMutex{},
|
||||
}
|
||||
if err := json.Unmarshal(buf, meta); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -137,6 +201,16 @@ func LoadChats(fs vfs.VFS) (*chats, error) {
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue