From 05bbc43b226c37cedd2682b9fe1e4fc5c763c3f4 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 2 Jan 2020 16:23:31 +1100 Subject: [PATCH] fix accept/decline friend request from conversationListItem --- js/models/conversations.js | 24 +++++++++++++++++++ js/models/messages.js | 2 -- ts/components/ConversationListItem.tsx | 7 ++++-- ts/components/session/ActionsPanel.tsx | 9 +++++-- .../session/LeftPaneContactSection.tsx | 6 ++--- 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index a427dfacd..3b733709d 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -231,7 +231,28 @@ this.trigger('change'); this.messageCollection.forEach(m => m.trigger('change')); }, + async acceptFriendRequest() { + const messages = await window.Signal.Data.getMessagesByConversation( + this.id, + { limit: 1, MessageCollection: Whisper.MessageCollection } + ); + + const lastMessageModel = messages.at(0); + if (lastMessageModel) { + lastMessageModel.acceptFriendRequest(); + } + }, + async declineFriendRequest() { + const messages = await window.Signal.Data.getMessagesByConversation( + this.id, + { limit: 1, MessageCollection: Whisper.MessageCollection } + ); + const lastMessageModel = messages.at(0); + if (lastMessageModel) { + lastMessageModel.declineFriendRequest(); + } + }, setMessageSelectionBackdrop() { const messageSelected = this.selectedMessages.size > 0; @@ -571,6 +592,8 @@ onDeleteContact: () => this.deleteContact(), onDeleteMessages: () => this.deleteMessages(), onCloseOverlay: () => this.resetMessageSelection(), + acceptFriendRequest: () => this.acceptFriendRequest(), + declineFriendRequest: () => this.declineFriendRequest(), }; this.updateAsyncPropsCache(); @@ -962,6 +985,7 @@ } if (this.hasReceivedFriendRequest()) { this.setFriendRequestStatus(FriendRequestStatusEnum.friends, options); + await this.respondToAllFriendRequests({ response: 'accepted', direction: 'incoming', diff --git a/js/models/messages.js b/js/models/messages.js index e1abafad2..d13d2e237 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -398,7 +398,6 @@ return; } const conversation = await this.getSourceDeviceConversation(); - // If we somehow received an old friend request (e.g. after having restored // from seed, we won't be able to accept it, we should initiate our own // friend request to reset the session: @@ -408,7 +407,6 @@ }); return; } - this.set({ friendStatus: 'accepted' }); await window.Signal.Data.saveMessage(this.attributes, { Message: Whisper.Message, diff --git a/ts/components/ConversationListItem.tsx b/ts/components/ConversationListItem.tsx index debe773d0..12e4fc3f3 100644 --- a/ts/components/ConversationListItem.tsx +++ b/ts/components/ConversationListItem.tsx @@ -57,6 +57,8 @@ type PropsHousekeeping = { onClearNickname?: () => void; onCopyPublicKey?: () => void; onUnblockContact?: () => void; + acceptFriendRequest?: () => void; + declineFriendRequest?: () => void; }; type Props = PropsData & PropsHousekeeping; @@ -280,10 +282,11 @@ export class ConversationListItem extends React.PureComponent { public renderFriendRequestButtons() { + const { acceptFriendRequest, declineFriendRequest } = this.props; return (
- - + +
); } diff --git a/ts/components/session/ActionsPanel.tsx b/ts/components/session/ActionsPanel.tsx index 9d688fdbd..226da5a77 100644 --- a/ts/components/session/ActionsPanel.tsx +++ b/ts/components/session/ActionsPanel.tsx @@ -201,9 +201,14 @@ export class ActionsPanel extends React.Component { static getFriendRequestsCount(conversations: Array | undefined): number { let friendRequestCount = 0; if (conversations !== undefined) { - // We assume a friend request already read is no longer a friend request (has been ignored) + // We assume a friend request already read is still a friend valid request conversations.some(function (conversation) { - friendRequestCount += conversation.showFriendRequestIndicator ? 1 : 0; + // Ignore friend request with lastmessage status as sent as this is a friend request we made ourself + const isValidFriendRequest = conversation.showFriendRequestIndicator + && conversation.lastMessage + && conversation.lastMessage.status !== 'sent' + && conversation.lastMessage.status !== 'sending'; + friendRequestCount += isValidFriendRequest ? 1 : 0; if (friendRequestCount > 9) { return true; } diff --git a/ts/components/session/LeftPaneContactSection.tsx b/ts/components/session/LeftPaneContactSection.tsx index bbd9af1e9..66c276012 100644 --- a/ts/components/session/LeftPaneContactSection.tsx +++ b/ts/components/session/LeftPaneContactSection.tsx @@ -185,8 +185,9 @@ export class LeftPaneContactSection extends React.Component { let conversationsList = conversations; console.log('conversations:', conversationsList); if (conversationsList !== undefined) { + // ignore friend request we made ourself conversationsList = conversationsList.filter( - conversation => conversation.showFriendRequestIndicator + conversation => conversation.showFriendRequestIndicator && conversation.lastMessage && conversation.lastMessage.status !== 'sent' && conversation.lastMessage.status !== 'sending' ); } @@ -202,8 +203,7 @@ export class LeftPaneContactSection extends React.Component { 'render: must provided friends if no search results are provided' ); } - const length = friends.length + (friendsRequest?friendsRequest.length:0); - + const length = friends.length + (friendsRequest ? friendsRequest.length : 0); // Note: conversations is not a known prop for List, but it is required to ensure that // it re-renders when our conversation data changes. Otherwise it would just render // on startup and scroll.