MOBILE-2927 messages: Detect push in group conversations
parent
c982017029
commit
1146eea0fa
|
@ -121,8 +121,8 @@ export class AddonMessagesModule {
|
|||
// Check if we have enough information to open the conversation.
|
||||
if (notification.convid && enabled) {
|
||||
pageParams.conversationId = Number(notification.convid);
|
||||
} else if (notification.userfromid) {
|
||||
pageParams.discussionUserId = Number(notification.userfromid);
|
||||
} else if (notification.userfromid || notification.useridfrom) {
|
||||
pageParams.discussionUserId = Number(notification.userfromid || notification.useridfrom);
|
||||
}
|
||||
|
||||
linkHelper.goInSite(undefined, pageName, pageParams, notification.site);
|
||||
|
|
|
@ -229,8 +229,59 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr
|
|||
* @return {Promise<any>} Promise resolved with the notifications.
|
||||
*/
|
||||
protected fetchMessages(siteId?: string): Promise<any> {
|
||||
return this.messagesProvider.getUnreadReceivedMessages(true, false, true, siteId).then((response) => {
|
||||
return response.messages;
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
if (site.isVersionGreaterEqualThan('3.7')) {
|
||||
|
||||
// Use get conversations WS to be able to get group conversations messages.
|
||||
return this.messagesProvider.getConversations(undefined, undefined, 0, site.id, undefined, false, true)
|
||||
.then((result) => {
|
||||
|
||||
// Find the first unmuted conversation.
|
||||
const conv = result.conversations.find((conversation) => {
|
||||
return !conversation.ismuted;
|
||||
});
|
||||
|
||||
if (conv.isread) {
|
||||
// The conversation is read, no unread messages.
|
||||
return [];
|
||||
}
|
||||
|
||||
const currentUserId = site.getUserId(),
|
||||
message = conv.messages[0]; // Treat only the last message, is the one we're interested.
|
||||
|
||||
if (!message || message.useridfrom == currentUserId) {
|
||||
// No last message or not from current user. Return empty list.
|
||||
return [];
|
||||
}
|
||||
|
||||
// Add some calculated data.
|
||||
message.contexturl = '';
|
||||
message.contexturlname = '';
|
||||
message.convid = conv.id;
|
||||
message.fullmessage = message.text;
|
||||
message.fullmessageformat = 0;
|
||||
message.fullmessagehtml = '';
|
||||
message.notification = 0;
|
||||
message.read = 0;
|
||||
message.smallmessage = message.smallmessage || message.text;
|
||||
message.subject = conv.name;
|
||||
message.timecreated = message.timecreated * 1000;
|
||||
message.timeread = 0;
|
||||
message.useridto = currentUserId;
|
||||
message.usertofullname = site.getInfo().fullname;
|
||||
|
||||
const userFrom = conv.members.find((member) => {
|
||||
return member.id == message.useridfrom;
|
||||
});
|
||||
message.userfromfullname = userFrom && userFrom.fullname;
|
||||
|
||||
return [message];
|
||||
});
|
||||
} else {
|
||||
return this.messagesProvider.getUnreadReceivedMessages(true, false, true, siteId).then((response) => {
|
||||
return response.messages;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -242,7 +293,7 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr
|
|||
*/
|
||||
protected getTitleAndText(message: any): Promise<any> {
|
||||
const data = {
|
||||
title: message.userfromfullname,
|
||||
title: message.name || message.userfromfullname,
|
||||
};
|
||||
|
||||
return this.textUtils.formatText(message.text, true, true).catch(() => {
|
||||
|
|
|
@ -882,14 +882,20 @@ export class AddonMessagesProvider {
|
|||
result.messages = result.messages.slice(0, limitTo);
|
||||
}
|
||||
|
||||
let lastReceived;
|
||||
|
||||
result.messages.forEach((message) => {
|
||||
// Convert time to milliseconds.
|
||||
message.timecreated = message.timecreated ? message.timecreated * 1000 : 0;
|
||||
|
||||
if (!lastReceived && message.useridfrom != userId) {
|
||||
lastReceived = message;
|
||||
}
|
||||
});
|
||||
|
||||
if (this.appProvider.isDesktop() && params.useridto == userId && limitFrom === 0) {
|
||||
if (this.appProvider.isDesktop() && limitFrom === 0 && lastReceived) {
|
||||
// Store the last received message (we cannot know if it's unread or not). Don't block the user for this.
|
||||
this.storeLastReceivedMessageIfNeeded(conversationId, result.messages[0], site.getId());
|
||||
this.storeLastReceivedMessageIfNeeded(conversationId, lastReceived, site.getId());
|
||||
}
|
||||
|
||||
if (excludePending) {
|
||||
|
@ -923,11 +929,13 @@ export class AddonMessagesProvider {
|
|||
* @param {number} [limitFrom=0] The offset to start at.
|
||||
* @param {string} [siteId] Site ID. If not defined, use current site.
|
||||
* @param {number} [userId] User ID. If not defined, current user in the site.
|
||||
* @param {boolean} [forceCache] True if it should return cached data. Has priority over ignoreCache.
|
||||
* @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down).
|
||||
* @return {Promise<any>} Promise resolved with the conversations.
|
||||
* @since 3.6
|
||||
*/
|
||||
getConversations(type?: number, favourites?: boolean, limitFrom: number = 0, siteId?: string, userId?: number)
|
||||
: Promise<{conversations: any[], canLoadMore: boolean}> {
|
||||
getConversations(type?: number, favourites?: boolean, limitFrom: number = 0, siteId?: string, userId?: number,
|
||||
forceCache?: boolean, ignoreCache?: boolean): Promise<{conversations: any[], canLoadMore: boolean}> {
|
||||
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
userId = userId || site.getUserId();
|
||||
|
@ -941,6 +949,13 @@ export class AddonMessagesProvider {
|
|||
limitnum: this.LIMIT_MESSAGES + 1,
|
||||
};
|
||||
|
||||
if (forceCache) {
|
||||
preSets['omitExpires'] = true;
|
||||
} else if (ignoreCache) {
|
||||
preSets['getFromCache'] = false;
|
||||
preSets['emergencyCache'] = false;
|
||||
}
|
||||
|
||||
if (typeof type != 'undefined' && type != null) {
|
||||
params.type = type;
|
||||
}
|
||||
|
@ -951,8 +966,15 @@ export class AddonMessagesProvider {
|
|||
return site.read('core_message_get_conversations', params, preSets).then((response) => {
|
||||
// Format the conversations, adding some calculated fields.
|
||||
const conversations = response.conversations.slice(0, this.LIMIT_MESSAGES).map((conversation) => {
|
||||
return this.formatConversation(conversation, userId);
|
||||
});
|
||||
return this.formatConversation(conversation, userId);
|
||||
}),
|
||||
conv = conversations[0],
|
||||
lastMessage = conv && conv.messages[0];
|
||||
|
||||
if (this.appProvider.isDesktop() && limitFrom === 0 && lastMessage && !conv.sentfromcurrentuser) {
|
||||
// Store the last received message (we cannot know if it's unread or not). Don't block the user for this.
|
||||
this.storeLastReceivedMessageIfNeeded(conv.id, lastMessage, site.getId());
|
||||
}
|
||||
|
||||
return {
|
||||
conversations: conversations,
|
||||
|
|
Loading…
Reference in New Issue