2015-01-16 13:54:37 -07:00
|
|
|
|
(function (globalScope) {
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (!globalScope.MediaBrowser) {
|
|
|
|
|
globalScope.MediaBrowser = {};
|
|
|
|
|
}
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
globalScope.MediaBrowser.ConnectionState = {
|
2015-02-15 20:05:21 -07:00
|
|
|
|
Unavailable: 0,
|
2014-10-23 21:54:35 -07:00
|
|
|
|
ServerSelection: 1,
|
|
|
|
|
ServerSignIn: 2,
|
2014-10-29 15:01:02 -07:00
|
|
|
|
SignedIn: 3,
|
|
|
|
|
ConnectSignIn: 4
|
2014-10-23 21:54:35 -07:00
|
|
|
|
};
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
globalScope.MediaBrowser.ConnectionMode = {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
Local: 0,
|
2015-02-15 17:33:06 -07:00
|
|
|
|
Remote: 1,
|
|
|
|
|
Manual: 2
|
2014-10-23 21:54:35 -07:00
|
|
|
|
};
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
globalScope.MediaBrowser.ConnectionManager = function (logger, credentialProvider, appName, appVersion, deviceName, deviceId, capabilities) {
|
2015-01-16 13:54:37 -07:00
|
|
|
|
|
|
|
|
|
logger.log('Begin MediaBrowser.ConnectionManager constructor');
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
|
|
|
|
var self = this;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
var apiClients = [];
|
|
|
|
|
|
|
|
|
|
function mergeServers(list1, list2) {
|
|
|
|
|
|
|
|
|
|
for (var i = 0, length = list2.length; i < length; i++) {
|
|
|
|
|
credentialProvider.addOrUpdateServer(list1, list2[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveWithFailure(deferred) {
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [
|
|
|
|
|
{
|
2015-02-15 20:05:21 -07:00
|
|
|
|
State: MediaBrowser.ConnectionState.Unavailable,
|
|
|
|
|
ConnectUser: self.connectUser()
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}]);
|
|
|
|
|
}
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
function updateServerInfo(server, systemInfo) {
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
server.Name = systemInfo.ServerName;
|
|
|
|
|
server.Id = systemInfo.Id;
|
|
|
|
|
|
|
|
|
|
if (systemInfo.LocalAddress) {
|
|
|
|
|
server.LocalAddress = systemInfo.LocalAddress;
|
|
|
|
|
}
|
|
|
|
|
if (systemInfo.WanAddress) {
|
|
|
|
|
server.RemoteAddress = systemInfo.WanAddress;
|
|
|
|
|
}
|
|
|
|
|
if (systemInfo.MacAddress) {
|
|
|
|
|
server.WakeOnLanInfos = [
|
|
|
|
|
{ MacAddress: systemInfo.MacAddress }
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-20 22:57:06 -07:00
|
|
|
|
function tryConnect(url, timeout) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
url += "/system/info/public";
|
|
|
|
|
|
|
|
|
|
logger.log('tryConnect url: ' + url);
|
|
|
|
|
|
|
|
|
|
return AjaxApi.ajax({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
type: "GET",
|
2015-02-15 17:33:06 -07:00
|
|
|
|
url: url,
|
2014-10-23 21:54:35 -07:00
|
|
|
|
dataType: "json",
|
|
|
|
|
|
2014-12-20 22:57:06 -07:00
|
|
|
|
timeout: timeout || 15000
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connectUser;
|
|
|
|
|
self.connectUser = function () {
|
|
|
|
|
return connectUser;
|
2014-10-21 05:42:02 -07:00
|
|
|
|
};
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
self.appVersion = function () {
|
2014-12-02 20:13:03 -07:00
|
|
|
|
return appVersion;
|
2014-10-25 11:32:58 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.deviceId = function () {
|
|
|
|
|
return deviceId;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.currentApiClient = function () {
|
|
|
|
|
|
|
|
|
|
return apiClients[0];
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-16 13:54:37 -07:00
|
|
|
|
self.connectUserId = function () {
|
|
|
|
|
return credentialProvider.credentials().ConnectUserId;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.connectToken = function () {
|
|
|
|
|
|
|
|
|
|
return credentialProvider.credentials().ConnectAccessToken;
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-28 16:17:55 -07:00
|
|
|
|
self.addApiClient = function (apiClient, enableAutomaticNetworking) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
|
|
|
|
apiClients.push(apiClient);
|
|
|
|
|
|
2014-11-18 19:45:12 -07:00
|
|
|
|
return apiClient.getPublicSystemInfo().done(function (systemInfo) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2014-10-28 16:17:55 -07:00
|
|
|
|
var server = credentialProvider.credentials().servers.filter(function (s) {
|
|
|
|
|
|
|
|
|
|
return s.Id == systemInfo.Id;
|
|
|
|
|
|
|
|
|
|
})[0] || {};
|
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
updateServerInfo(server, systemInfo);
|
|
|
|
|
|
|
|
|
|
apiClient.serverInfo(server);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
Events.trigger(self, 'apiclientcreated', [apiClient]);
|
2014-10-28 16:17:55 -07:00
|
|
|
|
|
|
|
|
|
if (enableAutomaticNetworking) {
|
|
|
|
|
self.connectToServer(server);
|
|
|
|
|
}
|
2014-10-25 11:32:58 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function onConnectUserSignIn(user) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
connectUser = user;
|
2015-02-15 17:33:06 -07:00
|
|
|
|
Events.trigger(self, 'connectusersignedin', [user]);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getOrAddApiClient(server, connectionMode) {
|
|
|
|
|
|
|
|
|
|
var apiClient = self.getApiClient(server.Id);
|
|
|
|
|
|
|
|
|
|
if (!apiClient) {
|
|
|
|
|
|
|
|
|
|
var url = connectionMode == MediaBrowser.ConnectionMode.Local ? server.LocalAddress : server.RemoteAddress;
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
apiClient = new MediaBrowser.ApiClient(logger, url, appName, appVersion, deviceName, deviceId, capabilities);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
apiClients.push(apiClient);
|
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
apiClient.serverInfo(server);
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
Events.on(apiClient, 'authenticated', function (e, result) {
|
|
|
|
|
onAuthenticated(this, result, {}, true);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
});
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
Events.trigger(self, 'apiclientcreated', [apiClient]);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (server.AccessToken) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
apiClient.setAuthenticationInfo(server.AccessToken, server.UserId);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
apiClient.clearAuthenticationInfo();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
logger.log('returning instance from getOrAddApiClient');
|
2014-10-23 21:54:35 -07:00
|
|
|
|
return apiClient;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 23:12:48 -07:00
|
|
|
|
self.getOrCreateApiClient = function (serverId) {
|
|
|
|
|
|
|
|
|
|
var apiClient = self.getApiClient(serverId);
|
|
|
|
|
|
|
|
|
|
if (apiClient) {
|
|
|
|
|
return apiClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
var server = credentials.servers.filter(function (s) {
|
|
|
|
|
return stringEqualsIgnoreCase(s.Id, serverId);
|
|
|
|
|
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
return getOrAddApiClient(server, server.LastConnectionMode);
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function onAuthenticated(apiClient, result, options, saveCredentials) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var server = apiClient.serverInfo;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var credentials = credentialProvider.credentials();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
server.DateLastAccessed = new Date().getTime();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (saveCredentials) {
|
|
|
|
|
server.UserId = result.User.Id;
|
|
|
|
|
server.AccessToken = result.AccessToken;
|
|
|
|
|
} else {
|
|
|
|
|
server.UserId = null;
|
|
|
|
|
server.AccessToken = null;
|
|
|
|
|
}
|
2014-10-26 17:13:47 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
credentials.addOrUpdateServer(credentials.servers, server);
|
|
|
|
|
saveUserInfoIntoCredentials(server, result.User);
|
|
|
|
|
credentialProvider.credentials(credentials);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
afterConnected(apiClient, options);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
onLocalUserSignIn(result.User);
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function saveUserInfoIntoCredentials(server, user) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
//ServerUserInfo info = new ServerUserInfo();
|
|
|
|
|
//info.setIsSignedInOffline(true);
|
|
|
|
|
//info.setId(user.getId());
|
|
|
|
|
|
|
|
|
|
//// Record user info here
|
|
|
|
|
//server.AddOrUpdate(info);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function afterConnected(apiClient, options) {
|
|
|
|
|
|
|
|
|
|
options = options || {};
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (options.reportCapabilities !== false) {
|
|
|
|
|
apiClient.reportCapabilities(capabilities);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.enableWebSocket !== false) {
|
|
|
|
|
if (!apiClient.isWebSocketOpenOrConnecting && apiClient.isWebSocketSupported()) {
|
|
|
|
|
logger.log('calling apiClient.openWebSocket');
|
|
|
|
|
|
|
|
|
|
apiClient.openWebSocket();
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onLocalUserSignIn(user) {
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
Events.trigger(self, 'localusersignedin', [user]);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ensureConnectUser(credentials) {
|
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-12-03 22:24:41 -07:00
|
|
|
|
if (connectUser && connectUser.Id == credentials.ConnectUserId) {
|
2014-10-30 21:57:24 -07:00
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
else if (credentials.ConnectUserId && credentials.ConnectAccessToken) {
|
2014-10-30 21:57:24 -07:00
|
|
|
|
|
|
|
|
|
connectUser = null;
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
getConnectUser(credentials.ConnectUserId, credentials.ConnectAccessToken).done(function (user) {
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
onConnectUserSignIn(user);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
});
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
deferred.resolveWith(null, [[]]);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getConnectUser(userId, accessToken) {
|
|
|
|
|
|
2014-11-04 16:50:26 -07:00
|
|
|
|
if (!userId) {
|
|
|
|
|
throw new Error("null userId");
|
|
|
|
|
}
|
|
|
|
|
if (!accessToken) {
|
|
|
|
|
throw new Error("null accessToken");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
var url = "https://connect.mediabrowser.tv/service/user?id=" + userId;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return AjaxApi.ajax({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
type: "GET",
|
|
|
|
|
url: url,
|
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
2014-12-03 22:24:41 -07:00
|
|
|
|
"X-Application": appName + "/" + appVersion,
|
|
|
|
|
"X-Connect-UserToken": accessToken
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addAuthenticationInfoFromConnect(server, connectionMode, credentials) {
|
|
|
|
|
|
2014-11-11 21:51:40 -07:00
|
|
|
|
if (!server.ExchangeToken) {
|
|
|
|
|
throw new Error("server.ExchangeToken cannot be null");
|
|
|
|
|
}
|
|
|
|
|
if (!credentials.ConnectUserId) {
|
|
|
|
|
throw new Error("credentials.ConnectUserId cannot be null");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
var url = connectionMode == MediaBrowser.ConnectionMode.Local ? server.LocalAddress : server.RemoteAddress;
|
|
|
|
|
|
2015-01-17 12:30:23 -07:00
|
|
|
|
url += "/Connect/Exchange?format=json&ConnectUserId=" + credentials.ConnectUserId;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return AjaxApi.ajax({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
type: "GET",
|
|
|
|
|
url: url,
|
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
|
|
|
|
"X-MediaBrowser-Token": server.ExchangeToken
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).done(function (auth) {
|
|
|
|
|
|
|
|
|
|
server.UserId = auth.LocalUserId;
|
|
|
|
|
server.AccessToken = auth.AccessToken;
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
|
|
|
|
server.UserId = null;
|
|
|
|
|
server.AccessToken = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateAuthentication(server, connectionMode) {
|
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
var url = connectionMode == MediaBrowser.ConnectionMode.Local ? server.LocalAddress : server.RemoteAddress;
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
AjaxApi.ajax({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
type: "GET",
|
2015-01-17 12:30:23 -07:00
|
|
|
|
url: url + "/system/info",
|
2014-10-23 21:54:35 -07:00
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
|
|
|
|
"X-MediaBrowser-Token": server.AccessToken
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).done(function (systemInfo) {
|
|
|
|
|
|
|
|
|
|
updateServerInfo(server, systemInfo);
|
|
|
|
|
|
|
|
|
|
if (server.UserId) {
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
AjaxApi.ajax({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
type: "GET",
|
2015-01-17 12:30:23 -07:00
|
|
|
|
url: url + "/users/" + server.UserId,
|
2014-10-23 21:54:35 -07:00
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
|
|
|
|
"X-MediaBrowser-Token": server.AccessToken
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).done(function (user) {
|
|
|
|
|
|
|
|
|
|
onLocalUserSignIn(user);
|
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
|
|
|
|
server.UserId = null;
|
|
|
|
|
server.AccessToken = null;
|
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
|
|
|
|
server.UserId = null;
|
|
|
|
|
server.AccessToken = null;
|
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
function getImageUrl(localUser) {
|
2014-10-27 14:45:50 -07:00
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
if (connectUser && connectUser.ImageUrl) {
|
|
|
|
|
return {
|
|
|
|
|
url: connectUser.ImageUrl
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (localUser.PrimaryImageTag) {
|
|
|
|
|
|
|
|
|
|
var apiClient = self.getApiClient(localUser);
|
|
|
|
|
|
|
|
|
|
var url = apiClient.getUserImageUrl(localUser.Id, {
|
|
|
|
|
tag: localUser.PrimaryImageTag,
|
|
|
|
|
type: "Primary"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
url: url,
|
|
|
|
|
supportsImageParams: true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
url: null,
|
|
|
|
|
supportsImageParams: false
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
self.user = function () {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
|
|
|
|
var localUser;
|
|
|
|
|
|
|
|
|
|
function onLocalUserDone() {
|
|
|
|
|
|
|
|
|
|
var image = getImageUrl(localUser);
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [
|
|
|
|
|
{
|
|
|
|
|
localUser: localUser,
|
|
|
|
|
name: connectUser ? connectUser.Name : localUser.Name,
|
2014-12-19 23:06:27 -07:00
|
|
|
|
canManageServer: localUser && localUser.Policy.IsAdministrator,
|
2014-10-25 11:32:58 -07:00
|
|
|
|
imageUrl: image.url,
|
|
|
|
|
supportsImageParams: image.supportsParams
|
|
|
|
|
}]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onEnsureConnectUserDone() {
|
2014-10-27 14:45:50 -07:00
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
var apiClient = self.currentApiClient();
|
|
|
|
|
if (apiClient && apiClient.getCurrentUserId()) {
|
|
|
|
|
apiClient.getUser(apiClient.getCurrentUserId()).done(function (u) {
|
|
|
|
|
localUser = u;
|
|
|
|
|
}).always(onLocalUserDone);
|
|
|
|
|
} else {
|
|
|
|
|
onLocalUserDone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
|
2014-10-30 21:57:24 -07:00
|
|
|
|
if (credentials.ConnectUserId && credentials.ConnectAccessToken && !(self.currentApiClient() && self.currentApiClient().getCurrentUserId())) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
ensureConnectUser(credentials).always(onEnsureConnectUserDone);
|
|
|
|
|
} else {
|
|
|
|
|
onEnsureConnectUserDone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-21 05:42:02 -07:00
|
|
|
|
self.isLoggedIntoConnect = function () {
|
|
|
|
|
|
|
|
|
|
return self.connectToken() && self.connectUserId();
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
self.logout = function () {
|
|
|
|
|
|
|
|
|
|
var promises = [];
|
|
|
|
|
|
2014-10-29 18:17:31 -07:00
|
|
|
|
for (var i = 0, length = apiClients.length; i < length; i++) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
var apiClient = apiClients[i];
|
|
|
|
|
|
|
|
|
|
if (apiClient.accessToken()) {
|
|
|
|
|
promises.push(apiClient.logout());
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
return DeferredBuilder.when(promises).done(function () {
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
|
2014-10-29 18:17:31 -07:00
|
|
|
|
var servers = credentials.servers.filter(function (u) {
|
|
|
|
|
return u.UserLinkType != "Guest";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (var j = 0, numServers = servers.length; j < numServers; j++) {
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
|
|
|
|
var server = servers[j];
|
|
|
|
|
server.UserId = null;
|
|
|
|
|
server.AccessToken = null;
|
|
|
|
|
server.ExchangeToken = null;
|
|
|
|
|
|
|
|
|
|
var serverUsers = server.Users || [];
|
|
|
|
|
|
|
|
|
|
for (var k = 0, numUsers = serverUsers.length; k < numUsers; k++) {
|
|
|
|
|
|
|
|
|
|
serverUsers[k].IsSignedInOffline = false;
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-29 18:17:31 -07:00
|
|
|
|
credentials.servers = servers;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
credentials.ConnectAccessToken = null;
|
|
|
|
|
credentials.ConnectUserId = null;
|
|
|
|
|
|
|
|
|
|
credentialProvider.credentials(credentials);
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (connectUser) {
|
|
|
|
|
connectUser = null;
|
|
|
|
|
Events.trigger(self, 'connectusersignedout');
|
|
|
|
|
}
|
2014-10-29 18:17:31 -07:00
|
|
|
|
});
|
2014-10-15 20:26:39 -07:00
|
|
|
|
};
|
2014-10-21 05:42:02 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
function getConnectServers(credentials) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-01-16 13:54:37 -07:00
|
|
|
|
logger.log('Begin getConnectServers');
|
2014-11-04 16:50:26 -07:00
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2014-10-21 05:42:02 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
if (!credentials.ConnectAccessToken || !credentials.ConnectUserId) {
|
2015-01-16 13:54:37 -07:00
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
var url = "https://connect.mediabrowser.tv/service/servers?userId=" + credentials.ConnectUserId;
|
2014-10-21 05:42:02 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
AjaxApi.ajax({
|
2014-10-21 05:42:02 -07:00
|
|
|
|
type: "GET",
|
|
|
|
|
url: url,
|
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
2014-12-03 22:24:41 -07:00
|
|
|
|
"X-Application": appName + "/" + appVersion,
|
2015-02-16 13:56:57 -07:00
|
|
|
|
"X-Connect-UserToken": credentials.ConnectAccessToken
|
2014-10-21 05:42:02 -07:00
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
}).done(function (servers) {
|
|
|
|
|
|
|
|
|
|
servers = servers.map(function (i) {
|
|
|
|
|
return {
|
|
|
|
|
ExchangeToken: i.AccessKey,
|
2014-11-18 19:45:12 -07:00
|
|
|
|
ConnectServerId: i.Id,
|
2014-10-23 21:54:35 -07:00
|
|
|
|
Id: i.SystemId,
|
|
|
|
|
Name: i.Name,
|
|
|
|
|
RemoteAddress: i.Url,
|
2014-10-29 18:17:31 -07:00
|
|
|
|
LocalAddress: i.LocalAddress,
|
|
|
|
|
UserLinkType: (i.UserType || '').toLowerCase() == "guest" ? "Guest" : "LinkedUser"
|
2014-10-23 21:54:35 -07:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [servers]);
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
deferred.resolveWith(null, [[]]);
|
|
|
|
|
|
2014-10-21 05:42:02 -07:00
|
|
|
|
});
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
self.getAvailableServers = function () {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
logger.log('Begin getAvailableServers');
|
2015-01-16 13:54:37 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
// Clone the array
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
var connectServersPromise = getConnectServers(credentials);
|
|
|
|
|
var findServersPromise = findServers();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
connectServersPromise.done(function (connectServers) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
findServersPromise.done(function (foundServers) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
var servers = credentials.servers.slice(0);
|
|
|
|
|
mergeServers(servers, foundServers);
|
|
|
|
|
mergeServers(servers, connectServers);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
servers = filterServers(servers, connectServers);
|
|
|
|
|
|
|
|
|
|
servers.sort(function (a, b) {
|
|
|
|
|
return b.DateLastAccessed - a.DateLastAccessed;
|
|
|
|
|
});
|
2015-01-16 13:54:37 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
credentials.servers = servers;
|
|
|
|
|
|
|
|
|
|
credentialProvider.credentials(credentials);
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [servers]);
|
|
|
|
|
});
|
2014-10-23 21:54:35 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
2014-10-21 05:42:02 -07:00
|
|
|
|
};
|
2014-10-15 20:26:39 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
function filterServers(servers, connectServers) {
|
|
|
|
|
|
|
|
|
|
return servers.filter(function (server) {
|
|
|
|
|
|
|
|
|
|
// It's not a connect server, so assume it's still valid
|
|
|
|
|
if (!server.ExchangeToken) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return connectServers.filter(function (connectServer) {
|
|
|
|
|
|
|
|
|
|
return server.Id == connectServer.Id;
|
|
|
|
|
|
|
|
|
|
}).length > 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findServers() {
|
|
|
|
|
|
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2015-02-16 20:49:47 -07:00
|
|
|
|
ServerDiscovery.findServers(2000).done(function (foundServers) {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
|
|
|
|
|
var servers = foundServers.map(function (foundServer) {
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
Id: foundServer.Id,
|
|
|
|
|
LocalAddress: foundServer.Address,
|
2015-02-16 20:49:47 -07:00
|
|
|
|
Name: foundServer.Name,
|
|
|
|
|
ManualAddress: convertEndpointAddressToManualAddress(foundServer)
|
2015-02-16 13:56:57 -07:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
deferred.resolveWith(null, [servers]);
|
|
|
|
|
});
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 20:49:47 -07:00
|
|
|
|
function convertEndpointAddressToManualAddress(info) {
|
|
|
|
|
|
|
|
|
|
if (info.Address && info.EndpointAddress) {
|
|
|
|
|
var address = info.EndpointAddress.split(":")[0];
|
|
|
|
|
|
|
|
|
|
// Determine the port, if any
|
|
|
|
|
var parts = info.Address.split(":");
|
|
|
|
|
if (parts.length > 1) {
|
|
|
|
|
var portString = parts[parts.length - 1];
|
|
|
|
|
|
|
|
|
|
if (!isNaN(parseInt(portString))) {
|
|
|
|
|
address += ":" + portString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return normalizeAddress(address);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
self.connect = function () {
|
|
|
|
|
|
2015-01-16 13:54:37 -07:00
|
|
|
|
logger.log('Begin connect');
|
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var isResolved = false;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (capabilities.SupportsOfflineAccess) {
|
|
|
|
|
if (!NetworkStatus.isNetworkAvailable()) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
deferred.resolveWith(null, [self.getOffineResult()]);
|
|
|
|
|
isResolved = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (!isResolved) {
|
|
|
|
|
self.getAvailableServers().done(function (servers) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
self.connectToServers(servers).done(function (result) {
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [result]);
|
|
|
|
|
|
|
|
|
|
});
|
2014-10-23 21:54:35 -07:00
|
|
|
|
});
|
2015-02-15 17:33:06 -07:00
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
self.getOffineResult = function () {
|
|
|
|
|
|
|
|
|
|
// TODO: Implement
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
self.connectToServers = function (servers) {
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
logger.log('Begin connectToServers, with ' + servers.length + ' servers');
|
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
if (servers.length == 1) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2014-10-29 15:01:02 -07:00
|
|
|
|
self.connectToServer(servers[0]).done(function (result) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2014-10-29 15:01:02 -07:00
|
|
|
|
if (result.State == MediaBrowser.ConnectionState.Unavailable) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-10-29 15:01:02 -07:00
|
|
|
|
result.State = result.ConnectUser == null ?
|
|
|
|
|
MediaBrowser.ConnectionState.ConnectSignIn :
|
|
|
|
|
MediaBrowser.ConnectionState.ServerSelection;
|
|
|
|
|
}
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
logger.log('resolving connectToServers with result.State: ' + result.State);
|
2014-10-29 15:01:02 -07:00
|
|
|
|
deferred.resolveWith(null, [result]);
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2014-10-29 15:01:02 -07:00
|
|
|
|
});
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var firstServer = servers[0];
|
|
|
|
|
// See if we have any saved credentials and can auto sign in
|
|
|
|
|
if (firstServer) {
|
|
|
|
|
self.connectToServer(firstServer).done(function (result) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-10-29 15:01:02 -07:00
|
|
|
|
if (result.State == MediaBrowser.ConnectionState.SignedIn) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-10-29 15:01:02 -07:00
|
|
|
|
deferred.resolveWith(null, [result]);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2014-10-29 15:01:02 -07:00
|
|
|
|
} else {
|
|
|
|
|
deferred.resolveWith(null, [
|
2014-10-23 21:54:35 -07:00
|
|
|
|
{
|
|
|
|
|
Servers: servers,
|
2014-10-29 15:01:02 -07:00
|
|
|
|
State: (!servers.length && !self.connectUser()) ? MediaBrowser.ConnectionState.ConnectSignIn : MediaBrowser.ConnectionState.ServerSelection,
|
2014-10-23 21:54:35 -07:00
|
|
|
|
ConnectUser: self.connectUser()
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}]);
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
deferred.resolveWith(null, [
|
|
|
|
|
{
|
|
|
|
|
Servers: servers,
|
2014-10-29 15:01:02 -07:00
|
|
|
|
State: (!servers.length && !self.connectUser()) ? MediaBrowser.ConnectionState.ConnectSignIn : MediaBrowser.ConnectionState.ServerSelection,
|
2014-10-23 21:54:35 -07:00
|
|
|
|
ConnectUser: self.connectUser()
|
|
|
|
|
}]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function beginWakeServer(server) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
self.connectToServer = function (server, options) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var tests = [];
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
if (server.LastConnectionMode != null) {
|
2015-02-15 17:33:06 -07:00
|
|
|
|
tests.push(server.LastConnectionMode);
|
|
|
|
|
}
|
|
|
|
|
if (tests.indexOf(MediaBrowser.ConnectionMode.Manual) == -1) { tests.push(MediaBrowser.ConnectionMode.Manual); }
|
|
|
|
|
if (tests.indexOf(MediaBrowser.ConnectionMode.Local) == -1) { tests.push(MediaBrowser.ConnectionMode.Local); }
|
|
|
|
|
if (tests.indexOf(MediaBrowser.ConnectionMode.Remote) == -1) { tests.push(MediaBrowser.ConnectionMode.Remote); }
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var isLocalNetworkAvailable = NetworkStatus.isAnyLocalNetworkAvailable();
|
|
|
|
|
var sendWakeOnLan = server.WakeOnLanInfos && server.WakeOnLanInfos.length && isLocalNetworkAvailable;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (sendWakeOnLan) {
|
|
|
|
|
beginWakeServer(server);
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var wakeOnLanSendTime = new Date().getTime();
|
|
|
|
|
|
|
|
|
|
testNextConnectionMode(tests, 0, isLocalNetworkAvailable, server, wakeOnLanSendTime, options, deferred);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function stringEqualsIgnoreCase(str1, str2) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return (str1 || '').toLowerCase() == (str2 || '').toLowerCase();
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function testNextConnectionMode(tests, index, isLocalNetworkAvailable, server, wakeOnLanSendTime, options, deferred) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (index >= tests.length) {
|
|
|
|
|
|
2015-02-15 21:00:33 -07:00
|
|
|
|
logger.log('Tested all connection modes. Failing server connection.');
|
|
|
|
|
resolveWithFailure(deferred);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var mode = tests[index];
|
|
|
|
|
var address = self.getServerAddress(server, mode);
|
|
|
|
|
var enableRetry = false;
|
|
|
|
|
var skipTest = false;
|
|
|
|
|
var timeout = 15000;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (mode == MediaBrowser.ConnectionMode.Local) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (!isLocalNetworkAvailable) {
|
|
|
|
|
skipTest = true;
|
|
|
|
|
}
|
|
|
|
|
enableRetry = true;
|
|
|
|
|
timeout = 5000;
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
else if (mode == MediaBrowser.ConnectionMode.Manual) {
|
|
|
|
|
|
|
|
|
|
if (stringEqualsIgnoreCase(address, server.LocalAddress) ||
|
|
|
|
|
stringEqualsIgnoreCase(address, server.RemoteAddress)) {
|
|
|
|
|
skipTest = true;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (skipTest || !address) {
|
|
|
|
|
testNextConnectionMode(tests, index + 1, isLocalNetworkAvailable, server, wakeOnLanSendTime, options, deferred);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
logger.log('testing connection mode ' + mode + ' with server ' + server.Name);
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
tryConnect(address, timeout).done(function (result) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 21:00:33 -07:00
|
|
|
|
logger.log('calling onSuccessfulConnection with connection mode ' + mode + ' with server ' + server.Name);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
onSuccessfulConnection(server, result, mode, options, deferred);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
2015-02-15 21:00:33 -07:00
|
|
|
|
logger.log('test failed for connection mode ' + mode + ' with server ' + server.Name);
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (enableRetry) {
|
|
|
|
|
|
|
|
|
|
var sleepTime = 10000 - (new Date().getTime() - wakeOnLanSendTime);
|
|
|
|
|
|
|
|
|
|
// TODO: Implement delay and retry
|
|
|
|
|
|
|
|
|
|
testNextConnectionMode(tests, index + 1, isLocalNetworkAvailable, server, wakeOnLanSendTime, options, deferred);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
} else {
|
2015-02-15 21:00:33 -07:00
|
|
|
|
testNextConnectionMode(tests, index + 1, isLocalNetworkAvailable, server, wakeOnLanSendTime, options, deferred);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
2015-02-15 17:33:06 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onSuccessfulConnection(server, systemInfo, connectionMode, options, deferred) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var credentials = credentialProvider.credentials();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (credentials.ConnectAccessToken) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
ensureConnectUser(credentials).done(function () {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (server.ExchangeToken) {
|
|
|
|
|
addAuthenticationInfoFromConnect(server, connectionMode, credentials).always(function () {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, deferred);
|
|
|
|
|
});
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, deferred);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
2015-02-15 17:33:06 -07:00
|
|
|
|
else {
|
|
|
|
|
afterConnectValidated(server, credentials, systemInfo, connectionMode, true, options, deferred);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
function afterConnectValidated(server, credentials, systemInfo, connectionMode, verifyLocalAuthentication, options, deferred) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
if (verifyLocalAuthentication && server.AccessToken) {
|
|
|
|
|
|
|
|
|
|
validateAuthentication(server, connectionMode).done(function () {
|
|
|
|
|
|
|
|
|
|
afterConnectValidated(server, credentials, systemInfo, connectionMode, false, options, deferred);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
});
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
updateServerInfo(server, systemInfo);
|
|
|
|
|
|
|
|
|
|
server.DateLastAccessed = new Date().getTime();
|
|
|
|
|
server.LastConnectionMode = connectionMode;
|
|
|
|
|
credentialProvider.addOrUpdateServer(credentials.servers, server);
|
|
|
|
|
credentialProvider.credentials(credentials);
|
|
|
|
|
|
|
|
|
|
var result = {
|
|
|
|
|
Servers: []
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.ApiClient = getOrAddApiClient(server, connectionMode);
|
|
|
|
|
result.State = server.AccessToken ?
|
|
|
|
|
MediaBrowser.ConnectionState.SignedIn :
|
|
|
|
|
MediaBrowser.ConnectionState.ServerSignIn;
|
|
|
|
|
|
|
|
|
|
result.Servers.push(server);
|
|
|
|
|
result.ApiClient.enableAutomaticNetworking(server, connectionMode);
|
|
|
|
|
|
|
|
|
|
if (result.State == MediaBrowser.ConnectionState.SignedIn) {
|
|
|
|
|
afterConnected(result.ApiClient, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [result]);
|
|
|
|
|
|
|
|
|
|
Events.trigger(self, 'connected', [result]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.getServerAddress = function (server, mode) {
|
|
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case MediaBrowser.ConnectionMode.Local:
|
|
|
|
|
return server.LocalAddress;
|
|
|
|
|
case MediaBrowser.ConnectionMode.Manual:
|
|
|
|
|
return server.ManualAddress;
|
|
|
|
|
case MediaBrowser.ConnectionMode.Remote:
|
|
|
|
|
return server.RemoteAddress;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error("Unexpected ConnectionMode");
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
};
|
|
|
|
|
|
2015-02-16 20:49:47 -07:00
|
|
|
|
function normalizeAddress(address) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
if (address.toLowerCase().indexOf('http') != 0) {
|
|
|
|
|
address = "http://" + address;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 20:49:47 -07:00
|
|
|
|
return address;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.connectToAddress = function (address) {
|
|
|
|
|
|
|
|
|
|
address = normalizeAddress(address);
|
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
|
|
|
|
tryConnect(address, 15000).done(function (publicInfo) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
logger.log('connectToAddress ' + address + ' succeeded');
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var server = {
|
|
|
|
|
ManualAddress: address,
|
|
|
|
|
LastConnectionMode: MediaBrowser.ConnectionMode.Manual
|
|
|
|
|
};
|
2014-10-23 21:54:35 -07:00
|
|
|
|
updateServerInfo(server, publicInfo);
|
|
|
|
|
|
|
|
|
|
self.connectToServer(server).done(function (result) {
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [result]);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
logger.log('connectToAddress ' + address + ' failed');
|
2014-10-23 21:54:35 -07:00
|
|
|
|
resolveWithFailure(deferred);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.loginToConnect = function (username, password) {
|
|
|
|
|
|
2014-11-04 16:50:26 -07:00
|
|
|
|
if (!username) {
|
|
|
|
|
throw new Error("null username");
|
|
|
|
|
}
|
|
|
|
|
if (!password) {
|
|
|
|
|
throw new Error("null password");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 21:57:24 -07:00
|
|
|
|
var md5 = self.getConnectPasswordHash(password);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return AjaxApi.ajax({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
type: "POST",
|
|
|
|
|
url: "https://connect.mediabrowser.tv/service/user/authenticate",
|
|
|
|
|
data: {
|
2014-11-04 15:43:02 -07:00
|
|
|
|
nameOrEmail: username,
|
2014-10-23 21:54:35 -07:00
|
|
|
|
password: md5
|
|
|
|
|
},
|
|
|
|
|
dataType: "json",
|
2014-12-02 20:13:03 -07:00
|
|
|
|
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Application": appName + "/" + appVersion
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
}).done(function (result) {
|
|
|
|
|
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
|
|
|
|
|
credentials.ConnectAccessToken = result.AccessToken;
|
|
|
|
|
credentials.ConnectUserId = result.User.Id;
|
|
|
|
|
|
|
|
|
|
credentialProvider.credentials(credentials);
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
onConnectUserSignIn(result.User);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-30 21:57:24 -07:00
|
|
|
|
self.getConnectPasswordHash = function (password) {
|
|
|
|
|
|
2014-11-04 15:43:02 -07:00
|
|
|
|
password = globalScope.MediaBrowser.ConnectService.cleanPassword(password);
|
2014-10-30 21:57:24 -07:00
|
|
|
|
|
|
|
|
|
return CryptoJS.MD5(password).toString();
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
self.getApiClient = function (item) {
|
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
// Accept string + object
|
|
|
|
|
if (item.ServerId) {
|
|
|
|
|
item = item.ServerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apiClients.filter(function (a) {
|
|
|
|
|
|
2014-10-30 21:57:24 -07:00
|
|
|
|
var serverInfo = a.serverInfo();
|
|
|
|
|
|
|
|
|
|
// We have to keep this hack in here because of the addApiClient method
|
|
|
|
|
return !serverInfo || serverInfo.Id == item;
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
|
|
|
|
})[0];
|
2014-10-23 21:54:35 -07:00
|
|
|
|
};
|
2014-11-04 05:41:12 -07:00
|
|
|
|
|
|
|
|
|
self.getUserInvitations = function () {
|
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
var connectToken = self.connectToken();
|
|
|
|
|
|
|
|
|
|
if (!connectToken) {
|
2014-11-04 16:50:26 -07:00
|
|
|
|
throw new Error("null connectToken");
|
|
|
|
|
}
|
|
|
|
|
if (!self.connectUserId()) {
|
|
|
|
|
throw new Error("null connectUserId");
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-04 05:41:12 -07:00
|
|
|
|
var url = "https://connect.mediabrowser.tv/service/servers?userId=" + self.connectUserId() + "&status=Waiting";
|
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return AjaxApi.ajax({
|
2014-11-04 05:41:12 -07:00
|
|
|
|
type: "GET",
|
|
|
|
|
url: url,
|
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
"X-Connect-UserToken": connectToken,
|
2014-12-02 20:13:03 -07:00
|
|
|
|
"X-Application": appName + "/" + appVersion
|
2014-11-04 05:41:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.deleteServer = function (serverId) {
|
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
var connectToken = self.connectToken();
|
|
|
|
|
|
2014-11-04 16:50:26 -07:00
|
|
|
|
if (!serverId) {
|
|
|
|
|
throw new Error("null serverId");
|
|
|
|
|
}
|
2015-02-16 13:56:57 -07:00
|
|
|
|
if (!connectToken) {
|
2014-11-04 16:50:26 -07:00
|
|
|
|
throw new Error("null connectToken");
|
|
|
|
|
}
|
|
|
|
|
if (!self.connectUserId()) {
|
|
|
|
|
throw new Error("null connectUserId");
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-04 05:41:12 -07:00
|
|
|
|
var url = "https://connect.mediabrowser.tv/service/serverAuthorizations?serverId=" + serverId + "&userId=" + self.connectUserId();
|
2014-11-18 19:45:12 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return AjaxApi.ajax({
|
2014-11-18 19:45:12 -07:00
|
|
|
|
type: "DELETE",
|
|
|
|
|
url: url,
|
|
|
|
|
headers: {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
"X-Connect-UserToken": connectToken,
|
2014-12-02 20:13:03 -07:00
|
|
|
|
"X-Application": appName + "/" + appVersion
|
2014-11-18 19:45:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).done(function () {
|
|
|
|
|
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
|
|
|
|
|
credentials.servers = credentials.servers.filter(function (s) {
|
|
|
|
|
return s.ConnectServerId != serverId;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
credentialProvider.credentials(credentials);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.rejectServer = function (serverId) {
|
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
var connectToken = self.connectToken();
|
|
|
|
|
|
2014-11-18 19:45:12 -07:00
|
|
|
|
if (!serverId) {
|
|
|
|
|
throw new Error("null serverId");
|
|
|
|
|
}
|
2015-02-16 13:56:57 -07:00
|
|
|
|
if (!connectToken) {
|
2014-11-18 19:45:12 -07:00
|
|
|
|
throw new Error("null connectToken");
|
|
|
|
|
}
|
|
|
|
|
if (!self.connectUserId()) {
|
|
|
|
|
throw new Error("null connectUserId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var url = "https://connect.mediabrowser.tv/service/serverAuthorizations?serverId=" + serverId + "&userId=" + self.connectUserId();
|
2014-11-04 05:41:12 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return AjaxApi.ajax({
|
2014-11-04 05:41:12 -07:00
|
|
|
|
type: "DELETE",
|
|
|
|
|
url: url,
|
|
|
|
|
headers: {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
"X-Connect-UserToken": connectToken,
|
2014-12-02 20:13:03 -07:00
|
|
|
|
"X-Application": appName + "/" + appVersion
|
2014-11-04 05:41:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-04 15:43:02 -07:00
|
|
|
|
self.acceptServer = function (serverId) {
|
2014-11-04 05:41:12 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
var connectToken = self.connectToken();
|
|
|
|
|
|
2014-11-04 16:50:26 -07:00
|
|
|
|
if (!serverId) {
|
|
|
|
|
throw new Error("null serverId");
|
|
|
|
|
}
|
2015-02-16 13:56:57 -07:00
|
|
|
|
if (!connectToken) {
|
2014-11-04 16:50:26 -07:00
|
|
|
|
throw new Error("null connectToken");
|
|
|
|
|
}
|
|
|
|
|
if (!self.connectUserId()) {
|
|
|
|
|
throw new Error("null connectUserId");
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-04 15:43:02 -07:00
|
|
|
|
var url = "https://connect.mediabrowser.tv/service/ServerAuthorizations/accept?serverId=" + serverId + "&userId=" + self.connectUserId();
|
2014-11-04 05:41:12 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
return AjaxApi.ajax({
|
2014-11-04 05:41:12 -07:00
|
|
|
|
type: "GET",
|
|
|
|
|
url: url,
|
|
|
|
|
headers: {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
"X-Connect-UserToken": connectToken,
|
2014-12-02 20:13:03 -07:00
|
|
|
|
"X-Application": appName + "/" + appVersion
|
2014-11-04 05:41:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-16 13:54:37 -07:00
|
|
|
|
return self;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
};
|
2014-10-21 05:42:02 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
})(window, window.Logger);
|