Merge pull request #41 from Mikunj/fix/friend-notification

Friend Notifications
pull/44/head
sachaaaaa 7 years ago committed by GitHub
commit 78d39ac177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1589,5 +1589,35 @@
"friendRequestDeclined": { "friendRequestDeclined": {
"message": "Friend request declined", "message": "Friend request declined",
"description": "Shown in the conversation history when the user declines a friend request" "description": "Shown in the conversation history when the user declines a friend request"
},
"friendRequestNotificationTitle": {
"message": "Friend request",
"description": "Shown in a notification title when receiving a friend request"
},
"friendRequestNotificationMessage": {
"message": "$name$ sent you a friend request",
"description":
"Shown in a notification body when receiving a friend request",
"placeholders": {
"name": {
"content": "$1",
"example": "Bob"
}
}
},
"friendRequestAcceptedNotificationTitle": {
"message": "Friend request accepted",
"description": "Shown in a notification title when friend request was accepted by the other user"
},
"friendRequestAcceptedNotificationMessage": {
"message": "$name$ accepted your friend request",
"description":
"Shown in a notification body when friend request was accepted by the other user",
"placeholders": {
"name": {
"content": "$1",
"example": "Bob"
}
}
} }
} }

@ -467,10 +467,13 @@
} }
} }
}, },
async onFriendRequestAccepted() { async onFriendRequestAccepted({ updateUnread }) {
// Make sure we don't keep incrementing the unread count
const unreadCount = this.isKeyExchangeCompleted() || !updateUnread ? {} : { unreadCount: this.get('unreadCount') + 1 };
this.set({ this.set({
friendRequestStatus: null, friendRequestStatus: null,
keyExchangeCompleted: true, keyExchangeCompleted: true,
...unreadCount,
}); });
await window.Signal.Data.updateConversation(this.id, this.attributes, { await window.Signal.Data.updateConversation(this.id, this.attributes, {
@ -482,18 +485,21 @@
// Update any pending outgoing messages // Update any pending outgoing messages
const pending = await this.getPendingFriendRequests('outgoing'); const pending = await this.getPendingFriendRequests('outgoing');
for (const request of pending) { await Promise.all(
// Only update successfully sent requests pending.map(async request => {
if (request.hasErrors()) continue; if (request.hasErrors()) return;
request.set({ friendStatus: 'accepted' }); request.set({ friendStatus: 'accepted' });
await window.Signal.Data.saveMessage(request.attributes, { await window.Signal.Data.saveMessage(request.attributes, {
Message: Whisper.Message, Message: Whisper.Message,
}); });
this.trigger('updateMessage', request); this.trigger('updateMessage', request);
} })
);
await this.updatePendingFriendRequests(); await this.updatePendingFriendRequests();
this.notifyFriendRequest(this.id, 'accepted')
}, },
async onFriendRequestTimedOut() { async onFriendRequestTimedOut() {
this.updateTextInputState(); this.updateTextInputState();
@ -720,6 +726,7 @@
this.set({ this.set({
active_at: Date.now(), active_at: Date.now(),
timestamp: Date.now(), timestamp: Date.now(),
unreadCount: this.get('unreadCount') + 1,
}); });
await window.Signal.Data.updateConversation(this.id, this.attributes, { await window.Signal.Data.updateConversation(this.id, this.attributes, {
@ -763,13 +770,13 @@
Message: Whisper.Message, Message: Whisper.Message,
}); });
this.trigger( const whisperMessage = new Whisper.Message({
'newmessage', ...message,
new Whisper.Message({ id,
...message, });
id,
}) this.trigger('newmessage', whisperMessage);
); this.notify(whisperMessage);
}, },
async addVerifiedChange(verifiedChangeId, verified, providedOptions) { async addVerifiedChange(verifiedChangeId, verified, providedOptions) {
const options = providedOptions || {}; const options = providedOptions || {};
@ -2061,6 +2068,8 @@
notify(message) { notify(message) {
if (!message.isIncoming()) { if (!message.isIncoming()) {
if (message.isFriendRequest())
return this.notifyFriendRequest(message.get('source'), 'requested');
return Promise.resolve(); return Promise.resolve();
} }
const conversationId = this.id; const conversationId = this.id;
@ -2092,6 +2101,44 @@
}) })
); );
}, },
// Notification for friend request received
async notifyFriendRequest(source, type) {
// Data validation
if (!source) return Promise.reject('Invalid source');
if (!['accepted', 'requested'].includes(type)) return Promise.reject('Type must be accepted or requested.');
// Call the notification on the right conversation
let conversation = this;
if (conversation.id !== source) {
try {
conversation = await ConversationController.getOrCreateAndWait(
source,
'private'
);
window.log.info(`Notify called on a different conversation. expected: ${this.id}. actual: ${conversation.id}`);
} catch (e) {
return Promise.reject('Failed to fetch conversation');
}
}
const isTypeAccepted = type === 'accepted';
const title = isTypeAccepted ? 'friendRequestAcceptedNotificationTitle' : 'friendRequestNotificationTitle';
const message = isTypeAccepted ? 'friendRequestAcceptedNotificationMessage' : 'friendRequestNotificationMessage';
conversation.getNotificationIcon().then(iconUrl => {
window.log.info('Add notification for friend request updated', {
conversationId: conversation.idForLogging(),
});
Whisper.Notifications.add({
conversationId: conversation.id,
iconUrl,
isExpiringMessage: false,
message: i18n(message, conversation.getTitle()),
messageSentAt: Date.now(),
title: i18n(title),
});
});
},
}); });
Whisper.ConversationCollection = Backbone.Collection.extend({ Whisper.ConversationCollection = Backbone.Collection.extend({

@ -193,6 +193,8 @@
getNotificationText() { getNotificationText() {
const description = this.getDescription(); const description = this.getDescription();
if (description) { if (description) {
if (this.isFriendRequest())
return `Friend Request: ${description}`;
return description; return description;
} }
if (this.get('attachments').length > 0) { if (this.get('attachments').length > 0) {

@ -1030,11 +1030,11 @@ MessageReceiver.prototype.extend({
if (savePreKey) { if (savePreKey) {
await this.handlePreKeyBundleMessage( await this.handlePreKeyBundleMessage(
envelope.source, envelope.source,
this.decodePreKeyBundleMessage(content.preKeyBundleMessage), this.decodePreKeyBundleMessage(content.preKeyBundleMessage)
); );
// Update the conversation // Update the conversation
await conversation.onFriendRequestAccepted(); await conversation.onFriendRequestAccepted({ updateUnread: true });
} }
} }
} }

Loading…
Cancel
Save