From 45cdbcbb2f6b3e17b1be9823664e8cd5c3361947 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 14 Jul 2020 16:22:37 +1000 Subject: [PATCH] handle sync of blocked contact/groups as sync events --- js/models/conversations.js | 10 ++++++++++ libtextsecure/account_manager.js | 2 +- libtextsecure/sendmessage.js | 15 ++++++++------- ts/receiver/groups.ts | 15 ++++++++------- ts/receiver/multidevice.ts | 12 +++++++++++- ts/util/blockedNumberController.ts | 20 ++++++++++++++++++++ 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index e75044b0e..fa8219d3b 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -253,6 +253,11 @@ this.trigger('change', this); this.messageCollection.forEach(m => m.trigger('change')); this.updateTextInputState(); + if (this.isPrivate()) { + await textsecure.messaging.sendContactSyncMessage([this]); + } else { + await textsecure.messaging.sendGroupSyncMessage([this]); + } }, async unblock() { if (!this.id || this.isPublic() || this.isRss()) { @@ -265,6 +270,11 @@ this.trigger('change', this); this.messageCollection.forEach(m => m.trigger('change')); this.updateTextInputState(); + if (this.isPrivate()) { + await textsecure.messaging.sendContactSyncMessage([this]); + } else { + await textsecure.messaging.sendGroupSyncMessage([this]); + } }, setMessageSelectionBackdrop() { const messageSelected = this.selectedMessages.size > 0; diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 573e31caa..46ec46f7a 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -653,7 +653,7 @@ const conversations = window.getConversations().models; await textsecure.messaging.sendGroupSyncMessage(conversations); await textsecure.messaging.sendOpenGroupsSyncMessage(conversations); - await textsecure.messaging.sendContactSyncMessage(conversations); + await textsecure.messaging.sendContactSyncMessage(); }, 5000); }, validatePubKeyHex(pubKey) { diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index f09aaa36d..f31114f99 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -360,8 +360,13 @@ MessageSender.prototype = { }); }, - async sendContactSyncMessage() { - const convosToSync = await libsession.Utils.SyncMessageUtils.getSyncContacts(); + async sendContactSyncMessage(convos) { + let convosToSync; + if (!convos) { + convosToSync = await libsession.Utils.SyncMessageUtils.getSyncContacts(); + } else { + convosToSync = convos; + } if (convosToSync.size === 0) { window.console.info('No contacts to sync.'); @@ -397,11 +402,7 @@ MessageSender.prototype = { } // We only want to sync across closed groups that we haven't left const sessionGroups = conversations.filter( - c => - c.isClosedGroup() && - !c.get('left') && - !c.isBlocked() && - !c.isMediumGroup() + c => c.isClosedGroup() && !c.get('left') && !c.isMediumGroup() ); if (sessionGroups.length === 0) { window.console.info('No closed group to sync.'); diff --git a/ts/receiver/groups.ts b/ts/receiver/groups.ts index de91fb7c8..498cc7cd7 100644 --- a/ts/receiver/groups.ts +++ b/ts/receiver/groups.ts @@ -146,13 +146,7 @@ interface GroupInfo { } export async function onGroupReceived(details: GroupInfo) { - const { - ConversationController, - libloki, - storage, - textsecure, - Whisper, - } = window; + const { ConversationController, libloki, textsecure, Whisper } = window; const { id } = details; @@ -204,6 +198,13 @@ export async function onGroupReceived(details: GroupInfo) { ); conversation.set(newAttributes); } + const isBlocked = details.blocked || false; + if (conversation.isClosedGroup()) { + await BlockedNumberController.setGroupBlocked(conversation.id, isBlocked); + } + + conversation.trigger('change', conversation); + conversation.updateTextInputState(); await window.Signal.Data.updateConversation(id, conversation.attributes, { Conversation: Whisper.Conversation, diff --git a/ts/receiver/multidevice.ts b/ts/receiver/multidevice.ts index 5cf786f64..9633c3247 100644 --- a/ts/receiver/multidevice.ts +++ b/ts/receiver/multidevice.ts @@ -12,6 +12,7 @@ import { MultiDeviceProtocol, SessionProtocol } from '../session/protocols'; import { PubKey } from '../session/types'; import ByteBuffer from 'bytebuffer'; +import { BlockedNumberController } from '../util'; async function unpairingRequestIsLegit(source: string, ourPubKey: string) { const { textsecure, storage, lokiFileServerAPI } = window; @@ -287,6 +288,7 @@ export async function handleContacts( await removeFromCache(envelope); } +// tslint:disable-next-line: max-func-body-length async function onContactReceived(details: any) { const { ConversationController, @@ -427,7 +429,15 @@ async function onContactReceived(details: any) { verifiedEvent.viaContactSync = true; await onVerified(verifiedEvent); } - await conversation.trigger('change'); + + const isBlocked = details.blocked || false; + + if (conversation.isPrivate()) { + await BlockedNumberController.setBlocked(conversation.id, isBlocked); + } + conversation.updateTextInputState(); + + await conversation.trigger('change', conversation); } catch (error) { window.log.error('onContactReceived error:', Errors.toLogFormat(error)); } diff --git a/ts/util/blockedNumberController.ts b/ts/util/blockedNumberController.ts index baaabe7d4..d4f9538bd 100644 --- a/ts/util/blockedNumberController.ts +++ b/ts/util/blockedNumberController.ts @@ -89,6 +89,26 @@ export class BlockedNumberController { } } + public static async setBlocked( + user: string | PubKey, + blocked: boolean + ): Promise { + if (blocked) { + return BlockedNumberController.block(user); + } + return BlockedNumberController.unblock(user); + } + + public static async setGroupBlocked( + groupId: string | PubKey, + blocked: boolean + ): Promise { + if (blocked) { + return BlockedNumberController.blockGroup(groupId); + } + return BlockedNumberController.unblockGroup(groupId); + } + public static async blockGroup(groupId: string | PubKey): Promise { await this.load(); const id = PubKey.cast(groupId);