status-go/protocol/communities/persistence_test_helpers.go
Samuel Hawksby-Robinson 89251e8416
Sync Communities (#2253)
* Added community sync protobuf

* Updated community sync send logic

* Integrated syncCommunity handling

* Added synced_at field and tidied up some other logic

* persistence testing

* Added testing and join functionality

* Fixed issue with empty scan params

* Finshed persistence tests for new db funcs

* Midway debug of description not persisting after sync

* Resolved final issues and tidied up

* Polish

* delint

* Fix error not handled on SetPrivateKey

* fix infinite loop, again

* Added muted option and test fix

* Added Muted to syncing functions, not just in persistence

* Fix bug introduced with Muted property

* Added a couple of notes for future devs

* Added most of the sync RequestToJoin functionality

Tests need to be completed and tests are giving some errors

* Finished tests for getJoinedAndPending

* Added note

* Resolving lint

* Fix of protobuf gen bug

* Fixes to community sync tests

* Fixes to test

* Continued fix of e2e

* Final fix to e2e testing

* Updated migration position

* resolve missing import

* Apparently the linter spellchecks

* Fix bug from  #2276 merge

* Bug fix for leaving quirkiness

* Addressed superfluous MessengerResponse field

* Addressed feedback

* VERSION bump
2021-08-06 16:40:23 +01:00

122 lines
3.1 KiB
Go

package communities
import (
"database/sql"
"github.com/status-im/status-go/protocol/protobuf"
)
type rawCommunityRow struct {
ID []byte
PrivateKey []byte
Description []byte
Joined bool
Verified bool
SyncedAt uint64
Muted bool
}
func fromSyncCommunityProtobuf(syncCommProto *protobuf.SyncCommunity) rawCommunityRow {
return rawCommunityRow{
ID: syncCommProto.Id,
PrivateKey: syncCommProto.PrivateKey,
Description: syncCommProto.Description,
Joined: syncCommProto.Joined,
Verified: syncCommProto.Verified,
SyncedAt: syncCommProto.Clock,
Muted: syncCommProto.Muted,
}
}
func (p *Persistence) scanRowToStruct(rowScan func(dest ...interface{}) error) (*rawCommunityRow, error) {
rcr := new(rawCommunityRow)
syncedAt := sql.NullTime{}
err := rowScan(
&rcr.ID,
&rcr.PrivateKey,
&rcr.Description,
&rcr.Joined,
&rcr.Verified,
&rcr.Muted,
&syncedAt,
)
if syncedAt.Valid {
rcr.SyncedAt = uint64(syncedAt.Time.Unix())
}
if err != nil {
return nil, err
}
return rcr, nil
}
func (p *Persistence) getAllCommunitiesRaw() (rcrs []*rawCommunityRow, err error) {
var rows *sql.Rows
// Keep "*", if the db table is updated, syncing needs to match, this fail will force us to update syncing.
rows, err = p.db.Query(`SELECT * FROM communities_communities`)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
// Don't shadow original error
_ = rows.Close()
return
}
err = rows.Close()
}()
for rows.Next() {
rcr, err := p.scanRowToStruct(rows.Scan)
if err != nil {
return nil, err
}
rcrs = append(rcrs, rcr)
}
return rcrs, nil
}
func (p *Persistence) getRawCommunityRow(id []byte) (*rawCommunityRow, error) {
// Keep "*", if the db table is updated, syncing needs to match, this fail will force us to update syncing.
qr := p.db.QueryRow(`SELECT * FROM communities_communities WHERE id = ?`, id)
return p.scanRowToStruct(qr.Scan)
}
func (p *Persistence) getSyncedRawCommunity(id []byte) (*rawCommunityRow, error) {
// Keep "*", if the db table is updated, syncing needs to match, this fail will force us to update syncing.
qr := p.db.QueryRow(`SELECT * FROM communities_communities WHERE id = ? AND synced_at > 0`, id)
return p.scanRowToStruct(qr.Scan)
}
func (p *Persistence) saveRawCommunityRow(rawCommRow rawCommunityRow) error {
_, err := p.db.Exec(
`INSERT INTO communities_communities ("id", "private_key", "description", "joined", "verified", "synced_at", "muted") VALUES (?, ?, ?, ?, ?, ?, ?)`,
rawCommRow.ID,
rawCommRow.PrivateKey,
rawCommRow.Description,
rawCommRow.Joined,
rawCommRow.Verified,
rawCommRow.SyncedAt,
rawCommRow.Muted,
)
return err
}
func (p *Persistence) saveRawCommunityRowWithoutSyncedAt(rawCommRow rawCommunityRow) error {
_, err := p.db.Exec(
`INSERT INTO communities_communities ("id", "private_key", "description", "joined", "verified", "muted") VALUES (?, ?, ?, ?, ?, ?)`,
rawCommRow.ID,
rawCommRow.PrivateKey,
rawCommRow.Description,
rawCommRow.Joined,
rawCommRow.Verified,
rawCommRow.Muted,
)
return err
}