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-06-08 14:32:20 -07:00
|
|
|
|
globalScope.MediaBrowser.ServerInfo = {
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
return server.ManualAddress || server.LocalAddress || server.RemoteAddress;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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 = [];
|
2015-07-06 19:25:23 -07:00
|
|
|
|
var defaultTimeout = 20000;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 19:50:20 -07:00
|
|
|
|
function getEmbyServerUrl(baseUrl, handler) {
|
|
|
|
|
return baseUrl + "/emby/" + handler;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-20 22:57:06 -07:00
|
|
|
|
function tryConnect(url, timeout) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-10-05 19:50:20 -07:00
|
|
|
|
url = getEmbyServerUrl(url, "system/info/public");
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
|
|
|
|
logger.log('tryConnect url: ' + url);
|
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
return HttpClient.send({
|
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",
|
|
|
|
|
|
2015-05-20 09:28:55 -07:00
|
|
|
|
timeout: timeout || defaultTimeout
|
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
|
|
|
|
};
|
|
|
|
|
|
2015-05-28 16:37:43 -07:00
|
|
|
|
self.capabilities = function () {
|
|
|
|
|
return capabilities;
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
self.deviceId = function () {
|
|
|
|
|
return deviceId;
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-28 16:37:43 -07:00
|
|
|
|
self.credentialProvider = function () {
|
|
|
|
|
return credentialProvider;
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-16 13:54:37 -07:00
|
|
|
|
self.connectUserId = function () {
|
|
|
|
|
return credentialProvider.credentials().ConnectUserId;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.connectToken = function () {
|
|
|
|
|
|
|
|
|
|
return credentialProvider.credentials().ConnectAccessToken;
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-09 10:49:44 -07:00
|
|
|
|
self.getServerInfo = function (id) {
|
|
|
|
|
|
|
|
|
|
var servers = credentialProvider.credentials().Servers;
|
|
|
|
|
|
2015-09-09 20:22:52 -07:00
|
|
|
|
return servers.filter(function (s) {
|
2015-09-09 10:49:44 -07:00
|
|
|
|
|
|
|
|
|
return s.Id == id;
|
|
|
|
|
|
|
|
|
|
})[0];
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
self.getLastUsedServer = function () {
|
|
|
|
|
|
|
|
|
|
var servers = credentialProvider.credentials().Servers;
|
|
|
|
|
|
|
|
|
|
servers.sort(function (a, b) {
|
|
|
|
|
return (b.DateLastAccessed || 0) - (a.DateLastAccessed || 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!servers.length) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return servers[0];
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-24 11:33:28 -07:00
|
|
|
|
self.getLastUsedApiClient = function () {
|
2015-05-20 09:28:55 -07:00
|
|
|
|
|
2015-05-28 16:37:43 -07:00
|
|
|
|
var servers = credentialProvider.credentials().Servers;
|
2015-05-20 09:28:55 -07:00
|
|
|
|
|
|
|
|
|
servers.sort(function (a, b) {
|
2015-06-08 14:32:20 -07:00
|
|
|
|
return (b.DateLastAccessed || 0) - (a.DateLastAccessed || 0);
|
2015-05-20 09:28:55 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!servers.length) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var server = servers[0];
|
|
|
|
|
|
|
|
|
|
return getOrAddApiClient(server, server.LastConnectionMode);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.addApiClient = function (apiClient) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
|
|
|
|
apiClients.push(apiClient);
|
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
var existingServers = credentialProvider.credentials().Servers.filter(function (s) {
|
2015-05-22 08:59:17 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
return stringEqualsIgnoreCase(s.ManualAddress, apiClient.serverAddress()) ||
|
|
|
|
|
stringEqualsIgnoreCase(s.LocalAddress, apiClient.serverAddress()) ||
|
|
|
|
|
stringEqualsIgnoreCase(s.RemoteAddress, apiClient.serverAddress());
|
2015-05-22 08:59:17 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
});
|
2015-05-22 08:59:17 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
var existingServer = existingServers.length ? existingServers[0] : {};
|
|
|
|
|
existingServer.DateLastAccessed = new Date().getTime();
|
|
|
|
|
existingServer.LastConnectionMode = MediaBrowser.ConnectionMode.Manual;
|
|
|
|
|
existingServer.ManualAddress = apiClient.serverAddress();
|
|
|
|
|
apiClient.serverInfo(existingServer);
|
2015-05-22 08:59:17 -07:00
|
|
|
|
|
2015-07-24 14:44:25 -07:00
|
|
|
|
apiClient.onAuthenticated = function (instance, result) {
|
|
|
|
|
onAuthenticated(instance, result, {}, true);
|
|
|
|
|
};
|
2015-05-20 09:28:55 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
if (!existingServers.length) {
|
2015-05-22 08:59:17 -07:00
|
|
|
|
var credentials = credentialProvider.credentials();
|
2015-06-03 08:26:39 -07:00
|
|
|
|
credentials.Servers = [existingServer];
|
|
|
|
|
credentialProvider.credentials(credentials);
|
|
|
|
|
}
|
2015-05-22 08:59:17 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
Events.trigger(self, 'apiclientcreated', [apiClient]);
|
2014-10-28 16:17:55 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
if (existingServer.Id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
apiClient.getPublicSystemInfo().done(function (systemInfo) {
|
2015-05-22 08:59:17 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
existingServer.Id = systemInfo.Id;
|
|
|
|
|
apiClient.serverInfo(existingServer);
|
2015-05-22 08:59:17 -07:00
|
|
|
|
|
2015-06-03 08:26:39 -07:00
|
|
|
|
credentials.Servers = [existingServer];
|
2015-05-22 08:59:17 -07:00
|
|
|
|
credentialProvider.credentials(credentials);
|
2014-10-25 11:32:58 -07:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-18 21:23:55 -07:00
|
|
|
|
self.clearData = function () {
|
|
|
|
|
|
|
|
|
|
logger.log('connection manager clearing data');
|
|
|
|
|
|
|
|
|
|
connectUser = null;
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
credentials.ConnectAccessToken = null;
|
|
|
|
|
credentials.ConnectUserId = null;
|
|
|
|
|
credentials.Servers = [];
|
|
|
|
|
credentialProvider.credentials(credentials);
|
|
|
|
|
};
|
|
|
|
|
|
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) {
|
|
|
|
|
|
2015-05-27 22:51:48 -07:00
|
|
|
|
var url = MediaBrowser.ServerInfo.getServerAddress(server, connectionMode);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-19 10:46:18 -07:00
|
|
|
|
apiClient = new MediaBrowser.ApiClient(logger, url, appName, appVersion, deviceName, deviceId);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
apiClients.push(apiClient);
|
|
|
|
|
|
2014-10-25 11:32:58 -07:00
|
|
|
|
apiClient.serverInfo(server);
|
|
|
|
|
|
2015-07-24 14:44:25 -07:00
|
|
|
|
apiClient.onAuthenticated = function (instance, result) {
|
|
|
|
|
onAuthenticated(instance, 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
|
|
|
|
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 credentials = credentialProvider.credentials();
|
2015-06-03 21:50:10 -07:00
|
|
|
|
var servers = credentials.Servers.filter(function (s) {
|
2015-02-16 23:12:48 -07:00
|
|
|
|
return stringEqualsIgnoreCase(s.Id, serverId);
|
|
|
|
|
|
2015-06-03 21:50:10 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!servers.length) {
|
|
|
|
|
throw new Error('Server not found: ' + serverId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var server = servers[0];
|
2015-02-16 23:12:48 -07:00
|
|
|
|
|
|
|
|
|
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 credentials = credentialProvider.credentials();
|
2015-06-03 21:50:10 -07:00
|
|
|
|
var servers = credentials.Servers.filter(function (s) {
|
|
|
|
|
return s.Id == result.ServerId;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var server = servers.length ? servers[0] : apiClient.serverInfo();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-09-09 10:49:44 -07:00
|
|
|
|
if (options.updateDateLastAccessed !== false) {
|
|
|
|
|
server.DateLastAccessed = new Date().getTime();
|
|
|
|
|
}
|
2015-05-24 11:33:28 -07:00
|
|
|
|
server.Id = result.ServerId;
|
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-05-28 16:37:43 -07:00
|
|
|
|
credentialProvider.addOrUpdateServer(credentials.Servers, server);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
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-09-11 09:26:06 -07:00
|
|
|
|
var info = {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
Id: user.Id,
|
|
|
|
|
IsSignedInOffline: true
|
|
|
|
|
}
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
2015-09-10 11:28:22 -07:00
|
|
|
|
credentialProvider.addOrUpdateUser(server, 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");
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 21:19:08 -07:00
|
|
|
|
var url = "https://connect.emby.media/service/user?id=" + userId;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
return HttpClient.send({
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-27 22:51:48 -07:00
|
|
|
|
var url = MediaBrowser.ServerInfo.getServerAddress(server, connectionMode);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-10-05 19:50:20 -07:00
|
|
|
|
url = getEmbyServerUrl(url, "Connect/Exchange?format=json&ConnectUserId=" + credentials.ConnectUserId);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
return HttpClient.send({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
type: "GET",
|
|
|
|
|
url: url,
|
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
2015-10-05 19:50:20 -07:00
|
|
|
|
"X-Emby-Token": server.ExchangeToken
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).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
|
|
|
|
|
2015-05-27 22:51:48 -07:00
|
|
|
|
var url = MediaBrowser.ServerInfo.getServerAddress(server, connectionMode);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
HttpClient.send({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
type: "GET",
|
2015-10-05 19:50:20 -07:00
|
|
|
|
url: getEmbyServerUrl(url, "System/Info"),
|
2014-10-23 21:54:35 -07:00
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
2015-10-05 19:50:20 -07:00
|
|
|
|
"X-Emby-Token": server.AccessToken
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).done(function (systemInfo) {
|
|
|
|
|
|
|
|
|
|
updateServerInfo(server, systemInfo);
|
|
|
|
|
|
|
|
|
|
if (server.UserId) {
|
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
HttpClient.send({
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
type: "GET",
|
2015-10-05 19:50:20 -07:00
|
|
|
|
url: getEmbyServerUrl(url, "users/" + server.UserId),
|
2014-10-23 21:54:35 -07:00
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
2015-10-05 19:50:20 -07:00
|
|
|
|
"X-Emby-Token": server.AccessToken
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).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
|
|
|
|
|
};
|
|
|
|
|
}
|
2015-02-25 11:11:49 -07:00
|
|
|
|
if (localUser && localUser.PrimaryImageTag) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
|
|
|
|
var apiClient = self.getApiClient(localUser);
|
|
|
|
|
|
|
|
|
|
var url = apiClient.getUserImageUrl(localUser.Id, {
|
|
|
|
|
tag: localUser.PrimaryImageTag,
|
|
|
|
|
type: "Primary"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
url: url,
|
2015-02-25 11:11:49 -07:00
|
|
|
|
supportsParams: true
|
2014-10-25 11:32:58 -07:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
url: null,
|
2015-02-25 11:11:49 -07:00
|
|
|
|
supportsParams: false
|
2014-10-25 11:32:58 -07:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
self.user = function (apiClient) {
|
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,
|
2015-02-25 11:11:49 -07:00
|
|
|
|
name: connectUser ? connectUser.Name : (localUser ? localUser.Name : null),
|
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
|
|
|
|
if (apiClient && apiClient.getCurrentUserId()) {
|
2015-04-27 11:52:54 -07:00
|
|
|
|
apiClient.getCurrentUser().done(function (u) {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
localUser = u;
|
|
|
|
|
}).always(onLocalUserDone);
|
|
|
|
|
} else {
|
|
|
|
|
onLocalUserDone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
if (credentials.ConnectUserId && credentials.ConnectAccessToken && !(apiClient && apiClient.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 () {
|
|
|
|
|
|
2015-02-23 11:55:38 -07:00
|
|
|
|
// Make sure it returns true or false
|
2015-04-01 10:09:08 -07:00
|
|
|
|
if (!self.connectToken() || !self.connectUserId()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2014-10-21 05:42:02 -07:00
|
|
|
|
};
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
self.logout = function () {
|
|
|
|
|
|
2015-06-26 20:27:38 -07:00
|
|
|
|
Logger.log('begin connectionManager loguot');
|
2014-10-23 21:54:35 -07:00
|
|
|
|
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()) {
|
2015-02-23 11:55:38 -07:00
|
|
|
|
promises.push(logoutOfServer(apiClient));
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
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();
|
|
|
|
|
|
2015-05-28 16:37:43 -07:00
|
|
|
|
var servers = credentials.Servers.filter(function (u) {
|
2014-10-29 18:17:31 -07:00
|
|
|
|
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];
|
2015-05-19 12:15:40 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-05-28 16:37:43 -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-23 11:55:38 -07:00
|
|
|
|
function logoutOfServer(apiClient) {
|
|
|
|
|
|
2015-02-25 11:11:49 -07:00
|
|
|
|
var serverInfo = apiClient.serverInfo() || {};
|
2015-02-23 11:55:38 -07:00
|
|
|
|
|
|
|
|
|
var logoutInfo = {
|
|
|
|
|
serverId: serverInfo.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return apiClient.logout().always(function () {
|
|
|
|
|
|
|
|
|
|
Events.trigger(self, 'localusersignedout', [logoutInfo]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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-08-27 21:19:08 -07:00
|
|
|
|
var url = "https://connect.emby.media/service/servers?userId=" + credentials.ConnectUserId;
|
2014-10-21 05:42:02 -07:00
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
HttpClient.send({
|
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-09-26 07:51:26 -07:00
|
|
|
|
self.getSavedServers = function () {
|
|
|
|
|
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
|
|
|
|
|
|
|
|
|
var servers = credentials.Servers.slice(0);
|
|
|
|
|
|
|
|
|
|
servers.sort(function (a, b) {
|
|
|
|
|
return (b.DateLastAccessed || 0) - (a.DateLastAccessed || 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return servers;
|
|
|
|
|
};
|
|
|
|
|
|
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-05-28 16:37:43 -07:00
|
|
|
|
var servers = credentials.Servers.slice(0);
|
2015-02-16 13:56:57 -07:00
|
|
|
|
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) {
|
2015-06-08 14:32:20 -07:00
|
|
|
|
return (b.DateLastAccessed || 0) - (a.DateLastAccessed || 0);
|
2015-02-16 13:56:57 -07:00
|
|
|
|
});
|
2015-01-16 13:54:37 -07:00
|
|
|
|
|
2015-05-28 16:37:43 -07:00
|
|
|
|
credentials.Servers = servers;
|
2015-02-16 13:56:57 -07:00
|
|
|
|
|
|
|
|
|
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-06-08 14:32:20 -07:00
|
|
|
|
require(['serverdiscovery'], function () {
|
2015-09-27 14:02:39 -07:00
|
|
|
|
ServerDiscovery.findServers(1000).done(function (foundServers) {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
var servers = foundServers.map(function (foundServer) {
|
|
|
|
|
|
|
|
|
|
var info = {
|
|
|
|
|
Id: foundServer.Id,
|
|
|
|
|
LocalAddress: foundServer.Address,
|
|
|
|
|
Name: foundServer.Name,
|
|
|
|
|
ManualAddress: convertEndpointAddressToManualAddress(foundServer),
|
|
|
|
|
DateLastLocalConnection: new Date().getTime()
|
|
|
|
|
};
|
2015-05-24 11:33:28 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
info.LastConnectionMode = info.ManualAddress ? MediaBrowser.ConnectionMode.Manual : MediaBrowser.ConnectionMode.Local;
|
2015-05-24 11:33:28 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
return info;
|
|
|
|
|
});
|
|
|
|
|
deferred.resolveWith(null, [servers]);
|
2015-02-16 13:56:57 -07:00
|
|
|
|
});
|
2015-06-08 14:32:20 -07:00
|
|
|
|
|
2015-02-16 13:56:57 -07:00
|
|
|
|
});
|
|
|
|
|
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();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-05-27 22:51:48 -07:00
|
|
|
|
self.getAvailableServers().done(function (servers) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-05-27 22:51:48 -07:00
|
|
|
|
self.connectToServers(servers).done(function (result) {
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
2015-05-27 22:51:48 -07:00
|
|
|
|
deferred.resolveWith(null, [result]);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
});
|
2015-05-27 22:51:48 -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-06-04 13:27:46 -07:00
|
|
|
|
var firstServer = servers.length ? servers[0] : null;
|
2015-02-15 17:33:06 -07:00
|
|
|
|
// 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-06-08 14:32:20 -07:00
|
|
|
|
require(['wakeonlan'], function () {
|
|
|
|
|
var infos = server.WakeOnLanInfos || [];
|
|
|
|
|
|
|
|
|
|
for (var i = 0, length = infos.length; i < length; i++) {
|
|
|
|
|
|
|
|
|
|
WakeOnLan.send(infos[i]);
|
|
|
|
|
}
|
|
|
|
|
});
|
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-10-01 23:14:04 -07:00
|
|
|
|
//tests.push(server.LastConnectionMode);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
}
|
|
|
|
|
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-06-08 14:32:20 -07:00
|
|
|
|
beginWakeServer(server);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var wakeOnLanSendTime = new Date().getTime();
|
|
|
|
|
|
2015-09-09 10:49:44 -07:00
|
|
|
|
options = options || {};
|
2015-05-27 22:51:48 -07:00
|
|
|
|
testNextConnectionMode(tests, 0, 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-05-27 22:51:48 -07:00
|
|
|
|
function testNextConnectionMode(tests, index, 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];
|
2015-05-27 22:51:48 -07:00
|
|
|
|
var address = MediaBrowser.ServerInfo.getServerAddress(server, mode);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
var enableRetry = false;
|
|
|
|
|
var skipTest = false;
|
2015-05-20 09:28:55 -07:00
|
|
|
|
var timeout = defaultTimeout;
|
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
|
|
|
|
enableRetry = true;
|
2015-10-05 18:22:56 -07:00
|
|
|
|
timeout = 10000;
|
2015-02-15 17:33:06 -07:00
|
|
|
|
}
|
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) {
|
2015-05-27 22:51:48 -07:00
|
|
|
|
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, deferred);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
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
|
|
|
|
|
|
2015-05-27 22:51:48 -07:00
|
|
|
|
testNextConnectionMode(tests, index + 1, server, wakeOnLanSendTime, options, deferred);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
} else {
|
2015-05-27 22:51:48 -07:00
|
|
|
|
testNextConnectionMode(tests, index + 1, 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();
|
|
|
|
|
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);
|
|
|
|
|
|
2015-09-09 10:49:44 -07:00
|
|
|
|
if (options.updateDateLastAccessed !== false) {
|
|
|
|
|
server.DateLastAccessed = new Date().getTime();
|
|
|
|
|
}
|
2015-02-15 17:33:06 -07:00
|
|
|
|
server.LastConnectionMode = connectionMode;
|
2015-05-28 16:37:43 -07:00
|
|
|
|
credentialProvider.addOrUpdateServer(credentials.Servers, server);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
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);
|
2015-06-03 21:50:10 -07:00
|
|
|
|
result.ApiClient.updateServerInfo(server, connectionMode);
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
|
|
|
|
if (result.State == MediaBrowser.ConnectionState.SignedIn) {
|
|
|
|
|
afterConnected(result.ApiClient, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [result]);
|
|
|
|
|
|
|
|
|
|
Events.trigger(self, 'connected', [result]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 20:49:47 -07:00
|
|
|
|
function normalizeAddress(address) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
// attempt to correct bad input
|
|
|
|
|
address = address.trim();
|
2015-06-07 12:13:44 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
if (address.toLowerCase().indexOf('http') != 0) {
|
|
|
|
|
address = "http://" + address;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 09:28:55 -07:00
|
|
|
|
// Seeing failures in iOS when protocol isn't lowercase
|
|
|
|
|
address = address.replace('Http:', 'http:');
|
|
|
|
|
address = address.replace('Https:', 'https:');
|
|
|
|
|
|
2015-02-16 20:49:47 -07:00
|
|
|
|
return address;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.connectToAddress = function (address) {
|
|
|
|
|
|
2015-02-15 20:05:21 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2015-02-15 17:33:06 -07:00
|
|
|
|
|
2015-05-06 05:56:26 -07:00
|
|
|
|
if (!address) {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
address = normalizeAddress(address);
|
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
function onFail() {
|
|
|
|
|
logger.log('connectToAddress ' + address + ' failed');
|
|
|
|
|
resolveWithFailure(deferred);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 09:28:55 -07:00
|
|
|
|
tryConnect(address, defaultTimeout).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]);
|
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
}).fail(onFail);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
}).fail(onFail);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.loginToConnect = function (username, password) {
|
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
2014-11-04 16:50:26 -07:00
|
|
|
|
if (!username) {
|
2015-05-06 05:56:26 -07:00
|
|
|
|
deferred.reject();
|
|
|
|
|
return deferred.promise();
|
2014-11-04 16:50:26 -07:00
|
|
|
|
}
|
|
|
|
|
if (!password) {
|
2015-05-06 05:56:26 -07:00
|
|
|
|
deferred.reject();
|
|
|
|
|
return deferred.promise();
|
2014-11-04 16:50:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
require(['connectservice'], function () {
|
|
|
|
|
|
|
|
|
|
var md5 = self.getConnectPasswordHash(password);
|
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
HttpClient.send({
|
2015-06-08 14:32:20 -07:00
|
|
|
|
type: "POST",
|
2015-08-27 21:19:08 -07:00
|
|
|
|
url: "https://connect.emby.media/service/user/authenticate",
|
2015-06-08 14:32:20 -07:00
|
|
|
|
data: {
|
|
|
|
|
nameOrEmail: username,
|
|
|
|
|
password: md5
|
|
|
|
|
},
|
|
|
|
|
dataType: "json",
|
|
|
|
|
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Application": appName + "/" + appVersion
|
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
}).done(function (result) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
var credentials = credentialProvider.credentials();
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
credentials.ConnectAccessToken = result.AccessToken;
|
|
|
|
|
credentials.ConnectUserId = result.User.Id;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
credentialProvider.credentials(credentials);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
onConnectUserSignIn(result.User);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
deferred.resolveWith(null, [result]);
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
2015-06-13 16:56:59 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.signupForConnect = function (email, username, password, passwordConfirm) {
|
|
|
|
|
|
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
|
|
|
|
if (!email) {
|
|
|
|
|
deferred.rejectWith(null, [{ errorCode: 'invalidinput' }]);
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
if (!username) {
|
|
|
|
|
deferred.rejectWith(null, [{ errorCode: 'invalidinput' }]);
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
if (!password) {
|
|
|
|
|
deferred.rejectWith(null, [{ errorCode: 'invalidinput' }]);
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
if (!passwordConfirm) {
|
|
|
|
|
deferred.rejectWith(null, [{ errorCode: 'passwordmatch' }]);
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
if (password != passwordConfirm) {
|
|
|
|
|
deferred.rejectWith(null, [{ errorCode: 'passwordmatch' }]);
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-17 08:39:46 -07:00
|
|
|
|
require(['connectservice'], function () {
|
|
|
|
|
|
|
|
|
|
var md5 = self.getConnectPasswordHash(password);
|
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
HttpClient.send({
|
2015-06-17 08:39:46 -07:00
|
|
|
|
type: "POST",
|
2015-08-27 21:19:08 -07:00
|
|
|
|
url: "https://connect.emby.media/service/register",
|
2015-06-17 08:39:46 -07:00
|
|
|
|
data: {
|
|
|
|
|
email: email,
|
|
|
|
|
userName: username,
|
|
|
|
|
password: md5
|
|
|
|
|
},
|
|
|
|
|
dataType: "json",
|
|
|
|
|
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Application": appName + "/" + appVersion,
|
|
|
|
|
"X-CONNECT-TOKEN": "CONNECT-REGISTER"
|
|
|
|
|
}
|
2015-06-13 16:56:59 -07:00
|
|
|
|
|
2015-06-17 08:39:46 -07:00
|
|
|
|
}).done(function (result) {
|
2015-06-13 16:56:59 -07:00
|
|
|
|
|
2015-06-17 08:39:46 -07:00
|
|
|
|
deferred.resolve(null, []);
|
2015-06-13 16:56:59 -07:00
|
|
|
|
|
2015-06-17 08:39:46 -07:00
|
|
|
|
}).fail(function (e) {
|
2015-06-13 16:56:59 -07:00
|
|
|
|
|
2015-06-17 08:39:46 -07:00
|
|
|
|
try {
|
2015-06-13 16:56:59 -07:00
|
|
|
|
|
2015-06-17 08:39:46 -07:00
|
|
|
|
var result = JSON.parse(e.responseText);
|
2015-06-13 16:56:59 -07:00
|
|
|
|
|
2015-06-17 08:39:46 -07:00
|
|
|
|
deferred.rejectWith(null, [{ errorCode: result.Status }]);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
deferred.rejectWith(null, [{}]);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-10-23 21:54:35 -07:00
|
|
|
|
});
|
2015-06-08 14:32:20 -07:00
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 21:19:08 -07:00
|
|
|
|
var url = "https://connect.emby.media/service/servers?userId=" + self.connectUserId() + "&status=Waiting";
|
2014-11-04 05:41:12 -07:00
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
return HttpClient.send({
|
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-08-03 09:54:01 -07:00
|
|
|
|
if (!serverId) {
|
|
|
|
|
throw new Error("null serverId");
|
|
|
|
|
}
|
2015-05-12 21:55:19 -07:00
|
|
|
|
|
2015-08-29 17:40:52 -07:00
|
|
|
|
var server = credentialProvider.credentials().Servers.filter(function (s) {
|
|
|
|
|
return s.Id == serverId;
|
|
|
|
|
});
|
|
|
|
|
server = server.length ? server[0] : null;
|
|
|
|
|
|
2015-08-03 09:54:01 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
2015-05-24 11:33:28 -07:00
|
|
|
|
|
2015-08-03 09:54:01 -07:00
|
|
|
|
function onDone() {
|
|
|
|
|
var credentials = credentialProvider.credentials();
|
2015-05-12 21:55:19 -07:00
|
|
|
|
|
2015-05-28 16:37:43 -07:00
|
|
|
|
credentials.Servers = credentials.Servers.filter(function (s) {
|
2015-08-29 17:40:52 -07:00
|
|
|
|
return s.Id != serverId;
|
2015-05-12 21:55:19 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
credentialProvider.credentials(credentials);
|
2015-08-03 09:54:01 -07:00
|
|
|
|
deferred.resolve();
|
2014-11-04 16:50:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 17:40:52 -07:00
|
|
|
|
if (!server.ConnectServerId) {
|
|
|
|
|
onDone();
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var connectToken = self.connectToken();
|
|
|
|
|
var connectUserId = self.connectUserId();
|
|
|
|
|
|
|
|
|
|
if (!connectToken || !connectUserId) {
|
2015-08-03 09:54:01 -07:00
|
|
|
|
onDone();
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
2014-11-18 19:45:12 -07:00
|
|
|
|
|
2015-08-29 17:40:52 -07:00
|
|
|
|
var url = "https://connect.emby.media/service/serverAuthorizations?serverId=" + server.ConnectServerId + "&userId=" + connectUserId;
|
2015-05-12 21:55:19 -07:00
|
|
|
|
|
2015-08-03 09:54:01 -07:00
|
|
|
|
HttpClient.send({
|
|
|
|
|
type: "DELETE",
|
|
|
|
|
url: url,
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Connect-UserToken": connectToken,
|
|
|
|
|
"X-Application": appName + "/" + appVersion
|
2014-11-18 19:45:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 09:54:01 -07:00
|
|
|
|
}).always(onDone);
|
2014-11-18 19:45:12 -07:00
|
|
|
|
|
2015-08-03 09:54:01 -07:00
|
|
|
|
return deferred.promise();
|
2014-11-18 19:45:12 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 21:19:08 -07:00
|
|
|
|
var url = "https://connect.emby.media/service/serverAuthorizations?serverId=" + serverId + "&userId=" + self.connectUserId();
|
2014-11-04 05:41:12 -07:00
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
return HttpClient.send({
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 21:19:08 -07:00
|
|
|
|
var url = "https://connect.emby.media/service/ServerAuthorizations/accept?serverId=" + serverId + "&userId=" + self.connectUserId();
|
2014-11-04 05:41:12 -07:00
|
|
|
|
|
2015-06-30 10:21:20 -07:00
|
|
|
|
return HttpClient.send({
|
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-05-26 08:31:50 -07:00
|
|
|
|
self.getRegistrationInfo = function (feature, apiClient) {
|
|
|
|
|
|
2015-05-26 10:48:05 -07:00
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
|
|
|
|
self.getAvailableServers().done(function (servers) {
|
|
|
|
|
|
|
|
|
|
var matchedServers = servers.filter(function (s) {
|
|
|
|
|
return stringEqualsIgnoreCase(s.Id, apiClient.serverInfo().Id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!matchedServers.length) {
|
|
|
|
|
deferred.resolveWith(null, [{}]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var match = matchedServers[0];
|
|
|
|
|
|
|
|
|
|
// 31 days
|
|
|
|
|
if ((new Date().getTime() - (match.DateLastLocalConnection || 0)) > 2678400000) {
|
|
|
|
|
deferred.resolveWith(null, [{}]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apiClient.getRegistrationInfo(feature).done(function (result) {
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [result]);
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
2015-05-26 08:31:50 -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);
|