2020-01-29 16:57:42 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2020-02-04 19:02:18 -07:00
|
|
|
"fmt"
|
2020-01-29 16:57:42 -07:00
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
2020-02-04 17:22:19 -07:00
|
|
|
"github.com/peterbourgon/diskv"
|
2020-01-29 16:57:42 -07:00
|
|
|
)
|
|
|
|
|
2020-02-04 17:22:19 -07:00
|
|
|
// MCStore implements a gomatrix.Storer and exposes a diskv db to be used for
|
2020-01-29 16:57:42 -07:00
|
|
|
// application storage (account info, config info etc).
|
|
|
|
type MCStore struct {
|
2020-02-04 17:22:19 -07:00
|
|
|
db *diskv.Diskv
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
2020-02-04 19:07:17 -07:00
|
|
|
// NewStore creates a new MCStore instance.
|
2020-01-29 16:57:42 -07:00
|
|
|
func NewStore(path string) (*MCStore, error) {
|
2020-02-04 17:22:19 -07:00
|
|
|
flatTransform := func(s string) []string { return []string{} }
|
|
|
|
db := diskv.New(diskv.Options{
|
|
|
|
BasePath: "db",
|
|
|
|
Transform: flatTransform,
|
|
|
|
CacheSizeMax: 1024 * 1024,
|
|
|
|
})
|
2020-01-29 16:57:42 -07:00
|
|
|
|
|
|
|
s := &MCStore{db: db}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-02-04 17:22:19 -07:00
|
|
|
func (s *MCStore) set(key string, value string) {
|
|
|
|
v := []byte(value)
|
|
|
|
s.db.Write(key, v)
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
2020-02-04 17:22:19 -07:00
|
|
|
func (s *MCStore) get(key string) (string, error) {
|
|
|
|
b, err := s.db.Read(key)
|
|
|
|
return string(b), err
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MCStore) encodeRoom(room *gomatrix.Room) ([]byte, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
enc := gob.NewEncoder(buf)
|
|
|
|
err := enc.Encode(room)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MCStore) decodeRoom(room []byte) (*gomatrix.Room, error) {
|
|
|
|
var r *gomatrix.Room
|
|
|
|
buf := bytes.NewBuffer(room)
|
|
|
|
dec := gob.NewDecoder(buf)
|
|
|
|
err := dec.Decode(&r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveFilterID exposed for gomatrix
|
|
|
|
func (s *MCStore) SaveFilterID(userID, filterID string) {
|
2020-02-04 19:02:18 -07:00
|
|
|
s.set(fmt.Sprintf("filter_%s", userID), filterID)
|
2020-01-29 16:57:42 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFilterID exposed for gomatrix
|
|
|
|
func (s *MCStore) LoadFilterID(userID string) string {
|
2020-02-04 19:02:18 -07:00
|
|
|
filter, _ := s.get(fmt.Sprintf("filter_%s", userID))
|
2020-01-29 16:57:42 -07:00
|
|
|
return string(filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveNextBatch exposed for gomatrix
|
|
|
|
func (s *MCStore) SaveNextBatch(userID, nextBatchToken string) {
|
2020-02-04 19:02:18 -07:00
|
|
|
s.set(fmt.Sprintf("batch_%s", userID), nextBatchToken)
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadNextBatch exposed for gomatrix
|
|
|
|
func (s *MCStore) LoadNextBatch(userID string) string {
|
2020-02-04 19:02:18 -07:00
|
|
|
batch, _ := s.get(fmt.Sprintf("batch_%s", userID))
|
2020-01-29 16:57:42 -07:00
|
|
|
return string(batch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveRoom exposed for gomatrix
|
|
|
|
func (s *MCStore) SaveRoom(room *gomatrix.Room) {
|
|
|
|
b, _ := s.encodeRoom(room)
|
2020-02-04 19:02:18 -07:00
|
|
|
s.set(fmt.Sprintf("room_%s", room.ID), string(b))
|
2020-01-29 16:57:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadRoom exposed for gomatrix
|
|
|
|
func (s *MCStore) LoadRoom(roomID string) *gomatrix.Room {
|
2020-02-04 19:02:18 -07:00
|
|
|
b, _ := s.get(fmt.Sprintf("room_%s", roomID))
|
2020-02-04 17:22:19 -07:00
|
|
|
room, _ := s.decodeRoom([]byte(b))
|
2020-01-29 16:57:42 -07:00
|
|
|
return room
|
|
|
|
}
|