diff --git a/scripts/langindex.json b/scripts/langindex.json index e8a39aa49..153d195fc 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -131,6 +131,10 @@ "addon.messages.blockcontact": "message", "addon.messages.blockcontactconfirm": "local_moodlemobileapp", "addon.messages.blocknoncontacts": "message", + "addon.messages.contactableprivacy": "message", + "addon.messages.contactableprivacy_coursemember": "message", + "addon.messages.contactableprivacy_onlycontacts": "message", + "addon.messages.contactableprivacy_site": "message", "addon.messages.contactlistempty": "local_moodlemobileapp", "addon.messages.contactname": "local_moodlemobileapp", "addon.messages.contacts": "message", @@ -1269,7 +1273,6 @@ "core.fileuploader.uploading": "local_moodlemobileapp", "core.fileuploader.uploadingperc": "local_moodlemobileapp", "core.fileuploader.video": "local_moodlemobileapp", - "core.login.findyoursite": "local_moodlemobileapp", "core.folder": "moodle", "core.forcepasswordchangenotice": "moodle", "core.fulllistofcourses": "moodle", @@ -1329,6 +1332,7 @@ "core.login.erroraccesscontrolalloworigin": "local_moodlemobileapp", "core.login.errordeletesite": "local_moodlemobileapp", "core.login.errorupdatesite": "local_moodlemobileapp", + "core.login.findyoursite": "local_moodlemobileapp", "core.login.firsttime": "moodle", "core.login.forgotten": "moodle", "core.login.getanothercaptcha": "auth", diff --git a/src/addon/messages/lang/en.json b/src/addon/messages/lang/en.json index a75924db0..c89f8ccf8 100644 --- a/src/addon/messages/lang/en.json +++ b/src/addon/messages/lang/en.json @@ -3,6 +3,10 @@ "blockcontact": "Block contact", "blockcontactconfirm": "You will no longer receive messages from this contact.", "blocknoncontacts": "Prevent non-contacts from messaging me", + "contactableprivacy": "Accept messages from:", + "contactableprivacy_coursemember": "My contacts and anyone in my courses", + "contactableprivacy_onlycontacts": "My contacts only", + "contactableprivacy_site": "Anyone on the site", "contactlistempty": "The contact list is empty", "contactname": "Contact name", "contacts": "Contacts", diff --git a/src/addon/messages/pages/settings/settings.html b/src/addon/messages/pages/settings/settings.html index 8c0f58441..fbdbabf1d 100644 --- a/src/addon/messages/pages/settings/settings.html +++ b/src/addon/messages/pages/settings/settings.html @@ -8,12 +8,28 @@ - + - + {{ 'addon.messages.blocknoncontacts' | translate }} - + + + + {{ 'addon.messages.contactableprivacy' | translate }} + + {{ 'addon.messages.contactableprivacy_onlycontacts' | translate }} + + + + {{ 'addon.messages.contactableprivacy_coursemember' | translate }} + + + + {{ 'addon.messages.contactableprivacy_site' | translate }} + + + diff --git a/src/addon/messages/pages/settings/settings.ts b/src/addon/messages/pages/settings/settings.ts index 72a4e0b26..05e87edb6 100644 --- a/src/addon/messages/pages/settings/settings.ts +++ b/src/addon/messages/pages/settings/settings.ts @@ -17,6 +17,7 @@ import { IonicPage } from 'ionic-angular'; import { AddonMessagesProvider } from '../../providers/messages'; import { CoreUserProvider } from '@core/user/providers/user'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreSitesProvider } from '@providers/sites'; /** * Page that displays the messages settings page. @@ -31,10 +32,21 @@ export class AddonMessagesSettingsPage implements OnDestroy { preferences: any; preferencesLoaded: boolean; - blockNonContactsState = false; + contactablePrivacy: number | boolean; + advancedContactable = false; // Whether the site supports "advanced" contactable privacy. + allowSiteMessaging = false; + onlyContactsValue = AddonMessagesProvider.MESSAGE_PRIVACY_ONLYCONTACTS; + courseMemberValue = AddonMessagesProvider.MESSAGE_PRIVACY_COURSEMEMBER; + siteValue = AddonMessagesProvider.MESSAGE_PRIVACY_SITE; + + protected previousContactableValue: number | boolean; constructor(private messagesProvider: AddonMessagesProvider, private domUtils: CoreDomUtilsProvider, - private userProvider: CoreUserProvider) { + private userProvider: CoreUserProvider, sitesProvider: CoreSitesProvider) { + + const currentSite = sitesProvider.getCurrentSite(); + this.advancedContactable = currentSite && currentSite.isVersionGreaterEqualThan('3.6'); + this.allowSiteMessaging = currentSite && currentSite.canUseAdvancedFeature('messagingallusers'); } /** @@ -54,7 +66,8 @@ export class AddonMessagesSettingsPage implements OnDestroy { protected fetchPreferences(): Promise { return this.messagesProvider.getMessagePreferences().then((preferences) => { this.preferences = preferences; - this.blockNonContactsState = preferences.blocknoncontacts; + this.contactablePrivacy = preferences.blocknoncontacts; + this.previousContactableValue = this.contactablePrivacy; }).catch((message) => { this.domUtils.showErrorModal(message); }).finally(() => { @@ -85,19 +98,31 @@ export class AddonMessagesSettingsPage implements OnDestroy { } /** - * Block non contacts. + * Save the contactable privacy setting.. * - * @param {boolean} block If it should be blocked or not. + * @param {number|boolean} value The value to set. */ - blockNonContacts(block: boolean): void { + saveContactablePrivacy(value: number | boolean): void { + if (this.contactablePrivacy == this.previousContactableValue) { + // Value hasn't changed from previous, it probably means that we just fetched the value from the server. + return; + } + const modal = this.domUtils.showModalLoading('core.sending', true); - this.userProvider.updateUserPreference('message_blocknoncontacts', block ? 1 : 0).then(() => { + + if (!this.advancedContactable) { + // Convert from boolean to number. + value = value ? 1 : 0; + } + + this.userProvider.updateUserPreference('message_blocknoncontacts', value).then(() => { // Update the preferences since they were modified. this.updatePreferencesAfterDelay(); + this.previousContactableValue = this.contactablePrivacy; }).catch((message) => { // Show error and revert change. this.domUtils.showErrorModal(message); - this.blockNonContactsState = !this.blockNonContactsState; + this.contactablePrivacy = this.previousContactableValue; }).finally(() => { modal.dismiss(); }); @@ -148,7 +173,7 @@ export class AddonMessagesSettingsPage implements OnDestroy { * * @param {any} refresher Refresher. */ - refreshEvent(refresher: any): void { + refreshPreferences(refresher: any): void { this.messagesProvider.invalidateMessagePreferences().finally(() => { this.fetchPreferences().finally(() => { refresher.complete(); diff --git a/src/addon/messages/providers/messages.ts b/src/addon/messages/providers/messages.ts index 4957047d4..17b907a11 100644 --- a/src/addon/messages/providers/messages.ts +++ b/src/addon/messages/providers/messages.ts @@ -37,6 +37,10 @@ export class AddonMessagesProvider { static POLL_INTERVAL = 10000; static PUSH_SIMULATION_COMPONENT = 'AddonMessagesPushSimulation'; + static MESSAGE_PRIVACY_COURSEMEMBER = 0; // Privacy setting for being messaged by anyone within courses user is member of. + static MESSAGE_PRIVACY_ONLYCONTACTS = 1; // Privacy setting for being messaged only by contacts. + static MESSAGE_PRIVACY_SITE = 2; // Privacy setting for being messaged by anyone on the site. + protected logger; constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private appProvider: CoreAppProvider, @@ -469,7 +473,7 @@ export class AddonMessagesProvider { return site.read('core_message_get_user_message_preferences', {}, preSets).then((data) => { if (data.preferences) { - data.preferences.blocknoncontacts = !!data.blocknoncontacts; + data.preferences.blocknoncontacts = data.blocknoncontacts; return data.preferences; } diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index ba1ffd309..40750d05f 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -131,6 +131,10 @@ "addon.messages.blockcontact": "Block contact", "addon.messages.blockcontactconfirm": "You will no longer receive messages from this contact.", "addon.messages.blocknoncontacts": "Prevent non-contacts from messaging me", + "addon.messages.contactableprivacy": "Accept messages from:", + "addon.messages.contactableprivacy_coursemember": "My contacts and anyone in my courses", + "addon.messages.contactableprivacy_onlycontacts": "My contacts only", + "addon.messages.contactableprivacy_site": "Anyone on the site", "addon.messages.contactlistempty": "The contact list is empty", "addon.messages.contactname": "Contact name", "addon.messages.contacts": "Contacts", diff --git a/src/classes/site.ts b/src/classes/site.ts index c545341a2..4b830350a 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -214,7 +214,8 @@ export class CoreSite { 3.2: 2016120500, 3.3: 2017051503, 3.4: 2017111300, - 3.5: 2018051700 + 3.5: 2018051700, + 3.6: 2018111000 // @todo: Replace it with the right 3.6 date when released. }; // Rest of variables.