Skip to content

Commit

Permalink
Ratelimit calendar imports to 1 item per 100ms
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlovl committed Oct 23, 2018
1 parent a15471f commit eb86004
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions js/app/controllers/importcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* Description: Takes care of importing calendars
*/

app.controller('ImportController', ['$scope', '$filter', 'CalendarService', 'VEventService', '$uibModalInstance', 'files', 'ImportFileWrapper', 'ColorUtility',
function($scope, $filter, CalendarService, VEventService, $uibModalInstance, files, ImportFileWrapper, ColorUtility) {
app.controller('ImportController', ['$scope', '$filter', '$timeout', 'CalendarService', 'VEventService', '$uibModalInstance', 'files', 'ImportFileWrapper', 'ColorUtility',
function($scope, $filter, $timeout, CalendarService, VEventService, $uibModalInstance, files, ImportFileWrapper, ColorUtility) {
'use strict';

$scope.nameSize = 25;
Expand All @@ -45,32 +45,37 @@ app.controller('ImportController', ['$scope', '$filter', 'CalendarService', 'VEv

var importCalendar = function(calendar) {
const objects = fileWrapper.splittedICal.objects;
var promise = $timeout();

angular.forEach(objects, function(object) {
VEventService.create(calendar, object, false).then(function(response) {
fileWrapper.state = ImportFileWrapper.stateImporting;
fileWrapper.progress++;

if (!response) {
fileWrapper.errors++;
}
}).catch(function(reason) {
if (reason.status === 400) {
const xml = reason.xhr.responseXML;
const error = xml.children[0];

if (error) {
const message = error.children[1].textContent;
if (message === 'Calendar object with uid already exists in this calendar collection.') {
fileWrapper.duplicates++;
}
}
}

fileWrapper.state = ImportFileWrapper.stateImporting;
fileWrapper.errors++;
fileWrapper.progress++;
});
promise = promise.then(function() {
VEventService.create(calendar, object, false).then(function(response) {
fileWrapper.state = ImportFileWrapper.stateImporting;
fileWrapper.progress++;

if (!response) {
fileWrapper.errors++;
}
}).catch(function(reason) {
if (reason.status === 400) {
const xml = reason.xhr.responseXML;
const error = xml.children[0];

if (error) {
const message = error.children[1].textContent;
if (message === 'Calendar object with uid already exists in this calendar collection.') {
fileWrapper.duplicates++;
}
}
}

fileWrapper.state = ImportFileWrapper.stateImporting;
fileWrapper.errors++;
fileWrapper.progress++;
});

return $timeout(100);
});
});
};

Expand Down

0 comments on commit eb86004

Please sign in to comment.