From 0eef19f80e1980f737894b7e258e2853c023a35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tinkl?= Date: Mon, 13 Mar 2023 15:30:19 +0100 Subject: [PATCH] feat: dynamic social links - completely replace social links on save - respect the order of items and also the URL when comparing Rationale: for MVP, we'll want the user to be able to add several links of the same type, and adjust/preserve their order by drag'n'drop Needed for https://github.com/status-im/status-desktop/issues/9777 --- .../settings_social_links/database.go | 7 +++---- .../settings_social_links/database_test.go | 19 ++++++++++++++----- protocol/identity/social_links.go | 15 ++------------- protocol/identity/social_links_test.go | 7 ------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/multiaccounts/settings_social_links/database.go b/multiaccounts/settings_social_links/database.go index ede2e8eca..ceffab13e 100644 --- a/multiaccounts/settings_social_links/database.go +++ b/multiaccounts/settings_social_links/database.go @@ -47,7 +47,7 @@ func (s *SocialLinksSettings) GetSocialLinks() (identity.SocialLinks, error) { return links, nil } -// Be careful, it removes every row except static links (__twitter, etc.) before insertion +// Be careful, it removes every row before insertion func (s *SocialLinksSettings) SetSocialLinks(links *identity.SocialLinks) (err error) { if links == nil { return errors.New("can't set social links, nil object provided") @@ -66,9 +66,8 @@ func (s *SocialLinksSettings) SetSocialLinks(links *identity.SocialLinks) (err e _ = tx.Rollback() }() - // remove everything except static links - _, err = tx.Exec(`DELETE from social_links_settings WHERE link_text != ? AND link_text != ? AND link_text != ? AND link_text != ? AND link_text != ? AND link_text != ?`, - identity.TwitterID, identity.PersonalSiteID, identity.GithubID, identity.YoutubeID, identity.DiscordID, identity.TelegramID) + // remove everything + _, err = tx.Exec(`DELETE from social_links_settings`) stmt, err := tx.Prepare("INSERT INTO social_links_settings (link_text, link_url) VALUES (?, ?)") if err != nil { diff --git a/multiaccounts/settings_social_links/database_test.go b/multiaccounts/settings_social_links/database_test.go index d0159f397..b398542f9 100644 --- a/multiaccounts/settings_social_links/database_test.go +++ b/multiaccounts/settings_social_links/database_test.go @@ -71,7 +71,7 @@ func TestDatabase(t *testing.T) { require.NoError(t, err) links, err = socialLinkSettings.GetSocialLinks() require.NoError(t, err) - require.True(t, links.Equals(socialLinksWithDefaults("", "", "", "", "", ""))) + require.True(t, links.Equals(identity.SocialLinks{})) // custom links links = identity.SocialLinks{ @@ -79,19 +79,28 @@ func TestDatabase(t *testing.T) { Text: identity.TwitterID, URL: "Status_ico", }, + { + Text: identity.TelegramID, + URL: "dummy.telegram", + }, { Text: "customLink", URL: "customLink.com", }, + } + err = socialLinkSettings.SetSocialLinks(&links) + require.NoError(t, err) + + expected := identity.SocialLinks{ + { + Text: identity.TwitterID, + URL: "Status_ico", + }, { Text: identity.TelegramID, URL: "dummy.telegram", }, } - err = socialLinkSettings.SetSocialLinks(&links) - require.NoError(t, err) - - expected := socialLinksWithDefaults("Status_ico", "", "", "", "", "dummy.telegram") expected = append(expected, identity.SocialLink{Text: "customLink", URL: "customLink.com"}) links, err = socialLinkSettings.GetSocialLinks() diff --git a/protocol/identity/social_links.go b/protocol/identity/social_links.go index 437fde5f1..e5ff03399 100644 --- a/protocol/identity/social_links.go +++ b/protocol/identity/social_links.go @@ -2,7 +2,7 @@ package identity import ( "encoding/json" - "sort" + "reflect" "github.com/status-im/status-go/protocol/protobuf" ) @@ -41,18 +41,7 @@ func (s *SocialLinks) ToProtobuf() []*protobuf.SocialLink { } func (s SocialLinks) Equals(rhs SocialLinks) bool { - if len(s) != len(rhs) { - return false - } - sort.Slice(s, func(i, j int) bool { return s[i].Text < s[j].Text }) - sort.Slice(rhs, func(i, j int) bool { return rhs[i].Text < rhs[j].Text }) - for i := range s { - if s[i] != rhs[i] { - return false - } - } - - return true + return reflect.DeepEqual(s, rhs) } func (s *SocialLinks) Serialize() ([]byte, error) { diff --git a/protocol/identity/social_links_test.go b/protocol/identity/social_links_test.go index a2ec65c59..3a53c3704 100644 --- a/protocol/identity/social_links_test.go +++ b/protocol/identity/social_links_test.go @@ -28,11 +28,4 @@ func TestEquals(t *testing.T) { protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"}) transformedLinks = NewSocialLinks(protobufLinks) require.True(t, socialLinks.Equals(*transformedLinks)) - - // order does not matter - protobufLinks = []*protobuf.SocialLink{} - protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"}) - protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "A", Url: "B"}) - transformedLinks = NewSocialLinks(protobufLinks) - require.True(t, socialLinks.Equals(*transformedLinks)) }