Remove empty anonymous sessions after a certain time period

This commit is contained in:
Ken-Håvard Lieng 2016-01-19 22:02:12 +01:00
parent 3bcea0ec98
commit e856b66f97
6 changed files with 77 additions and 14 deletions

View file

@ -2,6 +2,7 @@ package storage
import (
"bytes"
"os"
"strconv"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/boltdb/bolt"
@ -169,9 +170,12 @@ func (u *User) RemoveChannel(server, channel string) {
})
}
func (u *User) Close() {
u.messageLog.Close()
u.messageIndex.Close()
func (u *User) Remove() {
db.Batch(func(tx *bolt.Tx) error {
return tx.Bucket(bucketUsers).Delete(u.id)
})
u.closeMessageLog()
os.RemoveAll(Path.User(u.Username))
}
func (u *User) serverID(address string) []byte {

View file

@ -167,3 +167,8 @@ func (u *User) openMessageLog() error {
return nil
}
func (u *User) closeMessageLog() {
u.messageLog.Close()
u.messageIndex.Close()
}

View file

@ -2,6 +2,7 @@ package storage
import (
"io/ioutil"
"os"
"strconv"
"testing"
@ -41,7 +42,7 @@ func TestUser(t *testing.T) {
user.AddServer(srv)
user.AddChannel(chan1)
user.AddChannel(chan2)
user.Close()
user.closeMessageLog()
users := LoadUsers()
assert.Len(t, users, 1)
@ -69,6 +70,14 @@ func TestUser(t *testing.T) {
user.RemoveServer(srv.Host)
assert.Len(t, user.GetServers(), 0)
assert.Len(t, user.GetChannels(), 0)
user.Remove()
_, err = os.Stat(Path.User(user.Username))
assert.True(t, os.IsNotExist(err))
for _, storedUser := range LoadUsers() {
assert.NotEqual(t, user.ID, storedUser.ID)
}
}
func TestMessages(t *testing.T) {