Skip to content

Commit

Permalink
important fixes for posting comments
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed Mar 10, 2014
1 parent 006dba8 commit 6d7c2d9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
65 changes: 41 additions & 24 deletions client/static/js/buggy.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ function BugsController($scope, $timeout, $http, $interval, $location) {
//console.log('Success');
$scope.is_offline = false;
logDataDownloaded(data);
console.log("FETCHED BUG DATA", data);
//console.log("FETCHED BUG DATA", data);
if (data.bugs.length) {
var new_bug = data.bugs[0];
//console.log('NEW BUG', bug);
Expand Down Expand Up @@ -561,15 +561,15 @@ function BugsController($scope, $timeout, $http, $interval, $location) {
// the very first thing to do
angularForage.getItem($scope, 'products', function(value) {
if (value) {
console.log("Stored products", value);
// console.log("Stored products", value);
$scope.products = value;
showCloakDialog('Loading bugs from local storage...');
loadBugs(function() {
localForage.getItem('selected_bug', function(id) {
if (id) {
angularForage.getItem($scope, id, function(bug) {
if (bug) {
console.log('selected bug:', bug.id);
// console.log('selected bug:', bug.id);
$scope.selectBug(bug);
//$scope.bug = bug;
} else {
Expand Down Expand Up @@ -820,7 +820,7 @@ function BugsController($scope, $timeout, $http, $interval, $location) {
$scope.refreshBug = function(bug) {
startLoading('Refreshing bug and its comments');
fetchAndUpdateBug(bug, function() {
console.log('Afterwards bug.status=', bug.status, ' $scope.bug.status=', $scope.bug.status);
//console.log('Afterwards bug.status=', bug.status, ' $scope.bug.status=', $scope.bug.status);
fetchAndUpdateComments(bug, function() {
bug.things = $scope.getThings(bug);
fetchAndUpdateHistory(bug, function() {
Expand Down Expand Up @@ -1683,8 +1683,8 @@ app.controller('BugController', ['$scope', '$interval', '$http', '$timeout', fun
};

$scope.submitUpdate = function() {
console.log('$scope.post_queue', $scope.post_queue);
console.log('this.comment', this.comment, '$scope.comment', $scope.comment);
//console.log('$scope.post_queue', $scope.post_queue);
//console.log('this.comment', this.comment, '$scope.comment', $scope.comment);
var comment = this.comment || '';
var status = this.status || '';
var resolution = this.resolution || '';
Expand Down Expand Up @@ -1726,11 +1726,11 @@ app.controller('BugController', ['$scope', '$interval', '$http', '$timeout', fun
params.token = $scope.auth_token;
}
var url = BUGZILLA_URL + 'bug/' + bug_id + '/comment';
//var url = 'http://localhost:8888/' + 'bug/' + bug_id + '/comment';
// var url = 'http://localhost:8888/' + 'bug/' + bug_id + '/comment';
if (params.id !== bug_id) {
params.id = bug_id;
}
//console.log("BUGZILLA URL", url);
// console.log("BUGZILLA URL", url);
return $http.post(url, params);
}

Expand All @@ -1754,22 +1754,25 @@ app.controller('BugController', ['$scope', '$interval', '$http', '$timeout', fun
// '$scope.post_queue' is an object so to get a count of it we need to
// use an iterator
var count_bugs = 0;
_.each($scope.post_queue, function() {
count_bugs++;
_.each($scope.post_queue, function(arr) {
if (arr.length) {
count_bugs++;
}
});
var original_count_bugs = count_bugs;
var params;
_.each($scope.post_queue, function(posts, bug_id, index) {
count_bugs--;
var count_posts = posts.length;
_.each(posts, function(post) {
console.log('THING TO POST', post);
//console.log('THING TO POST', post);

// we need to keep track of how many things we have to do
// so we know when to execute a callback
var things_to_do = 0;
if (post.status) things_to_do++;
if (post.comment) things_to_do++;
L('things_to_do', things_to_do);
//L('things_to_do', things_to_do);

if (post.status) {
params = {
Expand Down Expand Up @@ -1797,7 +1800,7 @@ app.controller('BugController', ['$scope', '$interval', '$http', '$timeout', fun
count_posts--;
things_to_do--;
if (!things_to_do && count_posts <= 0 && !count_bugs) {
//console.log('Finally callback');
// console.log('Finally callback after putUpdate');
localForage.setItem('post_queue', $scope.post_queue);
if (callback) callback();
}
Expand All @@ -1809,17 +1812,29 @@ app.controller('BugController', ['$scope', '$interval', '$http', '$timeout', fun
'id': bug_id,
'comment': post.comment,
};

postComment(bug_id, params)
.success(function(data, status) {
$scope.is_offline = false;
console.log('YAY! POST COMMENT WORKED');
console.log(data);
$scope.post_queue[bug_id] = _.filter($scope.post_queue[bug_id], function(p) {
return p._when !== post._when;
});
if ($scope.bug.id === parseInt(bug_id)) {
// this is the currently chosen bug
$scope.refreshBug($scope.bug);
if (data.error) {
console.warn("Actually unable to post comment", data);
$scope.post_queue[bug_id] = _.map($scope.post_queue[bug_id], function(p) {
if (p._when === post._when) {
p._error = data;
}
return p;
});
} else {
console.log('YAY! POST COMMENT WORKED');
console.log(data);

$scope.post_queue[bug_id] = _.filter($scope.post_queue[bug_id], function(p) {
return p._when !== post._when;
});
if ($scope.bug.id === parseInt(bug_id)) {
// this is the currently chosen bug
$scope.refreshBug($scope.bug);
}
}
}).error(function(data, status) {
console.warn('Unable to post comment', data);
Expand All @@ -1837,16 +1852,16 @@ app.controller('BugController', ['$scope', '$interval', '$http', '$timeout', fun
count_posts--;
things_to_do--;
if (!things_to_do && count_posts <= 0 && !count_bugs) {
//console.log('Finally callback');
// console.log('Finally callback after postComment');
localForage.setItem('post_queue', $scope.post_queue);
if (callback) callback();
}
});
}
});
});
if (!count_bugs && callback) {
//console.log('Callback immediately');
if (!original_count_bugs && callback) {
// console.log('Callback immediately');
callback();
}
}
Expand All @@ -1871,6 +1886,8 @@ app.controller('BugController', ['$scope', '$interval', '$http', '$timeout', fun
clearPostQueue(function() {
_lock_post_queue = false;
});
} else {
console.warn("clear post queue is locked");
}
}, CLEAR_POST_QUEUE_FREQUENCY * 1000);

Expand Down
16 changes: 13 additions & 3 deletions fakeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@ def options(self, *args, **kwargs):

class CommentHandler(BaseHandler):
def post(self, bug_id):
sleep(3)
sleep(10)
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
self.set_header("Access-Control-Allow-Headers", "*")
self.set_header("Access-Control-Allow-Headers",
"Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, Cache-Control")
print self.request.body
if randint(1, 4) == 1:
if getattr(self.application, '_comments', None) is None:
self.application._comments = []
if self.request.body in self.application._comments:
self.set_status(500)
self.write({'error': 'Already posted that!'})
return
self.application._comments.append(self.request.body)
if 0 and randint(1, 4) == 1:
self.set_status(400)
self.write({'error': "Something terrible happened %s" % datetime.datetime.utcnow()})
else:
self.write("Hello, POST %s" % bug_id)
if 0 and randint(1, 2) == 1:
self.write({'error': 'Something is crazy about bugzilla right now'})
else:
self.write("Hello, POST %s" % bug_id)


class UpdateHandler(BaseHandler):
Expand Down

0 comments on commit 6d7c2d9

Please sign in to comment.