2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
25 constructor(userProfileService,userbarUpdateService, $log, $rootScope , $interval,$scope,$timeout,dashboardService) {
28 this.userProfileService = userProfileService;
29 this.$rootScope = $rootScope;
30 $rootScope.isAdminPortalAdmin = false;
31 $scope.updateRate = 10000; //default online user bar refreshing rate
32 var intervalPromise = null;
33 $scope.myservice = userbarUpdateService;
34 $scope.userList=this.userLists;
41 function socketSetup(initialPageVisit,_currentUser, _remoteUser, _f) {
47 var href = window.location.href;
48 var hostPatt = new RegExp(window.location.host +"/[^/]*");
49 var res = hostPatt.exec(href);
50 var protocol = window.location.protocol.replace("http","ws");
51 var signalingServerPath = protocol + "//" + res + "/opencontact";
52 var wsUri = signalingServerPath;
53 console.log("Connecting to " + wsUri);
54 websocket = new WebSocket(wsUri);
55 //localStorage.notifySocket = JSON.stringify(websocket);
56 //window.top.notifySocket = websocket;
58 currentUser = _currentUser;
59 remoteUser = _remoteUser;
65 //var websocket = JSON.parse(localStorage.notifySocket || "{}") ;
66 if( websocket != null) {
67 websocket.onopen = function(evt) { onOpen(evt); };
68 websocket.onmessage = function(evt) { onMessage(evt); };
69 websocket.onerror = function(evt) { onError(evt); };
76 //window.top.notifySocket.send("");
79 function socketSend(currentUser, remoteUser) {
83 websocket.send(JSON.stringify({
88 //window.top.notifySocket.send("");
94 function onOpen(evt) {
95 console.log("onOpen");
96 //writeToScreen("CONNECTED");
99 f(currentUser, remoteUser);
103 function onMessage(evt) {
104 if (typeof evt.data == "string") {
105 writeToScreen("RECEIVED (text): " + evt.data);
106 var chatObject = JSON.parse(evt.data);
107 if(confirm("User " + chatObject.from + " is requesting a chat session with you. Please click ok to accept")== true) {
109 var url = userProfileService.getSortedUserIdCombination(chatObject.from, chatObject.to);
110 var win = window.open(url, '_blank');
118 writeToScreen("RECEIVED (binary): " + evt.data);
122 function onError(evt) {
123 writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
126 function writeToScreen(message) {
127 console.log(message);
130 function updateActiveUsers() {
131 // $log.debug('UserbarCtrl::updateActiveUsers: intervalPromise is ' + intervalPromise);
132 // $log.info("UserbarCtrl:updateActiveUsers invoked.")
133 // decrement the counter every time updateActiveUser is called;
134 userbarUpdateService.decrementRefreshCount();
135 userProfileService.getActiveUser()
138 $log.debug('UserbarCtrl::updateActiveUsers: service returned ' + JSON.stringify(res));
140 $log.error('UserbarCtrl::updateActiveUsers: failed to get active user');
143 $scope.userList = [];
144 // defend against huge list that should never happen.
146 if (res.length < maxItems)
147 maxItems = res.length;
148 for (var i=0; i < maxItems; i++) {
151 linkQ:userProfileService.getSortedUserIdCombination(userProfileService.userProfile.orgUserId , res[i])
153 $scope.userList.push(data);
156 $log.debug('UserbarCtrl::updateActiveUsers: user list ' + JSON.stringify($scope.userList));
159 $log.error('UserbarCtrl::updateActiveUsers: caught exception: ' + err);
160 // Do not show stale result
161 $scope.userList = [];
164 var footerOff = $('#online-userbar').offset().top;
165 var headOff = $('#footer').offset().top;
166 var userbarHeight= parseInt($(".online-user-container").css('height'),10);
167 var defaultOffSet = 45;
168 $(".online-user-container").css({
169 "height" : headOff - footerOff-defaultOffSet
174 $scope.start = function(rate) {
175 // stops any running interval to avoid two intervals running at the same time
177 // store the interval promise
178 intervalPromise = $interval(updateActiveUsers, rate);
181 $scope.stop = function() {
182 if (intervalPromise != null) {
183 $interval.cancel(intervalPromise);
184 intervalPromise = null;
188 $scope.$watch('myservice.getRefreshCount()', function (newVal,oldVal) {
189 //$log.info("refresh "+$scope.myservice.refreshCount + " more time(s)");
191 // $log.info("UserbarCtrl: stopped refreshing online users");
193 } else if (newVal== $scope.myservice.maxCount){
194 // $log.info("UserbarCtrl: start refreshing online users at every "+($scope.updateRate/1000)+" sec");
195 // initial update of activate users
197 $scope.start($scope.updateRate);
201 // Load the active users.
202 //updateActiveUsers();
203 dashboardService.getOnlineUserUpdateRate().then(res=> {
204 // $log.info('getting Online User Update Rate init***********************', res);
205 if (res == null || res.response == null) {
206 $log.error('UserbarCtlr: failed to get online user update rate or duration, check system.properties file.');
208 // $log.debug('UserbarCtlr: got online User Update Rate ' + res.response);
209 var rate = parseInt(res.response.onlineUserUpdateRate);
210 // var updateRate = parseInt(res.response.onlineUserUpdateRate);
211 var duration = parseInt(res.response.onlineUserUpdateDuration);
212 userbarUpdateService.setMaxRefreshCount(parseInt(duration/rate)+1);
213 userbarUpdateService.setRefreshCount(userbarUpdateService.maxCount);
214 // $scope.refreshCount = userbarUpdateService.getRefreshCount();
216 if (rate != NaN && duration != NaN) {
217 // $log.debug('UserbarCtlr: scheduling function at interval ' + millis);
218 $scope.updateRate=rate;
219 $scope.start($scope.updateRate);
223 $log.error('UserbarCtlr: getOnlineUserUpdateRate() failed: ' + err);
227 UserbarCtrl.$inject = ['userProfileService', 'userbarUpdateService', '$log', '$rootScope','$interval','$scope','$timeout','dashboardService'];
228 angular.module('ecompApp').controller('UserbarCtrl', UserbarCtrl);