fx: deprecate profile and timeline chats (#3809)

This commit is contained in:
Igor Sirotin 2023-08-03 17:16:11 +03:00 committed by GitHub
parent fbffcdc7a7
commit d535cd95f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 209 additions and 57 deletions

31
deprecation/protocol.go Normal file
View File

@ -0,0 +1,31 @@
package deprecation
const (
ChatProfileDeprecated = true
ChatTimelineDeprecated = true
)
func AddChatsCount(count int) int {
var add = 0
if !ChatProfileDeprecated {
add++
}
if !ChatTimelineDeprecated {
add++
}
return count + add
}
func AddProfileFiltersCount(count int) int {
if ChatProfileDeprecated {
return count
}
return count + 1
}
func AddTimelineFiltersCount(count int) int {
if ChatTimelineDeprecated {
return count
}
return count + 1
}

View File

@ -7,6 +7,7 @@ import (
"math/rand"
"time"
"github.com/status-im/status-go/deprecation"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
userimage "github.com/status-im/status-go/images"
@ -32,7 +33,11 @@ const (
ChatTypeOneToOne ChatType = iota + 1
ChatTypePublic
ChatTypePrivateGroupChat
// Deprecated: CreateProfileChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
ChatTypeProfile
// Deprecated: ChatTypeTimeline shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
ChatTypeTimeline
ChatTypeCommunityChat
)
@ -63,6 +68,7 @@ const (
const pkStringLength = 68
// timelineChatID is a magic constant id for your own timeline
// Deprecated: timeline chats are no more supported
const timelineChatID = "@timeline70bd746ddcc12beb96b2c9d572d0784ab137ffc774f5383e50585a932080b57cca0484b259e61cecbaa33a4c98a300a"
type Chat struct {
@ -241,10 +247,14 @@ func (c *Chat) Public() bool {
return c.ChatType == ChatTypePublic
}
// Deprecated: ProfileUpdates shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func (c *Chat) ProfileUpdates() bool {
return c.ChatType == ChatTypeProfile || len(c.Profile) > 0
}
// Deprecated: Timeline shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func (c *Chat) Timeline() bool {
return c.ChatType == ChatTypeTimeline
}
@ -527,11 +537,19 @@ func CreatePublicChat(name string, timesource common.TimeSource) *Chat {
}
}
// Deprecated: buildProfileChatID shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func buildProfileChatID(publicKeyString string) string {
return "@" + publicKeyString
}
// Deprecated: CreateProfileChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func CreateProfileChat(pubkey string, timesource common.TimeSource) *Chat {
// Return nil to prevent usage of deprecated function
if deprecation.ChatProfileDeprecated {
return nil
}
id := buildProfileChatID(pubkey)
return &Chat{
@ -562,7 +580,14 @@ func CreateGroupChat(timesource common.TimeSource) Chat {
}
}
// Deprecated: CreateTimelineChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func CreateTimelineChat(timesource common.TimeSource) *Chat {
// Return nil to prevent usage of deprecated function
if deprecation.ChatTimelineDeprecated {
return nil
}
return &Chat{
ID: timelineChatID,
Name: "#" + timelineChatID,

View File

@ -32,6 +32,7 @@ import (
"github.com/status-im/status-go/appmetrics"
"github.com/status-im/status-go/connection"
"github.com/status-im/status-go/contracts"
"github.com/status-im/status-go/deprecation"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/images"
@ -1721,16 +1722,25 @@ func (m *Messenger) Init() error {
return errors.New("invalid chat type")
}
}
// Timeline and profile chats are deprecated.
// This code can be removed after some reasonable time.
// upsert timeline chat
if !deprecation.ChatProfileDeprecated {
err = m.ensureTimelineChat()
if err != nil {
return err
}
}
// upsert profile chat
if !deprecation.ChatTimelineDeprecated {
err = m.ensureMyOwnProfileChat()
if err != nil {
return err
}
}
// Get chat IDs and public keys from the contacts.
contacts, err := m.persistence.Contacts()

View File

@ -5,6 +5,7 @@ import (
"errors"
"strings"
"github.com/status-im/status-go/deprecation"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
@ -231,7 +232,14 @@ func (m *Messenger) CreatePublicChat(request *requests.CreatePublicChat) (*Messe
return m.createPublicChat(chatID, response)
}
// Deprecated: CreateProfileChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func (m *Messenger) CreateProfileChat(request *requests.CreateProfileChat) (*MessengerResponse, error) {
// Return error to prevent usage of deprecated function
if deprecation.ChatProfileDeprecated {
return nil, errors.New("profile chats are deprecated")
}
if err := request.Validate(); err != nil {
return nil, err
}
@ -528,7 +536,14 @@ func (m *Messenger) Join(chat *Chat) ([]*transport.Filter, error) {
}
}
// Deprecated: buildProfileChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func (m *Messenger) buildProfileChat(id string) *Chat {
// Return nil to prevent usage of deprecated function
if deprecation.ChatProfileDeprecated {
return nil
}
// Create the corresponding profile chat
profileChatID := buildProfileChatID(id)
profileChat, ok := m.allChats.Load(profileChatID)
@ -541,7 +556,14 @@ func (m *Messenger) buildProfileChat(id string) *Chat {
}
// Deprecated: ensureTimelineChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func (m *Messenger) ensureTimelineChat() error {
// Return error to prevent usage of deprecated function
if deprecation.ChatProfileDeprecated {
return errors.New("timeline chats are deprecated")
}
chat, err := m.persistence.Chat(timelineChatID)
if err != nil {
return err
@ -556,7 +578,14 @@ func (m *Messenger) ensureTimelineChat() error {
return m.saveChat(chat)
}
// Deprecated: ensureMyOwnProfileChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func (m *Messenger) ensureMyOwnProfileChat() error {
// Return error to prevent usage of deprecated function
if deprecation.ChatProfileDeprecated {
return errors.New("profile chats are deprecated")
}
chatID := common.PubkeyToHex(&m.identity.PublicKey)
_, ok := m.allChats.Load(chatID)
if ok {

View File

@ -180,9 +180,8 @@ func (s *MessengerContactRequestSuite) acceptContactRequest(contactRequest *comm
s.Require().Equal(resp.ActivityCenterNotifications()[0].Name, resp.Contacts[0].PrimaryName())
// Check we have active chat in the response
s.Require().Len(resp.Chats(), 2)
s.Require().True(resp.Chats()[0].Active) // This is unactive profile chat
s.Require().True(resp.Chats()[1].Active)
s.Require().Len(resp.Chats(), 1)
s.Require().True(resp.Chats()[0].Active)
// Make sure the sender is added to our contacts
contacts := receiver.AddedContacts()

View File

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/deprecation"
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"
@ -63,6 +64,11 @@ func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
// It should add the contact
s.Require().True(contact.added())
if deprecation.ChatProfileDeprecated {
// It should a one to one chat
s.Require().Len(response.Chats(), 1)
s.Require().False(response.Chats()[0].Active)
} else {
// It should create a profile chat & a one to one chat
s.Require().Len(response.Chats(), 2)
chats := response.Chats()
@ -71,6 +77,11 @@ func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
} else {
s.Require().False(chats[1].Active)
}
}
//// It should a one to one chat
//s.Require().Len(response.Chats(), 1)
//s.Require().False(response.Chats()[0].Active)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
@ -123,8 +134,13 @@ func (s *MessengerContactUpdateSuite) TestAddContact() {
s.Require().Len(response.Contacts, 1)
contact := response.Contacts[0]
if deprecation.ChatProfileDeprecated {
// It adds the one to one chat
s.Require().Len(response.Chats(), 1)
} else {
// It adds the profile chat and the one to one chat
s.Require().Len(response.Chats(), 2)
}
// It should add the contact
s.Require().True(contact.added())
@ -162,8 +178,13 @@ func (s *MessengerContactUpdateSuite) TestAddContactWithENS() {
s.Require().Len(response.Contacts, 1)
contact := response.Contacts[0]
if deprecation.ChatProfileDeprecated {
// It adds the one to one chat
s.Require().Len(response.Chats(), 1)
} else {
// It adds the profile chat and the one to one chat
s.Require().Len(response.Chats(), 2)
}
// It should add the contact
s.Require().True(contact.added())

View File

@ -10,6 +10,7 @@ import (
"github.com/golang/protobuf/proto"
"go.uber.org/zap"
"github.com/status-im/status-go/deprecation"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/common"
@ -395,8 +396,13 @@ func (m *Messenger) addContact(ctx context.Context, pubKey, ensName, nickname, d
return nil, err
}
// Profile chats are deprecated.
// Code below can be removed after some reasonable time.
//Create the corresponding chat
profileChat := m.buildProfileChat(contact.ID)
var profileChat *Chat
if !deprecation.ChatProfileDeprecated {
profileChat = m.buildProfileChat(contact.ID)
_, err = m.Join(profileChat)
if err != nil {
@ -406,6 +412,7 @@ func (m *Messenger) addContact(ctx context.Context, pubKey, ensName, nickname, d
if err := m.saveChat(profileChat); err != nil {
return nil, err
}
}
publicKey, err := contact.PublicKey()
if err != nil {
@ -454,13 +461,18 @@ func (m *Messenger) addContact(ctx context.Context, pubKey, ensName, nickname, d
return nil, err
}
// Profile chats are deprecated.
// Code below can be removed after some reasonable time.
// Add chat
if !deprecation.ChatProfileDeprecated {
response.AddChat(profileChat)
_, err = m.transport.InitFilters([]string{profileChat.ID}, []*ecdsa.PublicKey{publicKey})
if err != nil {
return nil, err
}
}
// Publish contact code
err = m.publishContactCode()
@ -643,7 +655,11 @@ func (m *Messenger) removeContact(ctx context.Context, response *MessengerRespon
return err
}
// Profile chats are deprecated.
// Code below can be removed after some reasonable time.
//Create the corresponding profile chat
if !deprecation.ChatProfileDeprecated {
profileChatID := buildProfileChatID(contact.ID)
_, ok = m.allChats.Load(profileChatID)
@ -657,6 +673,7 @@ func (m *Messenger) removeContact(ctx context.Context, response *MessengerRespon
return err
}
}
}
response.Contacts = []*Contact{contact}
return nil

View File

@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"github.com/status-im/status-go/deprecation"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/requests"
)
@ -43,7 +44,7 @@ func (s *MessengerMuteSuite) TestSetMute() {
s.NoError(error)
allChats := s.m.Chats()
s.Require().Len(allChats, 3)
s.Require().Len(allChats, deprecation.AddChatsCount(1))
var actualChat *Chat
@ -93,7 +94,7 @@ func (s *MessengerMuteSuite) TestSetMuteForDuration() {
s.Require().NoError(err)
allChats := s.m.Chats()
s.Require().Len(allChats, 3)
s.Require().Len(allChats, deprecation.AddChatsCount(1))
var actualChat *Chat

View File

@ -16,6 +16,7 @@ import (
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"github.com/status-im/status-go/deprecation"
coretypes "github.com/status-im/status-go/eth-node/core/types"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
@ -163,7 +164,7 @@ func (s *MessengerSuite) TestInit() {
_, err = s.m.AddContact(context.Background(), &requests.AddContact{ID: types.EncodeHex(crypto.FromECDSAPub(&key.PublicKey))})
s.Require().NoError(err)
},
AddedFilters: 2,
AddedFilters: deprecation.AddProfileFiltersCount(1),
},
}
@ -175,7 +176,7 @@ func (s *MessengerSuite) TestInit() {
s.Require().NoError(err)
filters := s.m.transport.Filters()
expectedFilters += tc.AddedFilters
s.Equal(expectedFilters+1, len(filters))
s.Equal(deprecation.AddTimelineFiltersCount(expectedFilters), len(filters))
})
}
}
@ -306,7 +307,7 @@ func (s *MessengerSuite) TestMarkAllRead() {
s.Require().NoError(err)
chats := s.m.Chats()
s.Require().Len(chats, 3)
s.Require().Len(chats, deprecation.AddChatsCount(1))
for idx := range chats {
if chats[idx].ID == chat.ID {
s.Require().Equal(uint(0), chats[idx].UnviewedMessagesCount)
@ -342,6 +343,11 @@ func (s *MessengerSuite) TestSendPublic() {
}
func (s *MessengerSuite) TestSendProfile() {
// Early exit to skip testing deprecated code
if deprecation.ChatProfileDeprecated {
return
}
chat := CreateProfileChat("0x"+hex.EncodeToString(crypto.FromECDSAPub(&s.privateKey.PublicKey)), s.m.transport)
chat.LastClockValue = uint64(100000000000000)
err := s.m.SaveChat(chat)
@ -1057,7 +1063,7 @@ func (s *MessengerSuite) TestChatPersistencePublic() {
s.Require().NoError(s.m.SaveChat(chat))
savedChats := s.m.Chats()
s.Require().Equal(3, len(savedChats))
s.Require().Equal(deprecation.AddChatsCount(1), len(savedChats))
}
func (s *MessengerSuite) TestDeleteChat() {
@ -1078,11 +1084,11 @@ func (s *MessengerSuite) TestDeleteChat() {
s.Require().NoError(s.m.SaveChat(chat))
savedChats := s.m.Chats()
s.Require().Equal(3, len(savedChats))
s.Require().Equal(deprecation.AddChatsCount(1), len(savedChats))
s.Require().NoError(s.m.DeleteChat(chatID))
savedChats = s.m.Chats()
s.Require().Equal(2, len(savedChats))
s.Require().Equal(deprecation.AddChatsCount(0), len(savedChats))
}
func (s *MessengerSuite) TestChatPersistenceUpdate() {
@ -1102,7 +1108,7 @@ func (s *MessengerSuite) TestChatPersistenceUpdate() {
s.Require().NoError(s.m.SaveChat(chat))
savedChats := s.m.Chats()
s.Require().Equal(3, len(savedChats))
s.Require().Equal(deprecation.AddChatsCount(1), len(savedChats))
var actualChat *Chat
for idx := range savedChats {
@ -1152,7 +1158,7 @@ func (s *MessengerSuite) TestChatPersistenceOneToOne() {
s.Require().NoError(s.m.SaveChat(chat))
savedChats := s.m.Chats()
s.Require().Equal(3, len(savedChats))
s.Require().Equal(deprecation.AddChatsCount(1), len(savedChats))
var actualChat *Chat
for idx := range savedChats {
@ -1233,7 +1239,7 @@ func (s *MessengerSuite) TestChatPersistencePrivateGroupChat() {
}
s.Require().NoError(s.m.SaveChat(chat))
savedChats := s.m.Chats()
s.Require().Equal(3, len(savedChats))
s.Require().Equal(deprecation.AddChatsCount(1), len(savedChats))
var actualChat *Chat
for idx := range savedChats {
@ -1425,7 +1431,7 @@ func (s *MessengerSuite) TestBlockContact() {
// The chat is deleted
actualChats := s.m.Chats()
s.Require().Equal(4, len(actualChats))
s.Require().Equal(deprecation.AddChatsCount(2), len(actualChats))
// The messages have been deleted
chat2Messages, _, err := s.m.MessageByChatID(chat2.ID, "", 20)

View File

@ -2,17 +2,30 @@ package requests
import (
"errors"
"github.com/status-im/status-go/deprecation"
)
var ErrCreateProfileChatInvalidID = errors.New("create-public-chat: invalid id")
// Deprecated: errCreateProfileChatInvalidID shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
var errCreateProfileChatInvalidID = errors.New("create-public-chat: invalid id")
// Deprecated: CreateProfileChat shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
type CreateProfileChat struct {
ID string `json:"id"`
}
// Deprecated: Validate shouldn't be used
// and is only left here in case profile chat feature is re-introduced.
func (c *CreateProfileChat) Validate() error {
// Return error to prevent usafe of deprecated function
if deprecation.ChatProfileDeprecated {
return errors.New("profile chats are deprecated")
}
if len(c.ID) == 0 {
return ErrCreateProfileChatInvalidID
return errCreateProfileChatInvalidID
}
return nil