subhub/handlers/note/get.go

54 lines
1.1 KiB
Go

package note
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
"net/http"
"net/url"
)
// Get returns a Note
//
// Expects a `{noteid}` url variable
// in the route: `/api/note/{noteid}`
func Get(c *gin.Context) {
noteId := c.Param("noteid")
if noteId == "" {
c.String(http.StatusInternalServerError, "Bad request, no slug in url")
return
}
// Attempt to grab the Note
note, err := models.GetNote(data.GetPool(), noteId)
// 500 because something went wrong with the database
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 404 because something we couldn't find the show
if show == nil {
http.Error(w, slug+" does not exist on this server", http.StatusNotFound)
return
}
// Convert to an ActivityPub object
url, err := url.Parse(handlers.GetFullHostname() + "/api/show/" + slug)
actor := activity.NewOrganization(show.Name, url)
// Turn the show into JSON
bytes, err := json.Marshal(actor)
// 500 because something went wrong marshaling the show
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Success!
w.Write(bytes)
}