1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*-
* ================================================================================
* eCOMP Portal
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ================================================================================
*/
'use strict';
(function () {
class UserbarCtrl {
constructor(userProfileService,userbarUpdateService, $log, $rootScope , $interval,$scope,$timeout,dashboardService) {
this.$log = $log;
this.userProfileService = userProfileService;
this.$rootScope = $rootScope;
$rootScope.isAdminPortalAdmin = false;
$scope.updateRate = 10000; //default online user bar refreshing rate
var intervalPromise = null;
$scope.myservice = userbarUpdateService;
$scope.userList=this.userLists;
var websocket = '';
var currentUser = '';
var remoteUser = '';
var f = '';
function socketSetup(initialPageVisit,_currentUser, _remoteUser, _f) {
if( true) {
var href = window.location.href;
var hostPatt = new RegExp(window.location.host +"/[^/]*");
var res = hostPatt.exec(href);
var protocol = window.location.protocol.replace("http","ws");
var signalingServerPath = protocol + "//" + res + "/opencontact";
var wsUri = signalingServerPath;
console.log("Connecting to " + wsUri);
websocket = new WebSocket(wsUri);
//localStorage.notifySocket = JSON.stringify(websocket);
//window.top.notifySocket = websocket;
currentUser = _currentUser;
remoteUser = _remoteUser;
f = socketSend;
}
//var websocket = JSON.parse(localStorage.notifySocket || "{}") ;
if( websocket != null) {
websocket.onopen = function(evt) { onOpen(evt); };
websocket.onmessage = function(evt) { onMessage(evt); };
websocket.onerror = function(evt) { onError(evt); };
}
//if(f != undefined)
// f();
//window.top.notifySocket.send("");
}
function socketSend(currentUser, remoteUser) {
websocket.send(JSON.stringify({
from: currentUser,
to: remoteUser
}));
//window.top.notifySocket.send("");
}
function onOpen(evt) {
console.log("onOpen");
//writeToScreen("CONNECTED");
if(f != undefined)
f(currentUser, remoteUser);
}
function onMessage(evt) {
if (typeof evt.data == "string") {
writeToScreen("RECEIVED (text): " + evt.data);
var chatObject = JSON.parse(evt.data);
if(confirm("User " + chatObject.from + " is requesting a chat session with you. Please click ok to accept")== true) {
var url = userProfileService.getSortedUserIdCombination(chatObject.from, chatObject.to);
var win = window.open(url, '_blank');
win.focus();
} else {
//
}
} else {
writeToScreen("RECEIVED (binary): " + evt.data);
}
}
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function writeToScreen(message) {
console.log(message);
}
function callAtInterval() {
userProfileService.getActiveUser()
.then(res=> {
$log.info('sasasasasa getting userinfo from shared context.. ');
if(res==null){
$log.info('failed getting userinfo from shared context.. ');
$log.info('getting userinfo from session ');
}else{
$log.info('getting ActiveUser successfully',res);
$scope.userList = [];
for(var i=0;i<res.length;i++){
var data= {
userId:res[i][0],
linkQ:userProfileService.getSortedUserIdCombination(userProfileService.userProfile.userId , res[i][0])
}
$scope.userList.push(data);
}
$log.info(' ActiveUser =',$scope.userList);
}
}).catch(err=> {
$log.error('Header Controller:: getActiveUser() failed: ' + err);
}).finally(() => {
var footerOff = $('#online-userbar').offset().top;
var headOff = $('#footer').offset().top;
var userbarHeight= parseInt($(".online-user-container").css('height'),10);
var defaultOffSet = 45;
$(".online-user-container").css({
"height" : headOff - footerOff-defaultOffSet
});
});
}
var intervalPromise;
dashboardService.getOnlineUserUpdateRate().then(res=> {
// $log.info('getting Online User Update Rate init***********************', res);
if (res == null || res.response == null) {
$log.error('UserbarCtlr: failed to get online user update rate!');
} else {
// $log.debug('UserbarCtlr: got online User Update Rate ' + res.response);
var rate = parseInt(res.response.onlineUserUpdateRate);
// var updateRate = parseInt(res.response.onlineUserUpdateRate);
var duration = parseInt(res.response.onlineUserUpdateDuration);
userbarUpdateService.setMaxRefreshCount(parseInt(duration/rate)+1);
userbarUpdateService.setRefreshCount(userbarUpdateService.maxCount);
// $scope.refreshCount = userbarUpdateService.getRefreshCount();
if (rate != NaN && duration != NaN) {
// $log.debug('UserbarCtlr: scheduling function at interval ' + millis);
$scope.updateRate=rate;
$scope.start($scope.updateRate);
}
}
}).catch(err=> {
$log.error('Header Controller:: getOnlineUserUpdateRate() failed: ' + err);
});
$scope.start = function(rate) {
// stops any running interval to avoid two intervals running at the same time
$scope.stop();
// store the interval promise
intervalPromise = $interval(updateActiveUsers, rate);
};
$scope.stop = function() {
$interval.cancel(intervalPromise);
};
$scope.$watch('myservice.getRefreshCount()', function (newVal,oldVal) {
//$log.info("refresh "+$scope.myservice.refreshCount + " more time(s)");
if (newVal<=0) {
// $log.info("UserbarCtrl: stopped refreshing online users");
$scope.stop();
} else if (newVal== $scope.myservice.maxCount){
// $log.info("UserbarCtrl: start refreshing online users at every "+($scope.updateRate/1000)+" sec");
// initial update of activate users
userProfileService.getUserProfile().then(res=> {
if (res == null || res.orgUserId == null) {
$log.error('UserbarCtlr: failed to get profile of the user!');
}
else {
updateActiveUsers();
$scope.start($scope.updateRate);
socketSetup(null, res.orgUserId, null, "socketSend");
}
});
}
});
function updateActiveUsers() {
userProfileService.getActiveUser()
.then(res=> {
$log.info('getting Active User init***********************');
// decrement the counter every time updateActiveUser is called;
userbarUpdateService.decrementRefreshCount();
if(res==null ||res.status!='OK'){
$log.error('failed updateActiveUsers res is null.. ');
}else{
$log.info('getting ActiveUser successfully',res);
$scope.userList = [];
var maxItems = 10;
var users = res.response;
if (users.length < maxItems)
maxItems = users.length;
for(var i=0;i<maxItems;i++){
var data= {
userId:users[i][0],
linkQ:userProfileService.getSortedUserIdCombination(userProfileService.userProfile.orgUserId , users[i][0])
}
$scope.userList.push(data);
}
$log.info(' ActiveUser =',$scope.userList);
}
}).catch(err=> {
$log.error('Header Controller:: getActiveUser() failed: ' + err);
}).finally(() => {
var footerOff = $('#online-userbar').offset().top;
var headOff = $('#footer').offset().top;
var userbarHeight= parseInt($(".online-user-container").css('height'),10);
var defaultOffSet = 45;
$(".online-user-container").css({
"height" : headOff - footerOff-defaultOffSet
});
});
}
}
}
UserbarCtrl.$inject = ['userProfileService', 'userbarUpdateService', '$log', '$rootScope','$interval','$scope','$timeout','dashboardService'];
angular.module('ecompApp').controller('UserbarCtrl', UserbarCtrl);
})();
|