MOBILE-3631 messages: Small fixes found during Peer review

main
Pau Ferrer Ocaña 2021-02-04 10:55:07 +01:00
parent 344eedfde5
commit a03e29eba3
10 changed files with 100 additions and 118 deletions

View File

@ -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.
};

View File

@ -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 {

View File

@ -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: <AddonMessagesMessageAreaContact[]> [],
loading: '',
text: '',
};

View File

@ -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<unknown>[] = [];
let data: { conversations: AddonMessagesConversationForList[]; canLoadMore: boolean } = {
conversations: [],
let data = {
conversations: <AddonMessagesConversationForList[]> [],
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<void> {
const promises: Promise<void>[] = [];
@ -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;

View File

@ -1,4 +1,4 @@
ion-app.app-root page-addon-messages-settings {
:host {
.list-header {
margin-bottom: 0;
border-top: 0;

View File

@ -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<AddonMessagesOfflineConversationMessagesDBRecord, 'conversation'> &
{
conversation?: AddonMessagesConversation; // Data about the conversation.
pending: boolean; // Will be always true.
useridfrom?: number; // User Id who send the message, will be likely us.
};

View File

@ -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<AddonMessagesOfflineAnyMessagesFormatted[]> {
const site = await CoreSites.instance.getSite(siteId);
const promises: [
Promise<AddonMessagesOfflineMessagesDBRecord[]>,
Promise<AddonMessagesOfflineConversationMessagesDBRecord[]>,
] = [
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<AddonMessagesOfflineMessagesDBRecord>(MESSAGES_TABLE, { deviceoffline: 1 }),
site.getDb().getRecords<AddonMessagesOfflineConversationMessagesDBRecord>(
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<AddonMessagesOfflineAnyMessagesFormatted[]> {
const site = await CoreSites.instance.getSite(siteId);
const promises: [
Promise<AddonMessagesOfflineMessagesDBRecord[]>,
Promise<AddonMessagesOfflineConversationMessagesDBRecord[]>,
] = [
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<AddonMessagesOfflineMessagesDBRecord>(MESSAGES_TABLE),
site.getDb().getAllRecords<AddonMessagesOfflineConversationMessagesDBRecord>(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<void> {
@ -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<AddonMessagesOfflineConversationMessagesDBRecord, 'conversation'> &
{
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;

View File

@ -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<AddonMessage
};
const groupMessagingEnabled = AddonMessages.instance.isGroupMessagingEnabled();
let messages: (AddonMessagesOfflineMessagesDBRecordFormatted | AddonMessagesOfflineConversationMessagesDBRecordFormatted)[];
let messages: AddonMessagesOfflineAnyMessagesFormatted[];
const errors: (string | CoreError | CoreTextErrorObject)[] = [];
if (conversationId) {

View File

@ -17,7 +17,12 @@ import { CoreLogger } from '@singletons/logger';
import { CoreSites } from '@services/sites';
import { CoreApp } from '@services/app';
import { CoreUser, CoreUserBasicData } from '@features/user/services/user';
import { AddonMessagesOffline } from './messages-offline';
import {
AddonMessagesOffline,
AddonMessagesOfflineAnyMessagesFormatted,
AddonMessagesOfflineConversationMessagesDBRecordFormatted,
AddonMessagesOfflineMessagesDBRecordFormatted,
} from './messages-offline';
import { CoreUtils } from '@services/utils/utils';
import { CoreTimeUtils } from '@services/utils/time';
import { CoreEvents } from '@singletons/events';
@ -25,10 +30,6 @@ import { CoreSite, CoreSiteWSPreSets } from '@classes/site';
import { CoreWSExternalWarning } from '@services/ws';
import { makeSingleton } from '@singletons';
import { CoreError } from '@classes/errors/error';
import {
AddonMessagesOfflineConversationMessagesDBRecordFormatted,
AddonMessagesOfflineMessagesDBRecordFormatted,
} from './database/messages';
import { AddonMessagesMainMenuHandlerService } from './handlers/mainmenu';
const ROOT_CACHE_KEY = 'mmaMessages:';
@ -554,7 +555,7 @@ export class AddonMessagesProvider {
* @return Promise resolved with the WS data.
* @deprecatedonmoodle since Moodle 3.6
*/
async getAllContacts(siteId?: string): Promise<AddonMessagesGetContactsResult> {
async getAllContacts(siteId?: string): Promise<AddonMessagesGetContactsWSResponse> {
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<AddonMessagesGetBlockedUsersResult> {
async getBlockedContacts(siteId?: string): Promise<AddonMessagesGetBlockedUsersWSResponse> {
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<AddonMessagesGetContactsResult> {
async getContacts(siteId?: string): Promise<AddonMessagesGetContactsWSResponse> {
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<AddonMessagesGetContactsResult>('core_message_get_contacts', undefined, preSets);
const contacts = await site.read<AddonMessagesGetContactsWSResponse>('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<AddonMessagesConversationFormatted>('core_message_get_conversation', params, preSets);
const conversation = await site.read<AddonMessagesGetConversationWSResponse>(
'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<AddonMessagesSendMessageResults> {
// Convenience function to store a message to be synchronized later.
const storeOffline = async (): Promise<{ sent: boolean; message: AddonMessagesSendInstantMessagesMessage }> => {
const storeOffline = async (): Promise<AddonMessagesSendMessageResults> => {
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<AddonMessagesSendMessageResults> {
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<AddonMessagesSendMessageResults> => {
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.
*/