MOBILE-2632 message: Get conversation after sending first message

main
Dani Palou 2018-11-20 15:08:24 +01:00
parent ffc98f2c71
commit 7588808b75
4 changed files with 42 additions and 17 deletions

View File

@ -141,7 +141,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy {
const position = this.messages.findIndex((message) => { const position = this.messages.findIndex((message) => {
return message.hash == hash; return message.hash == hash;
}); });
if (position > 0) { if (position >= 0) {
this.messages.splice(position, 1); this.messages.splice(position, 1);
} }
} }
@ -176,7 +176,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy {
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.
return this.getConversation().then((exists) => { return this.getConversation(this.conversationId, this.userId).then((exists) => {
if (exists) { if (exists) {
// Fetch the messages for the first time. // Fetch the messages for the first time.
return this.fetchMessages(); return this.fetchMessages();
@ -202,6 +202,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy {
this.checkCanDelete(); this.checkCanDelete();
this.resizeContent(); this.resizeContent();
this.loaded = true; this.loaded = true;
this.setPolling(); // Make sure we're polling messages.
}); });
// Recalculate footer position when keyboard is shown or hidden. // Recalculate footer position when keyboard is shown or hidden.
@ -324,22 +325,24 @@ export class AddonMessagesDiscussionPage implements OnDestroy {
/** /**
* Get the conversation. * Get the conversation.
* *
* @param {number} conversationId Conversation ID.
* @param {number} userId User ID.
* @return {Promise<boolean>} Promise resolved with a boolean: whether the conversation exists or not. * @return {Promise<boolean>} Promise resolved with a boolean: whether the conversation exists or not.
*/ */
protected getConversation(): Promise<boolean> { protected getConversation(conversationId: number, userId: number): Promise<boolean> {
let promise; let promise;
if (this.conversationId) { if (conversationId) {
// Retrieve the conversation. Invalidate data first to get the right unreadcount. // Retrieve the conversation. Invalidate data first to get the right unreadcount.
promise = this.messagesProvider.invalidateConversation(this.conversationId).then(() => { promise = this.messagesProvider.invalidateConversation(conversationId).then(() => {
return this.messagesProvider.getConversation(this.conversationId); return this.messagesProvider.getConversation(conversationId);
}); });
} else { } else {
// We don't have the conversation ID, check if it exists. // We don't have the conversation ID, check if it exists.
promise = this.messagesProvider.getConversationBetweenUsers(this.userId).catch((error) => { promise = this.messagesProvider.getConversationBetweenUsers(userId).catch((error) => {
// Probably conversation does not exist or user is offline. Try to load offline messages. // Probably conversation does not exist or user is offline. Try to load offline messages.
return this.messagesOffline.getMessages(this.userId).then((messages) => { return this.messagesOffline.getMessages(userId).then((messages) => {
if (messages && messages.length) { if (messages && messages.length) {
// We have offline messages, this probably means that the conversation didn't exist. Don't display error. // We have offline messages, this probably means that the conversation didn't exist. Don't display error.
messages.forEach((message) => { messages.forEach((message) => {
@ -358,6 +361,7 @@ export class AddonMessagesDiscussionPage implements OnDestroy {
return promise.then((conversation) => { return promise.then((conversation) => {
this.conversation = conversation; this.conversation = conversation;
if (conversation) { if (conversation) {
this.conversationId = conversation.id; this.conversationId = conversation.id;
this.title = conversation.name; this.title = conversation.name;
@ -822,8 +826,19 @@ export class AddonMessagesDiscussionPage implements OnDestroy {
this.messagesBeingSent--; this.messagesBeingSent--;
if (data.sent) { if (data.sent) {
// Message was sent, fetch messages right now. if (!this.conversationId && data.message && data.message.conversationid) {
promise = this.fetchMessages(); // Message sent to a new conversation, try to load the conversation.
promise = this.getConversation(data.message.conversationid, this.userId).then(() => {
// Now fetch messages.
return this.fetchMessages();
}).finally(() => {
// Start polling messages now that the conversation exists.
this.setPolling();
});
} else {
// Message was sent, fetch messages right now.
promise = this.fetchMessages();
}
} else { } else {
promise = Promise.reject(null); promise = Promise.reject(null);
} }

View File

@ -97,7 +97,7 @@
<!-- Template to render a list of conversations. --> <!-- Template to render a list of conversations. -->
<ng-template #conversationsTemplate let-conversations="conversations" let-avatarOptional="avatarOptional"> <ng-template #conversationsTemplate let-conversations="conversations" let-avatarOptional="avatarOptional">
<a ion-item text-wrap *ngFor="let conversation of conversations" [title]="conversation.name" detail-none (click)="gotoConversation(conversation.id, conversation.userid)" [class.core-split-item-selected]="conversation.id == selectedConversation"> <a ion-item text-wrap *ngFor="let conversation of conversations" [title]="conversation.name" detail-none (click)="gotoConversation(conversation.id, conversation.userid)" [class.core-split-item-selected]="(conversation.id && conversation.id == selectedConversationId) || (conversation.userid && conversation.userid == selectedUserId)">
<ion-avatar item-start *ngIf="conversation.imageurl || !avatarOptional"> <ion-avatar item-start *ngIf="conversation.imageurl || !avatarOptional">
<img src="{{conversation.imageurl}}" [alt]="conversation.name" core-external-content onError="this.src='assets/img/user-avatar.png'"> <img src="{{conversation.imageurl}}" [alt]="conversation.name" core-external-content onError="this.src='assets/img/user-avatar.png'">
<!-- @todo: Display connection status. <!-- @todo: Display connection status.

View File

@ -39,7 +39,8 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
loaded = false; loaded = false;
loadingMessage: string; loadingMessage: string;
selectedConversation: number; selectedConversationId: number;
selectedUserId: number;
search = { search = {
enabled: false, enabled: false,
showResults: false, showResults: false,
@ -267,7 +268,8 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
* @param {number} [messageId] Message to scroll after loading the discussion. Used when searching. * @param {number} [messageId] Message to scroll after loading the discussion. Used when searching.
*/ */
gotoConversation(conversationId: number, userId?: number, messageId?: number): void { gotoConversation(conversationId: number, userId?: number, messageId?: number): void {
this.selectedConversation = conversationId; this.selectedConversationId = conversationId;
this.selectedUserId = userId;
const params = { const params = {
conversationId: conversationId, conversationId: conversationId,

View File

@ -1438,8 +1438,11 @@ export class AddonMessagesProvider {
} }
// Online and no messages stored. Send it to server. // Online and no messages stored. Send it to server.
return this.sendMessageOnline(toUserId, message).then(() => { return this.sendMessageOnline(toUserId, message).then((result) => {
return { sent: true }; return {
sent: true,
message: result
};
}).catch((error) => { }).catch((error) => {
if (this.utils.isWebServiceError(error)) { if (this.utils.isWebServiceError(error)) {
// It's a WebService error, the user cannot send the message so don't store it. // It's a WebService error, the user cannot send the message so don't store it.
@ -1479,6 +1482,8 @@ export class AddonMessagesProvider {
return this.invalidateDiscussionCache(toUserId, siteId).catch(() => { return this.invalidateDiscussionCache(toUserId, siteId).catch(() => {
// Ignore errors. // Ignore errors.
}).then(() => {
return response[0];
}); });
}); });
} }
@ -1543,8 +1548,11 @@ export class AddonMessagesProvider {
} }
// Online and no messages stored. Send it to server. // Online and no messages stored. Send it to server.
return this.sendMessageToConversationOnline(conversation.id, message).then(() => { return this.sendMessageToConversationOnline(conversation.id, message).then((result) => {
return { sent: true }; return {
sent: true,
message: result
};
}).catch((error) => { }).catch((error) => {
if (this.utils.isWebServiceError(error)) { if (this.utils.isWebServiceError(error)) {
// It's a WebService error, the user cannot send the message so don't store it. // It's a WebService error, the user cannot send the message so don't store it.