/
lx-main-view-services.js
169 lines (150 loc) · 7.97 KB
/
lx-main-view-services.js
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
/*
# LexaVideo Copyright information - do not remove this copyright notice
# Copyright (C) 2015 - Alexander Marquardt
#
# LexaVideo - a fully responsive web-app featuring real-time browser-based video conferencing and text chat.
#
# Original author: Alexander Marquardt
#
# A demo version of LexaVideo can be seen at http://www.chatsurfing.com
#
# Please consider contributing your enhancements and modifications to the LexaVideo community.
# Git source code repository: https://github.com/alexander-marquardt/lexavideo
#
# 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';
/* global $ */
angular.module('lxMainView.services', [])
.factory('lxMainViewService',
function(
$location,
$log,
$routeParams,
lxAuthenticationHelper,
lxChatRoomMembersService,
lxChannelService,
lxHttpChannelService,
lxTurnService,
lxVideoService) {
var self = {
// This function logs the user out of the system.
// This requires
// 1) closing all current video sessions,
// 2) closing all currently open chat rooms,
// 3) removing login/session tokens
// 4) stopping the heartbeat to the server.
// 5) close the channel.
closeAllChatRoomsFn: function ($scope) {
var clientId, remoteClientId;
var idx;
clientId = $scope.lxMainCtrlDataObj.clientId;
lxChannelService.stopSendingHeartbeat();
lxHttpChannelService.manuallyDisconnectChannel(clientId, $scope.channelObject);
for (idx=0; idx<$scope.videoStateInfoObject.currentOpenVideoSessionsList.length; idx++) {
remoteClientId = $scope.videoStateInfoObject.currentOpenVideoSessionsList[idx];
lxVideoService.showVideoElementsAndStartVideoFn($scope, 'hangupVideoExchange',
$scope.lxMainCtrlDataObj.clientId , remoteClientId);
}
// loop over the list of room names in reverse, because we are eliminating each element
// in the list with each iteration of the loop.
var numRemoved = 0;
var listLength = $scope.normalizedOpenRoomNamesList.length;
for (idx=listLength-1; idx>=0; idx --){
var normalizedChatRoomName = $scope.normalizedOpenRoomNamesList[idx];
lxChatRoomMembersService.removeClientFromRoom($scope, normalizedChatRoomName)
.then()['finally'](
function () {
numRemoved++;
if (numRemoved == listLength) {
lxAuthenticationHelper.lxRemoveTokenAndSession();
}
}
);
}
// In order to prevent the username modal from immediately showing, we wait until
// the route has changed before clearing the clientId and userId.
var unbindRouteChange = $scope.$on('$routeChangeSuccess', function() {
$scope.lxMainCtrlDataObj.clientId = null;
$scope.lxMainCtrlDataObj.userId = null;
// re-start the userId and clientId watchers in case the user wishes to login again, without having
// to re-load the page.
self.watchUserIdThenGetClientId($scope);
self.watchClientIdThenInitializeChannel($scope);
// Remove this route change event handler.
unbindRouteChange();
});
$location.path('/');
},
// We must wait for the userId to be set before we can create a clientId. If the userId is not set,
// then a popup will be triggered by the lxMakeSureUserIsLoggedIn directive, which checks the userId
// when a chat room page is loaded and forces a login if it isn't set.
watchUserIdThenGetClientId: function($scope) {
var watchUserId = $scope.$watch(
function () {
return $scope.lxMainCtrlDataObj.userId;
},
function (userId) {
if (userId) {
if (!$scope.lxMainCtrlDataObj.clientId) {
lxAuthenticationHelper.lxCallGetAndStoreClientId($scope, userId).then(
function() {
// Kill this watcher once we have gotten the clientId
watchUserId();
}
);
}
}
}
)
},
// If clientId is set, then we can initialize the channel
watchClientIdThenInitializeChannel: function($scope) {
var watchClientIdBeforeInitializeChannel = $scope.$watch(
function () {
return $scope.lxMainCtrlDataObj.clientId;
},
function (clientId, previousClientId) {
if (clientId) {
$log.info('Calling lxChannelService.initializeChannel due to change in clientId from ' +
previousClientId + ' to ' + clientId);
lxChannelService.initializeChannel($scope);
// Since the previous clientId is no longer valid, we need to update the server to
// ensure that the new clientId is placed into all of the rooms that the client
// browser believes that he currently has open.
// This will only be a single room when the client first logs onto the website, but
// if a new clientId is allocated after the user already has a window open, then
// all the rooms that they are in should be opened on the server as they are already
// open in the client's data structures.
// We loop over this list in reverse, so that the last room open will be in position 0
// which will display that room as the currently viewed room.
if (angular.equals({}, $scope.roomOccupancyDict) && $routeParams.chatRoomName) {
// roomOccupancyDict isn't yet set up, so we pull the chatRoomName directly from the URL.
lxChatRoomMembersService.handleChatRoomName($scope, $routeParams.chatRoomName);
}
else {
for (var i = $scope.normalizedOpenRoomNamesList.length - 1; i >= 0; i--) {
var chatRoomNameAsWritten = $scope.roomOccupancyDict[$scope.normalizedOpenRoomNamesList[i]].chatRoomNameAsWritten;
lxChatRoomMembersService.handleChatRoomName($scope, chatRoomNameAsWritten);
}
}
// Kill this watch once we have initialized the channel
watchClientIdBeforeInitializeChannel();
}
}
);
}
};
return self;
}
);