diff --git a/src/addon/messages/providers/user-add-contact-handler.ts b/src/addon/messages/providers/user-add-contact-handler.ts index 4fdc8250b..903d2b2e2 100644 --- a/src/addon/messages/providers/user-add-contact-handler.ts +++ b/src/addon/messages/providers/user-add-contact-handler.ts @@ -95,14 +95,14 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle this.messagesProvider.isContact(user.id).then((isContact) => { if (isContact) { - const template = this.translate.instant('addon.messages.removecontactconfirm'), - title = this.translate.instant('addon.messages.removecontact'); + const message = this.translate.instant('addon.messages.removecontactconfirm', {$a: user.fullname}); + const okText = this.translate.instant('core.remove'); - return this.domUtils.showConfirm(template, title, title).then(() => { + return this.domUtils.showConfirm(message, undefined, okText).then(() => { return this.messagesProvider.removeContact(user.id); }); } else { - return this.messagesProvider.addContact(user.id); + return this.addContact(user); } }).catch((error) => { this.domUtils.showErrorModalDefault(error, 'core.error', true); @@ -125,10 +125,12 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle protected checkButton(userId: number): Promise { this.updateButton(userId, {spinner: true}); + const groupMessagingEnabled = this.messagesProvider.isGroupMessagingEnabled(); + return this.messagesProvider.isContact(userId).then((isContact) => { if (isContact) { this.updateButton(userId, { - title: 'addon.messages.removecontact', + title: groupMessagingEnabled ? 'addon.messages.removefromyourcontacts' : 'addon.messages.removecontact', class: 'addon-messages-removecontact-handler', icon: 'remove', hidden: false, @@ -136,7 +138,7 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle }); } else { this.updateButton(userId, { - title: 'addon.messages.addcontact', + title: groupMessagingEnabled ? 'addon.messages.addtoyourcontacts' : 'addon.messages.addcontact', class: 'addon-messages-addcontact-handler', icon: 'add', hidden: false, @@ -160,6 +162,42 @@ export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandle this.eventsProvider.trigger(CoreUserDelegate.UPDATE_HANDLER_EVENT, { handler: this.name, data: data, userId: userId }); } + /** + * Add a contact or send a contact request if group messaging is enabled. + * + * @param {any} user User to add as contact. + * @return {Promise} Promise resolved when done. + */ + protected addContact(user: any): Promise { + if (!this.messagesProvider.isGroupMessagingEnabled()) { + return this.messagesProvider.addContact(user.id); + } + + return this.messagesProvider.getMemberInfo(user.id).then((member) => { + const currentUserId = this.sitesProvider.getCurrentSiteUserId(); + const requestSent = member.contactrequests.some((request) => { + return request.userid == currentUserId && request.requesteduserid == user.id; + }); + + if (requestSent) { + const message = this.translate.instant('addon.messages.yourcontactrequestpending', {$a: user.fullname}); + + return this.domUtils.showAlert(null, message); + } + + const message = this.translate.instant('addon.messages.addcontactconfirm', {$a: user.fullname}); + const okText = this.translate.instant('core.add'); + + return this.domUtils.showConfirm(message, undefined, okText).then(() => { + return this.messagesProvider.createContactRequest(user.id); + }).then(() => { + const message = this.translate.instant('addon.messages.contactrequestsent'); + + return this.domUtils.showAlert(null, message); + }); + }); + } + /** * Destroyed method. */