Test message logging
This commit is contained in:
parent
d023f63a7c
commit
1def24500a
|
@ -49,8 +49,8 @@ func handleAuth(w http.ResponseWriter, r *http.Request) *Session {
|
|||
}
|
||||
|
||||
func newUser(w http.ResponseWriter, r *http.Request) *Session {
|
||||
user := storage.NewUser()
|
||||
if user == nil {
|
||||
user, err := storage.NewUser()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,10 @@ func TestMain(m *testing.M) {
|
|||
|
||||
storage.Initialize(tempdir)
|
||||
storage.Open()
|
||||
user = storage.NewUser()
|
||||
user, err = storage.NewUser()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
channelStore = storage.NewChannelStore()
|
||||
|
||||
code := m.Run()
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
|
@ -41,39 +40,34 @@ type Channel struct {
|
|||
Topic string `json:"topic,omitempty"`
|
||||
}
|
||||
|
||||
func NewUser() *User {
|
||||
func NewUser() (*User, error) {
|
||||
user := &User{}
|
||||
|
||||
err := db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket(bucketUsers)
|
||||
|
||||
var err error
|
||||
user.ID, err = b.NextSequence()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user.ID, _ = b.NextSequence()
|
||||
user.Username = strconv.FormatUint(user.ID, 10)
|
||||
user.id = idToBytes(user.ID)
|
||||
|
||||
data, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user.id = idToBytes(user.ID)
|
||||
return b.Put(user.id, data)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = user.openMessageLog()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func LoadUsers() []*User {
|
||||
|
|
|
@ -15,38 +15,42 @@ type Message struct {
|
|||
ID uint64 `json:"id"`
|
||||
Server string `json:"server"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
To string `json:"to,omitempty"`
|
||||
Content string `json:"content"`
|
||||
Time int64 `json:"time"`
|
||||
}
|
||||
|
||||
func (u *User) LogMessage(server, from, to, content string) {
|
||||
func (u *User) LogMessage(server, from, to, content string) error {
|
||||
message := Message{
|
||||
Server: server,
|
||||
From: from,
|
||||
To: to,
|
||||
Content: content,
|
||||
Time: time.Now().Unix(),
|
||||
}
|
||||
bucketKey := server + ":" + to
|
||||
var id uint64
|
||||
var idStr string
|
||||
var message Message
|
||||
|
||||
u.messageLog.Update(func(tx *bolt.Tx) error {
|
||||
b, _ := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey))
|
||||
id, _ = b.NextSequence()
|
||||
idStr = strconv.FormatUint(id, 10)
|
||||
|
||||
message = Message{
|
||||
ID: id,
|
||||
Content: content,
|
||||
Server: server,
|
||||
From: from,
|
||||
To: to,
|
||||
Time: time.Now().Unix(),
|
||||
err := u.messageLog.Batch(func(tx *bolt.Tx) error {
|
||||
b, err := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(message)
|
||||
b.Put([]byte(idStr), data)
|
||||
message.ID, _ = b.NextSequence()
|
||||
|
||||
return nil
|
||||
data, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return b.Put(idToBytes(message.ID), data)
|
||||
})
|
||||
|
||||
u.messageIndex.Index(bucketKey+":"+idStr, message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return u.messageIndex.Index(bucketKey+":"+strconv.FormatUint(message.ID, 10), message)
|
||||
}
|
||||
|
||||
func (u *User) GetLastMessages(server, channel string, count int) ([]Message, error) {
|
||||
|
@ -60,7 +64,7 @@ func (u *User) GetLastMessages(server, channel string, count int) ([]Message, er
|
|||
|
||||
c := b.Cursor()
|
||||
|
||||
for k, v := c.Last(); count > 0 && k != nil; k, v = c.Prev() {
|
||||
for _, v := c.Last(); count > 0 && v != nil; _, v = c.Prev() {
|
||||
count--
|
||||
json.Unmarshal(v, &messages[count])
|
||||
}
|
||||
|
@ -85,7 +89,7 @@ func (u *User) GetMessages(server, channel string, count int, fromID uint64) ([]
|
|||
}
|
||||
|
||||
c := b.Cursor()
|
||||
c.Seek([]byte(strconv.FormatUint(fromID, 10)))
|
||||
c.Seek(idToBytes(fromID))
|
||||
|
||||
for k, v := c.Prev(); count > 0 && k != nil; k, v = c.Prev() {
|
||||
count--
|
||||
|
@ -107,7 +111,7 @@ func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error)
|
|||
serverQuery.SetField("server")
|
||||
channelQuery := bleve.NewMatchQuery(channel)
|
||||
channelQuery.SetField("to")
|
||||
contentQuery := bleve.NewMatchQuery(phrase)
|
||||
contentQuery := bleve.NewFuzzyQuery(phrase)
|
||||
contentQuery.SetField("content")
|
||||
|
||||
query := bleve.NewBooleanQuery([]bleve.Query{serverQuery, channelQuery, contentQuery}, nil, nil)
|
||||
|
@ -125,9 +129,10 @@ func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error)
|
|||
for _, hit := range searchResults.Hits {
|
||||
idx := strings.LastIndex(hit.ID, ":")
|
||||
bc := b.Bucket([]byte(hit.ID[:idx]))
|
||||
var message Message
|
||||
id, _ := strconv.ParseUint(hit.ID[idx+1:], 10, 64)
|
||||
|
||||
json.Unmarshal(bc.Get([]byte(hit.ID[idx+1:])), &message)
|
||||
var message Message
|
||||
json.Unmarshal(bc.Get(idToBytes(id)), &message)
|
||||
messages = append(messages, message)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package storage
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||
|
@ -35,7 +36,8 @@ func TestUser(t *testing.T) {
|
|||
Name: "#testing",
|
||||
}
|
||||
|
||||
user := NewUser()
|
||||
user, err := NewUser()
|
||||
assert.Nil(t, err)
|
||||
user.AddServer(srv)
|
||||
user.AddChannel(chan1)
|
||||
user.AddChannel(chan2)
|
||||
|
@ -68,3 +70,62 @@ func TestUser(t *testing.T) {
|
|||
assert.Len(t, user.GetServers(), 0)
|
||||
assert.Len(t, user.GetChannels(), 0)
|
||||
}
|
||||
|
||||
func TestMessages(t *testing.T) {
|
||||
Initialize(tempdir())
|
||||
Open()
|
||||
|
||||
user, err := NewUser()
|
||||
assert.Nil(t, err)
|
||||
|
||||
messages, err := user.GetMessages("irc.freenode.net", "#go-nuts", 10, 6)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 0)
|
||||
|
||||
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 0)
|
||||
|
||||
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 0)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
err = user.LogMessage("irc.freenode.net", "nick", "#go-nuts", "message"+strconv.Itoa(i))
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 6)
|
||||
assert.Equal(t, "message0", messages[0].Content)
|
||||
assert.Equal(t, "message4", messages[4].Content)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 5)
|
||||
|
||||
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 100)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 5)
|
||||
|
||||
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 4)
|
||||
assert.Equal(t, "message0", messages[0].Content)
|
||||
assert.Equal(t, "message2", messages[2].Content)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 3)
|
||||
|
||||
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
|
||||
assert.Equal(t, "message0", messages[0].Content)
|
||||
assert.Equal(t, "message2", messages[2].Content)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 5)
|
||||
|
||||
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 3)
|
||||
assert.Equal(t, "message2", messages[0].Content)
|
||||
assert.Equal(t, "message4", messages[2].Content)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 3)
|
||||
|
||||
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, messages, 5)
|
||||
|
||||
Close()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue