Fix incoming friend request messages being deleted.

Set pending outgoing friend requests to declined if we receive an incoming friend request.
Fix text input not blocking.
pull/50/head
Mikunj 7 years ago
parent dba6a36e83
commit 7d8719f250

@ -79,6 +79,7 @@
unreadCount: 0, unreadCount: 0,
verified: textsecure.storage.protocol.VerifiedStatus.DEFAULT, verified: textsecure.storage.protocol.VerifiedStatus.DEFAULT,
keyExchangeCompleted: false, keyExchangeCompleted: false,
blockInput: false,
}; };
}, },
@ -134,7 +135,7 @@
this.updateLastMessage this.updateLastMessage
); );
this.on('newmessage', this.updateLastMessage); this.on('newmessage', this.onNewMessage);
this.on('change:profileKey', this.onChangeProfileKey); this.on('change:profileKey', this.onChangeProfileKey);
// Listening for out-of-band data updates // Listening for out-of-band data updates
@ -1030,22 +1031,32 @@
return true; return true;
}); });
}, },
async updateBlockInput(blockInput) {
if (this.get('blockInput') === blockInput) return;
this.set({ blockInput });
await window.Signal.Data.updateConversation(this.id, this.attributes, {
Conversation: Whisper.Conversation,
});
},
async updateTextInputState() { async updateTextInputState() {
// Check if we need to disable the text field // Check if we need to disable the text field
const isFriend = await this.isFriend(); const isFriend = await this.isFriend();
if (isFriend) { if (!isFriend) {
// Disable the input if we're waiting for friend request approval // Disable the input if we're waiting for friend request approval
const waiting = await this.waitingForFriendRequestApproval(); const waiting = await this.waitingForFriendRequestApproval();
if (waiting) { if (waiting) {
await this.updateBlockInput(true);
this.trigger('disable:input', true); this.trigger('disable:input', true);
this.trigger('change:placeholder', 'disabled'); this.trigger('change:placeholder', 'disabled');
return; return;
} }
// Tell the user to introduce themselves // Tell the user to introduce themselves
await this.updateBlockInput(false);
this.trigger('disable:input', false); this.trigger('disable:input', false);
this.trigger('change:placeholder', 'friend-request'); this.trigger('change:placeholder', 'friend-request');
return; return;
} }
await this.updateBlockInput(false);
this.trigger('disable:input', false); this.trigger('disable:input', false);
this.trigger('change:placeholder', 'chat'); this.trigger('change:placeholder', 'chat');
}, },
@ -1199,6 +1210,41 @@
}, },
}; };
}, },
async onNewMessage(message) {
if (message.get('type') === 'friend-request' && message.get('direction') === 'incoming') {
// We need to make sure we remove any pending requests that we may have
// This is to ensure that one user cannot spam us with multiple friend requests.
const incoming = await this.getPendingFriendRequests('incoming');
// Delete the old messages if it's pending
await Promise.all(
incoming
.filter(i => i.id !== message.id)
.map(request => this._removeMessage(request.id))
);
// We also need to update any outgoing pending requests and set them to denied.
// when we get an incoming friend request.
const outgoing = await this.getPendingFriendRequests('outgoing');
await Promise.all(
outgoing.map(async request => {
if (request.hasErrors()) return;
request.set({ friendStatus: 'declined' });
await window.Signal.Data.saveMessage(request.attributes, {
Message: Whisper.Message,
});
this.trigger('updateMessage', request);
})
);
// Trigger an update if we removed or updated messages
if (outgoing.length > 0 || incoming.length > 0)
this.trigger('change');
}
return this.updateLastMessage();
},
async updateLastMessage() { async updateLastMessage() {
if (!this.id) { if (!this.id) {
return; return;
@ -1248,20 +1294,6 @@
this.changed = {}; this.changed = {};
this.set(lastMessageUpdate); this.set(lastMessageUpdate);
// If we need to add new incoming friend requests
// Then we need to make sure we remove any pending requests that we may have
// This is to ensure that one user cannot spam us with multiple friend requests
if (lastMessage.isFriendRequest() && lastMessage.direction === 'incoming') {
const requests = await this.getPendingFriendRequests('incoming');
// Delete the old message if it's pending
await Promise.all(requests.map(request => this._removeMessage(request.id)));
// Trigger an update if we removed messages
hasChanged = hasChanged || (requests.length > 0);
}
if (this.hasChanged()) { if (this.hasChanged()) {
await window.Signal.Data.updateConversation(this.id, this.attributes, { await window.Signal.Data.updateConversation(this.id, this.attributes, {
Conversation: Whisper.Conversation, Conversation: Whisper.Conversation,

@ -1227,16 +1227,8 @@
hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments, hasVisualMediaAttachments: dataMessage.hasVisualMediaAttachments,
quote: dataMessage.quote, quote: dataMessage.quote,
schemaVersion: dataMessage.schemaVersion, schemaVersion: dataMessage.schemaVersion,
preKeyBundle: dataMessage.preKeyBundle || null,
}); });
if (type === 'friend-request') {
message.set({
friendStatus: dataMessage.friendStatus,
direction: dataMessage.direction,
});
}
if (type === 'outgoing') { if (type === 'outgoing') {
const receipts = Whisper.DeliveryReceipts.forMessage( const receipts = Whisper.DeliveryReceipts.forMessage(
conversation, conversation,

@ -70,10 +70,14 @@
template: $('#conversation').html(), template: $('#conversation').html(),
render_attributes() { render_attributes() {
let sendMessagePlaceholder = 'sendMessageFriendRequest'; let sendMessagePlaceholder = 'sendMessageFriendRequest';
if (this.model.isFriend()) { const sendDisabled = this.model.get('blockInput');
if (sendDisabled) {
sendMessagePlaceholder = 'sendMessageDisabled';
} else if (this.model.isFriend()) {
sendMessagePlaceholder = 'sendMessage'; sendMessagePlaceholder = 'sendMessage';
} }
return { return {
'disable-inputs': sendDisabled,
'send-message': i18n(sendMessagePlaceholder), 'send-message': i18n(sendMessagePlaceholder),
'android-length-warning': i18n('androidMessageLengthWarning'), 'android-length-warning': i18n('androidMessageLengthWarning'),
}; };
@ -140,6 +144,8 @@
this.render(); this.render();
this.model.updateTextInputState();
this.loadingScreen = new Whisper.ConversationLoadingScreen(); this.loadingScreen = new Whisper.ConversationLoadingScreen();
this.loadingScreen.render(); this.loadingScreen.render();
this.loadingScreen.$el.prependTo(this.$('.discussion-container')); this.loadingScreen.$el.prependTo(this.$('.discussion-container'));

Loading…
Cancel
Save