NewsBlur-viq/apps/push/views.py
Samuel Clay b8030b4701 Merge branch 'pipeline' into pro
* pipeline:
  Celery also has a set user.
  Setting the user for celery and newsblur_web in both prod and docker-compose so that logs are always under the right user. Fixes #1593.
  Making add_site callback optional to support iOS. #1584
  Adding check for renaming a feed that a user isn't subscribed to.
  Finishing up PostgreSQL migration. Needs to test backups.
  Handling unreadable POST for push feeds and their fat pings.
  Android v11.2
  Schedule and cancel subscription sync services based on user auth state.
  Check for authenticated user before a subscription sync.
  Subscription sync service running every 24 hours to sync if necessary the user's premium subscription state between NewsBlur and Play Store.
  Upgrade subscriptions to billing 4.0
  Subscription manager to handle billing and subscription syncs between Play Store and NewsBlur.
  #1573 Feed search favicon loading.
  Replace cursor loaders (#9)
  Fixing grafana sentry integration.
  Adding sentry grafana pluging.
  Boosting timeout for docker prune
  Don't prune docker unless needed.
  Update docker-compose.yml
2022-01-11 11:00:12 -05:00

72 lines
3.4 KiB
Python

# Adapted from djpubsubhubbub. See License: http://git.participatoryculture.org/djpubsubhubbub/tree/LICENSE
import feedparser
import random
import datetime
from django.http import HttpResponse, Http404
from django.http.request import UnreadablePostError
from django.shortcuts import get_object_or_404
from apps.push.models import PushSubscription
from apps.push.signals import verified
from apps.rss_feeds.models import MFetchHistory
from utils import log as logging
def push_callback(request, push_id):
if request.method == 'GET':
mode = request.GET['hub.mode']
topic = request.GET['hub.topic']
challenge = request.GET['hub.challenge']
lease_seconds = request.GET.get('hub.lease_seconds')
verify_token = request.GET.get('hub.verify_token', '')
if mode == 'subscribe':
if not verify_token.startswith('subscribe'):
raise Http404
subscription = get_object_or_404(PushSubscription,
pk=push_id,
topic=topic,
verify_token=verify_token)
subscription.verified = True
subscription.set_expiration(int(lease_seconds))
subscription.save()
subscription.feed.setup_push()
logging.debug(' ---> [%-30s] [%s] ~BBVerified PuSH' % (subscription.feed, subscription.feed_id))
verified.send(sender=subscription)
return HttpResponse(challenge, content_type='text/plain')
elif request.method == 'POST':
subscription = get_object_or_404(PushSubscription, pk=push_id)
fetch_history = MFetchHistory.feed(subscription.feed_id)
latest_push_date_delta = None
if fetch_history and fetch_history.get('push_history'):
latest_push = fetch_history['push_history'][0]['push_date']
latest_push_date = datetime.datetime.strptime(latest_push, '%Y-%m-%d %H:%M:%S')
latest_push_date_delta = datetime.datetime.now() - latest_push_date
if latest_push_date > datetime.datetime.now() - datetime.timedelta(minutes=1):
logging.debug(' ---> [%-30s] ~SN~FBSkipping feed fetch, pushed %s seconds ago' % (subscription.feed, latest_push_date_delta.seconds))
return HttpResponse('Slow down, you just pushed %s seconds ago...' % latest_push_date_delta.seconds, status=429)
# XXX TODO: Optimize this by removing feedparser. It just needs to find out
# the hub_url or topic has changed. ElementTree could do it.
if random.random() < 0.1:
try:
parsed = feedparser.parse(request.body)
subscription.check_urls_against_pushed_data(parsed)
except UnreadablePostError:
pass
# Don't give fat ping, just fetch.
# subscription.feed.queue_pushed_feed_xml(request.body)
if subscription.feed.active_subscribers >= 1:
subscription.feed.queue_pushed_feed_xml("Fetch me", latest_push_date_delta=latest_push_date_delta)
MFetchHistory.add(feed_id=subscription.feed_id,
fetch_type='push')
else:
logging.debug(' ---> [%-30s] ~FBSkipping feed fetch, no actives: %s' % (subscription.feed, subscription.feed))
return HttpResponse('OK')
return Http404