MOBILE-2620 messages: Contact requests in user profile handler

main
Albert Gasset 2018-12-07 13:30:05 +01:00
parent 43cbd655d8
commit 1cefb4ae04
1 changed files with 44 additions and 6 deletions

View File

@ -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<void> {
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<any>} Promise resolved when done.
*/
protected addContact(user: any): Promise<any> {
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.
*/