dispatch/storage/user.go

166 lines
3.2 KiB
Go
Raw Normal View History

2015-01-17 01:37:21 +00:00
package storage
import (
2016-01-22 17:30:47 +00:00
"crypto/tls"
"os"
2016-01-22 17:30:47 +00:00
"sync"
"time"
2015-01-17 01:37:21 +00:00
)
2016-01-22 17:30:47 +00:00
type User struct {
ID uint64
IDBytes []byte
2016-01-22 17:30:47 +00:00
Username string
store Store
messageLog MessageStore
messageIndex MessageSearchProvider
2016-01-22 17:30:47 +00:00
certificate *tls.Certificate
lock sync.Mutex
}
func NewUser(store Store) (*User, error) {
user := &User{store: store}
2015-01-17 01:37:21 +00:00
err := store.SaveUser(user)
if err != nil {
2016-01-17 20:15:29 +00:00
return nil, err
}
2015-01-17 01:37:21 +00:00
err = os.MkdirAll(Path.User(user.Username), 0700)
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(store Store) ([]*User, error) {
users, err := store.GetUsers()
if err != nil {
return nil, err
}
2015-01-17 01:37:21 +00:00
for _, user := range users {
user.store = store
user.loadCertificate()
}
return users, nil
2015-01-17 01:37:21 +00:00
}
func (u *User) SetMessageStore(store MessageStore) {
u.messageLog = store
2015-01-17 01:37:21 +00:00
}
func (u *User) SetMessageSearchProvider(search MessageSearchProvider) {
u.messageIndex = search
2015-01-17 01:37:21 +00:00
}
func (u *User) Remove() {
u.store.DeleteUser(u)
if u.messageLog != nil {
u.messageLog.Close()
}
if u.messageIndex != nil {
u.messageIndex.Close()
}
os.RemoveAll(Path.User(u.Username))
2015-01-17 01:37:21 +00:00
}
type Server struct {
Name string
Host string
Port string
TLS bool
Password string
Nick string
Username string
Realname string
2015-01-17 01:37:21 +00:00
}
func (u *User) GetServers() ([]*Server, error) {
return u.store.GetServers(u)
}
2015-02-04 01:43:49 +00:00
func (u *User) AddServer(server *Server) error {
return u.store.AddServer(u, server)
2015-02-04 01:43:49 +00:00
}
func (u *User) RemoveServer(address string) error {
return u.store.RemoveServer(u, address)
}
func (u *User) SetNick(nick, address string) error {
return u.store.SetNick(u, nick, address)
}
func (u *User) SetServerName(name, address string) error {
return u.store.SetServerName(u, name, address)
}
type Channel struct {
Server string
Name string
Topic string
}
func (u *User) GetChannels() ([]*Channel, error) {
return u.store.GetChannels(u)
}
func (u *User) AddChannel(channel *Channel) error {
return u.store.AddChannel(u, channel)
}
func (u *User) RemoveChannel(server, channel string) error {
return u.store.RemoveChannel(u, server, channel)
}
2015-01-17 01:37:21 +00:00
type Message struct {
ID string `json:"-" bleve:"-"`
Server string `json:"-" bleve:"server"`
From string `bleve:"-"`
To string `json:"-" bleve:"to"`
Content string `bleve:"content"`
Time int64 `bleve:"-"`
2015-01-17 01:37:21 +00:00
}
func (m Message) Type() string {
return "message"
}
2015-01-17 01:37:21 +00:00
func (u *User) LogMessage(id, server, from, to, content string) error {
message := &Message{
ID: id,
Server: server,
From: from,
To: to,
Content: content,
Time: time.Now().Unix(),
}
err := u.messageLog.LogMessage(message)
if err != nil {
return err
}
return u.messageIndex.Index(id, message)
}
func (u *User) GetMessages(server, channel string, count int, fromID string) ([]Message, bool, error) {
return u.messageLog.GetMessages(server, channel, count, fromID)
}
func (u *User) GetLastMessages(server, channel string, count int) ([]Message, bool, error) {
return u.GetMessages(server, channel, count, "")
}
func (u *User) SearchMessages(server, channel, q string) ([]Message, error) {
ids, err := u.messageIndex.SearchMessages(server, channel, q)
if err != nil {
return nil, err
}
return u.messageLog.GetMessagesByID(server, channel, ids)
}