From 8a8db3be533d990ab9f30a6ea1cffccd86b980a8 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 10 Nov 2018 01:29:48 -0500 Subject: [PATCH] Support retrieving posts as AS2 object Previously this was only supported on /api/collections/{alias}/posts/{id} -- this also allows it on /api/posts/{id}, so things like `Announce`s work. This closes #4 --- posts.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/posts.go b/posts.go index 194481d..75e5897 100644 --- a/posts.go +++ b/posts.go @@ -955,10 +955,12 @@ func pinPost(app *app, w http.ResponseWriter, r *http.Request) error { func fetchPost(app *app, w http.ResponseWriter, r *http.Request) error { var collID int64 + var coll *Collection + var err error vars := mux.Vars(r) if collAlias := vars["alias"]; collAlias != "" { // Fetch collection information, since an alias is provided - coll, err := app.db.GetCollection(collAlias) + coll, err = app.db.GetCollection(collAlias) if err != nil { return err } @@ -976,6 +978,27 @@ 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") { + // Fetch information about the collection this belongs to + if coll == nil && p.CollectionID.Valid { + coll, err = app.db.GetCollectionByID(p.CollectionID.Int64) + if err != nil { + return err + } + } + if coll == nil { + // This is a draft post; 404 for now + // TODO: return ActivityObject + return impart.HTTPError{http.StatusNotFound, ""} + } + + p.Collection = &CollectionObj{Collection: *coll} + po := p.ActivityObject() + po.Context = []interface{}{activitystreams.Namespace} + return impart.RenderActivityJSON(w, po, http.StatusOK) + } + return impart.WriteSuccess(w, p, http.StatusOK) }