From f7bf818d29393ceaccf4d2906557351fa6a4f49f Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 7 May 2022 21:21:38 -0700 Subject: added error func and general cleanup/organization --- routes/actor.go | 11 ++++++----- routes/admin.go | 9 +++------ routes/outbox.go | 25 +++++++++++-------------- routes/post.go | 5 +++-- routes/util.go | 41 +++++++++++++++++------------------------ routes/webfinger.go | 3 ++- 6 files changed, 42 insertions(+), 52 deletions(-) (limited to 'routes') diff --git a/routes/actor.go b/routes/actor.go index 86a5fb9..81f6dbe 100644 --- a/routes/actor.go +++ b/routes/actor.go @@ -34,15 +34,16 @@ func ActorInbox(ctx *fiber.Ctx) error { } if !db.VerifyHeaderSignature(ctx, *activity.Actor) { - response := activitypub.RejectActivity(activity) + response := activity.Reject() return db.MakeActivityRequest(response) } switch activity.Type { case "Create": for _, e := range activity.To { - if res, err := activitypub.IsActorLocal(e); err == nil && res { - if res, err := activitypub.IsActorLocal(activity.Actor.Id); err == nil && res { + actor := activitypub.Actor{Id: e} + if res, err := actor.IsLocal(); err == nil && res { + if res, err := activity.Actor.IsLocal(); err == nil && res { col, err := activity.Object.GetCollection() if err != nil { return err @@ -107,7 +108,7 @@ func ActorInbox(ctx *fiber.Ctx) error { for _, e := range activity.To { if res, err := activitypub.GetActorFromDB(e); err == nil && res.Id != "" { response := db.AcceptFollow(activity) - response, err := activitypub.SetActorFollowerDB(response) + response, err := response.SetFollower() if err != nil { return err } @@ -168,7 +169,7 @@ func ActorInbox(ctx *fiber.Ctx) error { return err } else { fmt.Println("follow request for rejected") - response := activitypub.RejectActivity(activity) + response := activity.Reject() return db.MakeActivityRequest(response) } } diff --git a/routes/admin.go b/routes/admin.go index f8f55eb..bd489a5 100644 --- a/routes/admin.go +++ b/routes/admin.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "io/ioutil" - "log" "net/http" "regexp" "time" @@ -30,7 +29,6 @@ func AdminVerify(ctx *fiber.Ctx) error { req, err := http.NewRequest("POST", config.Domain+"/auth", bytes.NewBuffer(j)) if err != nil { - log.Println("error making verify req") return err } @@ -39,7 +37,6 @@ func AdminVerify(ctx *fiber.Ctx) error { resp, err := http.DefaultClient.Do(req) if err != nil { - log.Println("error getting verify resp") return err } @@ -69,7 +66,6 @@ func AdminAuth(ctx *fiber.Ctx) error { err := json.Unmarshal(ctx.Body(), &verify) if err != nil { - log.Println("error get verify from json") return err } @@ -192,7 +188,8 @@ func AdminFollow(ctx *fiber.Ctx) error { } else { followActivity, _ := db.MakeFollowActivity(actorId, follow) - if isLocal, _ := activitypub.IsActorLocal(followActivity.Object.Actor); !isLocal && followActivity.Actor.Id == config.Domain { + actor := activitypub.Actor{Id: followActivity.Object.Actor} + if isLocal, _ := actor.IsLocal(); !isLocal && followActivity.Actor.Id == config.Domain { _, err := ctx.Write([]byte("main board can only follow local boards. Create a new board and then follow outside boards from it.")) return err } @@ -303,7 +300,7 @@ func AdminActorIndex(ctx *fiber.Ctx) error { data.Followers = followers data.Reported = reports data.Domain = config.Domain - data.IsLocal, _ = activitypub.IsActorLocal(actor.Id) + data.IsLocal, _ = actor.IsLocal() data.Title = "Manage /" + actor.Name + "/" data.Boards = webfinger.Boards diff --git a/routes/outbox.go b/routes/outbox.go index b2ffb42..902d3e6 100644 --- a/routes/outbox.go +++ b/routes/outbox.go @@ -12,7 +12,6 @@ import ( ) func Outbox(ctx *fiber.Ctx) error { - actor, err := webfinger.GetActorFromPath(ctx.Path(), "/") if err != nil { return err @@ -27,31 +26,29 @@ func Outbox(ctx *fiber.Ctx) error { } func OutboxGet(ctx *fiber.Ctx) error { + actor, err := activitypub.GetActorByNameFromDB(ctx.Params("actor")) - actor, _ := activitypub.GetActorByNameFromDB(ctx.Params("actor")) + if err != nil { + return nil + } if activitypub.AcceptActivity(ctx.Get("Accept")) { actor.GetInfoResp(ctx) return nil } - collection, valid, err := wantToServePage(ctx.Params("actor"), 0) - if err != nil { - return err - } else if !valid { - // TODO: 404 template - return ctx.SendString("404") - } - var page int - postNum := ctx.Query("page") - if postNum != "" { - page, err = strconv.Atoi(postNum) - if err != nil { + if postNum := ctx.Query("page"); postNum != "" { + if page, err = strconv.Atoi(postNum); err != nil { return err } } + collection, err := actor.WantToServePage(page) + if err != nil { + return err + } + var offset = 15 var pages []int pageLimit := (float64(collection.TotalItems) / float64(offset)) diff --git a/routes/post.go b/routes/post.go index ac722be..813f53d 100644 --- a/routes/post.go +++ b/routes/post.go @@ -12,14 +12,15 @@ import ( ) func PostGet(ctx *fiber.Ctx) error { + actor, err := activitypub.GetActorByNameFromDB(ctx.Params("actor")) if err != nil { - return err + return nil } // this is a activitpub json request return json instead of html page if activitypub.AcceptActivity(ctx.Get("Accept")) { - activitypub.GetActorPost(ctx, ctx.Path()) + GetActorPost(ctx, ctx.Path()) return nil } diff --git a/routes/util.go b/routes/util.go index 778f52b..de0c6e0 100644 --- a/routes/util.go +++ b/routes/util.go @@ -1,7 +1,7 @@ package routes import ( - "errors" + "encoding/json" "fmt" "html/template" "regexp" @@ -18,8 +18,6 @@ import ( "github.com/gofiber/template/html" ) -var ErrorPageLimit = errors.New("above page limit") - func getThemeCookie(c *fiber.Ctx) string { cookie := c.Cookies("theme") if cookie != "" { @@ -30,22 +28,17 @@ func getThemeCookie(c *fiber.Ctx) string { return "default" } -func wantToServePage(actorName string, page int) (activitypub.Collection, bool, error) { +func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) { var collection activitypub.Collection serve := false - // TODO: don't hard code? - if page > 10 { - return collection, serve, ErrorPageLimit - } - actor, err := activitypub.GetActorByNameFromDB(actorName) if err != nil { return collection, false, err } if actor.Id != "" { - collection, err = actor.GetCollectionPage(page) + collection, err = actor.GetCatalogCollection() if err != nil { return collection, false, err } @@ -57,7 +50,7 @@ func wantToServePage(actorName string, page int) (activitypub.Collection, bool, return collection, serve, nil } -func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) { +func wantToServeArchive(actorName string) (activitypub.Collection, bool, error) { var collection activitypub.Collection serve := false @@ -67,7 +60,7 @@ func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) } if actor.Id != "" { - collection, err = actor.GetCatalogCollection() + collection, err = actor.GetCollectionType("Archive") if err != nil { return collection, false, err } @@ -79,26 +72,26 @@ func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) return collection, serve, nil } -func wantToServeArchive(actorName string) (activitypub.Collection, bool, error) { - var collection activitypub.Collection - serve := false +func GetActorPost(ctx *fiber.Ctx, path string) error { + obj := activitypub.ObjectBase{Id: config.Domain + "" + path} + collection, err := obj.GetCollectionFromPath() - actor, err := activitypub.GetActorByNameFromDB(actorName) if err != nil { - return collection, false, err + return err } - if actor.Id != "" { - collection, err = actor.GetCollectionType("Archive") + if len(collection.OrderedItems) > 0 { + enc, err := json.MarshalIndent(collection, "", "\t") if err != nil { - return collection, false, err + return err } - collection.Actor = actor - return collection, true, nil + ctx.Response().Header.Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") + _, err = ctx.Write(enc) + return err } - return collection, serve, nil + return nil } func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { @@ -194,7 +187,7 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { return err } - if res, err := activitypub.IsActivityLocal(activity); err == nil && res { + if res, err := activity.IsLocal(); err == nil && res { if res := db.VerifyHeaderSignature(ctx, *activity.Actor); err == nil && !res { ctx.Response().Header.Set("Status", "403") _, err = ctx.Write([]byte("")) diff --git a/routes/webfinger.go b/routes/webfinger.go index 221b7dc..9789e39 100644 --- a/routes/webfinger.go +++ b/routes/webfinger.go @@ -33,7 +33,8 @@ func Webfinger(c *fiber.Ctx) error { actorDomain[0] = "/" + actorDomain[0] } - if res, err := activitypub.IsActorLocal(config.TP + "" + actorDomain[1] + "" + actorDomain[0]); err == nil && !res { + actor := activitypub.Actor{Id: config.TP + "" + actorDomain[1] + "" + actorDomain[0]} + if res, err := actor.IsLocal(); err == nil && !res { c.Status(fiber.StatusBadRequest) return c.Send([]byte("actor not local")) } else if err != nil { -- cgit v1.2.3