From 2d525f95035251d02c289c7f68a199353dfbeb7d Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Thu, 3 Sep 2020 09:30:03 +0200 Subject: [PATCH] Set block mentions --- appdatabase/migrations/bindata.go | 54 +++++++++++++++++-- .../sql/0010_add_block_mentions.down.sql | 1 + .../sql/0010_add_block_mentions.up.sql | 1 + multiaccounts/accounts/database.go | 13 ++++- peers/peerpool_test.go | 34 ++---------- protocol/messenger.go | 22 ++++++++ protocol/pushnotificationclient/client.go | 20 +++++++ services/ext/api.go | 16 ++++++ services/ext/service.go | 1 + 9 files changed, 126 insertions(+), 36 deletions(-) create mode 100644 appdatabase/migrations/sql/0010_add_block_mentions.down.sql create mode 100644 appdatabase/migrations/sql/0010_add_block_mentions.up.sql diff --git a/appdatabase/migrations/bindata.go b/appdatabase/migrations/bindata.go index a4e8248ee..f2511c2c9 100644 --- a/appdatabase/migrations/bindata.go +++ b/appdatabase/migrations/bindata.go @@ -15,6 +15,8 @@ // 0008_add_push_notifications.up.sql (349B) // 0009_enable_sending_push_notifications.down.sql (49B) // 0009_enable_sending_push_notifications.up.sql (49B) +// 0010_add_block_mentions.down.sql (83B) +// 0010_add_block_mentions.up.sql (89B) // doc.go (74B) package migrations @@ -359,7 +361,7 @@ func _0009_enable_sending_push_notificationsDownSql() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "0009_enable_sending_push_notifications.down.sql", size: 49, mode: os.FileMode(0644), modTime: time.Unix(1597918101, 0)} + info := bindataFileInfo{name: "0009_enable_sending_push_notifications.down.sql", size: 49, mode: os.FileMode(0644), modTime: time.Unix(1598949727, 0)} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe9, 0xae, 0x1b, 0x41, 0xcb, 0x9c, 0x2c, 0x93, 0xc6, 0x2a, 0x77, 0x3, 0xb9, 0x51, 0xe0, 0x68, 0x68, 0x0, 0xf7, 0x5b, 0xb3, 0x1e, 0x94, 0x44, 0xba, 0x9c, 0xd0, 0x3b, 0x80, 0x21, 0x6f, 0xb5}} return a, nil } @@ -379,11 +381,51 @@ func _0009_enable_sending_push_notificationsUpSql() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "0009_enable_sending_push_notifications.up.sql", size: 49, mode: os.FileMode(0644), modTime: time.Unix(1597918090, 0)} + info := bindataFileInfo{name: "0009_enable_sending_push_notifications.up.sql", size: 49, mode: os.FileMode(0644), modTime: time.Unix(1598949727, 0)} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x1b, 0x80, 0xe4, 0x9c, 0xc8, 0xb8, 0xd5, 0xef, 0xce, 0x74, 0x9b, 0x7b, 0xdd, 0xa, 0x99, 0x1e, 0xef, 0x7f, 0xb8, 0x99, 0x84, 0x4, 0x0, 0x6b, 0x1d, 0x2c, 0xa, 0xf8, 0x2c, 0x4f, 0xb5, 0x44}} return a, nil } +var __0010_add_block_mentionsDownSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x72\xf4\x09\x71\x0d\x52\x28\x4e\x2d\x29\xc9\xcc\x4b\x2f\x56\x70\x74\x71\x51\x70\xf6\xf7\x09\xf5\xf5\x53\x28\x28\x2d\xce\x88\xcf\xcb\x2f\xc9\x4c\xcb\x4c\x4e\x2c\xc9\xcc\xcf\x2b\x8e\x4f\xca\xc9\x4f\xce\x8e\xcf\x4d\xcd\x03\x73\x15\x9c\xfc\xfd\x7d\x5c\x1d\xfd\x14\x5c\x5c\xdd\x1c\x43\x7d\x42\x14\xdc\x1c\x7d\x82\x5d\xad\xb9\x00\x01\x00\x00\xff\xff\xa8\x45\x75\x3b\x53\x00\x00\x00") + +func _0010_add_block_mentionsDownSqlBytes() ([]byte, error) { + return bindataRead( + __0010_add_block_mentionsDownSql, + "0010_add_block_mentions.down.sql", + ) +} + +func _0010_add_block_mentionsDownSql() (*asset, error) { + bytes, err := _0010_add_block_mentionsDownSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "0010_add_block_mentions.down.sql", size: 83, mode: os.FileMode(0644), modTime: time.Unix(1599117686, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x6d, 0x9e, 0x27, 0x1e, 0xba, 0x9f, 0xca, 0xae, 0x98, 0x2e, 0x6e, 0xe3, 0xdd, 0xac, 0x73, 0x34, 0x4e, 0x69, 0x92, 0xb5, 0xf6, 0x9, 0xab, 0x50, 0x35, 0xd, 0xee, 0xeb, 0x3e, 0xcc, 0x7e, 0xce}} + return a, nil +} + +var __0010_add_block_mentionsUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x1c\xc7\x31\x0a\x42\x31\x0c\x06\xe0\xdd\x53\xfc\xf7\x70\xca\xb3\x79\x53\x7c\x05\x6d\xe7\xa2\xa5\x6a\x50\x53\x21\xf1\xfe\x82\xe3\x47\x52\xf8\x84\x42\x8b\x30\x7c\x44\xa8\xdd\x1d\x94\x12\x0e\x59\xea\x71\xc3\xe7\xeb\x8f\x66\x33\xf4\xa6\xfd\x12\x3a\xcd\xdb\xf5\x35\xfb\xb3\xbd\x87\xfd\x89\x25\x67\x61\xda\x90\x78\xa5\x2a\x05\x2b\xc9\x99\xf7\xbb\x5f\x00\x00\x00\xff\xff\x2b\x4e\x3f\xc5\x59\x00\x00\x00") + +func _0010_add_block_mentionsUpSqlBytes() ([]byte, error) { + return bindataRead( + __0010_add_block_mentionsUpSql, + "0010_add_block_mentions.up.sql", + ) +} + +func _0010_add_block_mentionsUpSql() (*asset, error) { + bytes, err := _0010_add_block_mentionsUpSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "0010_add_block_mentions.up.sql", size: 89, mode: os.FileMode(0644), modTime: time.Unix(1599118789, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xd7, 0x23, 0x85, 0xa2, 0xb5, 0xb6, 0xb4, 0x3f, 0xdc, 0x4e, 0xff, 0xe2, 0x6b, 0x66, 0x68, 0x5e, 0xb2, 0xb4, 0x14, 0xb2, 0x1b, 0x4d, 0xb1, 0xce, 0xf7, 0x6, 0x58, 0xa7, 0xaf, 0x93, 0x3f, 0x25}} + return a, nil +} + var _docGo = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x2c\xc9\xb1\x0d\xc4\x20\x0c\x05\xd0\x9e\x29\xfe\x02\xd8\xfd\x6d\xe3\x4b\xac\x2f\x44\x82\x09\x78\x7f\xa5\x49\xfd\xa6\x1d\xdd\xe8\xd8\xcf\x55\x8a\x2a\xe3\x47\x1f\xbe\x2c\x1d\x8c\xfa\x6f\xe3\xb4\x34\xd4\xd9\x89\xbb\x71\x59\xb6\x18\x1b\x35\x20\xa2\x9f\x0a\x03\xa2\xe5\x0d\x00\x00\xff\xff\x60\xcd\x06\xbe\x4a\x00\x00\x00") func docGoBytes() ([]byte, error) { @@ -525,6 +567,10 @@ var _bindata = map[string]func() (*asset, error){ "0009_enable_sending_push_notifications.up.sql": _0009_enable_sending_push_notificationsUpSql, + "0010_add_block_mentions.down.sql": _0010_add_block_mentionsDownSql, + + "0010_add_block_mentions.up.sql": _0010_add_block_mentionsUpSql, + "doc.go": docGo, } @@ -584,7 +630,9 @@ var _bintree = &bintree{nil, map[string]*bintree{ "0008_add_push_notifications.up.sql": &bintree{_0008_add_push_notificationsUpSql, map[string]*bintree{}}, "0009_enable_sending_push_notifications.down.sql": &bintree{_0009_enable_sending_push_notificationsDownSql, map[string]*bintree{}}, "0009_enable_sending_push_notifications.up.sql": &bintree{_0009_enable_sending_push_notificationsUpSql, map[string]*bintree{}}, - "doc.go": &bintree{docGo, map[string]*bintree{}}, + "0010_add_block_mentions.down.sql": &bintree{_0010_add_block_mentionsDownSql, map[string]*bintree{}}, + "0010_add_block_mentions.up.sql": &bintree{_0010_add_block_mentionsUpSql, map[string]*bintree{}}, + "doc.go": &bintree{docGo, map[string]*bintree{}}, }} // RestoreAsset restores an asset under the given directory. diff --git a/appdatabase/migrations/sql/0010_add_block_mentions.down.sql b/appdatabase/migrations/sql/0010_add_block_mentions.down.sql new file mode 100644 index 000000000..d7c56e1ca --- /dev/null +++ b/appdatabase/migrations/sql/0010_add_block_mentions.down.sql @@ -0,0 +1 @@ +ALTER settings ADD COLUMN push_notifications_block_mentions BOOLEAN DEFAULT FALSE; diff --git a/appdatabase/migrations/sql/0010_add_block_mentions.up.sql b/appdatabase/migrations/sql/0010_add_block_mentions.up.sql new file mode 100644 index 000000000..6d7edcf97 --- /dev/null +++ b/appdatabase/migrations/sql/0010_add_block_mentions.up.sql @@ -0,0 +1 @@ +ALTER TABLE settings ADD COLUMN push_notifications_block_mentions BOOLEAN DEFAULT FALSE; diff --git a/multiaccounts/accounts/database.go b/multiaccounts/accounts/database.go index 740893a6d..040e4cf50 100644 --- a/multiaccounts/accounts/database.go +++ b/multiaccounts/accounts/database.go @@ -70,7 +70,9 @@ type Settings struct { PushNotificationsServerEnabled bool `json:"push-notifications-server-enabled?,omitempty"` // PushNotificationsFromContactsOnly indicates whether we should only receive push notifications from contacts PushNotificationsFromContactsOnly bool `json:"push-notifications-from-contacts-only?,omitempty"` - RememberSyncingChoice bool `json:"remember-syncing-choice?,omitempty"` + // PushNotificationsBlockMentions indicates whether we should receive notifications for mentions + PushNotificationsBlockMentions bool `json:"push-notifications-block-mentions?,omitempty"` + RememberSyncingChoice bool `json:"remember-syncing-choice?,omitempty"` // RemotePushNotificationsEnabled indicates whether we should be using remote notifications (ios only for now) RemotePushNotificationsEnabled bool `json:"remote-push-notifications-enabled?,omitempty"` SigningPhrase string `json:"signing-phrase"` @@ -273,6 +275,12 @@ func (db *Database) SaveSetting(setting string, value interface{}) error { return ErrInvalidConfig } update, err = db.db.Prepare("UPDATE settings SET push_notifications_from_contacts_only = ? WHERE synthetic_id = 'id'") + case "push-notifications-block-mentions?": + _, ok := value.(bool) + if !ok { + return ErrInvalidConfig + } + update, err = db.db.Prepare("UPDATE settings SET push_notifications_block_mentions = ? WHERE synthetic_id = 'id'") case "send-push-notifications?": _, ok := value.(bool) if !ok { @@ -337,7 +345,7 @@ func (db *Database) GetNodeConfig(nodecfg interface{}) error { func (db *Database) GetSettings() (Settings, error) { var s Settings - err := db.db.QueryRow("SELECT address, chaos_mode, currency, current_network, custom_bootnodes, custom_bootnodes_enabled, dapps_address, eip1581_address, fleet, hide_home_tooltip, installation_id, key_uid, keycard_instance_uid, keycard_paired_on, keycard_pairing, last_updated, latest_derived_path, log_level, mnemonic, name, networks, notifications_enabled, push_notifications_server_enabled, push_notifications_from_contacts_only, remote_push_notifications_enabled, send_push_notifications, 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, usernames, appearance, wallet_root_address, wallet_set_up_passed, wallet_visible_tokens, waku_enabled, waku_bloom_filter_mode FROM settings WHERE synthetic_id = 'id'").Scan( + err := db.db.QueryRow("SELECT address, chaos_mode, currency, current_network, custom_bootnodes, custom_bootnodes_enabled, dapps_address, eip1581_address, fleet, hide_home_tooltip, installation_id, key_uid, keycard_instance_uid, keycard_paired_on, keycard_pairing, last_updated, latest_derived_path, log_level, mnemonic, 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, usernames, appearance, wallet_root_address, wallet_set_up_passed, wallet_visible_tokens, waku_enabled, waku_bloom_filter_mode FROM settings WHERE synthetic_id = 'id'").Scan( &s.Address, &s.ChaosMode, &s.Currency, @@ -364,6 +372,7 @@ func (db *Database) GetSettings() (Settings, error) { &s.PushNotificationsFromContactsOnly, &s.RemotePushNotificationsEnabled, &s.SendPushNotifications, + &s.PushNotificationsBlockMentions, &s.PhotoPath, &s.PinnedMailserver, &s.PreferredName, diff --git a/peers/peerpool_test.go b/peers/peerpool_test.go index 45e7018e8..e7a3684c4 100644 --- a/peers/peerpool_test.go +++ b/peers/peerpool_test.go @@ -30,50 +30,21 @@ import ( "github.com/status-im/status-go/whisper/v6" ) -// GetFreePort asks the kernel for a free open port that is ready to use. -func getFreePort() (int, error) { - addr, err := net.ResolveTCPAddr("tcp", "localhost:0") - if err != nil { - return 0, err - } - - l, err := net.ListenTCP("tcp", addr) - if err != nil { - return 0, err - } - defer l.Close() - return l.Addr().(*net.TCPAddr).Port, nil -} - type PeerPoolSimulationSuite struct { suite.Suite bootnode *p2p.Server peers []*p2p.Server discovery []discovery.Discovery - port uint16 rendezvousServer *server.Server } func TestPeerPoolSimulationSuite(t *testing.T) { s := &PeerPoolSimulationSuite{} - port, err := getFreePort() - if err != nil { - panic(err) - } - s.port = uint16(port) - suite.Run(t, s) } -func (s *PeerPoolSimulationSuite) nextPort() uint16 { - port, err := getFreePort() - s.Require().NoError(err) - return uint16(port) -} - func (s *PeerPoolSimulationSuite) SetupTest() { - bootnodePort := s.nextPort() key, _ := crypto.GenerateKey() name := common.MakeName("bootnode", "1.0") // 127.0.0.1 is invalidated by discovery v5 @@ -81,13 +52,14 @@ func (s *PeerPoolSimulationSuite) SetupTest() { Config: p2p.Config{ MaxPeers: 10, Name: name, - ListenAddr: fmt.Sprintf("0.0.0.0:%d", bootnodePort), + ListenAddr: ":0", PrivateKey: key, DiscoveryV5: true, NoDiscovery: true, }, } s.Require().NoError(s.bootnode.Start()) + bootnodePort := uint16(s.bootnode.NodeInfo().Ports.Listener) bootnodeV5 := discv5.NewNode(s.bootnode.DiscV5.Self().ID, net.ParseIP("127.0.0.1"), bootnodePort, bootnodePort) // 1 peer to initiate connection, 1 peer as a first candidate, 1 peer - for failover @@ -100,7 +72,7 @@ func (s *PeerPoolSimulationSuite) SetupTest() { Config: p2p.Config{ MaxPeers: 10, Name: common.MakeName("peer-"+strconv.Itoa(i), "1.0"), - ListenAddr: fmt.Sprintf("0.0.0.0:%d", s.nextPort()), + ListenAddr: ":0", PrivateKey: key, NoDiscovery: true, BootstrapNodesV5: []*discv5.Node{bootnodeV5}, diff --git a/protocol/messenger.go b/protocol/messenger.go index 6187d50ea..88a1fecb9 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -3592,6 +3592,28 @@ func (m *Messenger) DisablePushNotificationsFromContactsOnly() error { return m.pushNotificationClient.DisablePushNotificationsFromContactsOnly(m.pushNotificationOptions()) } +// EnablePushNotificationsBlockMentions is used to indicate that we dont want to received push notifications for mentions +func (m *Messenger) EnablePushNotificationsBlockMentions() error { + if m.pushNotificationClient == nil { + return errors.New("no push notification client") + } + m.mutex.Lock() + defer m.mutex.Unlock() + + return m.pushNotificationClient.EnablePushNotificationsBlockMentions(m.pushNotificationOptions()) +} + +// DisablePushNotificationsBlockMentions is used to indicate that we want to received push notifications for mentions +func (m *Messenger) DisablePushNotificationsBlockMentions() error { + if m.pushNotificationClient == nil { + return errors.New("no push notification client") + } + m.mutex.Lock() + defer m.mutex.Unlock() + + return m.pushNotificationClient.DisablePushNotificationsBlockMentions(m.pushNotificationOptions()) +} + // GetPushNotificationsServers returns the servers used for push notifications func (m *Messenger) GetPushNotificationsServers() ([]*pushnotificationclient.PushNotificationServer, error) { if m.pushNotificationClient == nil { diff --git a/protocol/pushnotificationclient/client.go b/protocol/pushnotificationclient/client.go index 66db70c29..7593fd6be 100644 --- a/protocol/pushnotificationclient/client.go +++ b/protocol/pushnotificationclient/client.go @@ -627,6 +627,26 @@ func (c *Client) DisablePushNotificationsFromContactsOnly(options *RegistrationO return nil } +func (c *Client) EnablePushNotificationsBlockMentions(options *RegistrationOptions) error { + c.config.Logger.Debug("disabling push notifications for mentions") + c.config.BlockMentions = true + if c.lastPushNotificationRegistration != nil && c.config.RemoteNotificationsEnabled { + c.config.Logger.Debug("re-registering after disabling push notifications for mentions") + return c.Register(c.deviceToken, c.apnTopic, c.tokenType, options) + } + return nil +} + +func (c *Client) DisablePushNotificationsBlockMentions(options *RegistrationOptions) error { + c.config.Logger.Debug("enabling push notifications for mentions") + c.config.BlockMentions = false + if c.lastPushNotificationRegistration != nil && c.config.RemoteNotificationsEnabled { + c.config.Logger.Debug("re-registering after enabling push notifications for mentions") + return c.Register(c.deviceToken, c.apnTopic, c.tokenType, options) + } + return nil +} + func encryptAccessToken(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) { c, err := aes.NewCipher(key) if err != nil { diff --git a/services/ext/api.go b/services/ext/api.go index e60654d3a..1cb89ad0c 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -506,6 +506,22 @@ func (api *PublicAPI) DisablePushNotificationsFromContactsOnly(ctx context.Conte return api.service.messenger.DisablePushNotificationsFromContactsOnly() } +func (api *PublicAPI) EnablePushNotificationsBlockMentions(ctx context.Context) error { + err := api.service.accountsDB.SaveSetting("push-notifications-block-mentions?", true) + if err != nil { + return err + } + return api.service.messenger.EnablePushNotificationsBlockMentions() +} + +func (api *PublicAPI) DisablePushNotificationsBlockMentions(ctx context.Context) error { + err := api.service.accountsDB.SaveSetting("push-notifications-block-mentions?", false) + if err != nil { + return err + } + return api.service.messenger.DisablePushNotificationsBlockMentions() +} + func (api *PublicAPI) AddPushNotificationsServer(ctx context.Context, publicKeyBytes types.HexBytes) error { publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes) if err != nil { diff --git a/services/ext/service.go b/services/ext/service.go index c2a609bc9..a288d1051 100644 --- a/services/ext/service.go +++ b/services/ext/service.go @@ -484,6 +484,7 @@ func buildMessengerOptions( options = append(options, protocol.WithPushNotificationClientConfig(&pushnotificationclient.Config{ DefaultServers: config.DefaultPushNotificationsServers, + BlockMentions: settings.PushNotificationsBlockMentions, SendEnabled: settings.SendPushNotifications, AllowFromContactsOnly: settings.PushNotificationsFromContactsOnly, RemoteNotificationsEnabled: settings.RemotePushNotificationsEnabled,