diff --git a/activity/vocab.go b/activity/vocab.go index 3ceb773..965ed4d 100644 --- a/activity/vocab.go +++ b/activity/vocab.go @@ -6,6 +6,25 @@ import ( const context = "https://www.w3.org/ns/activitystreams" +// A Group actor +// Ref: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-organization +type Group struct { + Context string `json:"@context"` + ID *url.URL `json:"id"` + Name string `json:"name"` + Type string `json:"type"` +} + +// NewOrganization creates an ActivityStreams Group +func NewGroup(name string, id *url.URL) Group { + return Group{ + Context: context, + Name: name, + ID: id, + Type: "Group", + } +} + // An Organization actor // Ref: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-organization type Organization struct { diff --git a/go.mod b/go.mod index 41f90f1..5ca344c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/DATA-DOG/go-txdb v0.1.0 github.com/davecgh/go-spew v1.1.1 github.com/fsnotify/fsnotify v1.4.7 + github.com/golang-migrate/migrate v3.5.4+incompatible // indirect github.com/gorilla/context v1.1.1 github.com/gorilla/mux v1.6.2 github.com/gosimple/slug v1.2.0 @@ -23,7 +24,7 @@ require ( github.com/spf13/jwalterweatherman v1.0.0 github.com/spf13/pflag v1.0.2 github.com/spf13/viper v0.0.0-20180907130602-3171ef9a2299 - github.com/stretchr/testify v1.2.2 + github.com/stretchr/testify v1.3.0 github.com/urfave/negroni v1.0.0 golang.org/x/crypto v0.0.0-20181012144002-a92615f3c490 golang.org/x/sys v0.0.0-20180925112736-b09afc3d579e diff --git a/go.sum b/go.sum index 4b349e1..96bc614 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,12 @@ github.com/DATA-DOG/go-txdb v0.1.0 h1:sC8/VRI7YvsXdthry93bEaqKwYGu/WehBFMyYwCHYpE= github.com/DATA-DOG/go-txdb v0.1.0/go.mod h1:aDC9AAfOY+kLbhVTKKXOwkqr2844my+djxj+Ou4wNb4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/golang-migrate/migrate v3.5.4+incompatible h1:R7OzwvCJTCgwapPCiX6DyBiu2czIUMDCB118gFTKTUA= +github.com/golang-migrate/migrate v3.5.4+incompatible/go.mod h1:IsVUlFN5puWOmXrqjgGUfIRIbU7mr8oNBE2tyERd9Wk= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk= github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= @@ -41,8 +44,11 @@ github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v0.0.0-20180907130602-3171ef9a2299 h1:VUZ7Yee6zqgqRLpVAdxT09x24SnCDiDLI6i3QmqmBZs= github.com/spf13/viper v0.0.0-20180907130602-3171ef9a2299/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaNVlI= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/handlers/groups/groups.go b/handlers/groups/groups.go new file mode 100644 index 0000000..9cb1c75 --- /dev/null +++ b/handlers/groups/groups.go @@ -0,0 +1,62 @@ +package groups + +import ( + "encoding/json" + "github.com/gorilla/mux" + "github.com/pubcast/pubcast/activity" + "github.com/pubcast/pubcast/data" + "github.com/pubcast/pubcast/data/models" + "github.com/pubcast/pubcast/handlers" + "net/http" + "net/url" +) + +// Get returns a Group +// +// Expects a `{slug}` url variable +// in the route: `/api/org/{slug}` +func Get(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + // These arise from the server setting up the routes incorrectly + if vars == nil { + http.Error(w, "Bad request", http.StatusInternalServerError) + return + } + slug := vars["slug"] + if slug == "" { + http.Error(w, "Bad request, no slug in url", http.StatusInternalServerError) + return + } + + // Attempt to grab the group + group, err := models.GetGroup(data.GetPool(), slug) + + // 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 group + if group == 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/group/" + slug) + actor := activity.NewGroup(group.Name, url) + + // Turn the org into JSON + bytes, err := json.Marshal(actor) + + // 500 because something went wrong marshaling the org + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Success! + w.Write(bytes) +} \ No newline at end of file diff --git a/handlers/groups/groups_test.go b/handlers/groups/groups_test.go new file mode 100644 index 0000000..74900ad --- /dev/null +++ b/handlers/groups/groups_test.go @@ -0,0 +1 @@ +package groups