@@ -22,14 +22,16 @@ angular.module('mm.addons.mod_chat')
2222 * @name mmaModChatChatCtrl
2323 */
2424. controller ( 'mmaModChatChatCtrl' , function ( $scope , $stateParams , $mmApp , $mmaModChat , $log , $ionicModal , $mmUtil , $ionicHistory ,
25- $ionicScrollDelegate , $timeout , $mmSite , $interval , mmaChatPollInterval ) {
25+ $ionicScrollDelegate , $timeout , $mmSite , $interval , mmaChatPollInterval , $q ) {
2626
2727 $log = $log . getInstance ( 'mmaModChatChatCtrl' ) ;
2828
2929 var chatId = $stateParams . chatid ,
3030 courseId = $stateParams . courseid ,
3131 title = $stateParams . title ,
32- polling ;
32+ polling ,
33+ chatLastTime = 0 ,
34+ pollingRunning = false ;
3335
3436 $scope . loaded = false ;
3537 $scope . title = title ;
@@ -42,7 +44,6 @@ angular.module('mm.addons.mod_chat')
4244 $scope . newMessage = {
4345 text : ''
4446 } ;
45- chatLastTime = 0 ;
4647
4748 // Chat users modal.
4849 $ionicModal . fromTemplateUrl ( 'addons/mod_chat/templates/users.html' , {
@@ -87,13 +88,31 @@ angular.module('mm.addons.mod_chat')
8788 return ! $mmApp . isOnline ( ) ;
8889 } ;
8990
91+ // Convenience function to login the user.
92+ function loginUser ( ) {
93+ return $mmaModChat . loginUser ( chatId ) . then ( function ( chatsid ) {
94+ $scope . chatsid = chatsid ;
95+ } ) ;
96+ }
97+
98+ // Convenience function to get chat messages.
99+ function getMessages ( ) {
100+ return $mmaModChat . getLatestMessages ( $scope . chatsid , chatLastTime ) . then ( function ( messagesInfo ) {
101+ chatLastTime = messagesInfo . chatnewlasttime ;
102+ return $mmaModChat . getMessagesUserData ( messagesInfo . messages , courseId ) . then ( function ( messages ) {
103+ $scope . messages = $scope . messages . concat ( messages ) ;
104+ } ) ;
105+ } ) ;
106+ }
107+
90108 // Show error modal.
91109 function showError ( error , defaultMessage ) {
92110 if ( typeof error === 'string' ) {
93111 $mmUtil . showErrorModal ( error ) ;
94112 } else {
95113 $mmUtil . showErrorModal ( defaultMessage , true ) ;
96114 }
115+ return $q . reject ( ) ;
97116 }
98117
99118 // 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')
133152 } ;
134153
135154 // Login the user.
136- $mmaModChat . loginUser ( chatId ) . then ( function ( chatsid ) {
137- return $mmaModChat . getLatestMessages ( chatsid , 0 ) . then ( function ( messagesInfo ) {
138- $scope . chatsid = chatsid ;
139- chatLastTime = messagesInfo . chatnewlasttime ;
140- return $mmaModChat . getMessagesUserData ( messagesInfo . messages , courseId ) . then ( function ( messages ) {
141- $scope . messages = $scope . messages . concat ( messages ) ;
142- } ) ;
143- } ) . catch ( function ( message ) {
144- showError ( message , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
155+ loginUser ( ) . then ( function ( ) {
156+ return getMessages ( ) . catch ( function ( error ) {
157+ return showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
145158 } ) ;
146159 } , function ( error ) {
147160 showError ( error , 'mma.mod_chat.errorwhileconnecting' ) ;
@@ -171,26 +184,31 @@ angular.module('mm.addons.mod_chat')
171184 // Start polling.
172185 polling = $interval ( function ( ) {
173186 $log . debug ( 'Polling for messages' ) ;
174- if ( ! $mmApp . isOnline ( ) ) {
187+ if ( ! $mmApp . isOnline ( ) || pollingRunning ) {
175188 // Obviously we cannot check for new messages when the app is offline.
176189 return ;
177190 }
178191
179- $mmaModChat . getLatestMessages ( $scope . chatsid , chatLastTime ) . then ( function ( data ) {
180- chatLastTime = data . chatnewlasttime ;
181- $mmaModChat . getMessagesUserData ( data . messages , courseId ) . then ( function ( messages ) {
182- $scope . messages = $scope . messages . concat ( messages ) ;
192+ pollingRunning = true ;
193+
194+ return getMessages ( ) . catch ( function ( ) {
195+ // Try to login, it might have failed because the session expired.
196+ return loginUser ( ) . then ( function ( ) {
197+ return getMessages ( ) ;
198+ } ) . catch ( function ( error ) {
199+ // Fail again. Stop polling.
200+ $interval . cancel ( polling ) ;
201+ return showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
183202 } ) ;
184- } , function ( error ) {
185- $interval . cancel ( polling ) ;
186- showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
203+ } ) . finally ( function ( ) {
204+ pollingRunning = false ;
187205 } ) ;
188206
189207 } , mmaChatPollInterval ) ;
190208 } ) ;
191209
192210 // Removing the polling as we leave the page.
193- $scope . $on ( '$ionicView.leave' , function ( e ) {
211+ $scope . $on ( '$ionicView.leave' , function ( ) {
194212 if ( polling ) {
195213 $log . debug ( 'Cancelling polling for conversation' ) ;
196214 $interval . cancel ( polling ) ;
0 commit comments