address feedback

This commit is contained in:
Andrea Maria Piana 2021-06-01 11:29:37 +02:00
parent af1e47c258
commit 60de443e89
6 changed files with 75 additions and 77 deletions

View File

@ -37,6 +37,7 @@ var (
SigningPhrase: "yurt joey vibe",
SendPushNotifications: true,
ProfilePicturesVisibility: ProfilePicturesVisibilityContactsOnly,
DefaultSyncPeriod: 86400,
UseMailservers: true,
LinkPreviewRequestEnabled: true,
WalletRootAddress: types.HexToAddress("0x3B591fd819F86D0A6a2EF2Bcb94f77807a7De1a6")}

View File

@ -452,58 +452,6 @@ func (m *MessageHandler) HandlePairInstallation(state *ReceivedMessageState, mes
return nil
}
// HandleCommunityDescription handles an community description
func (m *MessageHandler) HandleCommunityDescription(state *ReceivedMessageState, signer *ecdsa.PublicKey, description protobuf.CommunityDescription, rawPayload []byte) error {
communityResponse, err := m.communitiesManager.HandleCommunityDescriptionMessage(signer, &description, rawPayload)
if err != nil {
return err
}
community := communityResponse.Community
state.Response.AddCommunity(community)
state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes)
// If we haven't joined the org, nothing to do
if !community.Joined() {
return nil
}
// Update relevant chats names and add new ones
// Currently removal is not supported
chats := CreateCommunityChats(community, state.Timesource)
var chatIDs []string
for i, chat := range chats {
oldChat, ok := state.AllChats.Load(chat.ID)
if !ok {
// Beware, don't use the reference in the range (i.e chat) as it's a shallow copy
state.AllChats.Store(chat.ID, chats[i])
state.Response.AddChat(chat)
chatIDs = append(chatIDs, chat.ID)
// Update name, currently is the only field is mutable
} else if oldChat.Name != chat.Name {
oldChat.Name = chat.Name
// TODO(samyoul) remove storing of an updated reference pointer?
state.AllChats.Store(chat.ID, oldChat)
state.Response.AddChat(chat)
}
}
// Load transport filters
filters, err := m.transport.InitPublicFilters(chatIDs)
if err != nil {
return err
}
for _, filter := range filters {
state.AllFilters[filter.ChatID] = filter
}
return nil
}
// HandleCommunityInvitation handles an community invitation
func (m *MessageHandler) HandleCommunityInvitation(state *ReceivedMessageState, signer *ecdsa.PublicKey, invitation protobuf.CommunityInvitation, rawPayload []byte) error {
if invitation.PublicKey == nil {

View File

@ -2327,8 +2327,6 @@ type ReceivedMessageState struct {
AllInstallations *installationMap
// List of communities modified
ModifiedInstallations *stringBoolMap
// List of filters
AllFilters map[string]*transport.Filter
// Map of existing messages
ExistingMessagesMap map[string]bool
// EmojiReactions is a list of emoji reactions for the current batch
@ -2447,7 +2445,6 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
ModifiedInstallations: m.modifiedInstallations,
ExistingMessagesMap: make(map[string]bool),
EmojiReactions: make(map[string]*EmojiReaction),
AllFilters: make(map[string]*transport.Filter),
GroupChatInvitations: make(map[string]*GroupChatInvitation),
Response: &MessengerResponse{},
Timesource: m.getTimesource(),
@ -2794,7 +2791,7 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
case protobuf.CommunityDescription:
logger.Debug("Handling CommunityDescription")
err = m.handler.HandleCommunityDescription(messageState, publicKey, msg.ParsedMessage.Interface().(protobuf.CommunityDescription), msg.DecryptedPayload)
err = m.handleCommunityDescription(messageState, publicKey, msg.ParsedMessage.Interface().(protobuf.CommunityDescription), msg.DecryptedPayload)
if err != nil {
logger.Warn("failed to handle CommunityDescription", zap.Error(err))
allMessagesProcessed = false

View File

@ -37,6 +37,18 @@ func (m *Messenger) ActiveChats() []*Chat {
return chats
}
func (m *Messenger) initChatSyncFields(chat *Chat) error {
defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod()
if err != nil {
return err
}
timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod
chat.SyncedTo = timestamp
chat.SyncedFrom = timestamp
return nil
}
func (m *Messenger) CreatePublicChat(request *requests.CreatePublicChat) (*MessengerResponse, error) {
if err := request.Validate(); err != nil {
return nil, err
@ -68,13 +80,9 @@ func (m *Messenger) CreatePublicChat(request *requests.CreatePublicChat) (*Messe
// We set the synced to, synced from to the default time
if !willSync {
defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod()
if err != nil {
if err := m.initChatSyncFields(chat); err != nil {
return nil, err
}
timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod
chat.SyncedTo = timestamp
chat.SyncedFrom = timestamp
}
err = m.saveChat(chat)
@ -134,13 +142,9 @@ func (m *Messenger) CreateProfileChat(request *requests.CreateProfileChat) (*Mes
// We set the synced to, synced from to the default time
if !willSync {
defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod()
if err != nil {
if err := m.initChatSyncFields(chat); err != nil {
return nil, err
}
timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod
chat.SyncedTo = timestamp
chat.SyncedFrom = timestamp
}
_, err = m.scheduleSyncFilters([]*transport.Filter{filter})
@ -191,13 +195,9 @@ func (m *Messenger) CreateOneToOneChat(request *requests.CreateOneToOneChat) (*M
// We set the synced to, synced from to the default time
if !willSync {
defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod()
if err != nil {
if err := m.initChatSyncFields(chat); err != nil {
return nil, err
}
timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod
chat.SyncedTo = timestamp
chat.SyncedFrom = timestamp
}
err = m.saveChat(chat)

View File

@ -690,3 +690,55 @@ func (m *Messenger) passStoredCommunityInfoToSignalHandler(communityID string) {
m.config.messengerSignalsHandler.CommunityInfoFound(community)
m.forgetCommunityRequest(communityID)
}
// handleCommunityDescription handles an community description
func (m *Messenger) handleCommunityDescription(state *ReceivedMessageState, signer *ecdsa.PublicKey, description protobuf.CommunityDescription, rawPayload []byte) error {
communityResponse, err := m.communitiesManager.HandleCommunityDescriptionMessage(signer, &description, rawPayload)
if err != nil {
return err
}
community := communityResponse.Community
state.Response.AddCommunity(community)
state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes)
// If we haven't joined the org, nothing to do
if !community.Joined() {
return nil
}
// Update relevant chats names and add new ones
// Currently removal is not supported
chats := CreateCommunityChats(community, state.Timesource)
var chatIDs []string
for i, chat := range chats {
oldChat, ok := state.AllChats.Load(chat.ID)
if !ok {
// Beware, don't use the reference in the range (i.e chat) as it's a shallow copy
state.AllChats.Store(chat.ID, chats[i])
state.Response.AddChat(chat)
chatIDs = append(chatIDs, chat.ID)
// Update name, currently is the only field is mutable
} else if oldChat.Name != chat.Name {
oldChat.Name = chat.Name
// TODO(samyoul) remove storing of an updated reference pointer?
state.AllChats.Store(chat.ID, oldChat)
state.Response.AddChat(chat)
}
}
// Load transport filters
filters, err := m.transport.InitPublicFilters(chatIDs)
if err != nil {
return err
}
_, err = m.scheduleSyncFilters(filters)
if err != nil {
return err
}
return nil
}

View File

@ -167,7 +167,7 @@ func (m *Messenger) syncChat(chatID string) (*MessengerResponse, error) {
return m.syncFilters(filters)
}
func (m *Messenger) defaultSyncPeriod() (uint32, error) {
func (m *Messenger) defaultSyncPeriodFromNow() (uint32, error) {
defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod()
if err != nil {
return 0, err
@ -175,9 +175,9 @@ func (m *Messenger) defaultSyncPeriod() (uint32, error) {
return uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod, nil
}
// calculateSyncPeriod caps the sync period to the default
func (m *Messenger) calculateSyncPeriod(period uint32) (uint32, error) {
d, err := m.defaultSyncPeriod()
// capToDefaultSyncPeriod caps the sync period to the default
func (m *Messenger) capToDefaultSyncPeriod(period uint32) (uint32, error) {
d, err := m.defaultSyncPeriodFromNow()
if err != nil {
return 0, err
}
@ -232,7 +232,7 @@ func (m *Messenger) syncFilters(filters []*transport.Filter) (*MessengerResponse
topicData, ok := topicsData[filter.Topic.String()]
if !ok {
lastRequest, err := m.defaultSyncPeriod()
lastRequest, err := m.defaultSyncPeriodFromNow()
if err != nil {
return nil, err
}
@ -243,7 +243,7 @@ func (m *Messenger) syncFilters(filters []*transport.Filter) (*MessengerResponse
}
batch, ok := batches[topicData.LastRequest]
if !ok {
from, err := m.calculateSyncPeriod(uint32(topicData.LastRequest))
from, err := m.capToDefaultSyncPeriod(uint32(topicData.LastRequest))
if err != nil {
return nil, err
}