From 6fdc60cd8800eb6a2fc94a44393bd0a036b84448 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 7 Apr 2016 12:11:02 +0200 Subject: [PATCH 1/3] MOBILE-1499 chat: Try to auto-reconnect if get messages fails --- www/addons/mod_chat/controllers/chat.js | 60 ++++++++++++++++--------- www/addons/mod_chat/services/chat.js | 9 ++-- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/www/addons/mod_chat/controllers/chat.js b/www/addons/mod_chat/controllers/chat.js index f8e45abf132..092197e4cfa 100644 --- a/www/addons/mod_chat/controllers/chat.js +++ b/www/addons/mod_chat/controllers/chat.js @@ -22,14 +22,16 @@ angular.module('mm.addons.mod_chat') * @name mmaModChatChatCtrl */ .controller('mmaModChatChatCtrl', function($scope, $stateParams, $mmApp, $mmaModChat, $log, $ionicModal, $mmUtil, $ionicHistory, - $ionicScrollDelegate, $timeout, $mmSite, $interval, mmaChatPollInterval) { + $ionicScrollDelegate, $timeout, $mmSite, $interval, mmaChatPollInterval, $q) { $log = $log.getInstance('mmaModChatChatCtrl'); var chatId = $stateParams.chatid, courseId = $stateParams.courseid, title = $stateParams.title, - polling; + polling, + chatLastTime = 0, + pollingRunning = false; $scope.loaded = false; $scope.title = title; @@ -42,7 +44,6 @@ angular.module('mm.addons.mod_chat') $scope.newMessage = { text: '' }; - chatLastTime = 0; // Chat users modal. $ionicModal.fromTemplateUrl('addons/mod_chat/templates/users.html', { @@ -87,6 +88,23 @@ angular.module('mm.addons.mod_chat') return !$mmApp.isOnline(); }; + // Convenience function to login the user. + function loginUser() { + return $mmaModChat.loginUser(chatId).then(function(chatsid) { + $scope.chatsid = chatsid; + }); + } + + // Convenience function to get chat messages. + function getMessages() { + return $mmaModChat.getLatestMessages($scope.chatsid, chatLastTime).then(function(messagesInfo) { + chatLastTime = messagesInfo.chatnewlasttime; + return $mmaModChat.getMessagesUserData(messagesInfo.messages, courseId).then(function(messages) { + $scope.messages = $scope.messages.concat(messages); + }); + }); + } + // Show error modal. function showError(error, defaultMessage) { if (typeof error === 'string') { @@ -94,6 +112,7 @@ angular.module('mm.addons.mod_chat') } else { $mmUtil.showErrorModal(defaultMessage, true); } + return $q.reject(); } // Check if the date should be displayed between messages (when the day changes at midnight for example). @@ -133,15 +152,9 @@ angular.module('mm.addons.mod_chat') }; // Login the user. - $mmaModChat.loginUser(chatId).then(function(chatsid) { - return $mmaModChat.getLatestMessages(chatsid, 0).then(function(messagesInfo) { - $scope.chatsid = chatsid; - chatLastTime = messagesInfo.chatnewlasttime; - return $mmaModChat.getMessagesUserData(messagesInfo.messages, courseId).then(function(messages) { - $scope.messages = $scope.messages.concat(messages); - }); - }).catch(function(message) { - showError(message, 'mma.mod_chat.errorwhileretrievingmessages'); + loginUser().then(function() { + return getMessages().catch(function(error) { + return showError(error, 'mma.mod_chat.errorwhileretrievingmessages'); }); }, function(error) { showError(error, 'mma.mod_chat.errorwhileconnecting'); @@ -171,26 +184,31 @@ angular.module('mm.addons.mod_chat') // Start polling. polling = $interval(function() { $log.debug('Polling for messages'); - if (!$mmApp.isOnline()) { + if (!$mmApp.isOnline() || pollingRunning) { // Obviously we cannot check for new messages when the app is offline. return; } - $mmaModChat.getLatestMessages($scope.chatsid, chatLastTime).then(function(data) { - chatLastTime = data.chatnewlasttime; - $mmaModChat.getMessagesUserData(data.messages, courseId).then(function(messages) { - $scope.messages = $scope.messages.concat(messages); + pollingRunning = true; + + return getMessages().catch(function() { + // Try to login, it might have failed because the session expired. + return loginUser().then(function() { + return getMessages(); + }).catch(function(error) { + // Fail again. Stop polling. + $interval.cancel(polling); + return showError(error, 'mma.mod_chat.errorwhileretrievingmessages'); }); - }, function(error) { - $interval.cancel(polling); - showError(error, 'mma.mod_chat.errorwhileretrievingmessages'); + }).finally(function() { + pollingRunning = false; }); }, mmaChatPollInterval); }); // Removing the polling as we leave the page. - $scope.$on('$ionicView.leave', function(e) { + $scope.$on('$ionicView.leave', function() { if (polling) { $log.debug('Cancelling polling for conversation'); $interval.cancel(polling); diff --git a/www/addons/mod_chat/services/chat.js b/www/addons/mod_chat/services/chat.js index 330974b295b..092270a427d 100644 --- a/www/addons/mod_chat/services/chat.js +++ b/www/addons/mod_chat/services/chat.js @@ -164,12 +164,13 @@ angular.module('mm.addons.mod_chat') var params = { chatsid: chatsid, chatlasttime: lasttime - }; - var preSets = { - getFromCache: false + }, preSets = { + emergencyCache: false }; - return $mmSite.read('mod_chat_get_chat_latest_messages', params, preSets); + // We use write to not use cache. It doesn't make sense to store the messages in cache + // because we won't be able to retireve them if $mmaModChat#loginUser fails. + return $mmSite.write('mod_chat_get_chat_latest_messages', params, preSets); }; /** From 3d9db80fd545d145f525458fdc33625014ac490e Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 7 Apr 2016 12:13:47 +0200 Subject: [PATCH 2/3] MOBILE-1499 chat: Decrease delay to see sent message --- www/addons/mod_chat/controllers/chat.js | 49 +++++++++++++------------ 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/www/addons/mod_chat/controllers/chat.js b/www/addons/mod_chat/controllers/chat.js index 092197e4cfa..cb74d3fdc5e 100644 --- a/www/addons/mod_chat/controllers/chat.js +++ b/www/addons/mod_chat/controllers/chat.js @@ -115,6 +115,30 @@ angular.module('mm.addons.mod_chat') return $q.reject(); } + // Convenience function to be called every certain time to get chat messages. + function getMessagesInterval() { + $log.debug('Polling for messages'); + if (!$mmApp.isOnline() || pollingRunning) { + // Obviously we cannot check for new messages when the app is offline. + return; + } + + pollingRunning = true; + + return getMessages().catch(function() { + // Try to login, it might have failed because the session expired. + return loginUser().then(function() { + return getMessages(); + }).catch(function(error) { + // Fail again. Stop polling. + $interval.cancel(polling); + return showError(error, 'mma.mod_chat.errorwhileretrievingmessages'); + }); + }).finally(function() { + pollingRunning = false; + }); + } + // Check if the date should be displayed between messages (when the day changes at midnight for example). $scope.showDate = function(message, prevMessage) { if (!prevMessage) { @@ -142,6 +166,7 @@ angular.module('mm.addons.mod_chat') if (beep === '') { $scope.newMessage.text = ''; } + getMessagesInterval(); // Update messages to show the sent message. }, function(error) { // Only close the keyboard if an error happens, we want the user to be able to send multiple // messages withoutthe keyboard being closed. @@ -182,29 +207,7 @@ angular.module('mm.addons.mod_chat') } // Start polling. - polling = $interval(function() { - $log.debug('Polling for messages'); - if (!$mmApp.isOnline() || pollingRunning) { - // Obviously we cannot check for new messages when the app is offline. - return; - } - - pollingRunning = true; - - return getMessages().catch(function() { - // Try to login, it might have failed because the session expired. - return loginUser().then(function() { - return getMessages(); - }).catch(function(error) { - // Fail again. Stop polling. - $interval.cancel(polling); - return showError(error, 'mma.mod_chat.errorwhileretrievingmessages'); - }); - }).finally(function() { - pollingRunning = false; - }); - - }, mmaChatPollInterval); + polling = $interval(getMessagesInterval, mmaChatPollInterval); }); // Removing the polling as we leave the page. From ddd56ca833f0697ff73439218387075ac13bd28c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 7 Apr 2016 12:58:40 +0200 Subject: [PATCH 3/3] MOBILE-1499 chat: Show reconnect button if WS calls fail --- www/addons/mod_chat/controllers/chat.js | 52 ++++++++++++++++--------- www/addons/mod_chat/scss/styles.scss | 8 ++++ www/addons/mod_chat/templates/chat.html | 3 +- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/www/addons/mod_chat/controllers/chat.js b/www/addons/mod_chat/controllers/chat.js index cb74d3fdc5e..5df588d9af2 100644 --- a/www/addons/mod_chat/controllers/chat.js +++ b/www/addons/mod_chat/controllers/chat.js @@ -29,7 +29,6 @@ angular.module('mm.addons.mod_chat') var chatId = $stateParams.chatid, courseId = $stateParams.courseid, title = $stateParams.title, - polling, chatLastTime = 0, pollingRunning = false; @@ -115,12 +114,23 @@ angular.module('mm.addons.mod_chat') return $q.reject(); } + // Start the polling to get chat messages periodically. + function startPolling() { + // We already have the polling in place. + if ($scope.polling) { + return; + } + + // Start polling. + $scope.polling = $interval(getMessagesInterval, mmaChatPollInterval); + } + // Convenience function to be called every certain time to get chat messages. function getMessagesInterval() { $log.debug('Polling for messages'); if (!$mmApp.isOnline() || pollingRunning) { // Obviously we cannot check for new messages when the app is offline. - return; + return $q.reject(); } pollingRunning = true; @@ -130,8 +140,11 @@ angular.module('mm.addons.mod_chat') return loginUser().then(function() { return getMessages(); }).catch(function(error) { - // Fail again. Stop polling. - $interval.cancel(polling); + // Fail again. Stop polling if needed. + if ($scope.polling) { + $interval.cancel($scope.polling); + $scope.polling = undefined; + } return showError(error, 'mma.mod_chat.errorwhileretrievingmessages'); }); }).finally(function() { @@ -176,9 +189,23 @@ angular.module('mm.addons.mod_chat') }); }; + $scope.reconnect = function() { + var modal = $mmUtil.showModalLoading(); + + // Call startPolling would take a while for the first execution, so we'll execute it manually to check if it works now. + return getMessagesInterval().then(function() { + // It works, start the polling again. + startPolling(); + }).finally(function() { + modal.dismiss(); + }); + }; + // Login the user. loginUser().then(function() { - return getMessages().catch(function(error) { + return getMessages().then(function() { + startPolling(); + }).catch(function(error) { return showError(error, 'mma.mod_chat.errorwhileretrievingmessages'); }); }, function(error) { @@ -199,22 +226,11 @@ angular.module('mm.addons.mod_chat') } }; - // Set up the polling on a view enter, this allows for the user to go back and resume the polling. - $scope.$on('$ionicView.enter', function() { - // Strange case, we already have the polling in place. - if (polling) { - return; - } - - // Start polling. - polling = $interval(getMessagesInterval, mmaChatPollInterval); - }); - // Removing the polling as we leave the page. $scope.$on('$ionicView.leave', function() { - if (polling) { + if ($scope.polling) { $log.debug('Cancelling polling for conversation'); - $interval.cancel(polling); + $interval.cancel($scope.polling); } }); diff --git a/www/addons/mod_chat/scss/styles.scss b/www/addons/mod_chat/scss/styles.scss index f7ad8f4105d..a01de76b909 100644 --- a/www/addons/mod_chat/scss/styles.scss +++ b/www/addons/mod_chat/scss/styles.scss @@ -25,3 +25,11 @@ $mma-chat-notice-badge: "stable" !default; margin: 0; width: 100%; } + +.mma-chat-send-form { + width: 100%; +} + +.mma-chat-reconnect-button, .button.mma-chat-reconnect-button { + margin: 0; +} \ No newline at end of file diff --git a/www/addons/mod_chat/templates/chat.html b/www/addons/mod_chat/templates/chat.html index 539f573cc64..038b4688260 100644 --- a/www/addons/mod_chat/templates/chat.html +++ b/www/addons/mod_chat/templates/chat.html @@ -50,7 +50,7 @@

{{ message.userfullname }}

-
+
+