Skip to content

Commit

Permalink
fix: code review & lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MishkaRogachev committed Nov 1, 2023
1 parent 783aaed commit 5b2ab5b
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 77 deletions.
2 changes: 1 addition & 1 deletion protocol/identity/profile_showcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package identity
import "reflect"

type VisibleProfileShowcaseEntry struct {
EntryId string `json:"entryId"`
EntryID string `json:"entryId"`
Order int `json:"order"`
}

Expand Down
4 changes: 3 additions & 1 deletion protocol/identity_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/status-im/status-go/protocol/protobuf"
)

const CipherMessageAutentificationFailed = "cipher: message authentication failed"

func EncryptIdentityImagesWithContactPubKeys(iis map[string]*protobuf.IdentityImage, m *Messenger) (err error) {
// Make AES key
AESKey := make([]byte, 32)
Expand Down Expand Up @@ -77,7 +79,7 @@ image:
// Decrypt the main encryption AES key with AES encryption using the DH key
dAESKey, err := common.Decrypt(empk, sharedKey)
if err != nil {
if err.Error() == "cipher: message authentication failed" {
if err.Error() == CipherMessageAutentificationFailed {
continue
}
return err
Expand Down
20 changes: 11 additions & 9 deletions protocol/messenger_profile_showcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"errors"
"fmt"

"google.golang.org/protobuf/proto"

"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/protocol/protobuf"
"google.golang.org/protobuf/proto"
)

type ProfileShowcasePreferences struct {
Expand All @@ -23,13 +24,14 @@ func toProfileShowcaseUpdateEntries(entries []*ProfileShowcaseEntry, visibility
result := []*protobuf.ProfileShowcaseEntry{}

for _, entry := range entries {
if entry.ShowcaseVisibility == visibility {
update := &protobuf.ProfileShowcaseEntry{
EntryId: entry.ID,
Order: uint32(entry.Order),
}
result = append(result, update)
if entry.ShowcaseVisibility != visibility {
continue
}
update := &protobuf.ProfileShowcaseEntry{
EntryId: entry.ID,
Order: uint32(entry.Order),
}
result = append(result, update)
}
return result
}
Expand All @@ -38,7 +40,7 @@ func fromProfileShowcaseUpdateEntries(messages []*protobuf.ProfileShowcaseEntry)
entries := []*identity.VisibleProfileShowcaseEntry{}
for _, entry := range messages {
entries = append(entries, &identity.VisibleProfileShowcaseEntry{
EntryId: entry.EntryId,
EntryID: entry.EntryId,
Order: int(entry.Order),
})
}
Expand Down Expand Up @@ -186,7 +188,7 @@ func (m *Messenger) DecryptProfileShowcaseEntriesWithContactPubKeys(senderPubKey
// Decrypt the main encryption AES key with AES encryption using the DH key
dAESKey, err := common.Decrypt(eAESKey, sharedKey)
if err != nil {
if err.Error() == "cipher: message authentication failed" {
if err.Error() == CipherMessageAutentificationFailed {
continue
}
return nil, err
Expand Down
29 changes: 15 additions & 14 deletions protocol/messenger_profile_showcase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"crypto/ecdsa"
"testing"

"github.com/stretchr/testify/suite"
"go.uber.org/zap"

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/eth-node/types"
Expand All @@ -14,8 +17,6 @@ import (
"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"
"go.uber.org/zap"
)

func TestMessengerProfileShowcaseSuite(t *testing.T) { // nolint: deadcode,unused
Expand Down Expand Up @@ -77,7 +78,7 @@ func (s *TestMessengerProfileShowcase) mutualContact(theirMessenger *Messenger)
_, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool {
return len(r.Contacts) > 0 && len(r.Messages()) > 0 && len(r.ActivityCenterNotifications()) > 0
return len(r.Contacts) > 0 && len(r.Messages()) > 0
},
"no messages",
)
Expand Down Expand Up @@ -149,7 +150,7 @@ func (s *TestMessengerProfileShowcase) verifiedContact(theirMessenger *Messenger
s.Require().Equal(common.ContactVerificationStateTrusted, resp.Messages()[0].ContactVerificationState)
}

func (s *TestMessengerProfileShowcase) prepareShocasePreferencs() ProfileShowcasePreferences {
func (s *TestMessengerProfileShowcase) prepareShowcasePreferences() ProfileShowcasePreferences {
communityEntry1 := &ProfileShowcaseEntry{
ID: "0x01312357798976434",
EntryType: ProfileShowcaseEntryTypeCommunity,
Expand Down Expand Up @@ -203,7 +204,7 @@ func (s *TestMessengerProfileShowcase) prepareShocasePreferencs() ProfileShowcas
}

func (s *TestMessengerProfileShowcase) TestSetAndGetProfileShowcasePreferences() {
request := s.prepareShocasePreferencs()
request := s.prepareShowcasePreferences()
err := s.m.SetProfileShowcasePreferences(request)
s.Require().NoError(err)

Expand Down Expand Up @@ -310,7 +311,7 @@ func (s *TestMessengerProfileShowcase) TestShareShowcasePreferences() {
s.verifiedContact(verifiedContact)

// Save preferences to dispatch changes
request := s.prepareShocasePreferencs()
request := s.prepareShowcasePreferences()
err = s.m.SetProfileShowcasePreferences(request)
s.Require().NoError(err)

Expand All @@ -328,15 +329,15 @@ func (s *TestMessengerProfileShowcase) TestShareShowcasePreferences() {
profileShowcase := resp.Contacts[0].ProfileShowcase
s.Require().Len(profileShowcase.Communities, 2)
// For everyone
s.Require().Equal(profileShowcase.Communities[0].EntryId, request.Communities[0].ID)
s.Require().Equal(profileShowcase.Communities[0].EntryID, request.Communities[0].ID)
s.Require().Equal(profileShowcase.Communities[0].Order, request.Communities[0].Order)

// For contacts
s.Require().Equal(profileShowcase.Communities[1].EntryId, request.Communities[1].ID)
s.Require().Equal(profileShowcase.Communities[1].EntryID, request.Communities[1].ID)
s.Require().Equal(profileShowcase.Communities[1].Order, request.Communities[1].Order)

s.Require().Len(profileShowcase.Accounts, 1)
s.Require().Equal(profileShowcase.Accounts[0].EntryId, request.Accounts[0].ID)
s.Require().Equal(profileShowcase.Accounts[0].EntryID, request.Accounts[0].ID)
s.Require().Equal(profileShowcase.Accounts[0].Order, request.Accounts[0].Order)

s.Require().Len(profileShowcase.Collectibles, 0)
Expand All @@ -357,23 +358,23 @@ func (s *TestMessengerProfileShowcase) TestShareShowcasePreferences() {
profileShowcase = resp.Contacts[0].ProfileShowcase
s.Require().Len(profileShowcase.Communities, 3)
// For everyone
s.Require().Equal(profileShowcase.Communities[0].EntryId, request.Communities[0].ID)
s.Require().Equal(profileShowcase.Communities[0].EntryID, request.Communities[0].ID)
s.Require().Equal(profileShowcase.Communities[0].Order, request.Communities[0].Order)

// For contacts
s.Require().Equal(profileShowcase.Communities[1].EntryId, request.Communities[1].ID)
s.Require().Equal(profileShowcase.Communities[1].EntryID, request.Communities[1].ID)
s.Require().Equal(profileShowcase.Communities[1].Order, request.Communities[1].Order)

// For id verified
s.Require().Equal(profileShowcase.Communities[2].EntryId, request.Communities[2].ID)
s.Require().Equal(profileShowcase.Communities[2].EntryID, request.Communities[2].ID)
s.Require().Equal(profileShowcase.Communities[2].Order, request.Communities[2].Order)

s.Require().Len(profileShowcase.Accounts, 1)
s.Require().Equal(profileShowcase.Accounts[0].EntryId, request.Accounts[0].ID)
s.Require().Equal(profileShowcase.Accounts[0].EntryID, request.Accounts[0].ID)
s.Require().Equal(profileShowcase.Accounts[0].Order, request.Accounts[0].Order)

s.Require().Len(profileShowcase.Collectibles, 1)
s.Require().Equal(profileShowcase.Collectibles[0].EntryId, request.Collectibles[0].ID)
s.Require().Equal(profileShowcase.Collectibles[0].EntryID, request.Collectibles[0].ID)
s.Require().Equal(profileShowcase.Collectibles[0].Order, request.Collectibles[0].Order)

s.Require().Len(profileShowcase.Assets, 0)
Expand Down
48 changes: 24 additions & 24 deletions protocol/migrations/migrations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DROP TABLE profile_showcase_contacts;

CREATE TABLE IF NOT EXISTS profile_showcase_contacts (
CREATE TABLE profile_showcase_contacts (
contact_id TEXT NOT NULL,
entry_id TEXT NOT NULL,
entry_type INT NOT NULL,
Expand Down
28 changes: 14 additions & 14 deletions protocol/persistence_profile_showcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (db sqlitePersistence) GetProfileShowcasePreferencesByType(entryType Profil
return db.parseProfileShowcasePreferencesRows(rows)
}

func (db sqlitePersistence) GetProfileShowcaseForContact(contactId string) (*identity.ProfileShowcase, error) {
rows, err := db.db.Query(selectProfileShowcaseByContactQuery, contactId)
func (db sqlitePersistence) GetProfileShowcaseForContact(contactID string) (*identity.ProfileShowcase, error) {
rows, err := db.db.Query(selectProfileShowcaseByContactQuery, contactID)
if err != nil {
return nil, err
}
Expand All @@ -141,7 +141,7 @@ func (db sqlitePersistence) GetProfileShowcaseForContact(contactId string) (*ide
entry := &identity.VisibleProfileShowcaseEntry{}

err := rows.Scan(
&entry.EntryId,
&entry.EntryID,
&entry.Order,
&entryType,
)
Expand All @@ -164,20 +164,20 @@ func (db sqlitePersistence) GetProfileShowcaseForContact(contactId string) (*ide
return showcase, nil
}

func (db sqlitePersistence) ClearProfileShowcaseForContact(contactId string) error {
_, err := db.db.Exec(removeProfileShowcaseContactQuery, contactId)
func (db sqlitePersistence) ClearProfileShowcaseForContact(contactID string) error {
_, err := db.db.Exec(removeProfileShowcaseContactQuery, contactID)
if err != nil {
return err
}

return nil
}

func (db sqlitePersistence) saveProfileShowcaseContactEntries(tx *sql.Tx, contactId string, entryType ProfileShowcaseEntryType, entries []*identity.VisibleProfileShowcaseEntry) error {
func (db sqlitePersistence) saveProfileShowcaseContactEntries(tx *sql.Tx, contactID string, entryType ProfileShowcaseEntryType, entries []*identity.VisibleProfileShowcaseEntry) error {
for _, entry := range entries {
_, err := tx.Exec(insertOrUpdateProfileShowcaseContactQuery,
contactId,
entry.EntryId,
contactID,
entry.EntryID,
entryType,
entry.Order,
)
Expand All @@ -189,7 +189,7 @@ func (db sqlitePersistence) saveProfileShowcaseContactEntries(tx *sql.Tx, contac
return nil
}

func (db sqlitePersistence) SaveProfileShowcaseForContact(contactId string, showcase *identity.ProfileShowcase) error {
func (db sqlitePersistence) SaveProfileShowcaseForContact(contactID string, showcase *identity.ProfileShowcase) error {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
Expand All @@ -204,28 +204,28 @@ func (db sqlitePersistence) SaveProfileShowcaseForContact(contactId string, show
}()

// Remove old entries first
_, err = tx.Exec(removeProfileShowcaseContactQuery, contactId)
_, err = tx.Exec(removeProfileShowcaseContactQuery, contactID)
if err != nil {
return err
}

// Save all profile showcase entries
err = db.saveProfileShowcaseContactEntries(tx, contactId, ProfileShowcaseEntryTypeCommunity, showcase.Communities)
err = db.saveProfileShowcaseContactEntries(tx, contactID, ProfileShowcaseEntryTypeCommunity, showcase.Communities)
if err != nil {
return err
}

err = db.saveProfileShowcaseContactEntries(tx, contactId, ProfileShowcaseEntryTypeAccount, showcase.Accounts)
err = db.saveProfileShowcaseContactEntries(tx, contactID, ProfileShowcaseEntryTypeAccount, showcase.Accounts)
if err != nil {
return err
}

err = db.saveProfileShowcaseContactEntries(tx, contactId, ProfileShowcaseEntryTypeCollectible, showcase.Collectibles)
err = db.saveProfileShowcaseContactEntries(tx, contactID, ProfileShowcaseEntryTypeCollectible, showcase.Collectibles)
if err != nil {
return err
}

err = db.saveProfileShowcaseContactEntries(tx, contactId, ProfileShowcaseEntryTypeAsset, showcase.Assets)
err = db.saveProfileShowcaseContactEntries(tx, contactID, ProfileShowcaseEntryTypeAsset, showcase.Assets)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 5b2ab5b

Please sign in to comment.