Added function to help friend request notifications

pull/41/head
Mikunj 6 years ago
parent dfae580ffa
commit 061b8ab2cb

@ -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"
}
}
}
}

@ -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({

Loading…
Cancel
Save