Remove openDMs when deleting user

This commit is contained in:
Ken-Håvard Lieng 2020-05-20 00:23:16 +02:00
parent abbe739b04
commit f0eada0f75
2 changed files with 36 additions and 28 deletions

View File

@ -93,21 +93,16 @@ func (s *BoltStore) SaveUser(user *storage.User) error {
func (s *BoltStore) DeleteUser(user *storage.User) error { func (s *BoltStore) DeleteUser(user *storage.User) error {
return s.db.Batch(func(tx *bolt.Tx) error { return s.db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketServers) err := tx.Bucket(bucketUsers).Delete(user.IDBytes)
c := b.Cursor() if err != nil {
return err
for k, _ := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, _ = c.Next() {
b.Delete(k)
} }
b = tx.Bucket(bucketChannels) return deletePrefix(user.IDBytes,
c = b.Cursor() tx.Bucket(bucketServers),
tx.Bucket(bucketChannels),
for k, _ := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, _ = c.Next() { tx.Bucket(bucketOpenDMs),
b.Delete(k) )
}
return tx.Bucket(bucketUsers).Delete(user.IDBytes)
}) })
} }
@ -161,23 +156,15 @@ func (s *BoltStore) SaveServer(user *storage.User, server *storage.Server) error
func (s *BoltStore) RemoveServer(user *storage.User, address string) error { func (s *BoltStore) RemoveServer(user *storage.User, address string) error {
return s.db.Batch(func(tx *bolt.Tx) error { return s.db.Batch(func(tx *bolt.Tx) error {
serverID := serverID(user, address) serverID := serverID(user, address)
tx.Bucket(bucketServers).Delete(serverID) err := tx.Bucket(bucketServers).Delete(serverID)
if err != nil {
b := tx.Bucket(bucketChannels) return err
c := b.Cursor()
for k, _ := c.Seek(serverID); bytes.HasPrefix(k, serverID); k, _ = c.Next() {
b.Delete(k)
} }
b = tx.Bucket(bucketOpenDMs) return deletePrefix(serverID,
c = b.Cursor() tx.Bucket(bucketChannels),
tx.Bucket(bucketOpenDMs),
for k, _ := c.Seek(serverID); bytes.HasPrefix(k, serverID); k, _ = c.Next() { )
b.Delete(k)
}
return nil
}) })
} }
@ -403,6 +390,21 @@ func (s *BoltStore) DeleteSession(key string) error {
}) })
} }
func deletePrefix(prefix []byte, buckets ...*bolt.Bucket) error {
for _, b := range buckets {
c := b.Cursor()
for k, _ := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, _ = c.Next() {
err := b.Delete(k)
if err != nil {
return err
}
}
}
return nil
}
func serverID(user *storage.User, address string) []byte { func serverID(user *storage.User, address string) []byte {
id := make([]byte, 8+len(address)) id := make([]byte, 8+len(address))
copy(id, user.IDBytes) copy(id, user.IDBytes)

View File

@ -100,10 +100,16 @@ func TestUser(t *testing.T) {
assert.Equal(t, settings, user.GetClientSettings()) assert.Equal(t, settings, user.GetClientSettings())
assert.NotEqual(t, settings, storage.DefaultClientSettings()) assert.NotEqual(t, settings, storage.DefaultClientSettings())
user.AddOpenDM(srv.Host, "cake")
user.Remove() user.Remove()
_, err = os.Stat(storage.Path.User(user.Username)) _, err = os.Stat(storage.Path.User(user.Username))
assert.True(t, os.IsNotExist(err)) assert.True(t, os.IsNotExist(err))
openDMs, err = user.GetOpenDMs()
assert.Nil(t, err)
assert.Len(t, openDMs, 0)
users, err = storage.LoadUsers(db) users, err = storage.LoadUsers(db)
assert.Nil(t, err) assert.Nil(t, err)