2022-03-23 18:47:00 +00:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2022-12-12 10:20:32 +00:00
|
|
|
"time"
|
2022-03-23 18:47:00 +00:00
|
|
|
|
2023-10-24 10:15:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
|
2023-08-10 11:41:04 +00:00
|
|
|
"github.com/status-im/status-go/common/dbsetup"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/errors"
|
|
|
|
"github.com/status-im/status-go/nodecfg"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/sqlite"
|
|
|
|
)
|
|
|
|
|
2023-10-09 19:31:47 +00:00
|
|
|
type Notifier func(SettingField, interface{})
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
var (
|
|
|
|
// dbInstances holds a map of singleton instances of Database
|
|
|
|
dbInstances map[string]*Database
|
|
|
|
|
|
|
|
// mutex guards the instantiation of the dbInstances values, to prevent any concurrent instantiations
|
|
|
|
mutex sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// Database sql wrapper for operations with browser objects.
|
|
|
|
type Database struct {
|
2023-10-24 10:15:32 +00:00
|
|
|
db *sql.DB
|
|
|
|
SyncQueue chan SyncSettingField
|
|
|
|
changesSubscriptions []chan *SyncSettingField
|
|
|
|
notifier Notifier
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeNewDB ensures that a singleton instance of Database is returned per sqlite db file
|
|
|
|
func MakeNewDB(db *sql.DB) (*Database, error) {
|
2023-08-10 11:41:04 +00:00
|
|
|
filename, err := dbsetup.GetDBFilename(db)
|
2022-03-23 18:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &Database{
|
|
|
|
db: db,
|
|
|
|
SyncQueue: make(chan SyncSettingField, 100),
|
|
|
|
}
|
|
|
|
|
|
|
|
// An empty filename means that the sqlite database is held in memory
|
|
|
|
// In this case we don't want to restrict the instantiation
|
|
|
|
if filename == "" {
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock to protect the map from concurrent access
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
// init dbInstances if it hasn't been already
|
|
|
|
if dbInstances == nil {
|
|
|
|
dbInstances = map[string]*Database{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't seen this database file before make an instance
|
|
|
|
if _, ok := dbInstances[filename]; !ok {
|
|
|
|
dbInstances[filename] = d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the current dbInstance is closed, if closed assign new Database
|
|
|
|
if err := dbInstances[filename].db.Ping(); err != nil {
|
|
|
|
dbInstances[filename] = d
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbInstances[filename], nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 18:05:30 +00:00
|
|
|
func (db *Database) GetDB() *sql.DB {
|
|
|
|
return db.db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetSyncQueue() chan SyncSettingField {
|
|
|
|
return db.SyncQueue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetChangesSubscriptions() []chan *SyncSettingField {
|
|
|
|
return db.changesSubscriptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetNotifier() Notifier {
|
|
|
|
return db.notifier
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:31:47 +00:00
|
|
|
func (db *Database) SetSettingsNotifier(n Notifier) {
|
|
|
|
db.notifier = n
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// TODO remove photoPath from settings
|
|
|
|
func (db *Database) CreateSettings(s Settings, n params.NodeConfig) error {
|
|
|
|
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// don't shadow original error
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err = tx.Exec(`
|
|
|
|
INSERT INTO settings (
|
|
|
|
address,
|
|
|
|
currency,
|
|
|
|
current_network,
|
|
|
|
dapps_address,
|
2023-06-27 07:54:49 +00:00
|
|
|
device_name,
|
2022-03-23 18:47:00 +00:00
|
|
|
display_name,
|
2022-08-05 11:22:35 +00:00
|
|
|
bio,
|
2022-03-23 18:47:00 +00:00
|
|
|
eip1581_address,
|
|
|
|
installation_id,
|
|
|
|
key_uid,
|
|
|
|
keycard_instance_uid,
|
|
|
|
keycard_paired_on,
|
|
|
|
keycard_pairing,
|
2023-05-08 15:14:46 +00:00
|
|
|
latest_derived_path,
|
2022-03-23 18:47:00 +00:00
|
|
|
mnemonic,
|
|
|
|
name,
|
|
|
|
networks,
|
|
|
|
photo_path,
|
|
|
|
preview_privacy,
|
|
|
|
public_key,
|
|
|
|
signing_phrase,
|
|
|
|
wallet_root_address,
|
2022-08-23 12:25:34 +00:00
|
|
|
synthetic_id,
|
2022-10-05 10:53:42 +00:00
|
|
|
current_user_status,
|
|
|
|
profile_pictures_show_to,
|
2023-10-13 14:31:56 +00:00
|
|
|
profile_pictures_visibility,
|
2023-10-18 10:02:35 +00:00
|
|
|
url_unfurling_mode,
|
2023-12-01 11:30:42 +00:00
|
|
|
omit_transfers_history_scan,
|
|
|
|
mnemonic_was_not_shown
|
2022-03-23 18:47:00 +00:00
|
|
|
) VALUES (
|
2023-06-27 07:54:49 +00:00
|
|
|
?,?,?,?,?,?,?,?,?,?,?,?,?,
|
2023-12-01 11:30:42 +00:00
|
|
|
?,?,?,?,?,?,?,?,?,'id',?,?,?,?,?,?)`,
|
2022-03-23 18:47:00 +00:00
|
|
|
s.Address,
|
|
|
|
s.Currency,
|
|
|
|
s.CurrentNetwork,
|
|
|
|
s.DappsAddress,
|
2023-06-27 07:54:49 +00:00
|
|
|
s.DeviceName,
|
2022-03-23 18:47:00 +00:00
|
|
|
s.DisplayName,
|
2022-08-05 11:22:35 +00:00
|
|
|
s.Bio,
|
2022-03-23 18:47:00 +00:00
|
|
|
s.EIP1581Address,
|
|
|
|
s.InstallationID,
|
|
|
|
s.KeyUID,
|
|
|
|
s.KeycardInstanceUID,
|
2022-08-25 15:09:08 +00:00
|
|
|
s.KeycardPairedOn,
|
2022-03-23 18:47:00 +00:00
|
|
|
s.KeycardPairing,
|
2023-05-08 15:14:46 +00:00
|
|
|
s.LatestDerivedPath,
|
2022-03-23 18:47:00 +00:00
|
|
|
s.Mnemonic,
|
|
|
|
s.Name,
|
|
|
|
s.Networks,
|
|
|
|
s.PhotoPath,
|
|
|
|
s.PreviewPrivacy,
|
|
|
|
s.PublicKey,
|
|
|
|
s.SigningPhrase,
|
|
|
|
s.WalletRootAddress,
|
2022-08-23 12:25:34 +00:00
|
|
|
s.CurrentUserStatus,
|
2022-10-05 10:53:42 +00:00
|
|
|
s.ProfilePicturesShowTo,
|
|
|
|
s.ProfilePicturesVisibility,
|
2023-10-13 14:31:56 +00:00
|
|
|
s.URLUnfurlingMode,
|
2023-10-18 10:02:35 +00:00
|
|
|
s.OmitTransfersHistoryScan,
|
2023-12-01 11:30:42 +00:00
|
|
|
s.MnemonicWasNotShown,
|
2022-03-23 18:47:00 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-12 10:20:32 +00:00
|
|
|
if s.DisplayName != "" {
|
|
|
|
now := time.Now().Unix()
|
2022-12-28 11:51:27 +00:00
|
|
|
query := db.buildUpdateSyncClockQueryForField(DisplayName)
|
|
|
|
_, err := tx.Exec(query, uint64(now), uint64(now))
|
2022-12-12 10:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return nodecfg.SaveConfigWithTx(tx, &n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) getSettingFieldFromReactName(reactName string) (SettingField, error) {
|
|
|
|
for _, s := range SettingFieldRegister {
|
|
|
|
if s.GetReactName() == reactName {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SettingField{}, errors.ErrInvalidConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) makeSelectRow(setting SettingField) *sql.Row {
|
|
|
|
query := "SELECT %s FROM settings WHERE synthetic_id = 'id'"
|
|
|
|
query = fmt.Sprintf(query, setting.GetDBName())
|
|
|
|
return db.db.QueryRow(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) makeSelectString(setting SettingField) (string, error) {
|
|
|
|
var result sql.NullString
|
|
|
|
err := db.makeSelectRow(setting).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if result.Valid {
|
|
|
|
return result.String, nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) saveSetting(setting SettingField, value interface{}) error {
|
|
|
|
query := "UPDATE settings SET %s = ? WHERE synthetic_id = 'id'"
|
|
|
|
query = fmt.Sprintf(query, setting.GetDBName())
|
|
|
|
|
|
|
|
update, err := db.db.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = update.Exec(value)
|
2023-10-09 19:31:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.notifier != nil {
|
|
|
|
db.notifier(setting, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) parseSaveAndSyncSetting(sf SettingField, value interface{}) (err error) {
|
|
|
|
if sf.ValueHandler() != nil {
|
|
|
|
value, err = sf.ValueHandler()(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(samyoul) this is ugly as hell need a more elegant solution
|
|
|
|
if NodeConfig.GetReactName() == sf.GetReactName() {
|
|
|
|
if err = nodecfg.SaveNodeConfig(db.db, value.(*params.NodeConfig)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.saveSetting(sf, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:45:25 +00:00
|
|
|
if sf.GetDBName() == DBColumnMnemonic {
|
|
|
|
mnemonicRemoved := value == nil || value.(string) == ""
|
|
|
|
err = db.saveSetting(MnemonicRemoved, mnemonicRemoved)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sf = MnemonicRemoved
|
|
|
|
value = mnemonicRemoved
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
if sf.CanSync(FromInterface) {
|
|
|
|
db.SyncQueue <- SyncSettingField{sf, value}
|
|
|
|
}
|
2023-10-24 10:15:32 +00:00
|
|
|
|
|
|
|
db.postChangesToSubscribers(&SyncSettingField{sf, value})
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSetting stores data from any non-sync source
|
|
|
|
// If the field requires syncing the field data is pushed on to the SyncQueue
|
|
|
|
func (db *Database) SaveSetting(setting string, value interface{}) error {
|
|
|
|
sf, err := db.getSettingFieldFromReactName(setting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.parseSaveAndSyncSetting(sf, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSettingField is identical in functionality to SaveSetting, except the setting parameter is a SettingField and
|
|
|
|
// doesn't require any SettingFieldRegister lookup.
|
|
|
|
// This func is useful if you already know the SettingField to save
|
|
|
|
func (db *Database) SaveSettingField(sf SettingField, value interface{}) error {
|
|
|
|
return db.parseSaveAndSyncSetting(sf, value)
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:45:25 +00:00
|
|
|
func (db *Database) DeleteMnemonic() error {
|
|
|
|
return db.saveSetting(Mnemonic, nil)
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// SaveSyncSetting stores setting data from a sync protobuf source, note it does not call SettingField.ValueHandler()
|
2023-10-24 10:15:32 +00:00
|
|
|
// nor does this function attempt to write to the Database.SyncQueue,
|
|
|
|
// yet it still writes to Database.changesSubscriptions.
|
2022-03-23 18:47:00 +00:00
|
|
|
func (db *Database) SaveSyncSetting(setting SettingField, value interface{}, clock uint64) error {
|
|
|
|
ls, err := db.GetSettingLastSynced(setting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if clock <= ls {
|
|
|
|
return errors.ErrNewClockOlderThanCurrent
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.SetSettingLastSynced(setting, clock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:15:32 +00:00
|
|
|
err = db.saveSetting(setting, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.postChangesToSubscribers(&SyncSettingField{setting, value})
|
|
|
|
return nil
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetSettingLastSynced(setting SettingField) (result uint64, err error) {
|
|
|
|
query := "SELECT %s FROM settings_sync_clock WHERE synthetic_id = 'id'"
|
|
|
|
query = fmt.Sprintf(query, setting.GetDBName())
|
|
|
|
|
|
|
|
err = db.db.QueryRow(query).Scan(&result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-12-28 11:51:27 +00:00
|
|
|
func (db *Database) buildUpdateSyncClockQueryForField(setting SettingField) string {
|
2022-03-23 18:47:00 +00:00
|
|
|
query := "UPDATE settings_sync_clock SET %s = ? WHERE synthetic_id = 'id' AND %s < ?"
|
2022-12-28 11:51:27 +00:00
|
|
|
return fmt.Sprintf(query, setting.GetDBName(), setting.GetDBName())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SetSettingLastSynced(setting SettingField, clock uint64) error {
|
|
|
|
query := db.buildUpdateSyncClockQueryForField(setting)
|
2022-03-23 18:47:00 +00:00
|
|
|
|
|
|
|
_, err := db.db.Exec(query, clock, clock)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetSettings() (Settings, error) {
|
|
|
|
var s Settings
|
2023-08-02 07:39:58 +00:00
|
|
|
err := db.db.QueryRow(`
|
|
|
|
SELECT
|
|
|
|
address, anon_metrics_should_send, chaos_mode, currency, current_network,
|
|
|
|
custom_bootnodes, custom_bootnodes_enabled, dapps_address, display_name, bio, eip1581_address, fleet,
|
|
|
|
hide_home_tooltip, installation_id, key_uid, keycard_instance_uid, keycard_paired_on, keycard_pairing,
|
|
|
|
last_updated, latest_derived_path, link_preview_request_enabled, link_previews_enabled_sites, log_level,
|
|
|
|
mnemonic, mnemonic_removed, name, networks, notifications_enabled, push_notifications_server_enabled,
|
|
|
|
push_notifications_from_contacts_only, remote_push_notifications_enabled, send_push_notifications,
|
|
|
|
push_notifications_block_mentions, photo_path, pinned_mailservers, preferred_name, preview_privacy, public_key,
|
|
|
|
remember_syncing_choice, signing_phrase, stickers_packs_installed, stickers_packs_pending, stickers_recent_stickers,
|
|
|
|
syncing_on_mobile_network, default_sync_period, use_mailservers, messages_from_contacts_only, usernames, appearance,
|
|
|
|
profile_pictures_show_to, profile_pictures_visibility, wallet_root_address, wallet_set_up_passed, wallet_visible_tokens,
|
|
|
|
waku_bloom_filter_mode, webview_allow_permission_requests, current_user_status, send_status_updates, gif_recents,
|
|
|
|
gif_favorites, opensea_enabled, last_backup, backup_enabled, telemetry_server_url, auto_message_enabled, gif_api_key,
|
2023-12-04 10:18:05 +00:00
|
|
|
test_networks_enabled, mutual_contact_enabled, profile_migration_needed, is_sepolia_enabled, wallet_token_preferences_group_by_community, url_unfurling_mode,
|
2023-12-01 11:30:42 +00:00
|
|
|
omit_transfers_history_scan, mnemonic_was_not_shown
|
2023-08-02 07:39:58 +00:00
|
|
|
FROM
|
|
|
|
settings
|
|
|
|
WHERE
|
|
|
|
synthetic_id = 'id'`).Scan(
|
2022-03-23 18:47:00 +00:00
|
|
|
&s.Address,
|
|
|
|
&s.AnonMetricsShouldSend,
|
|
|
|
&s.ChaosMode,
|
|
|
|
&s.Currency,
|
|
|
|
&s.CurrentNetwork,
|
|
|
|
&s.CustomBootnodes,
|
|
|
|
&s.CustomBootnodesEnabled,
|
|
|
|
&s.DappsAddress,
|
|
|
|
&s.DisplayName,
|
2022-08-05 11:22:35 +00:00
|
|
|
&s.Bio,
|
2022-03-23 18:47:00 +00:00
|
|
|
&s.EIP1581Address,
|
|
|
|
&s.Fleet,
|
|
|
|
&s.HideHomeTooltip,
|
|
|
|
&s.InstallationID,
|
|
|
|
&s.KeyUID,
|
|
|
|
&s.KeycardInstanceUID,
|
2022-08-25 15:09:08 +00:00
|
|
|
&s.KeycardPairedOn,
|
2022-03-23 18:47:00 +00:00
|
|
|
&s.KeycardPairing,
|
|
|
|
&s.LastUpdated,
|
2023-05-08 15:14:46 +00:00
|
|
|
&s.LatestDerivedPath,
|
2022-03-23 18:47:00 +00:00
|
|
|
&s.LinkPreviewRequestEnabled,
|
|
|
|
&s.LinkPreviewsEnabledSites,
|
|
|
|
&s.LogLevel,
|
|
|
|
&s.Mnemonic,
|
2023-04-13 00:45:25 +00:00
|
|
|
&s.MnemonicRemoved,
|
2022-03-23 18:47:00 +00:00
|
|
|
&s.Name,
|
|
|
|
&s.Networks,
|
|
|
|
&s.NotificationsEnabled,
|
|
|
|
&s.PushNotificationsServerEnabled,
|
|
|
|
&s.PushNotificationsFromContactsOnly,
|
|
|
|
&s.RemotePushNotificationsEnabled,
|
|
|
|
&s.SendPushNotifications,
|
|
|
|
&s.PushNotificationsBlockMentions,
|
|
|
|
&s.PhotoPath,
|
|
|
|
&s.PinnedMailserver,
|
|
|
|
&s.PreferredName,
|
|
|
|
&s.PreviewPrivacy,
|
|
|
|
&s.PublicKey,
|
|
|
|
&s.RememberSyncingChoice,
|
|
|
|
&s.SigningPhrase,
|
|
|
|
&s.StickerPacksInstalled,
|
|
|
|
&s.StickerPacksPending,
|
|
|
|
&s.StickersRecentStickers,
|
|
|
|
&s.SyncingOnMobileNetwork,
|
|
|
|
&s.DefaultSyncPeriod,
|
|
|
|
&s.UseMailservers,
|
|
|
|
&s.MessagesFromContactsOnly,
|
|
|
|
&s.Usernames,
|
|
|
|
&s.Appearance,
|
|
|
|
&s.ProfilePicturesShowTo,
|
|
|
|
&s.ProfilePicturesVisibility,
|
|
|
|
&s.WalletRootAddress,
|
|
|
|
&s.WalletSetUpPassed,
|
|
|
|
&s.WalletVisibleTokens,
|
|
|
|
&s.WakuBloomFilterMode,
|
|
|
|
&s.WebViewAllowPermissionRequests,
|
|
|
|
&sqlite.JSONBlob{Data: &s.CurrentUserStatus},
|
|
|
|
&s.SendStatusUpdates,
|
|
|
|
&sqlite.JSONBlob{Data: &s.GifRecents},
|
|
|
|
&sqlite.JSONBlob{Data: &s.GifFavorites},
|
|
|
|
&s.OpenseaEnabled,
|
|
|
|
&s.LastBackup,
|
|
|
|
&s.BackupEnabled,
|
|
|
|
&s.TelemetryServerURL,
|
|
|
|
&s.AutoMessageEnabled,
|
|
|
|
&s.GifAPIKey,
|
2022-03-29 12:39:10 +00:00
|
|
|
&s.TestNetworksEnabled,
|
2022-06-08 16:18:56 +00:00
|
|
|
&s.MutualContactEnabled,
|
2023-08-02 07:39:58 +00:00
|
|
|
&s.ProfileMigrationNeeded,
|
2023-10-03 13:27:42 +00:00
|
|
|
&s.IsSepoliaEnabled,
|
2023-12-04 10:18:05 +00:00
|
|
|
&s.TokenGroupByCommunity,
|
2023-10-13 14:31:56 +00:00
|
|
|
&s.URLUnfurlingMode,
|
2023-10-18 10:02:35 +00:00
|
|
|
&s.OmitTransfersHistoryScan,
|
2023-12-01 11:30:42 +00:00
|
|
|
&s.MnemonicWasNotShown,
|
2022-03-23 18:47:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2022-05-20 08:59:36 +00:00
|
|
|
// We should remove this and realated things once mobile team starts usign `settings_notifications` package
|
2022-03-23 18:47:00 +00:00
|
|
|
func (db *Database) GetNotificationsEnabled() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(NotificationsEnabled).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetProfilePicturesVisibility() (result int, err error) {
|
|
|
|
err = db.makeSelectRow(ProfilePicturesVisibility).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetPublicKey() (string, error) {
|
|
|
|
return db.makeSelectString(PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetFleet() (string, error) {
|
|
|
|
return db.makeSelectString(Fleet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetDappsAddress() (rst types.Address, err error) {
|
|
|
|
err = db.makeSelectRow(DappsAddress).Scan(&rst)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetPinnedMailservers() (rst map[string]string, err error) {
|
|
|
|
rst = make(map[string]string)
|
|
|
|
var pinnedMailservers string
|
|
|
|
err = db.db.QueryRow("SELECT COALESCE(pinned_mailservers, '') FROM settings WHERE synthetic_id = 'id'").Scan(&pinnedMailservers)
|
|
|
|
if err == sql.ErrNoRows || pinnedMailservers == "" {
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(pinnedMailservers), &rst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) CanUseMailservers() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(UseMailservers).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) CanSyncOnMobileNetwork() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(SyncingOnMobileNetwork).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetDefaultSyncPeriod() (result uint32, err error) {
|
|
|
|
err = db.makeSelectRow(DefaultSyncPeriod).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetMessagesFromContactsOnly() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(MessagesFromContactsOnly).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-11-30 09:41:35 +00:00
|
|
|
func (db *Database) GetProfilePicturesShowTo() (result int64, err error) {
|
|
|
|
err = db.makeSelectRow(ProfilePicturesShowTo).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2023-05-08 15:14:46 +00:00
|
|
|
func (db *Database) GetLatestDerivedPath() (result uint, err error) {
|
|
|
|
err = db.makeSelectRow(LatestDerivedPath).Scan(&result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (db *Database) GetCurrentStatus(status interface{}) error {
|
|
|
|
err := db.makeSelectRow(CurrentUserStatus).Scan(&sqlite.JSONBlob{Data: &status})
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) ShouldBroadcastUserStatus() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(SendStatusUpdates).Scan(&result)
|
|
|
|
// If the `send_status_updates` value is nil the sql.ErrNoRows will be returned
|
|
|
|
// because this feature is opt out, `true` should be returned in the case where no value is found
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) BackupEnabled() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(BackupEnabled).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) AutoMessageEnabled() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(AutoMessageEnabled).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) LastBackup() (result uint64, err error) {
|
|
|
|
err = db.makeSelectRow(LastBackup).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SetLastBackup(time uint64) error {
|
|
|
|
return db.SaveSettingField(LastBackup, time)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SetBackupFetched(fetched bool) error {
|
|
|
|
return db.SaveSettingField(BackupFetched, fetched)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) BackupFetched() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(BackupFetched).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) ENSName() (string, error) {
|
|
|
|
return db.makeSelectString(PreferredName)
|
|
|
|
}
|
|
|
|
|
2023-06-27 07:54:49 +00:00
|
|
|
func (db *Database) DeviceName() (string, error) {
|
|
|
|
return db.makeSelectString(DeviceName)
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (db *Database) DisplayName() (string, error) {
|
|
|
|
return db.makeSelectString(DisplayName)
|
|
|
|
}
|
|
|
|
|
2022-08-05 11:22:35 +00:00
|
|
|
func (db *Database) Bio() (string, error) {
|
|
|
|
return db.makeSelectString(Bio)
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:45:25 +00:00
|
|
|
func (db *Database) Mnemonic() (string, error) {
|
|
|
|
return db.makeSelectString(Mnemonic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) MnemonicRemoved() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(MnemonicRemoved).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2023-12-01 11:30:42 +00:00
|
|
|
func (db *Database) GetMnemonicWasNotShown() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(MnemonicWasNotShown).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (db *Database) GifAPIKey() (string, error) {
|
|
|
|
return db.makeSelectString(GifAPIKey)
|
|
|
|
}
|
|
|
|
|
2022-06-08 16:18:56 +00:00
|
|
|
func (db *Database) MutualContactEnabled() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(MutualContactEnabled).Scan(&result)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (db *Database) GifRecents() (recents json.RawMessage, err error) {
|
|
|
|
err = db.makeSelectRow(GifRecents).Scan(&sqlite.JSONBlob{Data: &recents})
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return recents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GifFavorites() (favorites json.RawMessage, err error) {
|
|
|
|
err = db.makeSelectRow(GifFavourites).Scan(&sqlite.JSONBlob{Data: &favorites})
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return favorites, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetPreferredUsername() (string, error) {
|
|
|
|
return db.makeSelectString(PreferredName)
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:48:05 +00:00
|
|
|
func (db *Database) GetCurrency() (string, error) {
|
|
|
|
return db.makeSelectString(Currency)
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (db *Database) GetInstalledStickerPacks() (rst *json.RawMessage, err error) {
|
|
|
|
err = db.makeSelectRow(StickersPacksInstalled).Scan(&rst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetPendingStickerPacks() (rst *json.RawMessage, err error) {
|
|
|
|
err = db.makeSelectRow(StickersPacksPending).Scan(&rst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetRecentStickers() (rst *json.RawMessage, err error) {
|
|
|
|
err = db.makeSelectRow(StickersRecentStickers).Scan(&rst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SetPinnedMailservers(mailservers map[string]string) error {
|
2022-03-23 18:57:57 +00:00
|
|
|
return db.SaveSettingField(PinnedMailservers, mailservers)
|
2022-03-23 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SetUseMailservers(value bool) error {
|
|
|
|
return db.SaveSettingField(UseMailservers, value)
|
|
|
|
}
|
2022-04-11 16:35:18 +00:00
|
|
|
|
2022-05-09 08:00:48 +00:00
|
|
|
func (db *Database) GetWalletRootAddress() (rst types.Address, err error) {
|
2022-04-11 16:35:18 +00:00
|
|
|
err = db.makeSelectRow(WalletRootAddress).Scan(&rst)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-06-02 12:17:52 +00:00
|
|
|
|
2023-01-25 17:46:20 +00:00
|
|
|
func (db *Database) GetEIP1581Address() (rst types.Address, err error) {
|
|
|
|
err = db.makeSelectRow(EIP1581Address).Scan(&rst)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetMasterAddress() (rst types.Address, err error) {
|
|
|
|
err = db.makeSelectRow(MasterAddress).Scan(&rst)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 13:09:56 +00:00
|
|
|
func (db *Database) GetTestNetworksEnabled() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(TestNetworksEnabled).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
2023-01-31 22:46:47 +00:00
|
|
|
|
2023-10-03 13:27:42 +00:00
|
|
|
func (db *Database) GetIsSepoliaEnabled() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(IsSepoliaEnabled).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:18:05 +00:00
|
|
|
func (db *Database) GetTokenGroupByCommunity() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(TokenGroupByCommunity).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SetTokenGroupByCommunity(value bool) error {
|
|
|
|
return db.SaveSettingField(TokenGroupByCommunity, value)
|
|
|
|
}
|
|
|
|
|
2023-01-31 22:46:47 +00:00
|
|
|
func (db *Database) GetTelemetryServerURL() (string, error) {
|
|
|
|
return db.makeSelectString(TelemetryServerURL)
|
|
|
|
}
|
2023-06-28 10:05:06 +00:00
|
|
|
|
2023-08-02 07:39:58 +00:00
|
|
|
func (db *Database) ProfileMigrationNeeded() (result bool, err error) {
|
|
|
|
err = db.makeSelectRow(ProfileMigrationNeeded).Scan(&result)
|
|
|
|
return result, err
|
|
|
|
}
|
2023-10-13 14:31:56 +00:00
|
|
|
|
|
|
|
func (db *Database) URLUnfurlingMode() (result int64, err error) {
|
|
|
|
err = db.makeSelectRow(URLUnfurlingMode).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
2023-10-24 10:15:32 +00:00
|
|
|
|
|
|
|
func (db *Database) SubscribeToChanges() chan *SyncSettingField {
|
|
|
|
s := make(chan *SyncSettingField, 100)
|
|
|
|
db.changesSubscriptions = append(db.changesSubscriptions, s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) postChangesToSubscribers(change *SyncSettingField) {
|
|
|
|
// Publish on channels, drop if buffer is full
|
|
|
|
for _, s := range db.changesSubscriptions {
|
|
|
|
select {
|
|
|
|
case s <- change:
|
|
|
|
default:
|
|
|
|
log.Warn("settings changes subscription channel full, dropping message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 11:30:42 +00:00
|
|
|
|
|
|
|
func (db *Database) MnemonicWasShown() error {
|
|
|
|
return db.SaveSettingField(MnemonicWasNotShown, false)
|
|
|
|
}
|