diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index f52030485..05c883796 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -23,7 +23,7 @@ function MessageReceiver() { // bind events lokiPublicChatAPI.on( 'publicMessage', - window.NewReceiver.handleUnencryptedMessage + window.NewReceiver.handlePublicMessage ); openGroupBound = true; } diff --git a/ts/receiver/dataMessage.ts b/ts/receiver/dataMessage.ts index a42afcdcf..55ec87a3a 100644 --- a/ts/receiver/dataMessage.ts +++ b/ts/receiver/dataMessage.ts @@ -297,9 +297,13 @@ export async function handleDataMessage( const message = await processDecrypted(envelope, dataMessage); const ourPubKey = window.textsecure.storage.user.getNumber(); + const source = envelope.source; const senderPubKey = envelope.senderIdentity || envelope.source; const isMe = senderPubKey === ourPubKey; - const conversation = ConversationController.getInstance().get(senderPubKey); + const senderConversation = await ConversationController.getInstance().getOrCreateAndWait( + senderPubKey, + 'private' + ); const { UNPAIRING_REQUEST } = SignalService.DataMessage.Flags; @@ -311,18 +315,22 @@ export async function handleDataMessage( } // Check if we need to update any profile names - if (!isMe && conversation && message.profile) { - await updateProfile(conversation, message.profile, message.profileKey); + if (!isMe && senderConversation && message.profile) { + await updateProfile( + senderConversation, + message.profile, + message.profileKey + ); } if (isMessageEmpty(message)) { window.log.warn(`Message ${getEnvelopeId(envelope)} ignored; it was empty`); return removeFromCache(envelope); } - const source = envelope.senderIdentity || senderPubKey; - const ownDevice = await MultiDeviceProtocol.isOurDevice(source); + const ownDevice = await MultiDeviceProtocol.isOurDevice(senderPubKey); - const ownMessage = conversation?.isMediumGroup() && ownDevice; + const sourceConversation = ConversationController.getInstance().get(source); + const ownMessage = sourceConversation?.isMediumGroup() && ownDevice; const ev: any = {}; if (ownMessage) { @@ -342,7 +350,7 @@ export async function handleDataMessage( ev.confirm = () => removeFromCache(envelope); ev.data = { - source, + source: senderPubKey, sourceDevice: envelope.sourceDevice, timestamp: _.toNumber(envelope.timestamp), receivedAt: envelope.receivedAt, @@ -599,20 +607,14 @@ export async function handleMessageEvent(event: MessageEvent): Promise { ? ConversationType.GROUP : ConversationType.PRIVATE; - const { PROFILE_KEY_UPDATE } = SignalService.DataMessage.Flags; - - // tslint:disable-next-line: no-bitwise - const isProfileUpdate = Boolean(message.flags & PROFILE_KEY_UPDATE); let conversationId = isIncoming ? source : destination; - if (isProfileUpdate) { + if (message.profileKey?.length) { await handleProfileUpdate( message.profileKey, conversationId, type, isIncoming ); - confirm(); - return; } const msg = createMessage(data, isIncoming); diff --git a/ts/receiver/receiver.ts b/ts/receiver/receiver.ts index 6ac08615b..71c13930c 100644 --- a/ts/receiver/receiver.ts +++ b/ts/receiver/receiver.ts @@ -276,7 +276,7 @@ async function handleDecryptedEnvelope( } } -export async function handleUnencryptedMessage({ message: outerMessage }: any) { +export async function handlePublicMessage({ message: outerMessage }: any) { const { source } = outerMessage; const { group, profile, profileKey } = outerMessage.message; @@ -295,9 +295,15 @@ export async function handleUnencryptedMessage({ message: outerMessage }: any) { const isPublicChatMessage = group && group.id && !!group.id.match(/^publicChat:/); + if (!isPublicChatMessage) { + throw new Error( + 'handlePublicMessage Should only be called with public message groups' + ); + } + const ev = { // Public chat messages from ourselves should be outgoing - type: isPublicChatMessage && isOurDevice ? 'sent' : 'message', + type: isOurDevice ? 'sent' : 'message', data: outerMessage, confirm: () => { /* do nothing */