From e69eed4f4cb88ab9856a764fd244bb6c3180d742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 6 Nov 2024 11:41:09 +0100 Subject: [PATCH] MOBILE-4653 messages: Move AddonMessages constants --- src/addons/messages/constants.ts | 52 +++++ .../messages/pages/contacts-35/contacts.ts | 4 +- .../messages/pages/contacts/contacts.ts | 6 +- .../messages/pages/discussion/discussion.ts | 40 ++-- .../pages/discussions-35/discussions.ts | 6 +- .../group-conversations.ts | 43 ++-- src/addons/messages/pages/search/search.ts | 10 +- .../messages/pages/settings/settings.ts | 11 +- .../messages/services/handlers/mainmenu.ts | 9 +- src/addons/messages/services/messages-sync.ts | 23 +- src/addons/messages/services/messages.ts | 210 ++++++++++++------ 11 files changed, 287 insertions(+), 127 deletions(-) create mode 100644 src/addons/messages/constants.ts diff --git a/src/addons/messages/constants.ts b/src/addons/messages/constants.ts new file mode 100644 index 000000000..402afc3f5 --- /dev/null +++ b/src/addons/messages/constants.ts @@ -0,0 +1,52 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// 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. + +export const ADDON_MESSAGES_NEW_MESSAGE_EVENT = 'addon_messages_new_message_event'; +export const ADDON_MESSAGES_READ_CHANGED_EVENT = 'addon_messages_read_changed_event'; +// Notify a conversation should be opened. +export const ADDON_MESSAGES_OPEN_CONVERSATION_EVENT = 'addon_messages_open_conversation_event'; +export const ADDON_MESSAGES_UPDATE_CONVERSATION_LIST_EVENT = 'addon_messages_update_conversation_list_event'; +export const ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT = 'addon_messages_member_changed_event'; +export const ADDON_MESSAGES_UNREAD_CONVERSATION_COUNTS_EVENT = 'addon_messages_unread_conversation_counts_event'; +export const ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT = 'addon_messages_contact_requests_count_event'; + +export const ADDON_MESSAGES_POLL_INTERVAL = 10000; +export const ADDON_MESSAGES_PUSH_SIMULATION_COMPONENT = 'AddonMessagesPushSimulation'; + +export const enum AddonMessagesMessagePrivacy { + COURSEMEMBER = 0, // Privacy setting for being messaged by anyone within courses user is member. + ONLYCONTACTS = 1, // Privacy setting for being messaged only by contacts. + SITE = 2, // Privacy setting for being messaged by anyone on the site. +} + +export const enum AddonMessagesMessageConversationType { + INDIVIDUAL = 1, // An individual conversation. + GROUP = 2, // A group conversation. + SELF = 3, // A self conversation. +} + +export const ADDON_MESSAGES_LIMIT_CONTACTS = 50; +export const ADDON_MESSAGES_LIMIT_MESSAGES = 50; +export const ADDON_MESSAGES_LIMIT_INITIAL_USER_SEARCH = 3; +export const ADDON_MESSAGES_LIMIT_SEARCH = 50; + +export const ADDON_MESSAGES_NOTIFICATION_PREFERENCES_KEY = 'message_provider_moodle_instantmessage'; + +export const ADDON_MESSAGES_AUTO_SYNCED = 'addon_messages_autom_synced'; + +export const enum AddonMessagesUpdateConversationAction { + MUTE = 'mute', + FAVOURITE = 'favourite', + DELETE = 'delete', +} diff --git a/src/addons/messages/pages/contacts-35/contacts.ts b/src/addons/messages/pages/contacts-35/contacts.ts index 9484ab9f6..80d292b74 100644 --- a/src/addons/messages/pages/contacts-35/contacts.ts +++ b/src/addons/messages/pages/contacts-35/contacts.ts @@ -15,7 +15,6 @@ import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { CoreSites } from '@services/sites'; import { - AddonMessagesProvider, AddonMessagesGetContactsWSResponse, AddonMessagesSearchContactsContact, AddonMessagesGetContactsContact, @@ -29,6 +28,7 @@ import { CoreScreen } from '@services/screen'; import { CoreNavigator } from '@services/navigator'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; import { CoreKeyboard } from '@singletons/keyboard'; +import { ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT } from '@addons/messages/constants'; /** * Page that displays the list of contacts. @@ -73,7 +73,7 @@ export class AddonMessagesContacts35Page implements OnInit, OnDestroy { // Refresh the list when a contact request is confirmed. this.memberInfoObserver = CoreEvents.on( - AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, (data) => { if (data.contactRequestConfirmed) { this.refreshData(); diff --git a/src/addons/messages/pages/contacts/contacts.ts b/src/addons/messages/pages/contacts/contacts.ts index 69599ba3c..2e12a7d42 100644 --- a/src/addons/messages/pages/contacts/contacts.ts +++ b/src/addons/messages/pages/contacts/contacts.ts @@ -18,12 +18,12 @@ import { CoreSites } from '@services/sites'; import { AddonMessages, AddonMessagesConversationMember, - AddonMessagesProvider, } from '../../services/messages'; import { CoreNavigator } from '@services/navigator'; import { CoreScreen } from '@services/screen'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; +import { ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT, ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT } from '@addons/messages/constants'; /** * Page that displays contacts and contact requests. @@ -63,7 +63,7 @@ export class AddonMessagesContactsPage implements OnInit, OnDestroy { // Update the contact requests badge. this.contactRequestsCountObserver = CoreEvents.on( - AddonMessagesProvider.CONTACT_REQUESTS_COUNT_EVENT, + ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT, (data) => { this.requestsBadge = data.count > 0 ? String(data.count) : ''; }, @@ -72,7 +72,7 @@ export class AddonMessagesContactsPage implements OnInit, OnDestroy { // Update block status of a user. this.memberInfoObserver = CoreEvents.on( - AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, (data) => { if (data.userBlocked || data.userUnblocked) { const user = this.confirmedContacts.find((user) => user.id == data.userId); diff --git a/src/addons/messages/pages/discussion/discussion.ts b/src/addons/messages/pages/discussion/discussion.ts index 7fa11ca02..fdd9036a1 100644 --- a/src/addons/messages/pages/discussion/discussion.ts +++ b/src/addons/messages/pages/discussion/discussion.ts @@ -18,17 +18,15 @@ import { AlertOptions } from '@ionic/core'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreSites } from '@services/sites'; import { - AddonMessagesProvider, AddonMessagesConversationFormatted, AddonMessagesConversationMember, AddonMessagesGetMessagesMessage, AddonMessages, AddonMessagesConversationMessageFormatted, AddonMessagesSendMessageResults, - AddonMessagesUpdateConversationAction, } from '../../services/messages'; import { AddonMessagesOffline, AddonMessagesOfflineMessagesDBRecordFormatted } from '../../services/messages-offline'; -import { AddonMessagesSync, AddonMessagesSyncProvider } from '../../services/messages-sync'; +import { AddonMessagesSync } from '../../services/messages-sync'; import { CoreUser } from '@features/user/services/user'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreUtils } from '@services/utils/utils'; @@ -48,6 +46,18 @@ import { CoreText } from '@singletons/text'; import { CoreWait } from '@singletons/wait'; import { CoreModals } from '@services/modals'; import { CoreLoadings } from '@services/loadings'; +import { + ADDON_MESSAGES_AUTO_SYNCED, + ADDON_MESSAGES_LIMIT_MESSAGES, + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_NEW_MESSAGE_EVENT, + ADDON_MESSAGES_OPEN_CONVERSATION_EVENT, + ADDON_MESSAGES_POLL_INTERVAL, + ADDON_MESSAGES_READ_CHANGED_EVENT, + ADDON_MESSAGES_UPDATE_CONVERSATION_LIST_EVENT, + AddonMessagesMessageConversationType, + AddonMessagesUpdateConversationAction, +} from '@addons/messages/constants'; /** * Page that displays a message discussion page. @@ -125,7 +135,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView this.logger = CoreLogger.getInstance('AddonMessagesDiscussionPage'); // Refresh data if this discussion is synchronized automatically. - this.syncObserver = CoreEvents.on(AddonMessagesSyncProvider.AUTO_SYNCED, (data) => { + this.syncObserver = CoreEvents.on(ADDON_MESSAGES_AUTO_SYNCED, (data) => { if ((data.userId && data.userId == this.userId) || (data.conversationId && data.conversationId == this.conversationId)) { // Fetch messages. @@ -140,7 +150,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView // Refresh data if info of a mamber of the conversation have changed. this.memberInfoObserver = CoreEvents.on( - AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, (data) => { if (data.userId && (this.members[data.userId] || this.otherMember && data.userId == this.otherMember.id)) { this.fetchData(); @@ -595,13 +605,13 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView this.conversationId = this.conversation.id; this.title = this.conversation.name; this.conversationImage = this.conversation.imageurl; - this.isGroup = this.conversation.type == AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP; + this.isGroup = this.conversation.type === AddonMessagesMessageConversationType.GROUP; this.favouriteIcon = 'fas-star'; this.muteIcon = this.conversation.ismuted ? 'fas-bell' : 'fas-bell-slash'; if (!this.isGroup) { this.userId = this.conversation.userid; } - this.isSelf = this.conversation.type == AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_SELF; + this.isSelf = this.conversation.type === AddonMessagesMessageConversationType.SELF; return true; } else { @@ -645,7 +655,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView const messages: AddonMessagesConversationMessageFormatted[] = result.messages; if (pagesToLoad > 0 && result.canLoadMore) { - offset += AddonMessagesProvider.LIMIT_MESSAGES; + offset += ADDON_MESSAGES_LIMIT_MESSAGES; // Get more messages. const nextMessages = await this.getConversationMessages(pagesToLoad, offset); @@ -764,7 +774,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView } if (readChanged) { - CoreEvents.trigger(AddonMessagesProvider.READ_CHANGED_EVENT, { + CoreEvents.trigger(ADDON_MESSAGES_READ_CHANGED_EVENT, { conversationId: this.conversationId, userId: this.userId, }, this.siteId); @@ -789,7 +799,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView if (trigger) { // Update discussions last message. - CoreEvents.trigger(AddonMessagesProvider.NEW_MESSAGE_EVENT, { + CoreEvents.trigger(ADDON_MESSAGES_NEW_MESSAGE_EVENT, { conversationId: this.conversationId, userId: this.userId, message: this.lastMessage?.text, @@ -905,7 +915,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView this.fetchMessages().catch(() => { // Ignore errors. }); - }, AddonMessagesProvider.POLL_INTERVAL); + }, ADDON_MESSAGES_POLL_INTERVAL); } } @@ -1265,7 +1275,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView if (splitViewLoaded) { // Notify the left pane to load it, this way the right conversation will be highlighted. CoreEvents.trigger( - AddonMessagesProvider.OPEN_CONVERSATION_EVENT, + ADDON_MESSAGES_OPEN_CONVERSATION_EVENT, { userId }, this.siteId, ); @@ -1300,7 +1310,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView // Get the conversation data so it's cached. Don't block the user for this. AddonMessages.getConversation(this.conversation.id, undefined, true); - CoreEvents.trigger(AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, { + CoreEvents.trigger(ADDON_MESSAGES_UPDATE_CONVERSATION_LIST_EVENT, { conversationId: this.conversation.id, action: AddonMessagesUpdateConversationAction.FAVOURITE, value: this.conversation.isfavourite, @@ -1332,7 +1342,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView // Get the conversation data so it's cached. Don't block the user for this. AddonMessages.getConversation(this.conversation.id, undefined, true); - CoreEvents.trigger(AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, { + CoreEvents.trigger(ADDON_MESSAGES_UPDATE_CONVERSATION_LIST_EVENT, { conversationId: this.conversation.id, action: AddonMessagesUpdateConversationAction.MUTE, value: this.conversation.ismuted, @@ -1447,7 +1457,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView await AddonMessages.deleteConversation(this.conversation.id); CoreEvents.trigger( - AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, + ADDON_MESSAGES_UPDATE_CONVERSATION_LIST_EVENT, { conversationId: this.conversation.id, action: AddonMessagesUpdateConversationAction.DELETE, diff --git a/src/addons/messages/pages/discussions-35/discussions.ts b/src/addons/messages/pages/discussions-35/discussions.ts index d4c842a59..e4a140d66 100644 --- a/src/addons/messages/pages/discussions-35/discussions.ts +++ b/src/addons/messages/pages/discussions-35/discussions.ts @@ -19,7 +19,6 @@ import { AddonMessages, AddonMessagesDiscussion, AddonMessagesMessageAreaContact, - AddonMessagesProvider, } from '../../services/messages'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreUtils } from '@services/utils/utils'; @@ -33,6 +32,7 @@ import { CoreScreen } from '@services/screen'; import { CorePlatform } from '@services/platform'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; import { CoreKeyboard } from '@singletons/keyboard'; +import { ADDON_MESSAGES_NEW_MESSAGE_EVENT, ADDON_MESSAGES_READ_CHANGED_EVENT } from '@addons/messages/constants'; /** * Page that displays the list of discussions. @@ -75,7 +75,7 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy { // Update discussions when new message is received. this.newMessagesObserver = CoreEvents.on( - AddonMessagesProvider.NEW_MESSAGE_EVENT, + ADDON_MESSAGES_NEW_MESSAGE_EVENT, (data) => { if (data.userId && this.discussions) { const discussion = this.discussions.find((disc) => disc.message?.user === data.userId); @@ -97,7 +97,7 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy { // Update discussions when a message is read. this.readChangedObserver = CoreEvents.on( - AddonMessagesProvider.READ_CHANGED_EVENT, + ADDON_MESSAGES_READ_CHANGED_EVENT, (data) => { if (data.userId && this.discussions) { const discussion = this.discussions.find((disc) => disc.message?.user === data.userId); diff --git a/src/addons/messages/pages/group-conversations/group-conversations.ts b/src/addons/messages/pages/group-conversations/group-conversations.ts index 259997cb2..00be18411 100644 --- a/src/addons/messages/pages/group-conversations/group-conversations.ts +++ b/src/addons/messages/pages/group-conversations/group-conversations.ts @@ -17,12 +17,10 @@ import { AccordionGroupChangeEventDetail, IonAccordionGroup, IonContent } from ' import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreSites } from '@services/sites'; import { - AddonMessagesProvider, AddonMessagesConversationFormatted, AddonMessages, AddonMessagesNewMessagedEventData, AddonMessagesUnreadConversationCountsEventData, - AddonMessagesUpdateConversationAction, } from '../../services/messages'; import { AddonMessagesOffline, @@ -40,6 +38,17 @@ import { CoreNavigator } from '@services/navigator'; import { CoreScreen } from '@services/screen'; import { CorePlatform } from '@services/platform'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; +import { + ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT, + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_NEW_MESSAGE_EVENT, + ADDON_MESSAGES_OPEN_CONVERSATION_EVENT, + ADDON_MESSAGES_READ_CHANGED_EVENT, + ADDON_MESSAGES_UNREAD_CONVERSATION_COUNTS_EVENT, + ADDON_MESSAGES_UPDATE_CONVERSATION_LIST_EVENT, + AddonMessagesMessageConversationType, + AddonMessagesUpdateConversationAction, +} from '@addons/messages/constants'; const enum AddonMessagesGroupConversationOptionNames { FAVOURITES = 'favourites', @@ -82,7 +91,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { optionName: AddonMessagesGroupConversationOptionNames.GROUP, titleString: 'addon.messages.groupconversations', emptyString: 'addon.messages.nogroupconversations', - type: AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP, + type: AddonMessagesMessageConversationType.GROUP, favourites: false, count: 0, unread: 0, @@ -92,7 +101,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { optionName: AddonMessagesGroupConversationOptionNames.INDIVIDUAL, titleString: 'addon.messages.individualconversations', emptyString: 'addon.messages.noindividualconversations', - type: AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, + type: AddonMessagesMessageConversationType.INDIVIDUAL, favourites: false, count: 0, unread: 0, @@ -100,7 +109,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { }, ]; - typeGroup = AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP; + typeGroup = AddonMessagesMessageConversationType.GROUP; protected siteId: string; protected currentUserId: number; @@ -124,7 +133,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { // Update conversations when new message is received. this.newMessagesObserver = CoreEvents.on( - AddonMessagesProvider.NEW_MESSAGE_EVENT, + ADDON_MESSAGES_NEW_MESSAGE_EVENT, (data) => { // Check if the new message belongs to the option that is currently expanded. const expandedOption = this.getExpandedOption(); @@ -163,7 +172,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { conversation.lastmessagedate = data.timecreated / 1000; if (data.userFrom) { conversation.sentfromcurrentuser = data.userFrom.id === this.currentUserId; - if (conversation.type === AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP) { + if (conversation.type === AddonMessagesMessageConversationType.GROUP) { conversation.members[0] = data.userFrom; } } @@ -183,7 +192,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { ); // Update conversations when a message is read. - this.readChangedObserver = CoreEvents.on(AddonMessagesProvider.READ_CHANGED_EVENT, (data) => { + this.readChangedObserver = CoreEvents.on(ADDON_MESSAGES_READ_CHANGED_EVENT, (data) => { if (data.conversationId) { const conversation = this.findConversation(data.conversationId); @@ -200,7 +209,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { // Load a discussion if we receive an event to do so. this.openConversationObserver = CoreEvents.on( - AddonMessagesProvider.OPEN_CONVERSATION_EVENT, + ADDON_MESSAGES_OPEN_CONVERSATION_EVENT, (data) => { if (data.conversationId || data.userId) { this.gotoConversation(data.conversationId, data.userId); @@ -222,7 +231,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { // Update conversations if we receive an event to do so. this.updateConversationListObserver = CoreEvents.on( - AddonMessagesProvider.UPDATE_CONVERSATION_LIST_EVENT, + ADDON_MESSAGES_UPDATE_CONVERSATION_LIST_EVENT, (data) => { if (data?.action === AddonMessagesUpdateConversationAction.MUTE) { // If the conversation is displayed, change its muted value. @@ -256,7 +265,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { // Update unread conversation counts. this.cronObserver = CoreEvents.on( - AddonMessagesProvider.UNREAD_CONVERSATION_COUNTS_EVENT, + ADDON_MESSAGES_UNREAD_CONVERSATION_COUNTS_EVENT, (data) => { this.setCounts(data, 'unread'); }, @@ -265,7 +274,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { // Update the contact requests badge. this.contactRequestsCountObserver = CoreEvents.on( - AddonMessagesProvider.CONTACT_REQUESTS_COUNT_EVENT, + ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT, (data) => { this.contactRequestsCount = data.count; }, @@ -274,7 +283,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { // Update block status of a user. this.memberInfoObserver = CoreEvents.on( - AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, (data) => { if (!data.userBlocked && !data.userUnblocked) { // The block status has not changed, ignore. @@ -638,7 +647,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { // Conversation not found, it could be an old one or the message could belong to another option. conversation = { id: message.conversationid, - type: message.conversation?.type || AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, + type: message.conversation?.type || AddonMessagesMessageConversationType.INDIVIDUAL, membercount: message.conversation?.membercount || 0, ismuted: message.conversation?.ismuted || false, isfavourite: message.conversation?.isfavourite || false, @@ -657,7 +666,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { this.addOfflineConversation(conversation, option); } } - } else if (option.type === AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) { + } else if (option.type === AddonMessagesMessageConversationType.INDIVIDUAL) { // It's a new conversation. Check if we already created it (there is more than one message for the same user). const conversation = this.findConversation(undefined, message.touserid, option); @@ -675,7 +684,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { }).then((user) => { const conversation: AddonMessagesConversationForList = { id: 0, - type: AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, + type: AddonMessagesMessageConversationType.INDIVIDUAL, membercount: 0, // Faked data. ismuted: false, // Faked data. isfavourite: false, // Faked data. @@ -742,7 +751,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { return AddonMessagesGroupConversationOptionNames.FAVOURITES; } - if (conversation.type === AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP) { + if (conversation.type === AddonMessagesMessageConversationType.GROUP) { return AddonMessagesGroupConversationOptionNames.GROUP; } diff --git a/src/addons/messages/pages/search/search.ts b/src/addons/messages/pages/search/search.ts index 501d50ca8..81d6af6aa 100644 --- a/src/addons/messages/pages/search/search.ts +++ b/src/addons/messages/pages/search/search.ts @@ -16,7 +16,6 @@ import { Component, OnDestroy, ViewChild } from '@angular/core'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreSites } from '@services/sites'; import { - AddonMessagesProvider, AddonMessagesConversationMember, AddonMessagesMessageAreaContact, AddonMessages, @@ -26,6 +25,11 @@ import { CoreNavigator } from '@services/navigator'; import { CoreScreen } from '@services/screen'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; import { CoreKeyboard } from '@singletons/keyboard'; +import { + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_LIMIT_SEARCH, + ADDON_MESSAGES_LIMIT_INITIAL_USER_SEARCH, +} from '@addons/messages/constants'; /** * Page for searching users. @@ -74,7 +78,7 @@ export class AddonMessagesSearchPage implements OnDestroy { constructor() { // Update block status of a user. this.memberInfoObserver = CoreEvents.on( - AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, + ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, (data) => { if (!data.userBlocked && !data.userUnblocked) { // The block status has not changed, ignore. @@ -139,7 +143,7 @@ export class AddonMessagesSearchPage implements OnDestroy { let canLoadMoreMessages = false; if (!loadMore || loadMore == 'contacts' || loadMore == 'noncontacts') { - const limitNum = loadMore ? AddonMessagesProvider.LIMIT_SEARCH : AddonMessagesProvider.LIMIT_INITIAL_USER_SEARCH; + const limitNum = loadMore ? ADDON_MESSAGES_LIMIT_SEARCH : ADDON_MESSAGES_LIMIT_INITIAL_USER_SEARCH; let limitFrom = 0; if (loadMore == 'contacts') { limitFrom = this.contacts.results.length; diff --git a/src/addons/messages/pages/settings/settings.ts b/src/addons/messages/pages/settings/settings.ts index 9fcb9ede7..1d8142ffb 100644 --- a/src/addons/messages/pages/settings/settings.ts +++ b/src/addons/messages/pages/settings/settings.ts @@ -14,7 +14,7 @@ import { Component, OnDestroy, OnInit, signal } from '@angular/core'; import { - AddonMessagesProvider, AddonMessagesMessagePreferences, + AddonMessagesMessagePreferences, AddonMessagesMessagePreferencesNotification, AddonMessagesMessagePreferencesNotificationProcessor, AddonMessages, @@ -29,6 +29,7 @@ import { AddonNotificationsPreferencesNotificationProcessorState } from '@addons import { CorePlatform } from '@services/platform'; import { CoreErrorHelper } from '@services/error-helper'; import { CoreLoadings } from '@services/loadings'; +import { ADDON_MESSAGES_NOTIFICATION_PREFERENCES_KEY, AddonMessagesMessagePrivacy } from '@addons/messages/constants'; /** * Page that displays the messages settings page. @@ -46,9 +47,9 @@ export class AddonMessagesSettingsPage implements OnInit, OnDestroy { 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; + onlyContactsValue = AddonMessagesMessagePrivacy.ONLYCONTACTS; + courseMemberValue = AddonMessagesMessagePrivacy.COURSEMEMBER; + siteValue = AddonMessagesMessagePrivacy.SITE; groupMessagingEnabled = false; sendOnEnter = false; warningMessage = signal(undefined); @@ -91,7 +92,7 @@ export class AddonMessagesSettingsPage implements OnInit, OnDestroy { for (const component of preferences.components) { // Only display get the notification preferences. component.notifications = component.notifications.filter((notification) => - notification.preferencekey == AddonMessagesProvider.NOTIFICATION_PREFERENCES_KEY); + notification.preferencekey === ADDON_MESSAGES_NOTIFICATION_PREFERENCES_KEY); if (this.loggedInOffLegacyMode) { // Load enabled from loggedin / loggedoff values. diff --git a/src/addons/messages/services/handlers/mainmenu.ts b/src/addons/messages/services/handlers/mainmenu.ts index fe778d9ed..5e083663e 100644 --- a/src/addons/messages/services/handlers/mainmenu.ts +++ b/src/addons/messages/services/handlers/mainmenu.ts @@ -14,7 +14,6 @@ import { Injectable } from '@angular/core'; import { - AddonMessagesProvider, AddonMessages, } from '../messages'; import { CoreMainMenuHandler, CoreMainMenuHandlerToDisplay } from '@features/mainmenu/services/mainmenu-delegate'; @@ -28,6 +27,10 @@ import { import { CorePushNotificationsDelegate } from '@features/pushnotifications/services/push-delegate'; import { makeSingleton } from '@singletons'; import { CoreMainMenuProvider } from '@features/mainmenu/services/mainmenu'; +import { + ADDON_MESSAGES_UNREAD_CONVERSATION_COUNTS_EVENT, + ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT, +} from '@addons/messages/constants'; /** * Handler to inject an option into main menu. @@ -58,14 +61,14 @@ export class AddonMessagesMainMenuHandlerService implements CoreMainMenuHandler, constructor() { - CoreEvents.on(AddonMessagesProvider.UNREAD_CONVERSATION_COUNTS_EVENT, (data) => { + CoreEvents.on(ADDON_MESSAGES_UNREAD_CONVERSATION_COUNTS_EVENT, (data) => { this.unreadCount = data.favourites + data.individual + data.group + data.self; this.orMore = !!data.orMore; data.siteId && this.updateBadge(data.siteId); }); - CoreEvents.on(AddonMessagesProvider.CONTACT_REQUESTS_COUNT_EVENT, (data) => { + CoreEvents.on(ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT, (data) => { this.contactRequestsCount = data.count; data.siteId && this.updateBadge(data.siteId); diff --git a/src/addons/messages/services/messages-sync.ts b/src/addons/messages/services/messages-sync.ts index c4c415d4c..765d52a96 100644 --- a/src/addons/messages/services/messages-sync.ts +++ b/src/addons/messages/services/messages-sync.ts @@ -18,7 +18,6 @@ import { AddonMessagesOffline, AddonMessagesOfflineAnyMessagesFormatted, } from './messages-offline'; import { - AddonMessagesProvider, AddonMessages, AddonMessagesGetMessagesWSParams, } from './messages'; @@ -33,6 +32,20 @@ import { CoreError } from '@classes/errors/error'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; import { CoreWait } from '@singletons/wait'; import { CoreErrorHelper, CoreErrorObject } from '@services/error-helper'; +import { ADDON_MESSAGES_AUTO_SYNCED, ADDON_MESSAGES_LIMIT_MESSAGES } from '../constants'; + +declare module '@singletons/events' { + + /** + * Augment CoreEventsData interface with events specific to this service. + * + * @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation + */ + export interface CoreEventsData { + [ADDON_MESSAGES_AUTO_SYNCED]: AddonMessagesSyncEvents; + } + +} /** * Service to sync messages. @@ -40,8 +53,6 @@ import { CoreErrorHelper, CoreErrorObject } from '@services/error-helper'; @Injectable({ providedIn: 'root' }) export class AddonMessagesSyncProvider extends CoreSyncBaseProvider { - static readonly AUTO_SYNCED = 'addon_messages_autom_synced'; - constructor() { super('AddonMessagesSync'); } @@ -113,7 +124,7 @@ export class AddonMessagesSyncProvider extends CoreSyncBaseProvider { const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRequestConfirmed: true }; - CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); + CoreEvents.trigger(ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, data, site.id); }); } @@ -213,7 +283,7 @@ export class AddonMessagesProvider { await this.invalidateAllMemberInfo(userId, site).finally(() => { const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRequestCreated: true }; - CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); + CoreEvents.trigger(ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, data, site.id); }); } @@ -240,7 +310,7 @@ export class AddonMessagesProvider { this.refreshContactRequestsCount(site.id), ]).finally(() => { const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRequestDeclined: true }; - CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); + CoreEvents.trigger(ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, data, site.id); }); } @@ -374,8 +444,8 @@ export class AddonMessagesProvider { conversation.lastmessagedate = lastMessage ? lastMessage.timecreated : undefined; conversation.sentfromcurrentuser = lastMessage ? lastMessage.useridfrom == userId : undefined; - if (conversation.type != AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP) { - const isIndividual = conversation.type == AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_INDIVIDUAL; + if (conversation.type != AddonMessagesMessageConversationType.GROUP) { + const isIndividual = conversation.type == AddonMessagesMessageConversationType.INDIVIDUAL; const otherUser = conversation.members.find((member) => (isIndividual && member.id != userId) || (!isIndividual && member.id == userId)); @@ -680,14 +750,14 @@ export class AddonMessagesProvider { * Get the list of user contacts. * * @param limitFrom Position of the first contact to fetch. - * @param limitNum Number of contacts to fetch. Default is AddonMessagesProvider.LIMIT_CONTACTS. + * @param limitNum Number of contacts to fetch. Default is ADDON_MESSAGES_LIMIT_CONTACTS. * @param siteId Site ID. If not defined, use current site. * @returns Promise resolved with the list of user contacts. * @since 3.6 */ async getUserContacts( limitFrom: number = 0, - limitNum: number = AddonMessagesProvider.LIMIT_CONTACTS, + limitNum: number = ADDON_MESSAGES_LIMIT_CONTACTS, siteId?: string, ): Promise<{contacts: AddonMessagesConversationMember[]; canLoadMore: boolean}> { const site = await CoreSites.getSite(siteId); @@ -724,14 +794,14 @@ export class AddonMessagesProvider { * Get the contact request sent to the current user. * * @param limitFrom Position of the first contact request to fetch. - * @param limitNum Number of contact requests to fetch. Default is AddonMessagesProvider.LIMIT_CONTACTS. + * @param limitNum Number of contact requests to fetch. Default is ADDON_MESSAGES_LIMIT_CONTACTS. * @param siteId Site ID. If not defined, use current site. * @returns Promise resolved with the list of contact requests. * @since 3.6 */ async getContactRequests( limitFrom: number = 0, - limitNum: number = AddonMessagesProvider.LIMIT_CONTACTS, + limitNum: number = ADDON_MESSAGES_LIMIT_CONTACTS, siteId?: string, ): Promise<{requests: AddonMessagesConversationMember[]; canLoadMore: boolean}> { const site = await CoreSites.getSite(siteId); @@ -792,7 +862,7 @@ export class AddonMessagesProvider { }; // Notify the new count so all badges are updated. - CoreEvents.trigger(AddonMessagesProvider.CONTACT_REQUESTS_COUNT_EVENT, data , site.id); + CoreEvents.trigger(ADDON_MESSAGES_CONTACT_REQUESTS_COUNT_EVENT, data , site.id); return data.count; @@ -936,7 +1006,7 @@ export class AddonMessagesProvider { ): Promise<{members: AddonMessagesConversationMember[]; canLoadMore: boolean}> { const site = await CoreSites.getSite(siteId); userId = userId || site.getUserId(); - limitTo = limitTo ?? AddonMessagesProvider.LIMIT_MESSAGES; + limitTo = limitTo ?? ADDON_MESSAGES_LIMIT_MESSAGES; const preSets: CoreSiteWSPreSets = { cacheKey: this.getCacheKeyForConversationMembers(userId, conversationId), @@ -984,7 +1054,7 @@ export class AddonMessagesProvider { options.userId = options.userId || site.getUserId(); options.limitFrom = options.limitFrom || 0; - options.limitTo = options.limitTo ?? AddonMessagesProvider.LIMIT_MESSAGES; + options.limitTo = options.limitTo ?? ADDON_MESSAGES_LIMIT_MESSAGES; options.timeFrom = options.timeFrom || 0; options.newestFirst = options.newestFirst ?? true; @@ -1077,7 +1147,7 @@ export class AddonMessagesProvider { const params: AddonMessagesGetConversationsWSParams = { userid: userId, limitfrom: limitFrom, - limitnum: AddonMessagesProvider.LIMIT_MESSAGES + 1, + limitnum: ADDON_MESSAGES_LIMIT_MESSAGES + 1, }; if (forceCache) { @@ -1092,7 +1162,7 @@ export class AddonMessagesProvider { if (favourites !== undefined && favourites != null) { params.favourites = !!favourites; } - if (site.isVersionGreaterEqualThan('3.7') && type != AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP) { + if (site.isVersionGreaterEqualThan('3.7') && type != AddonMessagesMessageConversationType.GROUP) { // Add self conversation to the list. params.mergeself = true; } @@ -1113,12 +1183,12 @@ export class AddonMessagesProvider { // Format the conversations, adding some calculated fields. const conversations = response.conversations - .slice(0, AddonMessagesProvider.LIMIT_MESSAGES) + .slice(0, ADDON_MESSAGES_LIMIT_MESSAGES) .map((conversation) => this.formatConversation(conversation, userId!)); return { conversations, - canLoadMore: response.conversations.length > AddonMessagesProvider.LIMIT_MESSAGES, + canLoadMore: response.conversations.length > ADDON_MESSAGES_LIMIT_MESSAGES, }; } @@ -1145,9 +1215,9 @@ export class AddonMessagesProvider { const counts = { favourites: result.favourites, - individual: result.types[AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_INDIVIDUAL], - group: result.types[AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP], - self: result.types[AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_SELF] || 0, + individual: result.types[AddonMessagesMessageConversationType.INDIVIDUAL], + group: result.types[AddonMessagesMessageConversationType.GROUP], + self: result.types[AddonMessagesMessageConversationType.SELF] || 0, }; return counts; @@ -1190,7 +1260,7 @@ export class AddonMessagesProvider { const params: AddonMessagesGetMessagesWSParams = { useridto: site.getUserId(), useridfrom: userId, - limitnum: AddonMessagesProvider.LIMIT_MESSAGES, + limitnum: ADDON_MESSAGES_LIMIT_MESSAGES, }; if (lfReceivedUnread > 0 || lfReceivedRead > 0 || lfSentUnread > 0 || lfSentRead > 0) { @@ -1213,13 +1283,13 @@ export class AddonMessagesProvider { result.messages = result.messages.concat(sent); const hasSent = sent.length > 0; - if (result.messages.length > AddonMessagesProvider.LIMIT_MESSAGES) { + if (result.messages.length > ADDON_MESSAGES_LIMIT_MESSAGES) { // Sort messages and get the more recent ones. result.canLoadMore = true; result.messages = this.sortMessages(result['messages']); - result.messages = result.messages.slice(-AddonMessagesProvider.LIMIT_MESSAGES); + result.messages = result.messages.slice(-ADDON_MESSAGES_LIMIT_MESSAGES); } else { - result.canLoadMore = result.messages.length == AddonMessagesProvider.LIMIT_MESSAGES && (!hasReceived || !hasSent); + result.canLoadMore = result.messages.length == ADDON_MESSAGES_LIMIT_MESSAGES && (!hasReceived || !hasSent); } if (excludePending) { @@ -1290,7 +1360,7 @@ export class AddonMessagesProvider { const params: AddonMessagesGetMessagesWSParams = { useridto: currentUserId, useridfrom: 0, - limitnum: AddonMessagesProvider.LIMIT_MESSAGES, + limitnum: ADDON_MESSAGES_LIMIT_MESSAGES, }; const preSets: CoreSiteWSPreSets = { @@ -1573,9 +1643,9 @@ export class AddonMessagesProvider { counts = { favourites: result.favourites, - individual: result.types[AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_INDIVIDUAL], - group: result.types[AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP], - self: result.types[AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_SELF] || 0, + individual: result.types[AddonMessagesMessageConversationType.INDIVIDUAL], + group: result.types[AddonMessagesMessageConversationType.GROUP], + self: result.types[AddonMessagesMessageConversationType.SELF] || 0, }; } else { @@ -1593,7 +1663,7 @@ export class AddonMessagesProvider { } // Notify the new counts so all views are updated. - CoreEvents.trigger(AddonMessagesProvider.UNREAD_CONVERSATION_COUNTS_EVENT, counts, site.id); + CoreEvents.trigger(ADDON_MESSAGES_UNREAD_CONVERSATION_COUNTS_EVENT, counts, site.id); return counts; } @@ -1618,7 +1688,7 @@ export class AddonMessagesProvider { const params: AddonMessagesGetMessagesWSParams = { read: false, limitfrom: 0, - limitnum: AddonMessagesProvider.LIMIT_MESSAGES, + limitnum: ADDON_MESSAGES_LIMIT_MESSAGES, useridto: site.getUserId(), useridfrom: 0, }; @@ -2250,7 +2320,7 @@ export class AddonMessagesProvider { this.invalidateContactsCache(site.id), ]).then(() => { const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRemoved: true }; - CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); + CoreEvents.trigger(ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, data, site.id); return; }); @@ -2297,7 +2367,7 @@ export class AddonMessagesProvider { * @param query The query string. * @param userId The user ID. If not defined, current user. * @param limitFrom Position of the first result to get. Defaults to 0. - * @param limitNum Number of results to get. Defaults to AddonMessagesProvider.LIMIT_SEARCH. + * @param limitNum Number of results to get. Defaults to ADDON_MESSAGES_LIMIT_SEARCH. * @param siteId Site ID. If not defined, current site. * @returns Promise resolved with the results. */ @@ -2305,7 +2375,7 @@ export class AddonMessagesProvider { query: string, userId?: number, limitFrom: number = 0, - limitNum: number = AddonMessagesProvider.LIMIT_SEARCH, + limitNum: number = ADDON_MESSAGES_LIMIT_SEARCH, siteId?: string, ): Promise<{messages: AddonMessagesMessageAreaContact[]; canLoadMore: boolean}> { const site = await CoreSites.getSite(siteId); @@ -2350,7 +2420,7 @@ export class AddonMessagesProvider { * * @param query Text to search for. * @param limitFrom Position of the first found user to fetch. - * @param limitNum Number of found users to fetch. Defaults to AddonMessagesProvider.LIMIT_SEARCH. + * @param limitNum Number of found users to fetch. Defaults to ADDON_MESSAGES_LIMIT_SEARCH. * @param siteId Site ID. If not defined, use current site. * @returns Resolved with two lists of found users: contacts and non-contacts. * @since 3.6 @@ -2358,7 +2428,7 @@ export class AddonMessagesProvider { async searchUsers( query: string, limitFrom: number = 0, - limitNum: number = AddonMessagesProvider.LIMIT_SEARCH, + limitNum: number = ADDON_MESSAGES_LIMIT_SEARCH, siteId?: string, ): Promise<{ contacts: AddonMessagesConversationMember[]; @@ -2828,7 +2898,7 @@ export class AddonMessagesProvider { } finally { const data: AddonMessagesMemberInfoChangedEventData = { userId, userUnblocked: true }; - CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); + CoreEvents.trigger(ADDON_MESSAGES_MEMBER_INFO_CHANGED_EVENT, data, site.id); } }