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
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
return new Promise(function (resolve, reject) {
|
2015-09-09 20:22:52 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
var users = server.Users || [];
|
|
|
|
|
syncNext(users, 0, resolve, reject, apiClient, server);
|
|
|
|
|
});
|
2015-09-09 20:22:52 -07:00
|
|
|
|
};
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
function syncNext(users, index, resolve, reject, apiClient, server) {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
|
|
|
|
var length = users.length;
|
|
|
|
|
|
|
|
|
|
if (index >= length) {
|
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
resolve();
|
2015-09-10 11:28:22 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
var onFinish = function() {
|
|
|
|
|
syncNext(users, index + 1, resolve, reject, apiClient, server);
|
|
|
|
|
};
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
syncUser(users[index], apiClient).then(onFinish, onFinish);
|
2015-09-10 11:28:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function syncUser(user, apiClient) {
|
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
return new Promise(function (resolve, reject) {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
apiClient.getOfflineUser(user.Id).then(function (result) {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
require(['localassetmanager'], function () {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
LocalAssetManager.saveOfflineUser(result).then(resolve, resolve);
|
2015-09-10 11:28:22 -07:00
|
|
|
|
});
|
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
}, function () {
|
2015-09-15 20:55:26 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
// TODO: We should only delete if there's a 401 response
|
2015-09-15 20:55:26 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
require(['localassetmanager'], function () {
|
2015-09-15 20:55:26 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
LocalAssetManager.deleteOfflineUser(user.Id).then(resolve, resolve);
|
2015-09-15 20:55:26 -07:00
|
|
|
|
});
|
|
|
|
|
});
|
2015-09-10 11:28:22 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-09 20:22:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!globalScope.MediaBrowser) {
|
|
|
|
|
globalScope.MediaBrowser = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
globalScope.MediaBrowser.OfflineUserSync = offlineUserSync;
|
|
|
|
|
|
|
|
|
|
})(this);
|