diff --git a/README.md b/README.md index 6c352e4..6e14ee7 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ An `Activity` json might look something like this: ```json { "@context": "https://www.w3.org/ns/activitystreams", - "summary": status, + "summary": "Sally created a note", "type": "Create", "actor": { "type": "Person", @@ -132,7 +132,7 @@ An `Activity` json might look something like this: "object": { "type": "Note", "name": "A Simple Note", - "content": status + "content": "This is a simple note" } } ``` diff --git a/data/models/activity.go b/data/models/activity.go index 5213371..cb9cf99 100644 --- a/data/models/activity.go +++ b/data/models/activity.go @@ -7,7 +7,7 @@ import ( type Activity struct { Id string `json:"id"` - Payload string `json:"status"` + Payload string `json:"note"` Remote bool `json:"remote"` Group `json:"group"` CreatedAt time.Time `json:"created_at"` @@ -21,9 +21,9 @@ func GetActivity(db *sql.DB, slug string) (*Activity, error) { FROM activity WHERE id = $1 `, slug) - var status Activity - err := row.Scan(&status.Id, - &status.Name, &status.Note, &status.CreatedAt, &status.UpdatedAt) + var note Activity + err := row.Scan(¬e.Slug, + ¬e.Name, ¬e.Note, ¬e.CreatedAt, ¬e.UpdatedAt) // This is not an error from the user's perspective if err == sql.ErrNoRows { @@ -33,21 +33,21 @@ func GetActivity(db *sql.DB, slug string) (*Activity, error) { return nil, err } - return &status, nil + return ¬e, nil } /* -// PutNote creates a status with this name and status -func PutActivity(db *sql.DB, name string, status string) (string, error) { +// PutNote creates a note with this name and note +func PutActivity(db *sql.DB, name string, note string) (string, error) { // TODO make guid but not slug id := slugify.MakeLang(name, "en") query := ` - INSERT INTO activity (id, name, status) + INSERT INTO activity (id, name, note) VALUES ($1, $2, $3) ` - _, err := db.Exec(query, slug, name, status) + _, err := db.Exec(query, slug, name, note) return slug, err } */ diff --git a/data/models/group.go b/data/models/group.go index 4474437..2fb8f61 100644 --- a/data/models/group.go +++ b/data/models/group.go @@ -13,7 +13,7 @@ type Group struct { gorm.Model Id string `json:"id"` Name string `json:"name"` - Note string `json:"status"` + Note string `json:"note"` Type string UserID uint } @@ -21,12 +21,12 @@ type Group struct { // GetGroup returns a single Group object or nil func GetGroup(db *sql.DB, slug string) (*Group, error) { row := db.QueryRow(` - select slug, name, status, created_at, updated_at + select slug, name, note, created_at, updated_at from groups where slug = $1 `, slug) var group Group - err := row.Scan(&group.Id, &group.Name, &group.Note, &group.CreatedAt, &group.UpdatedAt) + err := row.Scan(&group.id, &group.Name, &group.Note, &group.CreatedAt, &group.UpdatedAt) if err == sql.ErrNoRows { return nil, nil @@ -43,7 +43,7 @@ func PutGroup(db *sql.DB, name string, note string) (string, error) { slug := slugify.MakeLang(name, "en") query := ` - insert into groups(slug, name, status) + insert into groups(slug, name, note) values ($1, $2, $3) ` _, err := db.Exec(query, slug, name, note) diff --git a/handlers/groups/get.go b/handlers/groups/get.go index 077fcd3..45adc9a 100644 --- a/handlers/groups/get.go +++ b/handlers/groups/get.go @@ -13,7 +13,7 @@ import ( // Get returns a Group // // Expects a `{name}` url variable -// in the route: `/group/:name` +// in the route: `/api/group/:name` func Get(c *gin.Context) { name := c.Param("name") if name == "" { @@ -31,7 +31,7 @@ func Get(c *gin.Context) { return } - url := handlers.GetFullHostName() + "/group/" + group.Id + url := handlers.GetFullHostName() + "/activity/group/" + group.Slug actor := activitypub.NewPerson(url) actor.PreferredUsername = group.Name actor.Name = group.Name diff --git a/handlers/groups/post.go b/handlers/groups/post.go index 5968a10..556280f 100644 --- a/handlers/groups/post.go +++ b/handlers/groups/post.go @@ -8,8 +8,8 @@ import ( ) type createGroupRequest struct { - Name string `json:"name"` - Status string `json:"status"` + Name string `json:"name"` + Note string `json:"note"` } type createGroupResponse struct { @@ -21,7 +21,8 @@ func Create(c *gin.Context) { var body createGroupRequest _ = c.BindJSON(&body) - slug, err := models.PutGroup(data.GetDB(), body.Name, body.Status) + db := data.GetDB() + slug, err := models.PutGroup(db, body.Name, body.Note) if err != nil { c.String(http.StatusInternalServerError, err.Error()) return diff --git a/handlers/note/get.go b/handlers/note/get.go new file mode 100644 index 0000000..22d96b3 --- /dev/null +++ b/handlers/note/get.go @@ -0,0 +1,53 @@ +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) +} diff --git a/handlers/status/get.go b/handlers/status/get.go deleted file mode 100644 index 698390e..0000000 --- a/handlers/status/get.go +++ /dev/null @@ -1,44 +0,0 @@ -package status - -import ( - "github.com/gin-gonic/gin" - _ "github.com/gorilla/mux" -) - -// Get returns a Note -// -// Expects a `{statusid}` url variable -// in the route: `/status/{statusid}` -func Get(c *gin.Context) { - /* - statusId := c.Param("statusid") - - if statusId == "" { - c.String(http.StatusInternalServerError, "Bad request, no status id in url") - return - } - - // Attempt to grab the Activity - status, err := models.GetActivity(data.GetDB(), statusId) - - // 500 because something went wrong with the database - if err != nil { - c.String(http.StatusInternalServerError, err.Error()) - return - } - - // 404 because something we couldn't find the status - if status == nil { - c.String(http.StatusNotFound, statusId+" does not exist on this server") - return - } - - // Convert to an ActivityPub object - url, err := url.Parse(handlers.GetFullHostName() + "/status/" + statusId) - o := activitypub.NewAnounceObject() - activity := activitypub.NewCreateActivity(0) - - c.JSON(http.StatusOK, activity) - //w.Write(bytes) - */ -} diff --git a/handlers/webfinder/webfinger.go b/handlers/webfinder/webfinger.go index 154667d..75aac1d 100644 --- a/handlers/webfinder/webfinger.go +++ b/handlers/webfinder/webfinger.go @@ -33,6 +33,11 @@ func Get(c *gin.Context) { c.String(http.StatusBadRequest, "Account not found") } + // bytes, err := json.Marshal(actor) + // if err != nil { + // http.Error(w, err.Error(), http.StatusBadRequest) + // return + // } c.JSON(http.StatusOK, actor) } diff --git a/main.go b/main.go index 0e427b5..b1df704 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ func main() { api := r.Group("/api") { api.GET("/group/:name", groups.Get) - api.GET("/group/:name/status/:statusid", groups.Get) api.GET("/group/:name/tags", groups.Get) api.GET("/group/:name/tags", groups.Get) api.GET("/group/:name/featured", groups.Get) @@ -33,14 +32,12 @@ func main() { api.POST("/group", groups.Create) } // r.GET("/@:name", groups.GetGroup) - // r.GET("/status/:noteid", status.GetNote) + // r.GET("/note/:noteid", note.GetNote) // Activity pub routes ap := r.Group("/activity") { ap.GET("/group/:name", groups.Get) - ap.GET("/group/:name/status/:statusid", groups.Get) - // TODO api.GET("/status/:statusid", groups.Get) ap.GET("/group/:name/followers", groups.Get) ap.GET("/group/:name/following", groups.Get) ap.GET("/group/:name/outbox", groups.Get) @@ -56,7 +53,7 @@ func main() { port = fromEnv } - log.WithField("port", port).Info("starting subhub api server") + log.WithField("port", port).Info("starting pubcast api server") log.Fatal(r.Run(":" + port)) }