diff options
| author | Raphael Kabo <mail@raphaelkabo.com> | 2022-11-17 11:11:55 +0000 | 
|---|---|---|
| committer | Raphael Kabo <mail@raphaelkabo.com> | 2023-05-13 00:04:46 +0100 | 
| commit | 93547a6ab50327bd5710f102c1479c472342bbf7 (patch) | |
| tree | 0209d7519b6caeef84f5a1472de554b94143dc6c | |
| parent | 6f7dd9002a5902c77488ac7a2ef6859137955f7d (diff) | |
Send AP responses as activitypub+json
| -rwxr-xr-x | src/routes.js | 56 | 
1 files changed, 51 insertions, 5 deletions
diff --git a/src/routes.js b/src/routes.js index 0d59b65..8c7f630 100755 --- a/src/routes.js +++ b/src/routes.js @@ -276,7 +276,15 @@ router.get("/:eventID/featured", (req, res) => {      type: "OrderedCollection",      orderedItems: [ap.createFeaturedPost(eventID)],    }; -  res.json(featured); +  if ( +    req.headers.accept && +    (req.headers.accept.includes("application/activity+json") || +      req.headers.accept.includes("application/ld+json")) +  ) { +    res.header("Content-Type", "application/activity+json").send(featured); +  } else { +    res.header("Content-Type", "application/json").send(featured); +  }  });  // return the JSON for a given activitypub message @@ -295,7 +303,19 @@ router.get("/:eventID/m/:hash", (req, res) => {        } else {          const message = event.activityPubMessages.find((el) => el.id === id);          if (message) { -          return res.json(JSON.parse(message.content)); +          if ( +            req.headers.accept && +            (req.headers.accept.includes("application/activity+json") || +              req.headers.accept.includes("application/ld+json")) +          ) { +            res +              .header("Content-Type", "application/activity+json") +              .send(JSON.parse(message.content)); +          } else { +            res +              .header("Content-Type", "application/json") +              .send(JSON.parse(message.content)); +          }          } else {            res.status(404);            return res.render("404", { url: req.url }); @@ -340,7 +360,19 @@ router.get("/.well-known/webfinger", (req, res) => {            res.status(404);            res.render("404", { url: req.url });          } else { -          res.json(ap.createWebfinger(eventID, domain)); +          if ( +            req.headers.accept && +            (req.headers.accept.includes("application/activity+json") || +              req.headers.accept.includes("application/ld+json")) +          ) { +            res +              .header("Content-Type", "application/activity+json") +              .send(ap.createWebfinger(eventID, domain)); +          } else { +            res +              .header("Content-Type", "application/json") +              .send(ap.createWebfinger(eventID, domain)); +          }          }        })        .catch((err) => { @@ -513,7 +545,9 @@ router.get("/:eventID", (req, res) => {              req.headers.accept.includes("application/json") ||              req.headers.accept.includes("application/json+ld"))          ) { -          res.json(JSON.parse(event.activityPubActor)); +          res +            .header("Content-Type", "application/activity+json") +            .send(JSON.parse(event.activityPubActor));          } else {            res.set("X-Robots-Tag", "noindex");            res.render("event", { @@ -588,7 +622,19 @@ router.get("/:eventID/followers", (req, res) => {          },          "@context": ["https://www.w3.org/ns/activitystreams"],        }; -      return res.json(followersCollection); +      if ( +        req.headers.accept && +        (req.headers.accept.includes("application/activity+json") || +          req.headers.accept.includes("application/ld+json")) +      ) { +        return res +          .header("Content-Type", "application/activity+json") +          .send(followersCollection); +      } else { +        return res +          .header("Content-Type", "application/json") +          .send(followersCollection); +      }      } else {        return res.status(400).send("Bad request.");      }  | 
