From c8af8a413f6f53e7d1a15a7d823ff28be2db3c23 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 14 Sep 2021 13:13:54 -0700 Subject: [PATCH] Use the @unclearParadigm REST parameter helper --- activitypub/controllers/actors.go | 21 +++++++++++---------- activitypub/controllers/object.go | 9 ++++++++- activitypub/router.go | 5 +++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/activitypub/controllers/actors.go b/activitypub/controllers/actors.go index dc02692c0..c454c8699 100644 --- a/activitypub/controllers/actors.go +++ b/activitypub/controllers/actors.go @@ -2,7 +2,6 @@ package controllers import ( "net/http" - "strings" log "github.com/sirupsen/logrus" @@ -10,6 +9,7 @@ import ( "github.com/owncast/owncast/activitypub/crypto" "github.com/owncast/owncast/activitypub/requests" "github.com/owncast/owncast/core/data" + "github.com/owncast/owncast/utils" ) // ActorHandler handles requests for a single actor. @@ -19,8 +19,12 @@ func ActorHandler(w http.ResponseWriter, r *http.Request) { return } - pathComponents := strings.Split(r.URL.Path, "/") - accountName := pathComponents[3] + accountName, err := utils.ReadRestURLParameter(r, "user") + if err != nil || accountName == "" { + w.WriteHeader(http.StatusBadRequest) + return + } + resource, _ := utils.ReadRestURLParameter(r, "resource") if _, valid := data.GetFederatedInboxMap()[accountName]; !valid { // User is not valid @@ -30,20 +34,17 @@ func ActorHandler(w http.ResponseWriter, r *http.Request) { // If this request is for an actor's inbox then pass // the request to the inbox controller. - if len(pathComponents) == 5 && pathComponents[4] == "inbox" { + if resource == "inbox" { InboxHandler(w, r) return - } else if len(pathComponents) == 5 && pathComponents[4] == "outbox" { + } else if resource == "outbox" { OutboxHandler(w, r) return - // } else if len(pathComponents) == 5 { - // ActorObjectHandler(w, r) - // return - } else if len(pathComponents) == 5 && pathComponents[4] == "followers" { + } else if resource == "followers" { // followers list FollowersHandler(w, r) return - } else if len(pathComponents) == 5 && pathComponents[4] == "following" { + } else if resource == "following" { // following list (none) w.WriteHeader(http.StatusNotFound) return diff --git a/activitypub/controllers/object.go b/activitypub/controllers/object.go index 69340b823..8dd4c5f9e 100644 --- a/activitypub/controllers/object.go +++ b/activitypub/controllers/object.go @@ -8,6 +8,7 @@ import ( "github.com/owncast/owncast/activitypub/persistence" "github.com/owncast/owncast/activitypub/requests" "github.com/owncast/owncast/core/data" + "github.com/owncast/owncast/utils" log "github.com/sirupsen/logrus" ) @@ -18,7 +19,13 @@ func ObjectHandler(w http.ResponseWriter, r *http.Request) { return } - object, err := persistence.GetObjectByIRI(r.URL.Path) + objectID, err := utils.ReadRestURLParameter(r, "object") + if err != nil || objectID == "" { + w.WriteHeader(http.StatusBadRequest) + return + } + + object, err := persistence.GetObjectByID(objectID) if err != nil { w.WriteHeader(http.StatusNotFound) return diff --git a/activitypub/router.go b/activitypub/router.go index 7b837f49d..4a8409aee 100644 --- a/activitypub/router.go +++ b/activitypub/router.go @@ -5,6 +5,7 @@ import ( "github.com/owncast/owncast/activitypub/controllers" "github.com/owncast/owncast/router/middleware" + "github.com/owncast/owncast/utils" ) // StartRouter will start the federation specific http router. @@ -28,8 +29,8 @@ func StartRouter() { http.HandleFunc("/api/v1/instance", controllers.InstanceV1Controller) // Single ActivityPub Actor - http.HandleFunc("/federation/user/", controllers.ActorHandler) + http.HandleFunc(utils.RestEndpoint("/federation/user/{user}/{resource}", controllers.ActorHandler)) // Single AP object - http.HandleFunc("/federation/", middleware.RequireActivityPubOrRedirect(controllers.ObjectHandler)) + http.HandleFunc(utils.RestEndpoint("/federation/{object}", middleware.RequireActivityPubOrRedirect(controllers.ObjectHandler))) }