40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
from flask import Blueprint, render_template, request, redirect, url_for, flash, abort
|
|
from ..nosql import get_nosql
|
|
from ..extensions import caching, caching_unless
|
|
|
|
bp = Blueprint('websub', __name__, url_prefix='/websub')
|
|
|
|
@bp.route('/c/<cap>', methods=['GET', 'POST'])
|
|
# Caching GET requests should be save since this endpoint is used as a capability URL
|
|
@caching.cached(unless=caching_unless)
|
|
def callback(cap):
|
|
if request.method == 'GET':
|
|
topic = request.args.get('hub.topic')
|
|
challenge = request.args.get('hub.challenge')
|
|
mode = request.args.get('hub.mode')
|
|
lease_seconds = request.args.get('hub.lease_seconds')
|
|
|
|
if mode not in ['subscribe', 'unsubscribe']:
|
|
return abort(400)
|
|
|
|
if not get_nosql().websub_existsCallback(cap):
|
|
return abort(404)
|
|
|
|
if mode == 'unsubscribe':
|
|
get_nosql().websub_retireCallback(cap)
|
|
return challenge
|
|
|
|
if not all([topic, challenge, mode, lease_seconds]):
|
|
return abort(400)
|
|
|
|
if not get_nosql().websub_activateCallback(cap, lease_seconds):
|
|
return abort(500)
|
|
|
|
return challenge
|
|
|
|
if get_nosql().websub_existsCallback(cap):
|
|
if not get_nosql().websub_savePost(cap, str(request.data)):
|
|
return abort(500)
|
|
return '', 202
|
|
|
|
return abort(404) |