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

View File

@ -277,11 +277,11 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
}
try {
const promises: Promise<void>[] = [];
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<void>[] = [];
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 {

View File

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

View File

@ -76,7 +76,7 @@ export class AddonMessagesProvider {
* @return Resolved when done.
* @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 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<void> {
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<void> {
async invalidateAllContactsCache(siteId?: string): Promise<void> {
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<void> {
async invalidateBlockedContactsCache(siteId?: string): Promise<void> {
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<boolean> {
async isBlocked(userId: number, siteId?: string): Promise<boolean> {
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;
});
}
/**