Skip to content

Commit

Permalink
fix: address reviewer feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri committed Jan 19, 2024
1 parent d1f6c04 commit d57dee2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
8 changes: 6 additions & 2 deletions multiaccounts/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,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 @@ -2415,15 +2415,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
16 changes: 12 additions & 4 deletions protocol/messenger_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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"
Expand All @@ -24,6 +25,9 @@ func (m *Messenger) SetCustomNodes(request *requests.SetCustomNodes) error {
}

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 {
Expand All @@ -32,20 +36,24 @@ func (m *Messenger) SetCustomizationColor(ctx context.Context, request *requests

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
}

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

if affected > 0 {
err = m.syncAccountCustomizationColor(ctx, acc)
if err != nil {
return err
}
}
return nil
}
4 changes: 4 additions & 0 deletions protocol/messenger_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"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"
)

Expand Down Expand Up @@ -94,7 +96,9 @@ func (s *MessengerSettingsSuite) TestSetCustomizationColor() {
}, "message syncAccountCustomizationColor not received")
s.Require().NoError(err)
acc, err = s.alice2.multiAccounts.GetAccount(s.alice.account.KeyUID)
s.Require().NoError(err)
acc2, err = s.alice2.multiAccounts.GetAccount(s.alice2.account.KeyUID)
s.Require().NoError(err)
s.Require().Equal(common.CustomizationColorBlue, acc.CustomizationColor)
s.Require().Equal(acc.CustomizationColor, acc2.CustomizationColor)
}
21 changes: 20 additions & 1 deletion protocol/requests/set_customization_color.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package requests

import "github.com/status-im/status-go/multiaccounts/common"
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
}

0 comments on commit d57dee2

Please sign in to comment.