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
This commit is contained in:
Matt Baer 2018-11-10 01:29:48 -05:00
parent 3bf10d8074
commit 8a8db3be53
1 changed files with 24 additions and 1 deletions

View File

@ -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 { func fetchPost(app *app, w http.ResponseWriter, r *http.Request) error {
var collID int64 var collID int64
var coll *Collection
var err error
vars := mux.Vars(r) vars := mux.Vars(r)
if collAlias := vars["alias"]; collAlias != "" { if collAlias := vars["alias"]; collAlias != "" {
// Fetch collection information, since an alias is provided // Fetch collection information, since an alias is provided
coll, err := app.db.GetCollection(collAlias) coll, err = app.db.GetCollection(collAlias)
if err != nil { if err != nil {
return err return err
} }
@ -976,6 +978,27 @@ func fetchPost(app *app, w http.ResponseWriter, r *http.Request) error {
p.extractData() 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) return impart.WriteSuccess(w, p, http.StatusOK)
} }