@@ -22,14 +22,15 @@ 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+ chatLastTime = 0 ,
33+ pollingRunning = false ;
3334
3435 $scope . loaded = false ;
3536 $scope . title = title ;
@@ -42,7 +43,6 @@ angular.module('mm.addons.mod_chat')
4243 $scope . newMessage = {
4344 text : ''
4445 } ;
45- chatLastTime = 0 ;
4646
4747 // Chat users modal.
4848 $ionicModal . fromTemplateUrl ( 'addons/mod_chat/templates/users.html' , {
@@ -87,13 +87,69 @@ angular.module('mm.addons.mod_chat')
8787 return ! $mmApp . isOnline ( ) ;
8888 } ;
8989
90+ // Convenience function to login the user.
91+ function loginUser ( ) {
92+ return $mmaModChat . loginUser ( chatId ) . then ( function ( chatsid ) {
93+ $scope . chatsid = chatsid ;
94+ } ) ;
95+ }
96+
97+ // Convenience function to get chat messages.
98+ function getMessages ( ) {
99+ return $mmaModChat . getLatestMessages ( $scope . chatsid , chatLastTime ) . then ( function ( messagesInfo ) {
100+ chatLastTime = messagesInfo . chatnewlasttime ;
101+ return $mmaModChat . getMessagesUserData ( messagesInfo . messages , courseId ) . then ( function ( messages ) {
102+ $scope . messages = $scope . messages . concat ( messages ) ;
103+ } ) ;
104+ } ) ;
105+ }
106+
90107 // Show error modal.
91108 function showError ( error , defaultMessage ) {
92109 if ( typeof error === 'string' ) {
93110 $mmUtil . showErrorModal ( error ) ;
94111 } else {
95112 $mmUtil . showErrorModal ( defaultMessage , true ) ;
96113 }
114+ return $q . reject ( ) ;
115+ }
116+
117+ // Start the polling to get chat messages periodically.
118+ function startPolling ( ) {
119+ // We already have the polling in place.
120+ if ( $scope . polling ) {
121+ return ;
122+ }
123+
124+ // Start polling.
125+ $scope . polling = $interval ( getMessagesInterval , mmaChatPollInterval ) ;
126+ }
127+
128+ // Convenience function to be called every certain time to get chat messages.
129+ function getMessagesInterval ( ) {
130+ $log . debug ( 'Polling for messages' ) ;
131+ if ( ! $mmApp . isOnline ( ) || pollingRunning ) {
132+ // Obviously we cannot check for new messages when the app is offline.
133+ return $q . reject ( ) ;
134+ }
135+
136+ pollingRunning = true ;
137+
138+ return getMessages ( ) . catch ( function ( ) {
139+ // Try to login, it might have failed because the session expired.
140+ return loginUser ( ) . then ( function ( ) {
141+ return getMessages ( ) ;
142+ } ) . catch ( function ( error ) {
143+ // Fail again. Stop polling if needed.
144+ if ( $scope . polling ) {
145+ $interval . cancel ( $scope . polling ) ;
146+ $scope . polling = undefined ;
147+ }
148+ return showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
149+ } ) ;
150+ } ) . finally ( function ( ) {
151+ pollingRunning = false ;
152+ } ) ;
97153 }
98154
99155 // Check if the date should be displayed between messages (when the day changes at midnight for example).
@@ -123,6 +179,7 @@ angular.module('mm.addons.mod_chat')
123179 if ( beep === '' ) {
124180 $scope . newMessage . text = '' ;
125181 }
182+ getMessagesInterval ( ) ; // Update messages to show the sent message.
126183 } , function ( error ) {
127184 // Only close the keyboard if an error happens, we want the user to be able to send multiple
128185 // messages withoutthe keyboard being closed.
@@ -132,16 +189,24 @@ angular.module('mm.addons.mod_chat')
132189 } ) ;
133190 } ;
134191
192+ $scope . reconnect = function ( ) {
193+ var modal = $mmUtil . showModalLoading ( ) ;
194+
195+ // Call startPolling would take a while for the first execution, so we'll execute it manually to check if it works now.
196+ return getMessagesInterval ( ) . then ( function ( ) {
197+ // It works, start the polling again.
198+ startPolling ( ) ;
199+ } ) . finally ( function ( ) {
200+ modal . dismiss ( ) ;
201+ } ) ;
202+ } ;
203+
135204 // 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' ) ;
205+ loginUser ( ) . then ( function ( ) {
206+ return getMessages ( ) . then ( function ( ) {
207+ startPolling ( ) ;
208+ } ) . catch ( function ( error ) {
209+ return showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
145210 } ) ;
146211 } , function ( error ) {
147212 showError ( error , 'mma.mod_chat.errorwhileconnecting' ) ;
@@ -161,39 +226,11 @@ angular.module('mm.addons.mod_chat')
161226 }
162227 } ;
163228
164- // Set up the polling on a view enter, this allows for the user to go back and resume the polling.
165- $scope . $on ( '$ionicView.enter' , function ( ) {
166- // Strange case, we already have the polling in place.
167- if ( polling ) {
168- return ;
169- }
170-
171- // Start polling.
172- polling = $interval ( function ( ) {
173- $log . debug ( 'Polling for messages' ) ;
174- if ( ! $mmApp . isOnline ( ) ) {
175- // Obviously we cannot check for new messages when the app is offline.
176- return ;
177- }
178-
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 ) ;
183- } ) ;
184- } , function ( error ) {
185- $interval . cancel ( polling ) ;
186- showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
187- } ) ;
188-
189- } , mmaChatPollInterval ) ;
190- } ) ;
191-
192229 // Removing the polling as we leave the page.
193- $scope . $on ( '$ionicView.leave' , function ( e ) {
194- if ( polling ) {
230+ $scope . $on ( '$ionicView.leave' , function ( ) {
231+ if ( $scope . polling ) {
195232 $log . debug ( 'Cancelling polling for conversation' ) ;
196- $interval . cancel ( polling ) ;
233+ $interval . cancel ( $scope . polling ) ;
197234 }
198235 } ) ;
199236
0 commit comments