2021-08-06 15:40:23 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
type RawCommunityRow struct {
|
2021-08-06 15:40:23 +00:00
|
|
|
ID []byte
|
|
|
|
PrivateKey []byte
|
|
|
|
Description []byte
|
|
|
|
Joined bool
|
2022-09-20 19:57:39 +00:00
|
|
|
Spectated bool
|
2021-08-06 15:40:23 +00:00
|
|
|
Verified bool
|
|
|
|
SyncedAt uint64
|
|
|
|
Muted bool
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func fromSyncCommunityProtobuf(syncCommProto *protobuf.SyncCommunity) RawCommunityRow {
|
|
|
|
return RawCommunityRow{
|
2021-08-06 15:40:23 +00:00
|
|
|
ID: syncCommProto.Id,
|
|
|
|
Description: syncCommProto.Description,
|
|
|
|
Joined: syncCommProto.Joined,
|
2022-09-20 19:57:39 +00:00
|
|
|
Spectated: syncCommProto.Spectated,
|
2021-08-06 15:40:23 +00:00
|
|
|
Verified: syncCommProto.Verified,
|
|
|
|
SyncedAt: syncCommProto.Clock,
|
|
|
|
Muted: syncCommProto.Muted,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func (p *Persistence) scanRowToStruct(rowScan func(dest ...interface{}) error) (*RawCommunityRow, error) {
|
|
|
|
rcr := new(RawCommunityRow)
|
2023-06-28 10:53:46 +00:00
|
|
|
var syncedAt, muteTill sql.NullTime
|
2021-08-06 15:40:23 +00:00
|
|
|
|
|
|
|
err := rowScan(
|
|
|
|
&rcr.ID,
|
|
|
|
&rcr.PrivateKey,
|
|
|
|
&rcr.Description,
|
|
|
|
&rcr.Joined,
|
|
|
|
&rcr.Verified,
|
2022-09-20 19:57:39 +00:00
|
|
|
&rcr.Spectated,
|
2023-06-22 15:49:58 +00:00
|
|
|
&rcr.Muted,
|
2023-06-17 08:19:05 +00:00
|
|
|
&muteTill,
|
2023-06-22 15:49:58 +00:00
|
|
|
&syncedAt,
|
2021-08-06 15:40:23 +00:00
|
|
|
)
|
|
|
|
if syncedAt.Valid {
|
|
|
|
rcr.SyncedAt = uint64(syncedAt.Time.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rcr, nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func (p *Persistence) getAllCommunitiesRaw() (rcrs []*RawCommunityRow, err error) {
|
2021-08-06 15:40:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func (p *Persistence) getRawCommunityRow(id []byte) (*RawCommunityRow, error) {
|
2021-08-06 15:40:23 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func (p *Persistence) getSyncedRawCommunity(id []byte) (*RawCommunityRow, error) {
|
2021-08-06 15:40:23 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func (p *Persistence) saveRawCommunityRow(rawCommRow RawCommunityRow) error {
|
2021-08-06 15:40:23 +00:00
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:10:40 +00:00
|
|
|
func (p *Persistence) saveRawCommunityRowWithoutSyncedAt(rawCommRow RawCommunityRow) error {
|
2021-08-06 15:40:23 +00:00
|
|
|
_, 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
|
|
|
|
}
|