Skip to content

Commit

Permalink
feat: implement new endpoint to set customization color (#4568)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri authored Feb 7, 2024
1 parent 101f1ae commit 70ee70a
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.174.0
0.174.1
8 changes: 6 additions & 2 deletions multiaccounts/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,12 @@ func (db *Database) UpdateAccountTimestamp(keyUID string, loginTimestamp int64)
return err
}

func (db *Database) UpdateAccountCustomizationColor(keyUID string, color string, clock uint64) (sql.Result, error) {
return db.db.Exec("UPDATE accounts SET customizationColor = ?, customizationColorClock = ? WHERE keyUid = ? AND customizationColorClock < ?", color, clock, keyUID, clock)
func (db *Database) UpdateAccountCustomizationColor(keyUID string, color string, clock uint64) (int64, error) {
result, err := db.db.Exec("UPDATE accounts SET customizationColor = ?, customizationColorClock = ? WHERE keyUid = ? AND customizationColorClock < ?", color, clock, keyUID, clock)
if err != nil {
return 0, err
}
return result.RowsAffected()
}

func (db *Database) DeleteAccount(keyUID string) error {
Expand Down
6 changes: 1 addition & 5 deletions protocol/messenger_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2449,15 +2449,11 @@ func (m *Messenger) HandleSyncSetting(messageState *ReceivedMessageState, messag
}

func (m *Messenger) HandleSyncAccountCustomizationColor(state *ReceivedMessageState, message *protobuf.SyncAccountCustomizationColor, statusMessage *v1protocol.StatusMessage) error {
result, err := m.multiAccounts.UpdateAccountCustomizationColor(message.GetKeyUid(), message.GetCustomizationColor(), message.GetUpdatedAt())
affected, err := m.multiAccounts.UpdateAccountCustomizationColor(message.GetKeyUid(), message.GetCustomizationColor(), message.GetUpdatedAt())
if err != nil {
return err
}

affected, err := result.RowsAffected()
if err != nil {
return err
}
if affected > 0 {
state.Response.CustomizationColor = message.GetCustomizationColor()
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/messenger_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"crypto/ecdsa"
"errors"

"go.uber.org/zap"

"github.com/golang/protobuf/proto"

"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"

"go.uber.org/zap"
)

var ErrInvalidEditOrDeleteAuthor = errors.New("sender is not the author of the message")
Expand Down
3 changes: 2 additions & 1 deletion protocol/messenger_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ func (r *MessengerResponse) IsEmpty() bool {
len(r.ensUsernameDetails) == 0 &&
r.currentStatus == nil &&
r.activityCenterState == nil &&
r.SocialLinksInfo == nil
r.SocialLinksInfo == nil &&
r.CustomizationColor == ""
}

// Merge takes another response and appends the new Chats & new Messages and replaces
Expand Down
37 changes: 37 additions & 0 deletions protocol/messenger_settings.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package protocol

import (
"context"

"github.com/status-im/status-go/nodecfg"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/timesource"
)

func (m *Messenger) SetLightClient(request *requests.SetLightClient) error {
Expand All @@ -20,3 +23,37 @@ func (m *Messenger) SetLogLevel(request *requests.SetLogLevel) error {
func (m *Messenger) SetCustomNodes(request *requests.SetCustomNodes) error {
return nodecfg.SetWakuV2CustomNodes(m.database, request.CustomNodes)
}

func (m *Messenger) SetCustomizationColor(ctx context.Context, request *requests.SetCustomizationColor) error {
if err := request.Validate(); err != nil {
return err
}

acc, err := m.multiAccounts.GetAccount(request.KeyUID)
if err != nil {
return err
}

acc.CustomizationColor = request.CustomizationColor

//Use a combination of wall clock + lamport timestamp, just like Chat#NextClockAndTimestamp
tNow := timesource.GetCurrentTimeInMillis()
if acc.CustomizationColorClock >= tNow {
acc.CustomizationColorClock++
} else {
acc.CustomizationColorClock = tNow
}

affected, err := m.multiAccounts.UpdateAccountCustomizationColor(request.KeyUID, string(acc.CustomizationColor), acc.CustomizationColorClock)
if err != nil {
return err
}

if affected > 0 {
err = m.syncAccountCustomizationColor(ctx, acc)
if err != nil {
return err
}
}
return nil
}
105 changes: 105 additions & 0 deletions protocol/messenger_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package protocol

import (
"context"
"testing"

gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"

"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/multiaccounts/common"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"

"github.com/stretchr/testify/suite"
)

func TestMessengerSettings(t *testing.T) {
suite.Run(t, new(MessengerSettingsSuite))
}

type MessengerSettingsSuite struct {
MessengerBaseTestSuite
m2 *Messenger
}

func (s *MessengerSettingsSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()

config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())

pk, err := crypto.GenerateKey()
s.Require().NoError(err)
s.m, err = newMessengerWithKey(s.shh, pk, s.logger, nil)
s.Require().NoError(err)

s.m2, err = newMessengerWithKey(s.shh, s.m.identity, s.logger, nil)
s.Require().NoError(err)

prepareMessengersForPairing(&s.Suite, s.m, s.m2)
}

func (s *MessengerSettingsSuite) TearDownTest() {
TearDownMessenger(&s.Suite, s.m)
TearDownMessenger(&s.Suite, s.m2)
_ = s.logger.Sync()
}

func prepareMessengersForPairing(s *suite.Suite, m1, m2 *Messenger) {
// Set m's installation metadata
aim := &multidevice.InstallationMetadata{
Name: "m's-device",
DeviceType: "m's-device-type",
}
err := m1.SetInstallationMetadata(m1.installationID, aim)
s.Require().NoError(err)

// Set m 2's installation metadata
a2im := &multidevice.InstallationMetadata{
Name: "m's-other-device",
DeviceType: "m's-other-device-type",
}
err = m2.SetInstallationMetadata(m2.installationID, a2im)
s.Require().NoError(err)
}

func (s *MessengerSettingsSuite) TestSetCustomizationColor() {
PairDevices(&s.Suite, s.m2, s.m)
PairDevices(&s.Suite, s.m, s.m2)

s.Require().Equal(s.m.account.KeyUID, s.m2.account.KeyUID)

err := s.m.multiAccounts.SaveAccount(*s.m.account)
s.Require().NoError(err)
err = s.m2.multiAccounts.SaveAccount(*s.m2.account)
s.Require().NoError(err)

// check that accounts have no customization color
acc, err := s.m.multiAccounts.GetAccount(s.m.account.KeyUID)
s.Require().NoError(err)
acc2, err := s.m2.multiAccounts.GetAccount(s.m2.account.KeyUID)
s.Require().NoError(err)
s.Require().Equal(acc.CustomizationColor, common.CustomizationColor(""))
s.Require().Equal(acc.CustomizationColorClock, uint64(0))
s.Require().Equal(acc2.CustomizationColor, common.CustomizationColor(""))
s.Require().Equal(acc2.CustomizationColorClock, uint64(0))

err = s.m.SetCustomizationColor(context.TODO(), &requests.SetCustomizationColor{KeyUID: s.m.account.KeyUID, CustomizationColor: common.CustomizationColorBlue})
s.Require().NoError(err)
_, err = WaitOnMessengerResponse(s.m2, func(r *MessengerResponse) bool {
return len(r.CustomizationColor) > 0
}, "message syncAccountCustomizationColor not received")
s.Require().NoError(err)
acc, err = s.m2.multiAccounts.GetAccount(s.m.account.KeyUID)
s.Require().NoError(err)
acc2, err = s.m2.multiAccounts.GetAccount(s.m2.account.KeyUID)
s.Require().NoError(err)
s.Require().Equal(common.CustomizationColorBlue, acc.CustomizationColor)
s.Require().Equal(acc.CustomizationColor, acc2.CustomizationColor)
}
27 changes: 27 additions & 0 deletions protocol/requests/set_customization_color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package requests

import (
"errors"

"github.com/status-im/status-go/multiaccounts/common"
)

var ErrSetCustomizationColorInvalidColor = errors.New("customizationColor: invalid color")
var ErrSetCustomizationColorInvalidKeyUID = errors.New("keyUid: invalid id")

type SetCustomizationColor struct {
CustomizationColor common.CustomizationColor `json:"customizationColor"`
KeyUID string `json:"keyUid"`
}

func (a *SetCustomizationColor) Validate() error {
if len(a.CustomizationColor) == 0 {
return ErrSetCustomizationColorInvalidColor
}

if len(a.KeyUID) == 0 {
return ErrSetCustomizationColorInvalidKeyUID
}

return nil
}
4 changes: 4 additions & 0 deletions services/ext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,10 @@ func (api *PublicAPI) SetCustomNodes(request *requests.SetCustomNodes) error {
return api.service.messenger.SetCustomNodes(request)
}

func (api *PublicAPI) SetCustomizationColor(ctx context.Context, request *requests.SetCustomizationColor) error {
return api.service.messenger.SetCustomizationColor(ctx, request)
}

// -----
// HELPER
// -----
Expand Down

0 comments on commit 70ee70a

Please sign in to comment.