dispatch/storage/user.go

224 lines
4.1 KiB
Go
Raw Normal View History

2015-01-17 01:37:21 +00:00
package storage
import (
"bytes"
2016-01-11 20:04:57 +00:00
"crypto/tls"
2015-01-17 01:37:21 +00:00
"encoding/json"
"strconv"
2016-01-11 20:04:57 +00:00
"sync"
2015-01-17 01:37:21 +00:00
2015-12-11 03:35:48 +00:00
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/blevesearch/bleve"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/boltdb/bolt"
2015-01-17 01:37:21 +00:00
)
type User struct {
ID uint64
Username string
id []byte
messageLog *bolt.DB
messageIndex bleve.Index
certificate *tls.Certificate
lock sync.Mutex
}
2015-01-17 01:37:21 +00:00
type Server struct {
Name string `json:"name"`
2016-01-14 04:56:53 +00:00
Host string `json:"host"`
Port string `json:"port,omitempty"`
TLS bool `json:"tls"`
Password string `json:"password,omitempty"`
Nick string `json:"nick"`
Username string `json:"username"`
Realname string `json:"realname"`
2015-01-17 01:37:21 +00:00
}
type Channel struct {
Server string `json:"server"`
Name string `json:"name"`
Users []string `json:"users,omitempty"`
Topic string `json:"topic,omitempty"`
2015-01-17 01:37:21 +00:00
}
2016-01-17 20:15:29 +00:00
func NewUser() (*User, error) {
user := &User{}
err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketUsers)
2016-01-17 20:15:29 +00:00
user.ID, _ = b.NextSequence()
user.Username = strconv.FormatUint(user.ID, 10)
2015-01-17 01:37:21 +00:00
data, err := json.Marshal(user)
if err != nil {
return err
}
2015-01-17 01:37:21 +00:00
2016-01-17 20:15:29 +00:00
user.id = idToBytes(user.ID)
return b.Put(user.id, data)
})
2015-01-17 01:37:21 +00:00
if err != nil {
2016-01-17 20:15:29 +00:00
return nil, err
}
2015-01-17 01:37:21 +00:00
err = user.openMessageLog()
if err != nil {
2016-01-17 20:15:29 +00:00
return nil, err
}
2016-01-17 20:15:29 +00:00
return user, nil
2015-01-17 01:37:21 +00:00
}
func LoadUsers() []*User {
var users []*User
2015-01-17 01:37:21 +00:00
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketUsers)
2015-01-17 01:37:21 +00:00
b.ForEach(func(k, _ []byte) error {
id := idFromBytes(k)
user := &User{
ID: id,
Username: strconv.FormatUint(id, 10),
id: make([]byte, 8),
}
copy(user.id, k)
users = append(users, user)
2015-01-17 01:37:21 +00:00
return nil
})
return nil
})
for _, user := range users {
user.openMessageLog()
user.loadCertificate()
}
2015-01-17 01:37:21 +00:00
return users
}
func (u *User) GetServers() []Server {
2015-01-17 01:37:21 +00:00
var servers []Server
db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(bucketServers).Cursor()
2015-01-17 01:37:21 +00:00
for k, v := c.Seek(u.id); bytes.HasPrefix(k, u.id); k, v = c.Next() {
2015-01-17 01:37:21 +00:00
var server Server
json.Unmarshal(v, &server)
servers = append(servers, server)
}
return nil
})
return servers
}
func (u *User) GetChannels() []Channel {
2015-01-17 01:37:21 +00:00
var channels []Channel
db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(bucketChannels).Cursor()
2015-01-17 01:37:21 +00:00
for k, v := c.Seek(u.id); bytes.HasPrefix(k, u.id); k, v = c.Next() {
2015-01-17 01:37:21 +00:00
var channel Channel
json.Unmarshal(v, &channel)
channels = append(channels, channel)
}
return nil
})
return channels
}
func (u *User) AddServer(server Server) {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketServers)
2015-01-17 01:37:21 +00:00
data, _ := json.Marshal(server)
b.Put(u.serverID(server.Host), data)
2015-01-17 01:37:21 +00:00
return nil
})
}
func (u *User) AddChannel(channel Channel) {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketChannels)
2015-01-17 01:37:21 +00:00
data, _ := json.Marshal(channel)
b.Put(u.channelID(channel.Server, channel.Name), data)
2015-01-17 01:37:21 +00:00
return nil
})
}
func (u *User) SetNick(nick, address string) {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketServers)
id := u.serverID(address)
2015-02-04 01:43:49 +00:00
var server Server
json.Unmarshal(b.Get(id), &server)
server.Nick = nick
data, _ := json.Marshal(server)
b.Put(id, data)
return nil
})
}
func (u *User) RemoveServer(address string) {
db.Update(func(tx *bolt.Tx) error {
serverID := u.serverID(address)
tx.Bucket(bucketServers).Delete(serverID)
b := tx.Bucket(bucketChannels)
c := b.Cursor()
for k, _ := c.Seek(serverID); bytes.HasPrefix(k, serverID); k, _ = c.Next() {
b.Delete(k)
}
2015-01-17 01:37:21 +00:00
return nil
})
}
func (u *User) RemoveChannel(server, channel string) {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketChannels)
id := u.channelID(server, channel)
2015-01-17 01:37:21 +00:00
b.Delete(id)
return nil
})
}
func (u *User) Close() {
u.messageLog.Close()
u.messageIndex.Close()
}
func (u *User) serverID(address string) []byte {
id := make([]byte, 8+len(address))
copy(id, u.id)
copy(id[8:], address)
return id
}
func (u *User) channelID(server, channel string) []byte {
id := make([]byte, 8+len(server)+1+len(channel))
copy(id, u.id)
copy(id[8:], server)
copy(id[8+len(server)+1:], channel)
return id
}