Re-register on adding or removing a chat

This commit is contained in:
Andrea Maria Piana 2020-09-03 09:19:46 +02:00
parent 0d998e1858
commit 4e18d21129
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
3 changed files with 44 additions and 14 deletions

View File

@ -248,11 +248,11 @@ func (m *MessageHandler) HandleSyncInstallationContact(state *ReceivedMessageSta
return nil
}
func (m *MessageHandler) HandleSyncInstallationPublicChat(state *ReceivedMessageState, message protobuf.SyncInstallationPublicChat) error {
func (m *MessageHandler) HandleSyncInstallationPublicChat(state *ReceivedMessageState, message protobuf.SyncInstallationPublicChat) (bool, error) {
chatID := message.Id
_, ok := state.AllChats[chatID]
if ok {
return nil
return false, nil
}
chat := CreatePublicChat(chatID, state.Timesource)
@ -260,7 +260,7 @@ func (m *MessageHandler) HandleSyncInstallationPublicChat(state *ReceivedMessage
state.AllChats[chat.ID] = &chat
state.ModifiedChats[chat.ID] = true
return nil
return true, nil
}
func (m *MessageHandler) HandleContactUpdate(state *ReceivedMessageState, message protobuf.ContactUpdate) error {

View File

@ -1358,7 +1358,7 @@ func (m *Messenger) LeaveGroupChat(ctx context.Context, chatID string, remove bo
}
func (m *Messenger) saveChat(chat *Chat) error {
_, ok := m.allChats[chat.ID]
previousChat, ok := m.allChats[chat.ID]
if chat.OneToOne() {
name, identicon, err := generateAliasAndIdenticon(chat.ID)
if err != nil {
@ -1370,11 +1370,21 @@ func (m *Messenger) saveChat(chat *Chat) error {
}
// Sync chat if it's a new active public chat
if !ok && chat.Active && chat.Public() {
if err := m.syncPublicChat(context.Background(), chat); err != nil {
return err
}
}
// We check if it's a new chat, or chat.Active has changed
if chat.Public() && (!ok && chat.Active) || (ok && chat.Active != previousChat.Active) {
// Re-register for push notifications, as we want to receive mentions
if err := m.reregisterForPushNotifications(); err != nil {
return err
}
}
err := m.persistence.SaveChat(*chat)
if err != nil {
return err
@ -1424,7 +1434,12 @@ func (m *Messenger) DeleteChat(chatID string) error {
if err != nil {
return err
}
chat, ok := m.allChats[chatID]
if ok && chat.Active && chat.Public() {
delete(m.allChats, chatID)
return m.reregisterForPushNotifications()
}
return nil
}
@ -2250,12 +2265,23 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
p := msg.ParsedMessage.Interface().(protobuf.SyncInstallationPublicChat)
logger.Debug("Handling SyncInstallationPublicChat", zap.Any("message", p))
err = m.handler.HandleSyncInstallationPublicChat(messageState, p)
added, err := m.handler.HandleSyncInstallationPublicChat(messageState, p)
if err != nil {
logger.Warn("failed to handle SyncInstallationPublicChat", zap.Error(err))
continue
}
// We re-register as we want to receive mentions from the newly joined public chat
if added {
logger.Debug("newly synced public chat, re-registering for push notifications")
err := m.reregisterForPushNotifications()
if err != nil {
logger.Warn("could not re-register for push notifications", zap.Error(err))
continue
}
}
case protobuf.RequestAddressForTransaction:
command := msg.ParsedMessage.Interface().(protobuf.RequestAddressForTransaction)
logger.Debug("Handling RequestAddressForTransaction", zap.Any("message", command))
@ -3490,6 +3516,7 @@ func (m *Messenger) EnableSendingPushNotifications() error {
func (m *Messenger) pushNotificationOptions() *pushnotificationclient.RegistrationOptions {
var contactIDs []*ecdsa.PublicKey
var mutedChatIDs []string
var publicChatIDs []string
for _, contact := range m.allContacts {
if contact.IsAdded() {
@ -3507,11 +3534,15 @@ func (m *Messenger) pushNotificationOptions() *pushnotificationclient.Registrati
if chat.Muted {
mutedChatIDs = append(mutedChatIDs, chat.ID)
}
if chat.Active && chat.Public() {
publicChatIDs = append(publicChatIDs, chat.ID)
}
}
return &pushnotificationclient.RegistrationOptions{
ContactIDs: contactIDs,
MutedChatIDs: mutedChatIDs,
PublicChatIDs: publicChatIDs,
}
}

View File

@ -383,10 +383,13 @@ func (s *Server) buildPushNotificationRequestResponse(request *protobuf.PushNoti
report.Error = protobuf.PushNotificationReport_NOT_REGISTERED
} else if registration.AccessToken != pn.AccessToken {
report.Error = protobuf.PushNotificationReport_WRONG_TOKEN
} else if s.contains(registration.BlockedChatList, pn.ChatId) {
} else if s.contains(registration.BlockedChatList, pn.ChatId) || !s.isValidMentionNotification(pn, registration) {
// We report as successful but don't send the notification
// for privacy reasons, as otherwise we would disclose that
// the sending client has been blocked or that the registering
// client has not joined a given public chat
report.Success = true
} else if s.isMessageNotification(pn) || s.isValidMentionNotification(pn, registration) {
} else {
// For now we just assume that the notification will be successful
requestAndRegistrations = append(requestAndRegistrations, &RequestAndRegistration{
Request: pn,
@ -471,10 +474,6 @@ func (s *Server) buildPushNotificationRegistrationResponse(publicKey *ecdsa.Publ
return response
}
func (s *Server) isMessageNotification(pn *protobuf.PushNotification) bool {
return pn.Type == protobuf.PushNotification_MESSAGE
}
func (s *Server) isValidMentionNotification(pn *protobuf.PushNotification, registration *protobuf.PushNotificationRegistration) bool {
return !registration.BlockMentions && pn.Type == protobuf.PushNotification_MENTION && s.contains(registration.AllowedMentionsChatList, pn.ChatId)
}