Navigation Menu

Skip to content

Commit

Permalink
Fix refresh system #31
Browse files Browse the repository at this point in the history
  • Loading branch information
socketubs committed Mar 18, 2013
1 parent ed117f2 commit 116397f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
8 changes: 4 additions & 4 deletions leselys/api.py
Expand Up @@ -57,12 +57,12 @@ def unread(story_id):
return jsonify(reader.unread(story_id))


# Refresh all feeds
@app.route('/api/refresh')
# Refresh a feed
@app.route('/api/refresh/<feed_id>')
@login_required
@cached(30)
def refresh():
return jsonify(success=True, content=reader.refresh_all())
def resfresh(feed_id):
return jsonify(reader.refresh(feed_id))


# Import opml
Expand Down
19 changes: 14 additions & 5 deletions leselys/reader.py
Expand Up @@ -171,12 +171,14 @@ def add(self, url):
'output': 'Feed added',
'counter': len(feed['entries'])}


def delete(self, feed_id):
if not storage.get_feed_by_id(feed_id):
return {'success': False, "output": "Feed not found"}
storage.remove_feed(feed_id)
return {"success": True, "output": "Feed removed"}


def get(self, feed_id, order_type='normal'):
res = []
for entry in storage.get_stories(feed_id):
Expand All @@ -203,6 +205,7 @@ def get(self, feed_id, order_type='normal'):
reverse=True)
return unreaded + readed


def get_subscriptions(self):
feeds = []
for feed in storage.get_feeds():
Expand All @@ -213,15 +216,20 @@ def get_subscriptions(self):
})
return feeds

def refresh_all(self):
for subscription in storage.get_feeds():
refresher = Refresher(subscription)
refresher.start()
return []

def refresh(self, feed_id):
feed = storage.get_feed_by_id(feed_id)
refresher = Refresher(feed)
refresher.start()
refresher.join()
feed['counter'] = self.get_unread(feed_id)
return {'success': True, 'content': feed}


def get_unread(self, feed_id):
return len(storage.get_feed_unread(feed_id))


def read(self, story_id):
"""
Return story content, set it at readed state and give
Expand All @@ -238,6 +246,7 @@ def read(self, story_id):
storage.update_story(story['_id'], copy.copy(story))
return {'success': True, 'content': story}


def unread(self, story_id):
story = storage.get_story_by_id(story_id)
if not story['read']:
Expand Down
31 changes: 13 additions & 18 deletions leselys/static/js/leselys.js
Expand Up @@ -22,6 +22,7 @@ function addSubscription() {
$(loader).hide();
$(loader).html('<a onClick="viewSubscription(&quot;' + data.feed_id + '&quot;)" href="/#' + data.feed_id + '">' + data.title + ' <span id="unread-counter" class="badge badge-inverse">' + data.counter + '</span></a>').fadeIn();
$(loader).attr('id', data.feed_id);
$(loader).addClass('story');
// Add new feed in settings/feeds if opened
if ($('#settings.tab-pane').length > 0) {
// Remove message if feeds list is empty
Expand Down Expand Up @@ -194,25 +195,20 @@ function viewSubscription(feedId) {
}

function refreshSubscriptions() {
$.each(refresher, function(i, refresh) {
refresh.abort();
refresher.shift();
});
var refresh = $.getJSON('/api/refresh', function(data) {
/////////////////////////////////////
// Go login page if not connected ///
if ($(data).find('.form-signin').length > 0) {
window.location = "/login";
$.each($('#menu .story'), function(i, story) {
var feedId = $(story).attr('id');
$.getJSON('/api/refresh/' + feedId), function(data) {
/////////////////////////////////////
// Go login page if not connected ///
if ($(data).find('.form-signin').length > 0) {
window.location = "/login";
}
/////////////////////////////////////
var feedTitle = data.content.title;
var feedCounter = data.content.counter;
$("#menu a[href=#" + feedId + "] span.badge").html(feedCounter);
}
/////////////////////////////////////
$.each(data.content, function(i,feed) {
var feed_title = feed[0];
var feed_id = feed[1];
var feed_counter = feed[2];
$("#menu a[href=#" + feed_id + "] span.badge").html(feed_counter);
});
});
refresher.push(refresh);
}

function readEntry(entryId) {
Expand Down Expand Up @@ -301,7 +297,6 @@ $(document).ready(function(){
e.preventDefault();
});
requests = new Array();
refresher = new Array();
importer = false;
initAddSubscription();
});

0 comments on commit 116397f

Please sign in to comment.