From 8f03da0ec151c51f5301ddcad37127e6aefc2b49 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 21 Sep 2023 16:16:57 -0400 Subject: [PATCH] Correctly respond to application/ld+json requests This returns ActivityStreams objects when the Accept header is `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`, per the ActivityPub spec. Fixes #564 --- collections.go | 3 +-- posts.go | 3 +-- request.go | 7 +++++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/collections.go b/collections.go index fc225a2..bed61fa 100644 --- a/collections.go +++ b/collections.go @@ -496,8 +496,7 @@ func apiCheckCollectionPermissions(app *App, r *http.Request, c *Collection) (in // fetchCollection handles the API endpoint for retrieving collection data. func fetchCollection(app *App, w http.ResponseWriter, r *http.Request) error { - accept := r.Header.Get("Accept") - if strings.Contains(accept, "application/activity+json") { + if IsActivityPubRequest(r) { return handleFetchCollectionActivities(app, w, r) } diff --git a/posts.go b/posts.go index e95532e..39c12b1 100644 --- a/posts.go +++ b/posts.go @@ -1119,8 +1119,7 @@ func fetchPost(app *App, w http.ResponseWriter, r *http.Request) error { p.extractData() - accept := r.Header.Get("Accept") - if strings.Contains(accept, "application/activity+json") { + if IsActivityPubRequest(r) { if coll == nil { // This is a draft post; 404 for now // TODO: return ActivityObject diff --git a/request.go b/request.go index 0bb6e92..9ff9983 100644 --- a/request.go +++ b/request.go @@ -13,6 +13,7 @@ package writefreely import ( "mime" "net/http" + "strings" ) func IsJSON(r *http.Request) bool { @@ -20,3 +21,9 @@ func IsJSON(r *http.Request) bool { accept := r.Header.Get("Accept") return ct == "application/json" || accept == "application/json" } + +func IsActivityPubRequest(r *http.Request) bool { + accept := r.Header.Get("Accept") + return strings.Contains(accept, "application/activity+json") || + accept == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" +}