2015-09-09 20:22:52 -07:00
|
|
|
|
(function (globalScope) {
|
|
|
|
|
|
|
|
|
|
function offlineUserSync() {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2015-09-10 11:28:22 -07:00
|
|
|
|
self.sync = function (apiClient, server) {
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
2015-09-10 11:28:22 -07:00
|
|
|
|
var users = server.Users || [];
|
|
|
|
|
syncNext(users, 0, deferred, apiClient, server);
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
|
|
|
|
function syncNext(users, index, deferred, apiClient, server) {
|
|
|
|
|
|
|
|
|
|
var length = users.length;
|
|
|
|
|
|
|
|
|
|
if (index >= length) {
|
|
|
|
|
|
|
|
|
|
deferred.resolve();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
syncUser(users[index], apiClient).then(function () {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
|
|
|
|
syncNext(users, index + 1, deferred, apiClient, server);
|
2015-12-14 08:43:03 -07:00
|
|
|
|
}, function () {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
syncNext(users, index + 1, deferred, apiClient, server);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function syncUser(user, apiClient) {
|
|
|
|
|
|
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
apiClient.getOfflineUser(user.Id).then(function (result) {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
|
|
|
|
require(['localassetmanager'], function () {
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
LocalAssetManager.saveOfflineUser(result).then(function () {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
deferred.resolve();
|
2015-12-14 08:43:03 -07:00
|
|
|
|
}, function () {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
deferred.resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
}, function () {
|
2015-09-15 20:55:26 -07:00
|
|
|
|
|
|
|
|
|
// TODO: We should only delete if there's a 401 response
|
|
|
|
|
|
|
|
|
|
require(['localassetmanager'], function () {
|
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
LocalAssetManager.deleteOfflineUser(user.Id).then(function () {
|
2015-09-15 20:55:26 -07:00
|
|
|
|
deferred.resolve();
|
2015-12-14 08:43:03 -07:00
|
|
|
|
}, function () {
|
2015-09-15 20:55:26 -07:00
|
|
|
|
deferred.resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-09-10 11:28:22 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-09 20:22:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!globalScope.MediaBrowser) {
|
|
|
|
|
globalScope.MediaBrowser = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
globalScope.MediaBrowser.OfflineUserSync = offlineUserSync;
|
|
|
|
|
|
|
|
|
|
})(this);
|