From 061b8ab2cb02a144b728406c7ed00743ebba1c61 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 15 Nov 2018 11:46:38 +1100 Subject: [PATCH] Added function to help friend request notifications --- _locales/en/messages.json | 30 ++++++++++++++++++++ js/models/conversations.js | 56 ++++++++++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index c3bf9d532..fe19c3d27 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1589,5 +1589,35 @@ "friendRequestDeclined": { "message": "Friend request declined", "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" + } + } } } diff --git a/js/models/conversations.js b/js/models/conversations.js index ee8382a99..6d36b8eec 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -487,20 +487,8 @@ } await this.updatePendingFriendRequests(); - - this.getNotificationIcon().then(iconUrl => { - window.log.info('Add notification for friend request updated', { - conversationId: this.idForLogging(), - }); - Whisper.Notifications.add({ - conversationId: this.id, - iconUrl, - isExpiringMessage: false, - message: `Accepted your friend request`, - messageSentAt: Date.now(), - title: this.getTitle(), - }); - }); + + this.notifyFriendRequest(this.id, 'accepted') }, async onFriendRequestTimedOut() { this.updateTextInputState(); @@ -2041,7 +2029,8 @@ }, notify(message) { - if (!(message.isIncoming() || message.isFriendRequest())) { + if (!message.isIncoming()) { + if (message.isFriendRequest()) return this.notifyFriendRequest(message.get('source'), 'requested'); return Promise.resolve(); } const conversationId = this.id; @@ -2073,6 +2062,43 @@ }) ); }, + // 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' + ); + } 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({