2018-01-28 18:28:46 -05:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
|
|
|
from entries.jobs import ping_hub, send_mentions
|
|
|
|
from entries.models import Cat, Entry
|
|
|
|
from entries.kinds import Article, Note, Reply, Like, Repost
|
|
|
|
from lemoncurry import utils
|
|
|
|
|
2018-07-02 20:03:35 -04:00
|
|
|
from .. import error
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def form_to_mf2(request):
|
|
|
|
properties = {}
|
|
|
|
post = request.POST
|
|
|
|
for key in post.keys():
|
2023-08-10 02:52:37 -04:00
|
|
|
if key.endswith("[]"):
|
2018-01-28 18:28:46 -05:00
|
|
|
key = key[:-2]
|
2023-08-10 02:52:37 -04:00
|
|
|
if key == "access_token":
|
2018-01-28 18:28:46 -05:00
|
|
|
continue
|
2023-08-10 02:52:37 -04:00
|
|
|
properties[key] = post.getlist(key) + post.getlist(key + "[]")
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
type = []
|
2023-08-10 02:52:37 -04:00
|
|
|
if "h" in properties:
|
|
|
|
type = ["h-" + p for p in properties["h"]]
|
|
|
|
del properties["h"]
|
|
|
|
return {"type": type, "properties": properties}
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def create(request):
|
|
|
|
normalise = {
|
2023-08-10 02:52:37 -04:00
|
|
|
"application/json": lambda r: r.json,
|
|
|
|
"application/x-www-form-urlencoded": form_to_mf2,
|
2018-01-28 18:28:46 -05:00
|
|
|
}
|
2023-08-10 02:52:37 -04:00
|
|
|
if "create" not in request.token:
|
|
|
|
raise error.bad_scope("create")
|
2018-01-28 18:28:46 -05:00
|
|
|
if request.content_type not in normalise:
|
2018-07-02 20:03:35 -04:00
|
|
|
raise error.unsupported_type(request.content_type)
|
2018-01-28 18:28:46 -05:00
|
|
|
body = normalise[request.content_type](request)
|
2023-08-10 02:52:37 -04:00
|
|
|
if "type" not in body:
|
|
|
|
raise error.bad_req("mf2 object type required")
|
|
|
|
if body["type"] != ["h-entry"]:
|
|
|
|
raise error.bad_req("only h-entry supported")
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
entry = Entry(author=request.token.user)
|
2023-08-10 02:52:37 -04:00
|
|
|
props = body.get("properties", {})
|
2018-01-28 18:28:46 -05:00
|
|
|
kind = Note
|
2023-08-10 02:52:37 -04:00
|
|
|
if "name" in props:
|
|
|
|
entry.name = "\n".join(props["name"])
|
2018-01-28 18:28:46 -05:00
|
|
|
kind = Article
|
2023-08-10 02:52:37 -04:00
|
|
|
if "content" in props:
|
|
|
|
entry.content = "\n".join(
|
|
|
|
c if isinstance(c, str) else c["html"] for c in props["content"]
|
2018-01-28 18:28:46 -05:00
|
|
|
)
|
2023-08-10 02:52:37 -04:00
|
|
|
if "in-reply-to" in props:
|
|
|
|
entry.in_reply_to = props["in-reply-to"]
|
2018-01-28 18:28:46 -05:00
|
|
|
kind = Reply
|
2023-08-10 02:52:37 -04:00
|
|
|
if "like-of" in props:
|
|
|
|
entry.like_of = props["like-of"]
|
2018-01-28 18:28:46 -05:00
|
|
|
kind = Like
|
2023-08-10 02:52:37 -04:00
|
|
|
if "repost-of" in props:
|
|
|
|
entry.repost_of = props["repost-of"]
|
2018-01-28 18:28:46 -05:00
|
|
|
kind = Repost
|
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
cats = [Cat.objects.from_name(c) for c in props.get("category", [])]
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
entry.kind = kind.id
|
|
|
|
entry.save()
|
2018-03-21 07:02:27 -04:00
|
|
|
entry.cats.set(cats)
|
2018-01-28 18:28:46 -05:00
|
|
|
entry.save()
|
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
for url in props.get("syndication", []):
|
2018-06-28 07:07:24 -04:00
|
|
|
entry.syndications.create(url=url)
|
|
|
|
|
2018-01-28 18:28:46 -05:00
|
|
|
base = utils.origin(request)
|
|
|
|
perma = urljoin(base, entry.url)
|
2018-05-09 23:42:59 -04:00
|
|
|
short = urljoin(base, entry.short_url)
|
2018-07-02 01:08:13 -04:00
|
|
|
ping_hub.delay(*entry.affected_urls)
|
2018-01-28 18:28:46 -05:00
|
|
|
send_mentions.delay(perma)
|
|
|
|
|
|
|
|
res = HttpResponse(status=201)
|
2023-08-10 02:52:37 -04:00
|
|
|
res["Location"] = perma
|
|
|
|
res["Link"] = '<{}>; rel="shortlink"'.format(short)
|
2018-01-28 18:28:46 -05:00
|
|
|
return res
|