MOBILE-2327 messages: Add and block user profile handlers
parent
dd49b78fb8
commit
06b8bcc026
|
@ -1,4 +1,7 @@
|
|||
{
|
||||
"addcontact": "Add contact",
|
||||
"blockcontact": "Block contact",
|
||||
"blockcontactconfirm": "You will no longer receive messages from this contact.",
|
||||
"blocknoncontacts": "Prevent non-contacts from messaging me",
|
||||
"contactlistempty": "The contact list is empty",
|
||||
"contactname": "Contact name",
|
||||
|
@ -6,6 +9,7 @@
|
|||
"deletemessage": "Delete message",
|
||||
"deletemessageconfirmation": "Are you sure you want to delete this message? It will only be deleted from your messaging history and will still be viewable by the user who sent or received the message.",
|
||||
"errordeletemessage": "Error while deleting the message.",
|
||||
"errorwhileretrievingcontacts": "Error while retrieving contacts from the server.",
|
||||
"errorwhileretrievingdiscussions": "Error while retrieving discussions from the server.",
|
||||
"errorwhileretrievingmessages": "Error while retrieving messages from the server.",
|
||||
"messagenotsent": "The message was not sent. Please try again later.",
|
||||
|
@ -16,10 +20,13 @@
|
|||
"newmessages": "New messages",
|
||||
"nomessages": "No messages",
|
||||
"nousersfound": "No users found",
|
||||
"removecontact": "Remove contact",
|
||||
"removecontactconfirm": "Contact will be removed from your contacts list.",
|
||||
"type_blocked": "Blocked",
|
||||
"type_offline": "Offline",
|
||||
"type_online": "Online",
|
||||
"type_search": "Search results",
|
||||
"type_strangers": "Others",
|
||||
"unblockcontact": "Unblock contact",
|
||||
"warningmessagenotsent": "Couldn't send message(s) to user {{user}}. {{error}}"
|
||||
}
|
|
@ -23,6 +23,8 @@ import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'
|
|||
import { CoreUserDelegate } from '@core/user/providers/user-delegate';
|
||||
import { CoreCronDelegate } from '@providers/cron';
|
||||
import { AddonMessagesSendMessageUserHandler } from './providers/user-send-message-handler';
|
||||
import { AddonMessagesAddContactUserHandler } from './providers/user-add-contact-handler';
|
||||
import { AddonMessagesBlockContactUserHandler } from './providers/user-block-contact-handler';
|
||||
import { AddonMessagesDiscussionLinkHandler } from './providers/discussion-link-handler';
|
||||
import { AddonMessagesIndexLinkHandler } from './providers/index-link-handler';
|
||||
import { AddonMessagesSyncCronHandler } from './providers/sync-cron-handler';
|
||||
|
@ -47,6 +49,8 @@ import { CoreUtilsProvider } from '@providers/utils/utils';
|
|||
AddonMessagesSyncProvider,
|
||||
AddonMessagesMainMenuHandler,
|
||||
AddonMessagesSendMessageUserHandler,
|
||||
AddonMessagesAddContactUserHandler,
|
||||
AddonMessagesBlockContactUserHandler,
|
||||
AddonMessagesDiscussionLinkHandler,
|
||||
AddonMessagesIndexLinkHandler,
|
||||
AddonMessagesSyncCronHandler,
|
||||
|
@ -62,12 +66,15 @@ export class AddonMessagesModule {
|
|||
localNotifications: CoreLocalNotificationsProvider, messagesProvider: AddonMessagesProvider,
|
||||
sitesProvider: CoreSitesProvider, linkHelper: CoreContentLinksHelperProvider,
|
||||
settingsHandler: AddonMessagesSettingsHandler, settingsDelegate: CoreSettingsDelegate,
|
||||
pushNotificationsDelegate: AddonPushNotificationsDelegate, utils: CoreUtilsProvider) {
|
||||
pushNotificationsDelegate: AddonPushNotificationsDelegate, utils: CoreUtilsProvider,
|
||||
addContactHandler: AddonMessagesAddContactUserHandler, blockContactHandler: AddonMessagesBlockContactUserHandler) {
|
||||
// Register handlers.
|
||||
mainMenuDelegate.registerHandler(mainmenuHandler);
|
||||
contentLinksDelegate.registerHandler(indexLinkHandler);
|
||||
contentLinksDelegate.registerHandler(discussionLinkHandler);
|
||||
userDelegate.registerHandler(sendMessageHandler);
|
||||
userDelegate.registerHandler(addContactHandler);
|
||||
userDelegate.registerHandler(blockContactHandler);
|
||||
cronDelegate.register(syncHandler);
|
||||
cronDelegate.register(mainmenuHandler);
|
||||
settingsDelegate.registerHandler(settingsHandler);
|
||||
|
|
|
@ -560,7 +560,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy {
|
|||
// Wait for new content height to be calculated.
|
||||
setTimeout(() => {
|
||||
// Visible content size changed, maintain the bottom position.
|
||||
if (this.content.contentHeight != this.oldContentHeight) {
|
||||
if (this.content && this.content.contentHeight != this.oldContentHeight) {
|
||||
if (!top) {
|
||||
top = this.content.getContentDimensions().scrollTop;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ import { CoreTimeUtilsProvider } from '@providers/utils/time';
|
|||
export class AddonMessagesProvider {
|
||||
protected ROOT_CACHE_KEY = 'mmaMessages:';
|
||||
protected LIMIT_MESSAGES = 50;
|
||||
protected LIMIT_SEARCH_MESSAGES = 50;
|
||||
static NEW_MESSAGE_EVENT = 'new_message_event';
|
||||
static READ_CHANGED_EVENT = 'read_changed_event';
|
||||
static READ_CRON_EVENT = 'read_cron_event';
|
||||
|
@ -43,6 +44,44 @@ export class AddonMessagesProvider {
|
|||
this.logger = logger.getInstance('AddonMessagesProvider');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a contact.
|
||||
*
|
||||
* @param {number} userId User ID of the person to add.
|
||||
* @param {string} [siteId] Site ID. If not defined, use current site.
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
addContact(userId: number, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const params = {
|
||||
userids: [ userId ]
|
||||
};
|
||||
|
||||
return site.write('core_message_create_contacts', params).then(() => {
|
||||
return this.invalidateAllContactsCache(site.getUserId(), site.getId());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Block a contact.
|
||||
*
|
||||
* @param {number} userId User ID of the person to block.
|
||||
* @param {string} [siteId] Site ID. If not defined, use current site.
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
blockContact(userId: number, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const params = {
|
||||
userids: [ userId ]
|
||||
};
|
||||
|
||||
return site.write('core_message_block_contacts', params).then(() => {
|
||||
return this.invalidateAllContactsCache(site.getUserId(), site.getId());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a message (online or offline).
|
||||
*
|
||||
|
@ -679,6 +718,46 @@ export class AddonMessagesProvider {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the a user is blocked by the current user.
|
||||
*
|
||||
* @param {number} userId The user ID to check against.
|
||||
* @param {string} [siteId] Site ID. If not defined, use current site.
|
||||
* @return {Promise<boolean>} Resolved with boolean, rejected when we do not know.
|
||||
*/
|
||||
isBlocked(userId: number, siteId?: string): Promise<boolean> {
|
||||
return this.getBlockedContacts(siteId).then((blockedContacts) => {
|
||||
if (!blockedContacts.users || blockedContacts.users.length < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return blockedContacts.users.some((user) => {
|
||||
return userId == user.id;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the a user is a contact of the current user.
|
||||
*
|
||||
* @param {number} userId The user ID to check against.
|
||||
* @param {string} [siteId] Site ID. If not defined, use current site.
|
||||
* @return {Promise<boolean>} Resolved with boolean, rejected when we do not know.
|
||||
*/
|
||||
isContact(userId: number, siteId?: string): Promise<boolean> {
|
||||
return this.getContacts(siteId).then((contacts) => {
|
||||
return ['online', 'offline'].some((type) => {
|
||||
if (contacts[type] && contacts[type].length > 0) {
|
||||
return contacts[type].some((user) => {
|
||||
return userId == user.id;
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not we can mark all messages as read.
|
||||
*
|
||||
|
@ -782,6 +861,28 @@ export class AddonMessagesProvider {
|
|||
return this.sitesProvider.getCurrentSite().write('core_message_mark_all_messages_as_read', params, preSets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a contact.
|
||||
*
|
||||
* @param {number} userId User ID of the person to remove.
|
||||
* @param {string} [siteId] Site ID. If not defined, use current site.
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
removeContact(userId: number, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const params = {
|
||||
userids: [ userId ]
|
||||
},
|
||||
preSets = {
|
||||
responseExpected: false
|
||||
};
|
||||
|
||||
return site.write('core_message_delete_contacts', params, preSets).then(() => {
|
||||
return this.invalidateContactsCache(site.getId());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for contacts.
|
||||
*
|
||||
|
@ -821,11 +922,11 @@ export class AddonMessagesProvider {
|
|||
* @param {string} query The query string
|
||||
* @param {number} [userId] The user ID. If not defined, current user.
|
||||
* @param {number} [from=0] Position of the first result to get. Defaults to 0.
|
||||
* @param {number} [limit] Number of results to get. Defaults to LIMIT_MESSAGES.
|
||||
* @param {number} [limit] Number of results to get. Defaults to LIMIT_SEARCH_MESSAGES.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved with the results.
|
||||
*/
|
||||
searchMessages(query: string, userId?: number, from: number = 0, limit: number = this.LIMIT_MESSAGES, siteId?: string):
|
||||
searchMessages(query: string, userId?: number, from: number = 0, limit: number = this.LIMIT_SEARCH_MESSAGES, siteId?: string):
|
||||
Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const param = {
|
||||
|
@ -1041,4 +1142,26 @@ export class AddonMessagesProvider {
|
|||
}
|
||||
this.userProvider.storeUsers(users, siteId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unblock a user.
|
||||
*
|
||||
* @param {number} userId User ID of the person to unblock.
|
||||
* @param {string} [siteId] Site ID. If not defined, use current site.
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
unblockContact(userId: number, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const params = {
|
||||
userids: [ userId ]
|
||||
},
|
||||
preSets = {
|
||||
responseExpected: false
|
||||
};
|
||||
|
||||
return site.write('core_message_unblock_contacts', params, preSets).then(() => {
|
||||
return this.invalidateAllContactsCache(site.getUserId(), site.getId());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,170 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Injectable, OnDestroy } from '@angular/core';
|
||||
import { CoreUserDelegate, CoreUserProfileHandler, CoreUserProfileHandlerData } from '@core/user/providers/user-delegate';
|
||||
import { CoreSitesProvider } from '@providers/sites';
|
||||
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
|
||||
import { AddonMessagesProvider } from './messages';
|
||||
import { AddonMessagesBlockContactUserHandler } from './user-block-contact-handler';
|
||||
import { CoreEventsProvider } from '@providers/events';
|
||||
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
|
||||
/**
|
||||
* Profile add/remove contact handler.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AddonMessagesAddContactUserHandler implements CoreUserProfileHandler, OnDestroy {
|
||||
/**
|
||||
* Update handler information event.
|
||||
* @type {string}
|
||||
*/
|
||||
static UPDATED_EVENT = 'AddonMessagesAddContactUserHandler_updated_event';
|
||||
|
||||
name = 'mmaMessages:blockContact';
|
||||
priority = 600;
|
||||
type = CoreUserDelegate.TYPE_ACTION;
|
||||
|
||||
protected disabled = false;
|
||||
protected updateObs: any;
|
||||
|
||||
constructor(private linkHelper: CoreContentLinksHelperProvider, protected sitesProvider: CoreSitesProvider,
|
||||
private messagesProvider: AddonMessagesProvider, protected eventsProvider: CoreEventsProvider,
|
||||
private domUtils: CoreDomUtilsProvider, private translate: TranslateService) {
|
||||
|
||||
this.updateObs = eventsProvider.on(AddonMessagesBlockContactUserHandler.UPDATED_EVENT, (data) => {
|
||||
this.checkButton(data.userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if handler is enabled.
|
||||
*
|
||||
* @return {Promise<any>} Promise resolved with true if enabled, rejected or resolved with false otherwise.
|
||||
*/
|
||||
isEnabled(): Promise<any> {
|
||||
return this.messagesProvider.isPluginEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if handler is enabled for this user in this context.
|
||||
*
|
||||
* @param {any} user User to check.
|
||||
* @param {number} courseId Course ID.
|
||||
* @param {any} [navOptions] Course navigation options for current user. See $mmCourses#getUserNavigationOptions.
|
||||
* @param {any} [admOptions] Course admin options for current user. See $mmCourses#getUserAdministrationOptions.
|
||||
* @return {boolean|Promise<boolean>} Promise resolved with true if enabled, resolved with false otherwise.
|
||||
*/
|
||||
isEnabledForUser(user: any, courseId: number, navOptions?: any, admOptions?: any): boolean | Promise<boolean> {
|
||||
return user.id != this.sitesProvider.getCurrentSiteUserId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data needed to render the handler.
|
||||
*
|
||||
* @return {CoreUserProfileHandlerData} Data needed to render the handler.
|
||||
*/
|
||||
getDisplayData(user: any, courseId: number): CoreUserProfileHandlerData {
|
||||
this.checkButton(user.id);
|
||||
|
||||
return {
|
||||
icon: '',
|
||||
title: '',
|
||||
spinner: false,
|
||||
class: '',
|
||||
action: (event, navCtrl, user, courseId): void => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (this.disabled) {
|
||||
return;
|
||||
}
|
||||
this.disabled = true;
|
||||
this.updateButton({spinner: true});
|
||||
|
||||
this.messagesProvider.isContact(user.id).then((isContact) => {
|
||||
if (isContact) {
|
||||
const template = this.translate.instant('addon.messages.removecontactconfirm'),
|
||||
title = this.translate.instant('addon.messages.removecontact');
|
||||
|
||||
return this.domUtils.showConfirm(template, title, title).then(() => {
|
||||
return this.messagesProvider.removeContact(user.id);
|
||||
}).catch(() => {
|
||||
// Ignore on cancel.
|
||||
});
|
||||
} else {
|
||||
return this.messagesProvider.addContact(user.id);
|
||||
}
|
||||
}).catch((error) => {
|
||||
this.domUtils.showErrorModal(error);
|
||||
}).finally(() => {
|
||||
this.eventsProvider.trigger(AddonMessagesAddContactUserHandler.UPDATED_EVENT, {userId: user.id});
|
||||
this.checkButton(user.id).finally(() => {
|
||||
this.disabled = false;
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Button with avalaible data.
|
||||
* @param {number} userId User Id to update.
|
||||
* @return {Promise<void>} Promise resolved when done.
|
||||
*/
|
||||
protected checkButton(userId: number): Promise<void> {
|
||||
this.updateButton({spinner: true});
|
||||
|
||||
return this.messagesProvider.isContact(userId).then((isContact) => {
|
||||
if (isContact) {
|
||||
this.updateButton({
|
||||
title: 'addon.messages.removecontact',
|
||||
class: 'mma-messages-removecontact-handler',
|
||||
icon: 'remove',
|
||||
hidden: false,
|
||||
spinner: false
|
||||
});
|
||||
} else {
|
||||
this.updateButton({
|
||||
title: 'addon.messages.addcontact',
|
||||
class: 'mma-messages-addcontact-handler',
|
||||
icon: 'add',
|
||||
hidden: false,
|
||||
spinner: false
|
||||
});
|
||||
}
|
||||
}).catch(() => {
|
||||
// This fails for some reason, let's just hide the button.
|
||||
this.updateButton({hidden: true});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers the event to update the handler information.
|
||||
* @param {any} data Data that should be updated.
|
||||
*/
|
||||
protected updateButton(data: any): void {
|
||||
// This fails for some reason, let's just hide the button.
|
||||
this.eventsProvider.trigger(CoreUserDelegate.UPDATE_HANDLER_EVENT, { handler: this.name, data: data });
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroyed method.
|
||||
*/
|
||||
ngOnDestroy(): void {
|
||||
this.updateObs && this.updateObs.off();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Injectable, OnDestroy } from '@angular/core';
|
||||
import { CoreUserDelegate, CoreUserProfileHandler, CoreUserProfileHandlerData } from '@core/user/providers/user-delegate';
|
||||
import { CoreSitesProvider } from '@providers/sites';
|
||||
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
|
||||
import { AddonMessagesProvider } from './messages';
|
||||
import { AddonMessagesAddContactUserHandler } from './user-add-contact-handler';
|
||||
import { CoreEventsProvider } from '@providers/events';
|
||||
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
|
||||
/**
|
||||
* Profile block/unblock contact handler.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AddonMessagesBlockContactUserHandler implements CoreUserProfileHandler, OnDestroy {
|
||||
/**
|
||||
* Update handler information event.
|
||||
* @type {string}
|
||||
*/
|
||||
static UPDATED_EVENT = 'AddonMessagesBlockContactUserHandler_updated_event';
|
||||
|
||||
name = 'mmaMessages:addContact';
|
||||
priority = 800;
|
||||
type = CoreUserDelegate.TYPE_ACTION;
|
||||
|
||||
protected disabled = false;
|
||||
protected updateObs: any;
|
||||
|
||||
constructor(private linkHelper: CoreContentLinksHelperProvider, protected sitesProvider: CoreSitesProvider,
|
||||
private messagesProvider: AddonMessagesProvider, protected eventsProvider: CoreEventsProvider,
|
||||
private domUtils: CoreDomUtilsProvider, private translate: TranslateService) {
|
||||
|
||||
this.updateObs = eventsProvider.on(AddonMessagesAddContactUserHandler.UPDATED_EVENT, (data) => {
|
||||
this.checkButton(data.userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if handler is enabled.
|
||||
*
|
||||
* @return {Promise<any>} Promise resolved with true if enabled, rejected or resolved with false otherwise.
|
||||
*/
|
||||
isEnabled(): Promise<any> {
|
||||
return this.messagesProvider.isPluginEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if handler is enabled for this user in this context.
|
||||
*
|
||||
* @param {any} user User to check.
|
||||
* @param {number} courseId Course ID.
|
||||
* @param {any} [navOptions] Course navigation options for current user. See $mmCourses#getUserNavigationOptions.
|
||||
* @param {any} [admOptions] Course admin options for current user. See $mmCourses#getUserAdministrationOptions.
|
||||
* @return {boolean|Promise<boolean>} Promise resolved with true if enabled, resolved with false otherwise.
|
||||
*/
|
||||
isEnabledForUser(user: any, courseId: number, navOptions?: any, admOptions?: any): boolean | Promise<boolean> {
|
||||
return user.id != this.sitesProvider.getCurrentSiteUserId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data needed to render the handler.
|
||||
*
|
||||
* @return {CoreUserProfileHandlerData} Data needed to render the handler.
|
||||
*/
|
||||
getDisplayData(user: any, courseId: number): CoreUserProfileHandlerData {
|
||||
|
||||
this.checkButton(user.id);
|
||||
|
||||
return {
|
||||
icon: '',
|
||||
title: '',
|
||||
spinner: false,
|
||||
class: '',
|
||||
action: (event, navCtrl, user, courseId): void => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (this.disabled) {
|
||||
return;
|
||||
}
|
||||
this.disabled = true;
|
||||
this.updateButton({spinner: true});
|
||||
|
||||
this.messagesProvider.isBlocked(user.id).then((isBlocked) => {
|
||||
if (isBlocked) {
|
||||
return this.messagesProvider.unblockContact(user.id);
|
||||
} else {
|
||||
const template = this.translate.instant('addon.messages.blockcontactconfirm'),
|
||||
title = this.translate.instant('addon.messages.blockcontact');
|
||||
|
||||
return this.domUtils.showConfirm(template, title, title).then(() => {
|
||||
return this.messagesProvider.blockContact(user.id);
|
||||
}).catch(() => {
|
||||
// Ignore on cancel.
|
||||
});
|
||||
}
|
||||
}).catch((error) => {
|
||||
this.domUtils.showErrorModal(error);
|
||||
}).finally(() => {
|
||||
this.eventsProvider.trigger(AddonMessagesBlockContactUserHandler.UPDATED_EVENT, {userId: user.id});
|
||||
this.checkButton(user.id).finally(() => {
|
||||
this.disabled = false;
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Button with avalaible data.
|
||||
* @param {number} userId User Id to update.
|
||||
* @return {Promise<void>} Promise resolved when done.
|
||||
*/
|
||||
protected checkButton(userId: number): Promise<void> {
|
||||
this.updateButton({spinner: true});
|
||||
|
||||
return this.messagesProvider.isBlocked(userId).then((isBlocked) => {
|
||||
if (isBlocked) {
|
||||
this.updateButton({
|
||||
title: 'addon.messages.unblockcontact',
|
||||
class: 'mma-messages-unblockcontact-handler',
|
||||
icon: 'checkmark-circle',
|
||||
hidden: false,
|
||||
spinner: false
|
||||
});
|
||||
} else {
|
||||
this.updateButton({
|
||||
title: 'addon.messages.blockcontact',
|
||||
class: 'mma-messages-blockcontact-handler',
|
||||
icon: 'close-circle',
|
||||
hidden: false,
|
||||
spinner: false
|
||||
});
|
||||
}
|
||||
}).catch(() => {
|
||||
// This fails for some reason, let's just hide the button.
|
||||
this.updateButton({hidden: true});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers the event to update the handler information.
|
||||
* @param {any} data Data that should be updated.
|
||||
*/
|
||||
protected updateButton(data: any): void {
|
||||
// This fails for some reason, let's just hide the button.
|
||||
this.eventsProvider.trigger(CoreUserDelegate.UPDATE_HANDLER_EVENT, { handler: this.name, data: data });
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroyed method.
|
||||
*/
|
||||
ngOnDestroy(): void {
|
||||
this.updateObs && this.updateObs.off();
|
||||
}
|
||||
}
|
|
@ -52,10 +52,10 @@
|
|||
</a>
|
||||
|
||||
<ion-item *ngIf="actionHandlers && actionHandlers.length">
|
||||
<button *ngFor="let actHandler of actionHandlers" ion-button block outline [ngClass]="['core-user-profile-handler', actHandler.class]" (click)="handlerClicked($event, actHandler)" [hidden]="actHandler.hidden" title="{{ actHandler.title | translate }}">
|
||||
<ion-icon *ngIf="actHandler.icon" [name]="actHandler.icon" item-start></ion-icon>
|
||||
{{ actHandler.title | translate }}
|
||||
<ion-spinner *ngIf="actHandler.spinner" item-end></ion-spinner>
|
||||
<button *ngFor="let actHandler of actionHandlers" ion-button block outline [ngClass]="['core-user-profile-handler', actHandler.class]" (click)="handlerClicked($event, actHandler)" [hidden]="actHandler.hidden" title="{{ actHandler.title | translate }}" icon-start>
|
||||
<ion-icon *ngIf="!actHandler.spinner && actHandler.icon" [name]="actHandler.icon" start></ion-icon>
|
||||
<span *ngIf="!actHandler.spinner">{{ actHandler.title | translate }}</span>
|
||||
<ion-spinner *ngIf="actHandler.spinner"></ion-spinner>
|
||||
</button>
|
||||
</ion-item>
|
||||
</ion-list>
|
||||
|
|
|
@ -39,6 +39,7 @@ export class CoreUserProfilePage {
|
|||
protected userId: number;
|
||||
protected site;
|
||||
protected obsProfileRefreshed: any;
|
||||
protected subscription;
|
||||
|
||||
userLoaded = false;
|
||||
isLoadingHandlers = false;
|
||||
|
@ -102,9 +103,7 @@ export class CoreUserProfilePage {
|
|||
this.user = user;
|
||||
this.title = user.fullname;
|
||||
|
||||
this.isLoadingHandlers = true;
|
||||
|
||||
this.userDelegate.getProfileHandlersFor(user, this.courseId).then((handlers) => {
|
||||
this.subscription = this.userDelegate.getProfileHandlersFor(user, this.courseId).subscribe((handlers) => {
|
||||
this.actionHandlers = [];
|
||||
this.newPageHandlers = [];
|
||||
this.communicationHandlers = [];
|
||||
|
@ -122,8 +121,8 @@ export class CoreUserProfilePage {
|
|||
break;
|
||||
}
|
||||
});
|
||||
}).finally(() => {
|
||||
this.isLoadingHandlers = false;
|
||||
|
||||
this.isLoadingHandlers = !this.userDelegate.areHandlersLoaded();
|
||||
});
|
||||
|
||||
}).catch((error) => {
|
||||
|
@ -208,6 +207,8 @@ export class CoreUserProfilePage {
|
|||
* Page destroyed.
|
||||
*/
|
||||
ngOnDestroy(): void {
|
||||
this.subscription && this.subscription.unsubscribe();
|
||||
this.obsProfileRefreshed && this.obsProfileRefreshed.off();
|
||||
this.userDelegate.clearUserHandlers();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import { CoreCoursesProvider } from '@core/courses/providers/courses';
|
|||
import { CoreLoggerProvider } from '@providers/logger';
|
||||
import { CoreSitesProvider } from '@providers/sites';
|
||||
import { CoreEventsProvider } from '@providers/events';
|
||||
import { Subject, BehaviorSubject } from 'rxjs';
|
||||
|
||||
/**
|
||||
* Interface that all user profile handlers must implement.
|
||||
|
@ -108,6 +109,12 @@ export interface CoreUserProfileHandlerData {
|
|||
* Data returned by the delegate for each handler.
|
||||
*/
|
||||
export interface CoreUserProfileHandlerToDisplay {
|
||||
/**
|
||||
* Name of the handler.
|
||||
* @type {string}
|
||||
*/
|
||||
name?: string;
|
||||
|
||||
/**
|
||||
* Data to display.
|
||||
* @type {CoreUserProfileHandlerData}
|
||||
|
@ -138,7 +145,6 @@ export class CoreUserDelegate extends CoreDelegate {
|
|||
* @type {string}
|
||||
*/
|
||||
static TYPE_COMMUNICATION = 'communication';
|
||||
|
||||
/**
|
||||
* User profile handler type for new page.
|
||||
* @type {string}
|
||||
|
@ -150,13 +156,55 @@ export class CoreUserDelegate extends CoreDelegate {
|
|||
*/
|
||||
static TYPE_ACTION = 'action';
|
||||
|
||||
/**
|
||||
* Update handler information event.
|
||||
* @type {string}
|
||||
*/
|
||||
static UPDATE_HANDLER_EVENT = 'CoreUserDelegate_update_handler_event';
|
||||
|
||||
protected handlers: { [s: string]: CoreUserProfileHandler } = {};
|
||||
protected enabledHandlers: { [s: string]: CoreUserProfileHandler } = {};
|
||||
protected observableHandlers: Subject<CoreUserProfileHandlerToDisplay[]> =
|
||||
new BehaviorSubject<CoreUserProfileHandlerToDisplay[]>([]);
|
||||
protected userHandlers: CoreUserProfileHandlerToDisplay[] = [];
|
||||
protected featurePrefix = '$mmUserDelegate_';
|
||||
protected loaded = false;
|
||||
|
||||
constructor(protected loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider,
|
||||
private coursesProvider: CoreCoursesProvider, protected eventsProvider: CoreEventsProvider) {
|
||||
super('CoreUserDelegate', loggerProvider, sitesProvider, eventsProvider);
|
||||
|
||||
eventsProvider.on(CoreUserDelegate.UPDATE_HANDLER_EVENT, (data) => {
|
||||
if (data && data.handler) {
|
||||
const handler = this.userHandlers.find((userHandler) => {
|
||||
return userHandler.name == data.handler;
|
||||
});
|
||||
if (handler) {
|
||||
for (const x in data.data) {
|
||||
handler.data[x] = data.data[x];
|
||||
}
|
||||
this.observableHandlers.next(this.userHandlers);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if handlers are loaded.
|
||||
*
|
||||
* @return {boolean} True if handlers are loaded, false otherwise.
|
||||
*/
|
||||
areHandlersLoaded(): boolean {
|
||||
return this.loaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear current user handlers.
|
||||
*/
|
||||
clearUserHandlers(): void {
|
||||
this.loaded = false;
|
||||
this.userHandlers = [];
|
||||
this.observableHandlers.next(this.userHandlers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -164,14 +212,15 @@ export class CoreUserDelegate extends CoreDelegate {
|
|||
*
|
||||
* @param {any} user The user object.
|
||||
* @param {number} courseId The course ID.
|
||||
* @return {Promise<CoreUserProfileHandlerToDisplay[]>} Resolved with the handlers.
|
||||
* @return {Subject<CoreUserProfileHandlerToDisplay[]>} Resolved with the handlers.
|
||||
*/
|
||||
getProfileHandlersFor(user: any, courseId: number): Promise<CoreUserProfileHandlerToDisplay[]> {
|
||||
const handlers: CoreUserProfileHandlerToDisplay[] = [],
|
||||
promises = [];
|
||||
getProfileHandlersFor(user: any, courseId: number): Subject<CoreUserProfileHandlerToDisplay[]> {
|
||||
const promises = [];
|
||||
|
||||
this.userHandlers = [];
|
||||
|
||||
// Retrieve course options forcing cache.
|
||||
return this.coursesProvider.getUserCourses(true).then((courses) => {
|
||||
this.coursesProvider.getUserCourses(true).then((courses) => {
|
||||
const courseIds = courses.map((course) => {
|
||||
return course.id;
|
||||
});
|
||||
|
@ -188,7 +237,8 @@ export class CoreUserDelegate extends CoreDelegate {
|
|||
isEnabledForUser = handler.isEnabledForUser(user, courseId, navOptions, admOptions),
|
||||
promise = Promise.resolve(isEnabledForUser).then((enabled) => {
|
||||
if (enabled) {
|
||||
handlers.push({
|
||||
this.userHandlers.push({
|
||||
name: name,
|
||||
data: handler.getDisplayData(user, courseId),
|
||||
priority: handler.priority,
|
||||
type: handler.type || CoreUserDelegate.TYPE_NEW_PAGE
|
||||
|
@ -203,12 +253,20 @@ export class CoreUserDelegate extends CoreDelegate {
|
|||
}
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
return handlers;
|
||||
// Sort them by priority.
|
||||
this.userHandlers.sort((a, b) => {
|
||||
return b.priority - a.priority;
|
||||
});
|
||||
this.loaded = true;
|
||||
this.observableHandlers.next(this.userHandlers);
|
||||
});
|
||||
});
|
||||
}).catch(() => {
|
||||
// Never fails.
|
||||
return handlers;
|
||||
this.loaded = true;
|
||||
this.observableHandlers.next(this.userHandlers);
|
||||
});
|
||||
|
||||
return this.observableHandlers;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue