Skip to content

Commit

Permalink
Added getting Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
dkun7944 committed Feb 23, 2019
1 parent 1478d6f commit b46dd3a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
19 changes: 19 additions & 0 deletions activity/vocab.go
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions 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=
Expand Down Expand Up @@ -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=
Expand Down
62 changes: 62 additions & 0 deletions 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)
}
1 change: 1 addition & 0 deletions handlers/groups/groups_test.go
@@ -0,0 +1 @@
package groups

0 comments on commit b46dd3a

Please sign in to comment.