Browse Source

Implement the Micropub source query internally rather than by simply parsing the visible content

Danielle McLean 8 months ago
parent
commit
1e4df2d1b5
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 8EB789DDF3ABD240
1 changed files with 18 additions and 12 deletions
  1. 18
    12
      micropub/views/query.py

+ 18
- 12
micropub/views/query.py View File

@@ -1,10 +1,13 @@
1 1
 from django.http import JsonResponse
2 2
 from django.urls import reverse
3
+
4
+from entries.from_url import from_url
3 5
 from lemoncurry import requests
4 6
 from lemoncurry.utils import absolute_url
5 7
 from .. import error
6 8
 
7 9
 
10
+
8 11
 def config(request):
9 12
     config = syndicate_to(request)
10 13
     config['media-endpoint'] = absolute_url(request, reverse('micropub:media'))
@@ -12,19 +15,22 @@ def config(request):
12 15
 
13 16
 
14 17
 def source(request):
15
-    get = request.GET
16
-    if 'url' not in get:
18
+    if 'url' not in request.GET:
17 19
         raise error.bad_req('must specify url parameter for source query')
18
-    mf2 = requests.mf2(get['url']).to_dict(filter_by_type='h-entry')
19
-    if not mf2:
20
-        raise error.bad_req('no h-entry at the requested url')
21
-    entry = mf2[0]
22
-    keys = get.getlist('properties', []) + get.getlist('properties[]', [])
23
-    if not keys:
24
-        return entry
25
-
26
-    props = entry['properties']
27
-    return {'properties': {k: props[k] for k in keys if k in props}}
20
+    entry = from_url(request.GET['url'])
21
+    props = {}
22
+
23
+    keys = set(request.GET.getlist('properties') + request.GET.getlist('properties[]'))
24
+    if not keys or 'content' in keys:
25
+        props['content'] = [entry.content]
26
+    if (not keys or 'category' in keys) and entry.cats.exists():
27
+        props['category'] = [cat.name for cat in entry.cats.all()]
28
+    if (not keys or 'name' in keys) and entry.name:
29
+        props['name'] = [entry.name]
30
+    if (not keys or 'syndication' in keys) and entry.syndications.exists():
31
+        props['syndication'] = [synd.url for synd in entry.syndications.all()]
32
+
33
+    return {'type': ['h-entry'], 'properties': props}
28 34
 
29 35
 
30 36
 def syndicate_to(request):

Loading…
Cancel
Save