From e4f4328232fe45089e53631549f9a94e2ee0ab07 Mon Sep 17 00:00:00 2001 From: warrickct Date: Wed, 2 Mar 2022 13:04:33 +1100 Subject: [PATCH] rename request evaluating function for clarity. Prevent disappearing messsages from being activated by request recipient before the request has been accepted. --- .../ConversationRequestButtons.tsx | 2 +- .../conversation/ConversationRequestInfo.tsx | 2 +- ts/hooks/useParamSelector.ts | 2 +- ts/interactions/conversationInteractions.ts | 4 +- ts/models/conversation.ts | 37 +++++++++++++++++-- ts/receiver/configMessage.ts | 2 +- ts/receiver/queuedJob.ts | 2 +- ts/state/selectors/conversations.ts | 2 +- 8 files changed, 42 insertions(+), 11 deletions(-) diff --git a/ts/components/conversation/ConversationRequestButtons.tsx b/ts/components/conversation/ConversationRequestButtons.tsx index 606afefb0..dbafe53f7 100644 --- a/ts/components/conversation/ConversationRequestButtons.tsx +++ b/ts/components/conversation/ConversationRequestButtons.tsx @@ -41,7 +41,7 @@ export const ConversationMessageRequestButtons = () => { } const convoModel = getConversationController().get(selectedConversation.id); - const showMsgRequestUI = convoModel && convoModel.isRequest(); + const showMsgRequestUI = convoModel && convoModel.isIncomingRequest(); const handleDeclineConversationRequest = () => { declineConversationWithConfirm(selectedConversation.id, true); diff --git a/ts/components/conversation/ConversationRequestInfo.tsx b/ts/components/conversation/ConversationRequestInfo.tsx index 27464dd55..99d942fd3 100644 --- a/ts/components/conversation/ConversationRequestInfo.tsx +++ b/ts/components/conversation/ConversationRequestInfo.tsx @@ -10,7 +10,7 @@ export const ConversationRequestinfo = () => { const selectedConversation = useSelector(getSelectedConversation); const convoModel = selectedConversation && getConversationController().get(selectedConversation.id); - const showMsgRequestUI = selectedConversation && convoModel && convoModel.isRequest(); + const showMsgRequestUI = selectedConversation && convoModel && convoModel.isIncomingRequest(); const [hasIncomingMessages, setHasIncomingMessages] = useState(false); const [incomingChecked, setIncomingChecked] = useState(false); diff --git a/ts/hooks/useParamSelector.ts b/ts/hooks/useParamSelector.ts index 0b69ded65..913ccea17 100644 --- a/ts/hooks/useParamSelector.ts +++ b/ts/hooks/useParamSelector.ts @@ -128,7 +128,7 @@ export function useIsRequest(convoId: string) { const convoProps = useConversationPropsById(convoId); return Boolean( convoProps && - ConversationModel.hasValidRequestValues({ + ConversationModel.hasValidIncomingRequestValues({ isMe: convoProps.isMe, isApproved: convoProps.isApproved, isPrivate: convoProps.isPrivate, diff --git a/ts/interactions/conversationInteractions.ts b/ts/interactions/conversationInteractions.ts index c21cb4447..30f97519a 100644 --- a/ts/interactions/conversationInteractions.ts +++ b/ts/interactions/conversationInteractions.ts @@ -358,7 +358,9 @@ export async function setDisappearingMessagesByConvoId( ) { const conversation = getConversationController().get(conversationId); - if (!conversation.didApproveMe() && conversation.isPrivate()) { + const canSetDisappearing = !conversation.isOutgoingRequest() && !conversation.isIncomingRequest(); + + if (!canSetDisappearing) { ToastUtils.pushMustBeApproved(); return; } diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index 734948a45..b18165411 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -246,7 +246,7 @@ export class ConversationModel extends Backbone.Model { * Method to evalute if a convo contains the right values * @param values Required properties to evaluate if this is a message request */ - public static hasValidRequestValues({ + public static hasValidIncomingRequestValues({ isMe, isApproved, isBlocked, @@ -260,6 +260,22 @@ export class ConversationModel extends Backbone.Model { return Boolean(!isMe && !isApproved && isPrivate && !isBlocked); } + public static hasValidOutgoingRequestValues({ + isMe, + didApproveMe, + isApproved, + isBlocked, + isPrivate, + }: { + isMe?: boolean; + isApproved?: boolean; + didApproveMe?: boolean; + isBlocked?: boolean; + isPrivate?: boolean; + }): boolean { + return Boolean(!isMe && isApproved && isPrivate && !isBlocked && !didApproveMe); + } + public idForLogging() { if (this.isPrivate()) { return this.id; @@ -728,10 +744,23 @@ export class ConversationModel extends Backbone.Model { /** * Does this conversation contain the properties to be considered a message request */ - public isRequest(): boolean { - return ConversationModel.hasValidRequestValues({ + public isIncomingRequest(): boolean { + return ConversationModel.hasValidIncomingRequestValues({ + isMe: this.isMe(), + isApproved: this.isApproved(), + isBlocked: this.isBlocked(), + isPrivate: this.isPrivate(), + }); + } + + /** + * Is this conversation an outgoing message request + */ + public isOutgoingRequest(): boolean { + return ConversationModel.hasValidOutgoingRequestValues({ isMe: this.isMe(), isApproved: this.isApproved(), + didApproveMe: this.didApproveMe(), isBlocked: this.isBlocked(), isPrivate: this.isPrivate(), }); @@ -1123,7 +1152,7 @@ export class ConversationModel extends Backbone.Model { `Sending ${read.length} read receipts?`, window.storage.get(SettingsKey.settingsReadReceipt) || false ); - const dontSendReceipt = this.isBlocked() || this.isRequest(); + const dontSendReceipt = this.isBlocked() || this.isIncomingRequest(); if (window.storage.get(SettingsKey.settingsReadReceipt) && !dontSendReceipt) { const timestamps = _.map(read, 'timestamp').filter(t => !!t) as Array; const receiptMessage = new ReadReceiptMessage({ diff --git a/ts/receiver/configMessage.ts b/ts/receiver/configMessage.ts index efb52e6a6..91db6646f 100644 --- a/ts/receiver/configMessage.ts +++ b/ts/receiver/configMessage.ts @@ -167,7 +167,7 @@ const handleContactReceived = async ( // only set for explicit true/false values incase outdated sender doesn't have the fields if (contactReceived.isBlocked === true) { - if (contactConvo.isRequest()) { + if (contactConvo.isIncomingRequest()) { // handling case where restored device's declined message requests were getting restored await ConversationInteraction.deleteAllMessagesByConvoIdNoConfirmation(contactConvo.id); } diff --git a/ts/receiver/queuedJob.ts b/ts/receiver/queuedJob.ts index ee8b0a710..e52fd18e3 100644 --- a/ts/receiver/queuedJob.ts +++ b/ts/receiver/queuedJob.ts @@ -259,7 +259,7 @@ async function handleRegularMessage( ); const isFirstRequestMessage = incomingMessageCount < 2; if ( - conversation.isRequest() && + conversation.isIncomingRequest() && isFirstRequestMessage && window.inboxStore?.getState().userConfig.hideMessageRequests ) { diff --git a/ts/state/selectors/conversations.ts b/ts/state/selectors/conversations.ts index 723b3fd2a..681a49469 100644 --- a/ts/state/selectors/conversations.ts +++ b/ts/state/selectors/conversations.ts @@ -445,7 +445,7 @@ const _getConversationRequests = ( ): Array => { return _.filter(sortedConversations, conversation => { const { isApproved, isBlocked, isPrivate, isMe } = conversation; - const isRequest = ConversationModel.hasValidRequestValues({ + const isRequest = ConversationModel.hasValidIncomingRequestValues({ isApproved, isBlocked, isPrivate,