2015-01-17 01:37:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-04-29 21:54:44 +00:00
|
|
|
"log"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2015-05-21 07:15:57 +00:00
|
|
|
"strings"
|
2015-04-29 21:54:44 +00:00
|
|
|
"time"
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/blevesearch/bleve"
|
2015-04-26 22:45:49 +00:00
|
|
|
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/boltdb/bolt"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2015-02-07 02:10:58 +00:00
|
|
|
Name string `json:"name"`
|
2015-01-22 00:03:25 +00:00
|
|
|
Address string `json:"address"`
|
2015-02-02 00:54:26 +00:00
|
|
|
TLS bool `json:"tls"`
|
2015-02-07 02:10:58 +00:00
|
|
|
Password string `json:"password,omitempty"`
|
2015-01-22 00:03:25 +00:00
|
|
|
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"`
|
2015-02-12 01:14:56 +00:00
|
|
|
Users []string `json:"users,omitempty"`
|
2015-01-21 02:06:34 +00:00
|
|
|
Topic string `json:"topic,omitempty"`
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
type Message struct {
|
|
|
|
ID uint64 `json:"id"`
|
|
|
|
Server string `json:"server"`
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Time int64 `json:"time"`
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
type User struct {
|
|
|
|
UUID string
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
messageLog *bolt.DB
|
|
|
|
messageIndex bleve.Index
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func NewUser(uuid string) *User {
|
|
|
|
user := &User{
|
2015-01-17 01:37:21 +00:00
|
|
|
UUID: uuid,
|
|
|
|
}
|
|
|
|
|
2015-06-07 04:16:16 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2015-01-17 01:37:21 +00:00
|
|
|
b := tx.Bucket([]byte("Users"))
|
|
|
|
data, _ := json.Marshal(user)
|
|
|
|
|
|
|
|
b.Put([]byte(uuid), data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-06-07 04:16:16 +00:00
|
|
|
user.openMessageLog()
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +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([]byte("Users"))
|
|
|
|
|
|
|
|
b.ForEach(func(k, v []byte) error {
|
2015-04-29 21:54:44 +00:00
|
|
|
user := User{UUID: string(k)}
|
2015-06-07 04:16:16 +00:00
|
|
|
user.openMessageLog()
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
users = append(users, &user)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
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([]byte("Servers")).Cursor()
|
|
|
|
prefix := []byte(u.UUID)
|
|
|
|
|
|
|
|
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
|
|
|
var server Server
|
|
|
|
json.Unmarshal(v, &server)
|
|
|
|
servers = append(servers, server)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
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([]byte("Channels")).Cursor()
|
|
|
|
|
|
|
|
prefix := []byte(u.UUID)
|
|
|
|
|
|
|
|
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
|
|
|
var channel Channel
|
|
|
|
json.Unmarshal(v, &channel)
|
|
|
|
channels = append(channels, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) AddServer(server Server) {
|
2015-06-07 04:16:16 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2015-01-17 01:37:21 +00:00
|
|
|
b := tx.Bucket([]byte("Servers"))
|
|
|
|
data, _ := json.Marshal(server)
|
|
|
|
|
|
|
|
b.Put([]byte(u.UUID+":"+server.Address), data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) AddChannel(channel Channel) {
|
2015-06-07 04:16:16 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2015-01-17 01:37:21 +00:00
|
|
|
b := tx.Bucket([]byte("Channels"))
|
|
|
|
data, _ := json.Marshal(channel)
|
|
|
|
|
|
|
|
b.Put([]byte(u.UUID+":"+channel.Server+":"+channel.Name), data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) SetNick(nick, address string) {
|
2015-06-07 04:16:16 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2015-02-04 01:43:49 +00:00
|
|
|
b := tx.Bucket([]byte("Servers"))
|
|
|
|
id := []byte(u.UUID + ":" + address)
|
|
|
|
var server Server
|
|
|
|
|
|
|
|
json.Unmarshal(b.Get(id), &server)
|
|
|
|
server.Nick = nick
|
|
|
|
|
|
|
|
data, _ := json.Marshal(server)
|
|
|
|
b.Put(id, data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) RemoveServer(address string) {
|
2015-06-07 04:16:16 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2015-01-21 02:06:34 +00:00
|
|
|
serverID := []byte(u.UUID + ":" + address)
|
|
|
|
|
|
|
|
tx.Bucket([]byte("Servers")).Delete(serverID)
|
|
|
|
|
|
|
|
b := tx.Bucket([]byte("Channels"))
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) RemoveChannel(server, channel string) {
|
2015-06-07 04:16:16 +00:00
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
2015-01-21 02:06:34 +00:00
|
|
|
tx.Bucket([]byte("Channels")).Delete([]byte(u.UUID + ":" + server + ":" + channel))
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
func (u *User) LogMessage(server, from, to, content string) {
|
2015-06-07 04:16:16 +00:00
|
|
|
bucketKey := server + ":" + to
|
|
|
|
var id uint64
|
|
|
|
var idStr string
|
|
|
|
var message Message
|
|
|
|
|
|
|
|
u.messageLog.Update(func(tx *bolt.Tx) error {
|
2015-05-21 07:15:57 +00:00
|
|
|
b, _ := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey))
|
2015-06-07 04:16:16 +00:00
|
|
|
id, _ = b.NextSequence()
|
|
|
|
idStr = strconv.FormatUint(id, 10)
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2015-06-07 04:16:16 +00:00
|
|
|
message = Message{
|
2015-05-21 07:15:57 +00:00
|
|
|
ID: id,
|
2015-04-29 21:54:44 +00:00
|
|
|
Content: content,
|
|
|
|
Server: server,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
data, _ := json.Marshal(message)
|
2015-05-21 07:15:57 +00:00
|
|
|
b.Put([]byte(idStr), data)
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2015-06-07 04:16:16 +00:00
|
|
|
|
|
|
|
u.messageIndex.Index(bucketKey+":"+idStr, message)
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 07:15:57 +00:00
|
|
|
func (u *User) GetLastMessages(server, channel string, count int) ([]Message, error) {
|
|
|
|
messages := make([]Message, count)
|
|
|
|
|
|
|
|
u.messageLog.View(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket(bucketMessages).Bucket([]byte(server + ":" + channel))
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := b.Cursor()
|
|
|
|
|
|
|
|
for k, v := c.Last(); count > 0 && k != nil; k, v = c.Prev() {
|
|
|
|
count--
|
|
|
|
json.Unmarshal(v, &messages[count])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if count < len(messages) {
|
|
|
|
return messages[count:], nil
|
|
|
|
} else {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) GetMessages(server, channel string, count int, fromID uint64) ([]Message, error) {
|
|
|
|
messages := make([]Message, count)
|
|
|
|
|
|
|
|
u.messageLog.View(func(tx *bolt.Tx) error {
|
2015-05-21 07:15:57 +00:00
|
|
|
b := tx.Bucket(bucketMessages).Bucket([]byte(server + ":" + channel))
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2015-05-21 07:15:57 +00:00
|
|
|
c := b.Cursor()
|
|
|
|
c.Seek([]byte(strconv.FormatUint(fromID, 10)))
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2015-05-21 07:15:57 +00:00
|
|
|
for k, v := c.Prev(); count > 0 && k != nil; k, v = c.Prev() {
|
|
|
|
count--
|
|
|
|
json.Unmarshal(v, &messages[count])
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-05-21 07:15:57 +00:00
|
|
|
if count < len(messages) {
|
|
|
|
return messages[count:], nil
|
|
|
|
} else {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error) {
|
|
|
|
serverQuery := bleve.NewMatchQuery(server)
|
2015-05-10 23:09:59 +00:00
|
|
|
serverQuery.SetField("server")
|
2015-04-29 21:54:44 +00:00
|
|
|
channelQuery := bleve.NewMatchQuery(channel)
|
2015-05-10 23:09:59 +00:00
|
|
|
channelQuery.SetField("to")
|
2015-04-29 21:54:44 +00:00
|
|
|
contentQuery := bleve.NewMatchQuery(phrase)
|
2015-05-10 23:09:59 +00:00
|
|
|
contentQuery.SetField("content")
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
query := bleve.NewBooleanQuery([]bleve.Query{serverQuery, channelQuery, contentQuery}, nil, nil)
|
|
|
|
|
|
|
|
search := bleve.NewSearchRequest(query)
|
|
|
|
searchResults, err := u.messageIndex.Search(search)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-10 23:09:59 +00:00
|
|
|
messages := []Message{}
|
2015-04-29 21:54:44 +00:00
|
|
|
u.messageLog.View(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket(bucketMessages)
|
|
|
|
|
|
|
|
for _, hit := range searchResults.Hits {
|
2015-05-21 07:15:57 +00:00
|
|
|
idx := strings.LastIndex(hit.ID, ":")
|
|
|
|
bc := b.Bucket([]byte(hit.ID[:idx]))
|
2015-04-29 21:54:44 +00:00
|
|
|
var message Message
|
|
|
|
|
2015-05-21 07:15:57 +00:00
|
|
|
json.Unmarshal(bc.Get([]byte(hit.ID[idx+1:])), &message)
|
2015-04-29 21:54:44 +00:00
|
|
|
messages = append(messages, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return messages, nil
|
|
|
|
}
|
|
|
|
|
2015-06-07 04:16:16 +00:00
|
|
|
func (u *User) Close() {
|
|
|
|
u.messageLog.Close()
|
|
|
|
u.messageIndex.Close()
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
func (u *User) openMessageLog() {
|
|
|
|
var err error
|
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
u.messageLog, err = bolt.Open(path.Join(appDir, "logs", u.UUID+"_log"), 0600, nil)
|
2015-04-29 21:54:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u.messageLog.Update(func(tx *bolt.Tx) error {
|
|
|
|
tx.CreateBucketIfNotExists(bucketMessages)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
indexPath := path.Join(appDir, "logs", u.UUID+"_index")
|
2015-04-29 21:54:44 +00:00
|
|
|
u.messageIndex, err = bleve.Open(indexPath)
|
|
|
|
if err == bleve.ErrorIndexPathDoesNotExist {
|
|
|
|
mapping := bleve.NewIndexMapping()
|
|
|
|
u.messageIndex, err = bleve.New(indexPath, mapping)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|