From 0fa19b2c75249abfa67d491fa0c84b2d54687860 Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Thu, 29 Mar 2012 12:25:21 -0700 Subject: [PATCH] Refactoring updated urls in PuSH feeds. --- apps/push/models.py | 25 +++++++++++++++++++++++++ apps/push/views.py | 36 ++++++------------------------------ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/apps/push/models.py b/apps/push/models.py index 3c9c85a79..c678c517e 100644 --- a/apps/push/models.py +++ b/apps/push/models.py @@ -110,7 +110,32 @@ class PushSubscription(models.Model): self.verify_token = token self.save() return token + + def check_urls_against_pushed_data(self, parsed): + if hasattr(parsed.feed, 'links'): # single notification + hub_url = self.hub + self_url = self.topic + for link in parsed.feed.links: + if link['rel'] == 'hub': + hub_url = link['href'] + elif link['rel'] == 'self': + self_url = link['href'] + needs_update = False + if hub_url and self.hub != hub_url: + # hub URL has changed; let's update our subscription + needs_update = True + elif self_url != self.topic: + # topic URL has changed + needs_update = True + + if needs_update: + expiration_time = self.lease_expires - datetime.datetime.now() + seconds = expiration_time.days*86400 + expiration_time.seconds + PushSubscription.objects.subscribe( + self_url, feed=self.feed, hub=hub_url, + lease_seconds=seconds) + def __unicode__(self): if self.verified: verified = u'verified' diff --git a/apps/push/views.py b/apps/push/views.py index 8d147e84e..3f1aca49a 100644 --- a/apps/push/views.py +++ b/apps/push/views.py @@ -1,6 +1,5 @@ # Adapted from djpubsubhubbub. See License: http://git.participatoryculture.org/djpubsubhubbub/tree/LICENSE -from datetime import datetime import feedparser from django.http import HttpResponse, Http404 @@ -33,35 +32,12 @@ def push_callback(request, push_id): elif request.method == 'POST': subscription = get_object_or_404(PushSubscription, pk=push_id) parsed = feedparser.parse(request.raw_post_data) - if parsed.feed.links: # single notification - hub_url = subscription.hub - self_url = subscription.topic - for link in parsed.feed.links: - if link['rel'] == 'hub': - hub_url = link['href'] - elif link['rel'] == 'self': - self_url = link['href'] + subscription.check_urls_against_pushed_data(parsed) + updated.send(sender=subscription, update=parsed) - needs_update = False - if hub_url and subscription.hub != hub_url: - # hub URL has changed; let's update our subscription - needs_update = True - elif self_url != subscription.topic: - # topic URL has changed - needs_update = True + # subscription.feed.queue_pushed_feed_xml(request.raw_post_data) + # Don't give fat ping, just fetch. + subscription.feed.queue_pushed_feed_xml("Fetch me") - if needs_update: - expiration_time = subscription.lease_expires - datetime.now() - seconds = expiration_time.days*86400 + expiration_time.seconds - PushSubscription.objects.subscribe( - self_url, feed=subscription.feed, hub=hub_url, - callback=request.build_absolute_uri(), - lease_seconds=seconds) - - # subscription.feed.queue_pushed_feed_xml(request.raw_post_data) - # Don't give fat ping, just fetch. - subscription.feed.queue_pushed_feed_xml("Fetch me") - - updated.send(sender=subscription, update=parsed) - return HttpResponse('') + return HttpResponse('') return Http404