MOBILE-3631 messages: Add option to block and add contacts < 3.6

main
Pau Ferrer Ocaña 2021-01-27 13:40:07 +01:00
parent 67666f6f17
commit a666fad0d4
4 changed files with 89 additions and 55 deletions

View File

@ -41,7 +41,6 @@ import { CoreNavigator } from '@services/navigator';
}) })
export class AddonMessagesContacts35Page implements OnInit, OnDestroy { export class AddonMessagesContacts35Page implements OnInit, OnDestroy {
protected currentUserId: number;
protected searchingMessages: string; protected searchingMessages: string;
protected loadingMessages: string; protected loadingMessages: string;
protected siteId: string; protected siteId: string;
@ -67,7 +66,6 @@ export class AddonMessagesContacts35Page implements OnInit, OnDestroy {
constructor( constructor(
protected route: ActivatedRoute, protected route: ActivatedRoute,
) { ) {
this.currentUserId = CoreSites.instance.getCurrentSiteUserId();
this.siteId = CoreSites.instance.getCurrentSiteId(); this.siteId = CoreSites.instance.getCurrentSiteId();
this.searchingMessages = Translate.instance.instant('core.searching'); this.searchingMessages = Translate.instance.instant('core.searching');
this.loadingMessages = Translate.instance.instant('core.loading'); this.loadingMessages = Translate.instance.instant('core.loading');
@ -140,7 +138,7 @@ export class AddonMessagesContacts35Page implements OnInit, OnDestroy {
await this.performSearch(this.searchString); await this.performSearch(this.searchString);
} else { } else {
// Update contacts. // Update contacts.
await AddonMessages.instance.invalidateAllContactsCache(this.currentUserId); await AddonMessages.instance.invalidateAllContactsCache();
await this.fetchData(); await this.fetchData();
} }
} finally { } finally {

View File

@ -277,11 +277,11 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
} }
try { try {
const promises: Promise<void>[] = [];
if (this.groupMessagingEnabled) { if (this.groupMessagingEnabled) {
// Get the conversation ID if it exists and we don't have it yet. // Get the conversation ID if it exists and we don't have it yet.
const exists = await this.getConversation(this.conversationId, this.userId); const exists = await this.getConversation(this.conversationId, this.userId);
const promises: Promise<void>[] = [];
if (exists) { if (exists) {
// Fetch the messages for the first time. // Fetch the messages for the first time.
promises.push(this.fetchMessages()); promises.push(this.fetchMessages());
@ -289,13 +289,14 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
if (this.userId) { if (this.userId) {
// Get the member info. Invalidate first to make sure we get the latest status. // Get the member info. Invalidate first to make sure we get the latest status.
promises.push(AddonMessages.instance.invalidateMemberInfo(this.userId).then(() => promises.push(AddonMessages.instance.invalidateMemberInfo(this.userId).then(async () => {
AddonMessages.instance.getMemberInfo(this.userId!)).then((member) => { this.otherMember = await AddonMessages.instance.getMemberInfo(this.userId!);
if (!exists && member) {
this.conversationImage = member.profileimageurl; if (!exists && this.otherMember) {
this.title = member.fullname; 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; return;
})); }));
@ -303,26 +304,55 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
this.otherMember = undefined; this.otherMember = undefined;
} }
await Promise.all(promises);
} else { } 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. // Fetch the messages for the first time.
await this.fetchMessages(); promises.push(this.fetchMessages().then(() => {
if (!this.title && this.messages.length) {
if (!this.title && this.messages.length) { // Didn't receive the fullname via argument. Try to get it from messages.
// 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.
// It's possible that name cannot be resolved when no messages were yet exchanged. const firstMessage = this.messages[0];
const firstMessage = this.messages[0]; if ('usertofullname' in firstMessage) {
if ('usertofullname' in firstMessage) { if (firstMessage.useridto != this.currentUserId) {
if (firstMessage.useridto != this.currentUserId) { this.title = firstMessage.usertofullname || '';
this.title = firstMessage.usertofullname || ''; } else {
} else { this.title = firstMessage.userfromfullname || '';
this.title = firstMessage.userfromfullname || ''; }
} }
} }
}
return;
}));
} }
await Promise.all(promises);
} catch (error) { } catch (error) {
CoreDomUtils.instance.showErrorModalDefault(error, 'addon.messages.errorwhileretrievingmessages', true); CoreDomUtils.instance.showErrorModalDefault(error, 'addon.messages.errorwhileretrievingmessages', true);
} finally { } finally {

View File

@ -186,7 +186,7 @@ export class AddonMessagesAddContactUserHandlerService implements CoreUserProfil
*/ */
protected async addContact(user: CoreUserProfile): Promise<void> { protected async addContact(user: CoreUserProfile): Promise<void> {
if (!AddonMessages.instance.isGroupMessagingEnabled()) { if (!AddonMessages.instance.isGroupMessagingEnabled()) {
return AddonMessages.instance.addContact(user.id); return AddonMessages.instance.createContactRequest(user.id);
} }
const member = await AddonMessages.instance.getMemberInfo(user.id); const member = await AddonMessages.instance.getMemberInfo(user.id);

View File

@ -76,7 +76,7 @@ export class AddonMessagesProvider {
* @return Resolved when done. * @return Resolved when done.
* @deprecatedonmoodle since Moodle 3.6 * @deprecatedonmoodle since Moodle 3.6
*/ */
async addContact(userId: number, siteId?: string): Promise<void> { protected async addContact(userId: number, siteId?: string): Promise<void> {
const site = await CoreSites.instance.getSite(siteId); const site = await CoreSites.instance.getSite(siteId);
const params = { const params = {
@ -85,7 +85,7 @@ export class AddonMessagesProvider {
await site.write('core_message_create_contacts', params); 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<void> { async createContactRequest(userId: number, siteId?: string): Promise<void> {
const site = await CoreSites.instance.getSite(siteId); const site = await CoreSites.instance.getSite(siteId);
const params: AddonMessagesCreateContactRequestWSParams = { // Use legacy function if not available.
userid: site.getUserId(), if (!site.wsAvailable('core_message_create_contact_request')) {
requesteduserid: userId, 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(() => { await this.invalidateAllMemberInfo(userId, site).finally(() => {
const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRequestCreated: true }; const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRequestCreated: true };
@ -1647,12 +1652,12 @@ export class AddonMessagesProvider {
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Resolved when done. * @return Resolved when done.
*/ */
async invalidateAllContactsCache(userId: number, siteId?: string): Promise<void> { async invalidateAllContactsCache(siteId?: string): Promise<void> {
siteId = siteId || CoreSites.instance.getCurrentSiteId(); siteId = siteId || CoreSites.instance.getCurrentSiteId();
await this.invalidateContactsCache(siteId); 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 userId The user ID.
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
*/ */
async invalidateBlockedContactsCache(userId: number, siteId?: string): Promise<void> { async invalidateBlockedContactsCache(siteId?: string): Promise<void> {
const site = await CoreSites.instance.getSite(siteId); const site = await CoreSites.instance.getSite(siteId);
const userId = site.getUserId();
await site.invalidateWsCacheForKey(this.getCacheKeyForBlockedContacts(userId)); await site.invalidateWsCacheForKey(this.getCacheKeyForBlockedContacts(userId));
} }
@ -1875,6 +1882,7 @@ export class AddonMessagesProvider {
await CoreUtils.instance.allPromises([ await CoreUtils.instance.allPromises([
this.invalidateMemberInfo(userId, site.id), this.invalidateMemberInfo(userId, site.id),
this.invalidateUserContacts(site.id), this.invalidateUserContacts(site.id),
this.invalidateBlockedContactsCache(site.id),
this.invalidateContactRequestsCache(site.id), this.invalidateContactRequestsCache(site.id),
this.invalidateConversations(site.id), this.invalidateConversations(site.id),
this.getConversationBetweenUsers( this.getConversationBetweenUsers(
@ -1938,18 +1946,19 @@ export class AddonMessagesProvider {
* @param siteId Site ID. If not defined, use current site. * @param siteId Site ID. If not defined, use current site.
* @return Resolved with boolean, rejected when we do not know. * @return Resolved with boolean, rejected when we do not know.
*/ */
isBlocked(userId: number, siteId?: string): Promise<boolean> { async isBlocked(userId: number, siteId?: string): Promise<boolean> {
if (this.isGroupMessagingEnabled()) { 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) => { const blockedContacts = await this.getBlockedContacts(siteId);
if (!blockedContacts.users || blockedContacts.users.length < 1) { if (!blockedContacts.users || blockedContacts.users.length < 1) {
return false; 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); await site.write('core_message_delete_contacts', params, preSets);
if (this.isGroupMessagingEnabled()) { return CoreUtils.instance.allPromises([
return CoreUtils.instance.allPromises([ this.invalidateUserContacts(site.id),
this.invalidateUserContacts(site.id), this.invalidateAllMemberInfo(userId, site),
this.invalidateAllMemberInfo(userId, site), this.invalidateContactsCache(site.id),
]).then(() => { ]).then(() => {
const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRemoved: true }; const data: AddonMessagesMemberInfoChangedEventData = { userId, contactRemoved: true };
CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id); CoreEvents.trigger(AddonMessagesProvider.MEMBER_INFO_CHANGED_EVENT, data, site.id);
return; return;
}); });
} else {
return this.invalidateContactsCache(site.id);
}
} }
/** /**