2023-10-26 06:30:18 +00:00
|
|
|
package community
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-11-16 04:18:03 +00:00
|
|
|
"time"
|
2023-10-26 06:30:18 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DataDB struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDataDB(sqlDb *sql.DB) *DataDB {
|
|
|
|
return &DataDB{
|
|
|
|
db: sqlDb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
type InfoState struct {
|
|
|
|
LastUpdateTimestamp uint64
|
|
|
|
LastUpdateSuccesful bool
|
|
|
|
}
|
|
|
|
|
2023-12-15 20:29:39 +00:00
|
|
|
const communityInfoColumns = "id, name, color, image, image_payload"
|
|
|
|
const selectCommunityInfoColumns = "name, color, image, image_payload"
|
2023-11-16 04:18:03 +00:00
|
|
|
|
|
|
|
const communityInfoStateColumns = "id, last_update_timestamp, last_update_successful"
|
|
|
|
const selectCommunityInfoStateColumns = "last_update_timestamp, last_update_successful"
|
2023-10-26 06:30:18 +00:00
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
func (o *DataDB) SetCommunityInfo(id string, c *thirdparty.CommunityInfo) (err error) {
|
2023-10-26 06:30:18 +00:00
|
|
|
tx, err := o.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
setState, err := tx.Prepare(fmt.Sprintf(`INSERT OR REPLACE INTO community_data_cache_state (%s)
|
|
|
|
VALUES (?, ?, ?)`, communityInfoStateColumns))
|
2023-10-26 06:30:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
valid := c != nil
|
|
|
|
_, err = setState.Exec(
|
2023-10-26 06:30:18 +00:00
|
|
|
id,
|
2023-11-16 04:18:03 +00:00
|
|
|
time.Now().Unix(),
|
|
|
|
valid,
|
2023-10-26 06:30:18 +00:00
|
|
|
)
|
2023-11-16 04:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if valid {
|
|
|
|
setInfo, err := tx.Prepare(fmt.Sprintf(`INSERT OR REPLACE INTO community_data_cache (%s)
|
2023-12-15 20:29:39 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?)`, communityInfoColumns))
|
2023-11-16 04:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = setInfo.Exec(
|
|
|
|
id,
|
|
|
|
c.CommunityName,
|
|
|
|
c.CommunityColor,
|
|
|
|
c.CommunityImage,
|
2023-12-15 20:29:39 +00:00
|
|
|
c.CommunityImagePayload,
|
2023-11-16 04:18:03 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 06:30:18 +00:00
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
return nil
|
2023-10-26 06:30:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
func (o *DataDB) GetCommunityInfo(id string) (*thirdparty.CommunityInfo, *InfoState, error) {
|
2023-12-13 12:19:25 +00:00
|
|
|
if id == "" {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
var info thirdparty.CommunityInfo
|
|
|
|
var state InfoState
|
|
|
|
var row *sql.Row
|
|
|
|
|
|
|
|
getState, err := o.db.Prepare(fmt.Sprintf(`SELECT %s
|
|
|
|
FROM community_data_cache_state
|
|
|
|
WHERE id=?`, selectCommunityInfoStateColumns))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
row = getState.QueryRow(id)
|
|
|
|
|
|
|
|
err = row.Scan(
|
|
|
|
&state.LastUpdateTimestamp,
|
|
|
|
&state.LastUpdateSuccesful,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-10-26 06:30:18 +00:00
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
getInfo, err := o.db.Prepare(fmt.Sprintf(`SELECT %s
|
2023-10-26 06:30:18 +00:00
|
|
|
FROM community_data_cache
|
2023-11-16 04:18:03 +00:00
|
|
|
WHERE id=?`, selectCommunityInfoColumns))
|
2023-10-26 06:30:18 +00:00
|
|
|
if err != nil {
|
2023-11-16 04:18:03 +00:00
|
|
|
return nil, nil, err
|
2023-10-26 06:30:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
row = getInfo.QueryRow(id)
|
2023-10-26 06:30:18 +00:00
|
|
|
|
|
|
|
err = row.Scan(
|
2023-11-16 04:18:03 +00:00
|
|
|
&info.CommunityName,
|
|
|
|
&info.CommunityColor,
|
|
|
|
&info.CommunityImage,
|
2023-12-15 20:29:39 +00:00
|
|
|
&info.CommunityImagePayload,
|
2023-10-26 06:30:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
2023-11-16 04:18:03 +00:00
|
|
|
return nil, &state, nil
|
2023-10-26 06:30:18 +00:00
|
|
|
} else if err != nil {
|
2023-11-16 04:18:03 +00:00
|
|
|
return nil, nil, err
|
2023-10-26 06:30:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 04:18:03 +00:00
|
|
|
return &info, &state, nil
|
2023-10-26 06:30:18 +00:00
|
|
|
}
|