From a666fad0d4019ed5087be43bc26793db9ae1ff96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 27 Jan 2021 13:40:07 +0100 Subject: [PATCH] MOBILE-3631 messages: Add option to block and add contacts < 3.6 --- .../pages/contacts-35/contacts.page.ts | 4 +- .../pages/discussion/discussion.page.ts | 72 +++++++++++++------ .../services/handlers/user-add-contact.ts | 2 +- src/addons/messages/services/messages.ts | 66 +++++++++-------- 4 files changed, 89 insertions(+), 55 deletions(-) diff --git a/src/addons/messages/pages/contacts-35/contacts.page.ts b/src/addons/messages/pages/contacts-35/contacts.page.ts index efeae33c5..61e9745fe 100644 --- a/src/addons/messages/pages/contacts-35/contacts.page.ts +++ b/src/addons/messages/pages/contacts-35/contacts.page.ts @@ -41,7 +41,6 @@ import { CoreNavigator } from '@services/navigator'; }) export class AddonMessagesContacts35Page implements OnInit, OnDestroy { - protected currentUserId: number; protected searchingMessages: string; protected loadingMessages: string; protected siteId: string; @@ -67,7 +66,6 @@ export class AddonMessagesContacts35Page implements OnInit, OnDestroy { constructor( protected route: ActivatedRoute, ) { - this.currentUserId = CoreSites.instance.getCurrentSiteUserId(); this.siteId = CoreSites.instance.getCurrentSiteId(); this.searchingMessages = Translate.instance.instant('core.searching'); this.loadingMessages = Translate.instance.instant('core.loading'); @@ -140,7 +138,7 @@ export class AddonMessagesContacts35Page implements OnInit, OnDestroy { await this.performSearch(this.searchString); } else { // Update contacts. - await AddonMessages.instance.invalidateAllContactsCache(this.currentUserId); + await AddonMessages.instance.invalidateAllContactsCache(); await this.fetchData(); } } finally { diff --git a/src/addons/messages/pages/discussion/discussion.page.ts b/src/addons/messages/pages/discussion/discussion.page.ts index 9d48e5259..e423e4dd9 100644 --- a/src/addons/messages/pages/discussion/discussion.page.ts +++ b/src/addons/messages/pages/discussion/discussion.page.ts @@ -277,11 +277,11 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView } try { + const promises: Promise[] = []; if (this.groupMessagingEnabled) { // Get the conversation ID if it exists and we don't have it yet. const exists = await this.getConversation(this.conversationId, this.userId); - const promises: Promise[] = []; if (exists) { // Fetch the messages for the first time. promises.push(this.fetchMessages()); @@ -289,13 +289,14 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView if (this.userId) { // Get the member info. Invalidate first to make sure we get the latest status. - promises.push(AddonMessages.instance.invalidateMemberInfo(this.userId).then(() => - AddonMessages.instance.getMemberInfo(this.userId!)).then((member) => { - if (!exists && member) { - this.conversationImage = member.profileimageurl; - this.title = member.fullname; + promises.push(AddonMessages.instance.invalidateMemberInfo(this.userId).then(async () => { + this.otherMember = await AddonMessages.instance.getMemberInfo(this.userId!); + + if (!exists && this.otherMember) { + this.conversationImage = this.otherMember.profileimageurl; + this.title = this.otherMember.fullname; } - this.blockIcon = this.otherMember?.isblocked ? 'fas-user-lock' : 'fas-user-check'; + this.blockIcon = this.otherMember.isblocked ? 'fas-user-lock' : 'fas-user-check'; return; })); @@ -303,26 +304,55 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView this.otherMember = undefined; } - await Promise.all(promises); } else { - this.otherMember = undefined; + if (this.userId) { + // Fake the user member info. + promises.push(CoreUser.instance.getProfile(this.userId!).then(async (user) => { + this.otherMember = { + id: user.id, + fullname: user.fullname, + profileurl: '', + profileimageurl: user.profileimageurl || '', + profileimageurlsmall: user.profileimageurlsmall || '', + isonline: false, + showonlinestatus: false, + isblocked: false, + iscontact: false, + isdeleted: false, + canmessageevenifblocked: true, + canmessage: true, + requirescontact: false, + }; + this.otherMember.isblocked = await AddonMessages.instance.isBlocked(this.userId!); + this.otherMember.iscontact = await AddonMessages.instance.isContact(this.userId!); + this.blockIcon = this.otherMember.isblocked ? 'fas-user-lock' : 'fas-user-check'; + + return; + })); + + + } // Fetch the messages for the first time. - await this.fetchMessages(); - - if (!this.title && this.messages.length) { - // Didn't receive the fullname via argument. Try to get it from messages. - // It's possible that name cannot be resolved when no messages were yet exchanged. - const firstMessage = this.messages[0]; - if ('usertofullname' in firstMessage) { - if (firstMessage.useridto != this.currentUserId) { - this.title = firstMessage.usertofullname || ''; - } else { - this.title = firstMessage.userfromfullname || ''; + promises.push(this.fetchMessages().then(() => { + if (!this.title && this.messages.length) { + // Didn't receive the fullname via argument. Try to get it from messages. + // It's possible that name cannot be resolved when no messages were yet exchanged. + const firstMessage = this.messages[0]; + if ('usertofullname' in firstMessage) { + if (firstMessage.useridto != this.currentUserId) { + this.title = firstMessage.usertofullname || ''; + } else { + this.title = firstMessage.userfromfullname || ''; + } } } - } + + return; + })); } + + await Promise.all(promises); } catch (error) { CoreDomUtils.instance.showErrorModalDefault(error, 'addon.messages.errorwhileretrievingmessages', true); } finally { diff --git a/src/addons/messages/services/handlers/user-add-contact.ts b/src/addons/messages/services/handlers/user-add-contact.ts index 72dfadcdd..81fa0ac8f 100644 --- a/src/addons/messages/services/handlers/user-add-contact.ts +++ b/src/addons/messages/services/handlers/user-add-contact.ts @@ -186,7 +186,7 @@ export class AddonMessagesAddContactUserHandlerService implements CoreUserProfil */ protected async addContact(user: CoreUserProfile): Promise { if (!AddonMessages.instance.isGroupMessagingEnabled()) { - return AddonMessages.instance.addContact(user.id); + return AddonMessages.instance.createContactRequest(user.id); } const member = await AddonMessages.instance.getMemberInfo(user.id); diff --git a/src/addons/messages/services/messages.ts b/src/addons/messages/services/messages.ts index d47ac63d0..7a2c355a3 100644 --- a/src/addons/messages/services/messages.ts +++ b/src/addons/messages/services/messages.ts @@ -76,7 +76,7 @@ export class AddonMessagesProvider { * @return Resolved when done. * @deprecatedonmoodle since Moodle 3.6 */ - async addContact(userId: number, siteId?: string): Promise { + protected async addContact(userId: number, siteId?: string): Promise { const site = await CoreSites.instance.getSite(siteId); const params = { @@ -85,7 +85,7 @@ export class AddonMessagesProvider { await site.write('core_message_create_contacts', params); - await this.invalidateAllContactsCache(site.getUserId(), site.getId()); + await this.invalidateAllContactsCache(site.getId()); } /** @@ -161,12 +161,17 @@ export class AddonMessagesProvider { async createContactRequest(userId: number, siteId?: string): Promise { const site = await CoreSites.instance.getSite(siteId); - const params: AddonMessagesCreateContactRequestWSParams = { - userid: site.getUserId(), - requesteduserid: userId, - }; + // Use legacy function if not available. + if (!site.wsAvailable('core_message_create_contact_request')) { + await this.addContact(userId, site.getId()); + } else { + const params: AddonMessagesCreateContactRequestWSParams = { + userid: site.getUserId(), + requesteduserid: userId, + }; - await site.write('core_message_create_contact_request', params); + await site.write('core_message_create_contact_request', params); + } await this.invalidateAllMemberInfo(userId, site).finally(() => { const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRequestCreated: true }; @@ -1647,12 +1652,12 @@ export class AddonMessagesProvider { * @param siteId Site ID. If not defined, current site. * @return Resolved when done. */ - async invalidateAllContactsCache(userId: number, siteId?: string): Promise { + async invalidateAllContactsCache(siteId?: string): Promise { siteId = siteId || CoreSites.instance.getCurrentSiteId(); await this.invalidateContactsCache(siteId); - await this.invalidateBlockedContactsCache(userId, siteId); + await this.invalidateBlockedContactsCache(siteId); } /** @@ -1661,9 +1666,11 @@ export class AddonMessagesProvider { * @param userId The user ID. * @param siteId Site ID. If not defined, current site. */ - async invalidateBlockedContactsCache(userId: number, siteId?: string): Promise { + async invalidateBlockedContactsCache(siteId?: string): Promise { const site = await CoreSites.instance.getSite(siteId); + const userId = site.getUserId(); + await site.invalidateWsCacheForKey(this.getCacheKeyForBlockedContacts(userId)); } @@ -1875,6 +1882,7 @@ export class AddonMessagesProvider { await CoreUtils.instance.allPromises([ this.invalidateMemberInfo(userId, site.id), this.invalidateUserContacts(site.id), + this.invalidateBlockedContactsCache(site.id), this.invalidateContactRequestsCache(site.id), this.invalidateConversations(site.id), this.getConversationBetweenUsers( @@ -1938,18 +1946,19 @@ export class AddonMessagesProvider { * @param siteId Site ID. If not defined, use current site. * @return Resolved with boolean, rejected when we do not know. */ - isBlocked(userId: number, siteId?: string): Promise { + async isBlocked(userId: number, siteId?: string): Promise { if (this.isGroupMessagingEnabled()) { - return this.getMemberInfo(userId, siteId).then((member) => member.isblocked); + const member = await this.getMemberInfo(userId, siteId); + + return member.isblocked; } - return this.getBlockedContacts(siteId).then((blockedContacts) => { - if (!blockedContacts.users || blockedContacts.users.length < 1) { - return false; - } + const blockedContacts = await this.getBlockedContacts(siteId); + if (!blockedContacts.users || blockedContacts.users.length < 1) { + return false; + } - return blockedContacts.users.some((user) => userId == user.id); - }); + return blockedContacts.users.some((user) => userId == user.id); } /** @@ -2291,19 +2300,16 @@ export class AddonMessagesProvider { await site.write('core_message_delete_contacts', params, preSets); - if (this.isGroupMessagingEnabled()) { - return CoreUtils.instance.allPromises([ - this.invalidateUserContacts(site.id), - this.invalidateAllMemberInfo(userId, site), - ]).then(() => { - const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRemoved: true }; - CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); + return CoreUtils.instance.allPromises([ + this.invalidateUserContacts(site.id), + this.invalidateAllMemberInfo(userId, site), + this.invalidateContactsCache(site.id), + ]).then(() => { + const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRemoved: true }; + CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); - return; - }); - } else { - return this.invalidateContactsCache(site.id); - } + return; + }); } /**