diff --git a/src/addons/messages/pages/contacts-35/contacts.page.ts b/src/addons/messages/pages/contacts-35/contacts.page.ts index ce3c9260b..cb291043d 100644 --- a/src/addons/messages/pages/contacts-35/contacts.page.ts +++ b/src/addons/messages/pages/contacts-35/contacts.page.ts @@ -17,7 +17,7 @@ import { IonRefresher } from '@ionic/angular'; import { CoreSites } from '@services/sites'; import { AddonMessagesProvider, - AddonMessagesGetContactsResult, + AddonMessagesGetContactsWSResponse, AddonMessagesSearchContactsContact, AddonMessagesGetContactsContact, AddonMessages, @@ -268,6 +268,6 @@ export class AddonMessagesContacts35Page implements OnInit, OnDestroy { /** * Contacts with some calculated data. */ -export type AddonMessagesGetContactsFormatted = AddonMessagesGetContactsResult & { +export type AddonMessagesGetContactsFormatted = AddonMessagesGetContactsWSResponse & { search?: AddonMessagesSearchContactsContact[]; // Calculated in the app. Result of searching users. }; diff --git a/src/addons/messages/pages/discussion/discussion.page.ts b/src/addons/messages/pages/discussion/discussion.page.ts index eeaab6792..ecbe47ef8 100644 --- a/src/addons/messages/pages/discussion/discussion.page.ts +++ b/src/addons/messages/pages/discussion/discussion.page.ts @@ -24,15 +24,14 @@ import { AddonMessagesGetMessagesMessage, AddonMessages, AddonMessagesMemberInfoChangedEventData, - AddonMessagesSendInstantMessagesMessage, - AddonMessagesSendMessagesToConversationMessage, AddonMessagesReadChangedEventData, AddonMessagesNewMessagedEventData, AddonMessagesUpdateConversationListEventData, AddonMessagesConversationMessageFormatted, AddonMessagesOpenConversationEventData, + AddonMessagesSendMessageResults, } from '../../services/messages'; -import { AddonMessagesOffline } from '../../services/messages-offline'; +import { AddonMessagesOffline, AddonMessagesOfflineMessagesDBRecordFormatted } from '../../services/messages-offline'; import { AddonMessagesSync, AddonMessagesSyncEvents, AddonMessagesSyncProvider } from '../../services/messages-sync'; import { CoreUser } from '@features/user/services/user'; import { CoreDomUtils } from '@services/utils/dom'; @@ -49,9 +48,6 @@ import { ModalController, Translate } from '@singletons'; import { CoreNavigator } from '@services/navigator'; import { CoreIonLoadingElement } from '@classes/ion-loading'; import { ActivatedRoute } from '@angular/router'; -import { - AddonMessagesOfflineMessagesDBRecordFormatted, -} from '@addons/messages/services/database/messages'; import { AddonMessagesConversationInfoComponent } from '../../components/conversation-info/conversation-info'; /** @@ -1187,10 +1183,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView } finally { try { - let data: { - sent: boolean; - message: AddonMessagesSendMessagesToConversationMessage | AddonMessagesSendInstantMessagesMessage; - }; + let data: AddonMessagesSendMessageResults; if (this.conversationId) { data = await AddonMessages.instance.sendMessageToConversation(this.conversation!, text); } else { diff --git a/src/addons/messages/pages/discussions-35/discussions.html b/src/addons/messages/pages/discussions-35/discussions.html index eb37901dc..e5bce97cd 100644 --- a/src/addons/messages/pages/discussions-35/discussions.html +++ b/src/addons/messages/pages/discussions-35/discussions.html @@ -51,8 +51,8 @@ + [title]="discussion.fullname" (click)="gotoDiscussion(discussion.message!.user)" + [class.core-selected-item]="discussion.message!.user == discussionUserId">

{{ discussion.fullname }}

@@ -73,10 +73,10 @@ + [message]="'addon.messages.nomessagesfound' | translate"> + [message]="'core.noresults' | translate"> diff --git a/src/addons/messages/pages/discussions-35/discussions.page.ts b/src/addons/messages/pages/discussions-35/discussions.page.ts index 613a08974..b9cfa73a5 100644 --- a/src/addons/messages/pages/discussions-35/discussions.page.ts +++ b/src/addons/messages/pages/discussions-35/discussions.page.ts @@ -57,16 +57,10 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy { discussions: AddonMessagesDiscussion[] = []; discussionUserId?: number; - search: { - enabled: boolean; - showResults: boolean; - results: AddonMessagesMessageAreaContact[]; - loading: string; - text: string; - } = { + search = { enabled: false, showResults: false, - results: [], + results: [], loading: '', text: '', }; diff --git a/src/addons/messages/pages/group-conversations/group-conversations.page.ts b/src/addons/messages/pages/group-conversations/group-conversations.page.ts index b367b472d..7e965b31b 100644 --- a/src/addons/messages/pages/group-conversations/group-conversations.page.ts +++ b/src/addons/messages/pages/group-conversations/group-conversations.page.ts @@ -28,7 +28,10 @@ import { AddonMessagesNewMessagedEventData, AddonMessagesOpenConversationEventData, } from '../../services/messages'; -import { AddonMessagesOffline } from '../../services/messages-offline'; +import { + AddonMessagesOffline, + AddonMessagesOfflineAnyMessagesFormatted, +} from '../../services/messages-offline'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreUser } from '@features/user/services/user'; import { CorePushNotificationsDelegate } from '@features/pushnotifications/services/push-delegate'; @@ -38,10 +41,6 @@ import { CorePushNotificationsNotificationBasicData } from '@features/pushnotifi import { ActivatedRoute, Params } from '@angular/router'; import { CoreUtils } from '@services/utils/utils'; import { CoreNavigator } from '@services/navigator'; -import { - AddonMessagesOfflineConversationMessagesDBRecordFormatted, - AddonMessagesOfflineMessagesDBRecordFormatted, -} from '@addons/messages/services/database/messages'; import { AddonMessagesSettingsHandlerService } from '@addons/messages/services/handlers/settings'; import { CoreScreen } from '@services/screen'; @@ -402,12 +401,12 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { const limitFrom = loadingMore ? option.conversations.length : 0; const promises: Promise[] = []; - let data: { conversations: AddonMessagesConversationForList[]; canLoadMore: boolean } = { - conversations: [], + let data = { + conversations: [], canLoadMore: false, }; let offlineMessages: - (AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted)[] = []; + AddonMessagesOfflineAnyMessagesFormatted[] = []; // Get the conversations and, if needed, the offline messages. Always try to get the latest data. promises.push(AddonMessages.instance.invalidateConversations(this.siteId).then(async () => { @@ -574,7 +573,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { */ protected async loadOfflineMessages( option: AddonMessagesGroupConversationOption, - messages: (AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted)[], + messages: AddonMessagesOfflineAnyMessagesFormatted[], ): Promise { const promises: Promise[] = []; @@ -605,7 +604,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { userid: 0, // Faked data. name: message.conversation?.name, imageurl: message.conversation?.imageurl || '', - }; message.conversation || {}; + }; if (this.getConversationOption(conversation) == option) { // Message belongs to current option, add the conversation. @@ -674,7 +673,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { */ protected addLastOfflineMessage( conversation: AddonMessagesConversationForList, - message: AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted, + message: AddonMessagesOfflineAnyMessagesFormatted, ): void { conversation.lastmessage = message.text; conversation.lastmessagedate = message.timecreated / 1000; diff --git a/src/addons/messages/pages/settings/settings.scss b/src/addons/messages/pages/settings/settings.scss index 138395573..b0fc90bb0 100644 --- a/src/addons/messages/pages/settings/settings.scss +++ b/src/addons/messages/pages/settings/settings.scss @@ -1,4 +1,4 @@ -ion-app.app-root page-addon-messages-settings { +:host { .list-header { margin-bottom: 0; border-top: 0; @@ -7,4 +7,4 @@ ion-app.app-root page-addon-messages-settings { .toggle { display: inline-block; } -} \ No newline at end of file +} diff --git a/src/addons/messages/services/database/messages.ts b/src/addons/messages/services/database/messages.ts index f2626561b..75e04d639 100644 --- a/src/addons/messages/services/database/messages.ts +++ b/src/addons/messages/services/database/messages.ts @@ -13,7 +13,6 @@ // limitations under the License. import { CoreSiteSchema } from '@services/sites'; -import { AddonMessagesConversation } from '../messages'; /** * Database variables for AddonMessagesOffline service. @@ -87,11 +86,6 @@ export type AddonMessagesOfflineMessagesDBRecord = { deviceoffline: number; // If message was stored because device was offline. }; -export type AddonMessagesOfflineMessagesDBRecordFormatted = AddonMessagesOfflineMessagesDBRecord & { - pending?: boolean; // Will be likely true. - text?: string; // Copy of smallmessage. -}; - export type AddonMessagesOfflineConversationMessagesDBRecord = { conversationid: number; text: string; @@ -99,13 +93,3 @@ export type AddonMessagesOfflineConversationMessagesDBRecord = { deviceoffline: number; // If message was stored because device was offline. conversation: string; // Data about the conversation. }; - -export type AddonMessagesOfflineConversationMessagesDBRecordFormatted = - Omit & - { - conversation?: AddonMessagesConversation; // Data about the conversation. - pending: boolean; // Will be always true. - useridfrom?: number; // User Id who send the message, will be likely us. - }; - - diff --git a/src/addons/messages/services/messages-offline.ts b/src/addons/messages/services/messages-offline.ts index 6e4d5f396..73cb35d69 100644 --- a/src/addons/messages/services/messages-offline.ts +++ b/src/addons/messages/services/messages-offline.ts @@ -18,9 +18,7 @@ import { CoreApp } from '@services/app'; import { CoreTextUtils } from '@services/utils/text'; import { AddonMessagesOfflineConversationMessagesDBRecord, - AddonMessagesOfflineConversationMessagesDBRecordFormatted, AddonMessagesOfflineMessagesDBRecord, - AddonMessagesOfflineMessagesDBRecordFormatted, CONVERSATION_MESSAGES_TABLE, MESSAGES_TABLE, } from './database/messages'; @@ -94,28 +92,23 @@ export class AddonMessagesOfflineProvider { */ async getAllDeviceOfflineMessages( siteId?: string, - ): Promise<(AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted)[]> { + ): Promise { const site = await CoreSites.instance.getSite(siteId); - const promises: [ - Promise, - Promise, - ] = [ - site.getDb().getRecords(MESSAGES_TABLE, { deviceoffline: 1 }), - site.getDb().getRecords(CONVERSATION_MESSAGES_TABLE, { deviceoffline: 1 }), - ]; - const [ messages, conversations, - ]: [ - AddonMessagesOfflineMessagesDBRecord[], - AddonMessagesOfflineConversationMessagesDBRecord[], - ] = await Promise.all(promises); + ] = await Promise.all([ + site.getDb().getRecords(MESSAGES_TABLE, { deviceoffline: 1 }), + site.getDb().getRecords( + CONVERSATION_MESSAGES_TABLE, + { deviceoffline: 1 }, + ), + ]); const messageResult: - (AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted)[] = + AddonMessagesOfflineAnyMessagesFormatted[] = this.parseMessages(messages); const formattedConv = this.parseConversationMessages(conversations); @@ -130,28 +123,19 @@ export class AddonMessagesOfflineProvider { */ async getAllMessages( siteId?: string, - ): Promise<(AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted)[]> { + ): Promise { const site = await CoreSites.instance.getSite(siteId); - const promises: [ - Promise, - Promise, - ] = [ - site.getDb().getAllRecords(MESSAGES_TABLE), - site.getDb().getAllRecords(CONVERSATION_MESSAGES_TABLE), - ]; - const [ messages, conversations, - ]: [ - AddonMessagesOfflineMessagesDBRecord[], - AddonMessagesOfflineConversationMessagesDBRecord[], - ] = await Promise.all(promises); - + ] = await Promise.all([ + site.getDb().getAllRecords(MESSAGES_TABLE), + site.getDb().getAllRecords(CONVERSATION_MESSAGES_TABLE), + ]); const messageResult: - (AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted)[] = + AddonMessagesOfflineAnyMessagesFormatted[] = this.parseMessages(messages); const formattedConv = this.parseConversationMessages(conversations); @@ -348,7 +332,7 @@ export class AddonMessagesOfflineProvider { * @return Promise resolved if stored, rejected if failure. */ async setMessagesDeviceOffline( - messages: (AddonMessagesOfflineMessagesDBRecordFormatted | AddonMessagesOfflineConversationMessagesDBRecordFormatted)[], + messages: AddonMessagesOfflineAnyMessagesFormatted[], value: boolean, siteId?: string, ): Promise { @@ -381,3 +365,20 @@ export class AddonMessagesOfflineProvider { } export class AddonMessagesOffline extends makeSingleton(AddonMessagesOfflineProvider) {} + +export type AddonMessagesOfflineMessagesDBRecordFormatted = AddonMessagesOfflineMessagesDBRecord & { + pending?: boolean; // Will be likely true. + text?: string; // Copy of smallmessage. +}; + +export type AddonMessagesOfflineConversationMessagesDBRecordFormatted = + Omit & + { + conversation?: AddonMessagesConversation; // Data about the conversation. + pending: boolean; // Will be always true. + useridfrom?: number; // User Id who send the message, will be likely us. + }; + + +export type AddonMessagesOfflineAnyMessagesFormatted = + AddonMessagesOfflineConversationMessagesDBRecordFormatted | AddonMessagesOfflineMessagesDBRecordFormatted; diff --git a/src/addons/messages/services/messages-sync.ts b/src/addons/messages/services/messages-sync.ts index 1fc060fd7..be3a2b599 100644 --- a/src/addons/messages/services/messages-sync.ts +++ b/src/addons/messages/services/messages-sync.ts @@ -14,7 +14,9 @@ import { Injectable } from '@angular/core'; import { CoreSyncBaseProvider } from '@classes/base-sync'; -import { AddonMessagesOffline } from './messages-offline'; +import { + AddonMessagesOffline, AddonMessagesOfflineAnyMessagesFormatted, +} from './messages-offline'; import { AddonMessagesProvider, AddonMessages, @@ -28,10 +30,6 @@ import { CoreApp } from '@services/app'; import { CoreConstants } from '@/core/constants'; import { CoreUser } from '@features/user/services/user'; import { CoreError } from '@classes/errors/error'; -import { - AddonMessagesOfflineConversationMessagesDBRecordFormatted, - AddonMessagesOfflineMessagesDBRecordFormatted, -} from './database/messages'; import { CoreTextErrorObject, CoreTextUtils } from '@services/utils/text'; import { CoreSiteWSPreSets } from '@classes/site'; @@ -177,7 +175,7 @@ export class AddonMessagesSyncProvider extends CoreSyncBaseProvider { + async getAllContacts(siteId?: string): Promise { siteId = siteId || CoreSites.instance.getCurrentSiteId(); const contacts = await this.getContacts(siteId); @@ -580,7 +581,7 @@ export class AddonMessagesProvider { * @param siteId Site ID. If not defined, use current site. * @return Promise resolved with the WS data. */ - async getBlockedContacts(siteId?: string): Promise { + async getBlockedContacts(siteId?: string): Promise { const site = await CoreSites.instance.getSite(siteId); const userId = site.getUserId(); @@ -606,7 +607,7 @@ export class AddonMessagesProvider { * @return Promise resolved with the WS data. * @deprecatedonmoodle since Moodle 3.6 */ - async getContacts(siteId?: string): Promise { + async getContacts(siteId?: string): Promise { const site = await CoreSites.instance.getSite(siteId); const preSets: CoreSiteWSPreSets = { @@ -614,10 +615,10 @@ export class AddonMessagesProvider { updateFrequency: CoreSite.FREQUENCY_OFTEN, }; - const contacts = await site.read('core_message_get_contacts', undefined, preSets); + const contacts = await site.read('core_message_get_contacts', undefined, preSets); // Filter contacts with negative ID, they are notifications. - const validContacts: AddonMessagesGetContactsResult = { + const validContacts: AddonMessagesGetContactsWSResponse = { online: [], offline: [], strangers: [], @@ -809,7 +810,11 @@ export class AddonMessagesProvider { newestmessagesfirst: newestFirst, }; - const conversation = await site.read('core_message_get_conversation', params, preSets); + const conversation = await site.read( + 'core_message_get_conversation', + params, + preSets, + ); return this.formatConversation(conversation, userId); } @@ -2467,10 +2472,10 @@ export class AddonMessagesProvider { toUserId: number, message: string, siteId?: string, - ): Promise<{ sent: boolean; message: AddonMessagesSendInstantMessagesMessage }> { + ): Promise { // Convenience function to store a message to be synchronized later. - const storeOffline = async (): Promise<{ sent: boolean; message: AddonMessagesSendInstantMessagesMessage }> => { + const storeOffline = async (): Promise => { const entry = await AddonMessagesOffline.instance.saveMessage(toUserId, message, siteId); return { @@ -2599,13 +2604,13 @@ export class AddonMessagesProvider { conversation: AddonMessagesConversation, message: string, siteId?: string, - ): Promise<{ sent: boolean; message: AddonMessagesSendMessagesToConversationMessage }> { + ): Promise { const site = await CoreSites.instance.getSite(siteId); siteId = site.getId(); // Convenience function to store a message to be synchronized later. - const storeOffline = async(): Promise<{ sent: boolean; message: AddonMessagesSendMessagesToConversationMessage }> => { + const storeOffline = async(): Promise => { const entry = await AddonMessagesOffline.instance.saveConversationMessage(conversation, message, siteId); return { @@ -2789,21 +2794,17 @@ export class AddonMessagesProvider { * @param messages Array of messages containing the key 'timecreated'. * @return Messages sorted with most recent last. */ - sortMessages( - messages: AddonMessagesConversationMessageFormatted[], - ): AddonMessagesConversationMessageFormatted[]; + sortMessages(messages: AddonMessagesConversationMessageFormatted[]): AddonMessagesConversationMessageFormatted[]; sortMessages( messages: (AddonMessagesGetMessagesMessage | AddonMessagesOfflineMessagesDBRecordFormatted)[], ): (AddonMessagesGetMessagesMessage | AddonMessagesOfflineMessagesDBRecordFormatted)[]; - sortMessages( - messages: (AddonMessagesOfflineMessagesDBRecordFormatted | AddonMessagesOfflineConversationMessagesDBRecordFormatted)[], - ): (AddonMessagesOfflineMessagesDBRecordFormatted | AddonMessagesOfflineConversationMessagesDBRecordFormatted)[]; + sortMessages(messages: AddonMessagesOfflineAnyMessagesFormatted[]): AddonMessagesOfflineAnyMessagesFormatted[]; sortMessages( messages: (AddonMessagesGetMessagesMessage | AddonMessagesOfflineMessagesDBRecordFormatted)[] | - (AddonMessagesOfflineMessagesDBRecordFormatted | AddonMessagesOfflineConversationMessagesDBRecordFormatted)[] | + AddonMessagesOfflineAnyMessagesFormatted[] | AddonMessagesConversationMessageFormatted[], ): (AddonMessagesGetMessagesMessage | AddonMessagesOfflineMessagesDBRecordFormatted)[] | - (AddonMessagesOfflineMessagesDBRecordFormatted | AddonMessagesOfflineConversationMessagesDBRecordFormatted)[] | + AddonMessagesOfflineAnyMessagesFormatted[] | AddonMessagesConversationMessageFormatted[] { return messages.sort((a, b) => { // Pending messages last. @@ -2831,7 +2832,7 @@ export class AddonMessagesProvider { * * @param contactTypes List of contacts grouped in types. */ - protected storeUsersFromAllContacts(contactTypes: AddonMessagesGetContactsResult): void { + protected storeUsersFromAllContacts(contactTypes: AddonMessagesGetContactsWSResponse): void { for (const x in contactTypes) { CoreUser.instance.storeUsers(contactTypes[x]); } @@ -2945,6 +2946,11 @@ type AddonMessagesGetConversationWSParams = { newestmessagesfirst?: boolean; // Order messages by newest first. }; +/** + * Data returned by core_message_get_conversation WS. + */ +type AddonMessagesGetConversationWSResponse = AddonMessagesConversation; + /** * Params of core_message_get_self_conversation WS. */ @@ -2955,7 +2961,6 @@ type AddonMessagesGetSelfConversationWSParams = { newestmessagesfirst?: boolean; // Order messages by newest first. }; - /** * Conversation with some calculated data. */ @@ -3172,7 +3177,7 @@ type AddonMessagesGetBlockedUsersWSParams = { /** * Result of WS core_message_get_blocked_users. */ -export type AddonMessagesGetBlockedUsersResult = { +export type AddonMessagesGetBlockedUsersWSResponse = { users: AddonMessagesBlockedUser[]; // List of blocked users. warnings?: CoreWSExternalWarning[]; }; @@ -3189,7 +3194,7 @@ export type AddonMessagesBlockedUser = { /** * Result of WS core_message_get_contacts. */ -export type AddonMessagesGetContactsResult = { +export type AddonMessagesGetContactsWSResponse = { online: AddonMessagesGetContactsContact[]; // List of online contacts. offline: AddonMessagesGetContactsContact[]; // List of offline contacts. strangers: AddonMessagesGetContactsContact[]; // List of users that are not in the user's contact list but have sent a message. @@ -3416,6 +3421,14 @@ export type AddonMessagesSendMessagesToConversationMessage = { timecreated: number; // The timecreated timestamp for the message. }; +/** + * Result for Send Messages functions trying online or storing in offline. + */ +export type AddonMessagesSendMessageResults = { + sent: boolean; + message: AddonMessagesSendMessagesToConversationMessage | AddonMessagesSendInstantMessagesMessage; +}; + /** * Calculated data for core_message_get_contacts. */