2019-04-10 19:43:37 +00:00
|
|
|
from django_kepi import ATSIGN_CONTEXT, create as kepi_create
|
|
|
|
from django_kepi.validation import IncomingMessage
|
2018-08-07 15:21:45 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2018-08-11 16:21:56 +00:00
|
|
|
import django.views
|
2018-10-03 19:28:52 +00:00
|
|
|
from django.http import HttpResponse, JsonResponse, Http404
|
2018-08-07 15:21:45 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2018-10-03 19:28:52 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2018-09-27 16:34:59 +00:00
|
|
|
import logging
|
2018-08-24 17:13:57 +00:00
|
|
|
import urllib.parse
|
2018-08-07 15:21:45 +00:00
|
|
|
import json
|
|
|
|
import re
|
2019-04-10 19:11:29 +00:00
|
|
|
from collections import defaultdict
|
2018-08-07 15:21:45 +00:00
|
|
|
|
2019-04-10 19:43:37 +00:00
|
|
|
logger = logging.getLogger(name='django_kepi')
|
|
|
|
|
2018-08-24 17:13:57 +00:00
|
|
|
PAGE_LENGTH = 50
|
2018-08-24 23:04:08 +00:00
|
|
|
PAGE_FIELD = 'page'
|
2018-08-24 17:13:57 +00:00
|
|
|
|
2018-08-11 16:21:56 +00:00
|
|
|
class ActivityObjectView(django.views.View):
|
2018-08-07 15:21:45 +00:00
|
|
|
|
2018-08-11 16:21:56 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2018-08-07 15:21:45 +00:00
|
|
|
|
2018-08-31 16:55:57 +00:00
|
|
|
result = self.objectDetails(*args, **kwargs)
|
2018-08-06 13:44:35 +00:00
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
return self._render(result)
|
2018-08-24 17:13:57 +00:00
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
def _make_query_page(
|
|
|
|
self,
|
|
|
|
request,
|
|
|
|
page_number,
|
|
|
|
):
|
2018-08-26 19:07:27 +00:00
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
fields = dict(request.GET)
|
2018-08-26 19:07:27 +00:00
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
if page_number is None:
|
|
|
|
if PAGE_FIELD in fields:
|
|
|
|
del fields[PAGE_FIELD]
|
|
|
|
else:
|
|
|
|
fields[PAGE_FIELD] = page_number
|
|
|
|
|
|
|
|
encoded = urllib.parse.urlencode(fields)
|
|
|
|
|
|
|
|
if encoded!='':
|
|
|
|
encoded = '?'+encoded
|
|
|
|
|
|
|
|
return '{}://{}{}{}'.format(
|
|
|
|
request.scheme,
|
|
|
|
request.get_host(),
|
|
|
|
request.path,
|
|
|
|
encoded,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _render(self, data):
|
|
|
|
result = JsonResponse(
|
|
|
|
data=data,
|
|
|
|
json_dumps_params={
|
|
|
|
'sort_keys': True,
|
|
|
|
'indent': 2,
|
|
|
|
}
|
|
|
|
)
|
2018-08-26 19:07:27 +00:00
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
result['Content-Type'] = 'application/activity+json'
|
2018-08-26 19:07:27 +00:00
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
return result
|
2018-08-26 19:07:27 +00:00
|
|
|
|
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
class CollectionView(ActivityObjectView):
|
2018-08-24 17:13:57 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
items = self.get_collection_items(*args, **kwargs)
|
2018-08-26 18:27:45 +00:00
|
|
|
# XXX assert that items.ordered
|
2018-08-24 17:13:57 +00:00
|
|
|
|
2018-08-24 23:04:08 +00:00
|
|
|
our_url = request.build_absolute_uri()
|
2018-08-28 12:16:07 +00:00
|
|
|
index_url = self._make_query_page(request, None)
|
2018-08-24 17:13:57 +00:00
|
|
|
|
|
|
|
if PAGE_FIELD in request.GET:
|
2018-08-26 19:07:27 +00:00
|
|
|
|
2018-08-24 17:13:57 +00:00
|
|
|
page_number = int(request.GET[PAGE_FIELD])
|
|
|
|
|
|
|
|
start = (page_number-1) * PAGE_LENGTH
|
|
|
|
|
2018-08-26 19:07:27 +00:00
|
|
|
listed_items = items[start: start+PAGE_LENGTH]
|
2018-08-24 17:13:57 +00:00
|
|
|
|
|
|
|
result = {
|
|
|
|
"@context": ATSIGN_CONTEXT,
|
|
|
|
"type" : "OrderedCollectionPage",
|
|
|
|
"id" : our_url,
|
2018-08-24 23:04:08 +00:00
|
|
|
"totalItems" : items.count(),
|
2018-08-26 20:03:00 +00:00
|
|
|
"orderedItems" : [self._stringify_object(x)
|
|
|
|
for x in listed_items],
|
2018-08-26 19:07:27 +00:00
|
|
|
"partOf": index_url,
|
2018-08-24 17:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if page_number > 1:
|
2018-08-28 12:16:07 +00:00
|
|
|
result["prev"] = self._make_query_page(request, page_number-1)
|
2018-08-24 17:13:57 +00:00
|
|
|
|
2018-08-26 21:44:27 +00:00
|
|
|
if start+PAGE_LENGTH < items.count():
|
2018-08-28 12:16:07 +00:00
|
|
|
result["next"] = self._make_query_page(request, page_number+1)
|
2018-08-24 17:13:57 +00:00
|
|
|
|
|
|
|
else:
|
2018-08-24 23:04:08 +00:00
|
|
|
|
|
|
|
# Index page.
|
|
|
|
|
2018-08-24 17:13:57 +00:00
|
|
|
result = {
|
|
|
|
"@context": ATSIGN_CONTEXT,
|
|
|
|
"type": "OrderedCollection",
|
2018-08-26 19:07:27 +00:00
|
|
|
"id": index_url,
|
2018-08-24 23:04:08 +00:00
|
|
|
"totalItems" : items.count(),
|
2018-08-24 17:13:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 18:27:45 +00:00
|
|
|
if items.exists():
|
2018-08-24 17:13:57 +00:00
|
|
|
result["first"] = "{}?page=1".format(our_url,)
|
|
|
|
|
2018-08-28 12:16:07 +00:00
|
|
|
return self._render(result)
|
2018-08-24 17:13:57 +00:00
|
|
|
|
|
|
|
def get_collection_items(self, *args, **kwargs):
|
2018-09-05 22:09:11 +00:00
|
|
|
return kwargs['items']
|
2018-08-24 17:13:57 +00:00
|
|
|
|
2018-08-26 20:03:00 +00:00
|
|
|
def _stringify_object(self, obj):
|
|
|
|
return str(obj)
|
|
|
|
|
2018-09-01 21:54:51 +00:00
|
|
|
class FollowingView(CollectionView):
|
|
|
|
|
|
|
|
def get_collection_items(self, *args, **kwargs):
|
|
|
|
return Following.objects.filter(follower__url=kwargs['url'])
|
|
|
|
|
|
|
|
def _stringify_object(self, obj):
|
|
|
|
return obj.following.url
|
|
|
|
|
2018-09-01 17:19:10 +00:00
|
|
|
class FollowersView(CollectionView):
|
|
|
|
|
|
|
|
def get_collection_items(self, *args, **kwargs):
|
|
|
|
return Following.objects.filter(following__url=kwargs['url'])
|
|
|
|
|
|
|
|
def _stringify_object(self, obj):
|
2018-09-01 21:54:51 +00:00
|
|
|
return obj.follower.url
|
2018-09-01 17:19:10 +00:00
|
|
|
|
2018-09-13 15:06:33 +00:00
|
|
|
########################################
|
2018-09-01 17:19:10 +00:00
|
|
|
|
2018-09-13 15:06:33 +00:00
|
|
|
class InboxView(django.views.View):
|
|
|
|
|
|
|
|
def post(self, request, name=None, *args, **kwargs):
|
|
|
|
|
|
|
|
# username is None for the shared inbox.
|
|
|
|
|
2019-04-10 19:43:37 +00:00
|
|
|
headers = defaultdict(lambda: '',
|
2019-04-10 19:11:29 +00:00
|
|
|
[(f[5:],v) for f,v in request.META.items() if f.startswith("HTTP_")])
|
|
|
|
|
|
|
|
capture = IncomingMessage(
|
|
|
|
date = headers['DATE'],
|
|
|
|
host = headers[''],
|
|
|
|
path = request.path,
|
|
|
|
signature = headers['SIGNATURE'],
|
2018-09-13 15:06:33 +00:00
|
|
|
body = str(request.body, encoding='UTF-8'),
|
|
|
|
)
|
|
|
|
capture.save()
|
2019-04-10 19:11:29 +00:00
|
|
|
logger.debug('%s: received %s at %s',
|
|
|
|
capture,
|
|
|
|
str(request.body, encoding='UTF-8'),
|
|
|
|
request.path,
|
|
|
|
)
|
2018-09-23 20:57:50 +00:00
|
|
|
|
2018-09-13 15:06:33 +00:00
|
|
|
return HttpResponse(
|
|
|
|
status = 200,
|
|
|
|
reason = 'Thank you',
|
|
|
|
content = '',
|
|
|
|
content_type = 'text/plain',
|
|
|
|
)
|
|
|
|
|
|
|
|
# We need to support GET (as a collection)
|
|
|
|
# but we don't yet.
|
2018-10-03 18:36:39 +00:00
|
|
|
|
|
|
|
########################################
|
|
|
|
|
|
|
|
class AsyncResultView(django.views.View):
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
uuid_passcode = request.GET['uuid']
|
2018-10-12 15:26:17 +00:00
|
|
|
success = int(request.GET['success'])!=0
|
2018-10-03 18:36:39 +00:00
|
|
|
|
|
|
|
if success:
|
|
|
|
|
2018-10-03 19:28:52 +00:00
|
|
|
if not request.body:
|
2018-10-12 15:26:17 +00:00
|
|
|
logger.warn('Batch notification had success==1 but no body')
|
2018-10-03 19:28:52 +00:00
|
|
|
return django.http.HttpResponseBadRequest()
|
|
|
|
|
|
|
|
body = str(request.body, encoding='UTF-8')
|
|
|
|
|
|
|
|
# Why not use request.POST? Because the batch process might
|
|
|
|
# reasonably use Content-Type: application/activity+json,
|
|
|
|
# which Django wouldn't recognise as proper JSON, so
|
|
|
|
# request.POST wouldn't get populated.
|
|
|
|
|
|
|
|
# XXX we might want to check the Content-Type here
|
2018-10-03 18:36:39 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
2018-10-03 19:28:52 +00:00
|
|
|
if request.POST:
|
2018-10-12 15:26:17 +00:00
|
|
|
logger.warn('Batch notification had success==0 but supplied a body')
|
2018-10-03 19:28:52 +00:00
|
|
|
return django.http.HttpResponseBadRequest()
|
2018-10-03 18:36:39 +00:00
|
|
|
|
|
|
|
body = None
|
|
|
|
|
|
|
|
try:
|
2018-10-03 19:28:52 +00:00
|
|
|
message_need = get_object_or_404(QuarantinedMessageNeeds, id=uuid_passcode)
|
|
|
|
except ValidationError as e:
|
|
|
|
logger.warn('Invalid UUID supplied: %s', uuid_passcode)
|
|
|
|
raise e
|
|
|
|
except QuarantinedMessageNeeds.NotFound as e:
|
2018-10-03 18:36:39 +00:00
|
|
|
logger.warn('Batch notification for unknown UUID: %s',
|
|
|
|
uuid_passcode)
|
|
|
|
raise e
|
|
|
|
|
|
|
|
if success:
|
|
|
|
logger.info('Batch processing has retrieved %s:',
|
|
|
|
message_need.needs_to_fetch)
|
|
|
|
logger.debug(' -- its contents are %s', body)
|
|
|
|
else:
|
2018-10-04 23:12:03 +00:00
|
|
|
logger.info('Batch processing has failed to retrieve %s:',
|
2018-10-03 18:36:39 +00:00
|
|
|
message_need.needs_to_fetch)
|
|
|
|
|
2018-10-12 15:26:17 +00:00
|
|
|
if success and body is not None:
|
2018-10-03 18:36:39 +00:00
|
|
|
try:
|
|
|
|
fields = json.loads(body)
|
2018-10-04 23:12:03 +00:00
|
|
|
kepi_create(fields)
|
2018-10-03 18:36:39 +00:00
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
fields = None
|
|
|
|
success = False
|
|
|
|
logger.warn('Body was not JSON. Treating as failure.')
|
|
|
|
|
2018-10-04 23:12:03 +00:00
|
|
|
if success:
|
|
|
|
logger.debug(' -- trying to deploy all matching messages again')
|
|
|
|
else:
|
|
|
|
logger.debug(' -- deleting all messages which relied on it')
|
2018-10-03 18:36:39 +00:00
|
|
|
|
|
|
|
for need in list(QuarantinedMessageNeeds.objects.filter(
|
|
|
|
needs_to_fetch = message_need.needs_to_fetch)):
|
|
|
|
|
2018-10-11 23:59:44 +00:00
|
|
|
message = need.message
|
|
|
|
need.delete()
|
|
|
|
|
2018-10-03 18:36:39 +00:00
|
|
|
if success:
|
2018-10-12 15:26:17 +00:00
|
|
|
logger.debug(' -- trying to deploy %s', str(need.message))
|
2018-10-11 23:59:44 +00:00
|
|
|
message.deploy(retrying=True)
|
2018-10-03 18:36:39 +00:00
|
|
|
else:
|
2018-10-12 15:26:17 +00:00
|
|
|
logger.debug(' -- deleting %s', str(need.message))
|
2018-10-11 23:59:44 +00:00
|
|
|
message.delete()
|
2018-10-03 18:36:39 +00:00
|
|
|
|
2018-10-04 23:12:03 +00:00
|
|
|
logger.debug(' -- finished')
|
2018-10-03 18:36:39 +00:00
|
|
|
|
|
|
|
return HttpResponse(
|
|
|
|
status = 200,
|
|
|
|
reason = 'All is well',
|
|
|
|
content = '',
|
|
|
|
content_type = 'text/plain',
|
|
|
|
)
|
|
|
|
|