From 6a0f664b565716ad08301e7699d6c0393dbba977 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sun, 22 May 2022 13:02:48 -0700 Subject: following pass through --- routes/actor.go | 38 +++++++++++++--------------- routes/admin.go | 72 +++++++++-------------------------------------------- routes/util.go | 12 +++++---- routes/webfinger.go | 2 +- 4 files changed, 37 insertions(+), 87 deletions(-) (limited to 'routes') diff --git a/routes/actor.go b/routes/actor.go index 0200d93..4f848f1 100644 --- a/routes/actor.go +++ b/routes/actor.go @@ -105,9 +105,10 @@ func ActorInbox(ctx *fiber.Ctx) error { case "Follow": for _, e := range activity.To { - if res, err := activitypub.GetActorFromDB(e); err == nil && res.Id != "" { + if _, err := activitypub.GetActorFromDB(e); err == nil { response := activity.AcceptFollow() response, err := response.SetActorFollower() + if err != nil { return util.MakeError(err, "ActorInbox") } @@ -116,52 +117,47 @@ func ActorInbox(ctx *fiber.Ctx) error { return util.MakeError(err, "ActorInbox") } - alreadyFollow := false - alreadyFollowing := false - autoSub, err := response.Actor.GetAutoSubscribe() - if err != nil { - return util.MakeError(err, "ActorInbox") - } + alreadyFollowing, err := response.Actor.IsAlreadyFollowing(response.Object.Id) - following, err := response.Actor.GetFollowing() if err != nil { return util.MakeError(err, "ActorInbox") } - for _, e := range following { - if e.Id == response.Object.Id { - alreadyFollow = true - } - } + objActor, err := activitypub.FingerActor(response.Object.Actor) - actor, err := activitypub.FingerActor(response.Object.Actor) - if err != nil { + if err != nil || objActor.Id == "" { return util.MakeError(err, "ActorInbox") } - reqActivity := activitypub.Activity{Id: actor.Following} + reqActivity := activitypub.Activity{Id: objActor.Following} remoteActorFollowingCol, err := reqActivity.GetCollection() + if err != nil { return util.MakeError(err, "ActorInbox") } + alreadyFollow := false + for _, e := range remoteActorFollowingCol.Items { if e.Id == response.Actor.Id { alreadyFollowing = true } } + autoSub, err := response.Actor.GetAutoSubscribe() + + if err != nil { + return util.MakeError(err, "ActorInbox") + } + if autoSub && !alreadyFollow && alreadyFollowing { followActivity, err := response.Actor.MakeFollowActivity(response.Object.Actor) + if err != nil { return util.MakeError(err, "ActorInbox") } - if res, err := activitypub.FingerActor(response.Object.Actor); err == nil && res.Id != "" { - if err := followActivity.MakeRequestOutbox(); err != nil { - return util.MakeError(err, "ActorInbox") - } - } else if err != nil { + if err := followActivity.MakeRequestOutbox(); err != nil { return util.MakeError(err, "ActorInbox") } } diff --git a/routes/admin.go b/routes/admin.go index 24c1479..d837a2f 100644 --- a/routes/admin.go +++ b/routes/admin.go @@ -5,7 +5,6 @@ import ( "encoding/json" "io/ioutil" "net/http" - "regexp" "time" "github.com/FChannel0/FChannel-Server/activitypub" @@ -138,77 +137,30 @@ func AdminIndex(ctx *fiber.Ctx) error { } func AdminFollow(ctx *fiber.Ctx) error { - actor, _ := webfinger.GetActorFromPath(ctx.Path(), "/"+config.Key+"/") - - following := regexp.MustCompile(`(.+)\/following`) - followers := regexp.MustCompile(`(.+)\/followers`) - follow := ctx.FormValue("follow") actorId := ctx.FormValue("actor") - //follow all of boards following - if following.MatchString(follow) { - followingActor, _ := activitypub.FingerActor(follow) - reqActivity := activitypub.Activity{Id: followingActor.Following} - col, _ := reqActivity.GetCollection() - - var nObj activitypub.ObjectBase - nObj.Id = followingActor.Id - - col.Items = append(col.Items, nObj) - - for _, e := range col.Items { - if isFollowing, _ := actor.IsAlreadyFollowing(e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId { - actor := activitypub.Actor{Id: actorId} - followActivity, _ := actor.MakeFollowActivity(e.Id) - - if actor, _ := activitypub.FingerActor(e.Id); actor.Id != "" { - followActivity.MakeRequestOutbox() - } - } - } - - //follow all of boards followers - } else if followers.MatchString(follow) { - followersActor, _ := activitypub.FingerActor(follow) - reqActivity := activitypub.Activity{Id: followersActor.Followers} - col, _ := reqActivity.GetCollection() - - var nObj activitypub.ObjectBase - nObj.Id = followersActor.Id - - col.Items = append(col.Items, nObj) - - for _, e := range col.Items { - if isFollowing, _ := actor.IsAlreadyFollowing(e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId { - actor := activitypub.Actor{Id: actorId} - followActivity, _ := actor.MakeFollowActivity(e.Id) - if actor, _ := activitypub.FingerActor(e.Id); actor.Id != "" { - followActivity.MakeRequestOutbox() - } - } - } + actor := activitypub.Actor{Id: actorId} + followActivity, _ := actor.MakeFollowActivity(follow) - //do a normal follow to a single board - } else { - actor := activitypub.Actor{Id: actorId} - followActivity, _ := actor.MakeFollowActivity(follow) + objActor := activitypub.Actor{Id: followActivity.Object.Actor} - 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 util.MakeError(err, "AdminIndex") - } + if isLocal, _ := objActor.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 util.MakeError(err, "AdminIndex") + } - if actor, _ := activitypub.FingerActor(follow); actor.Id != "" { - followActivity.MakeRequestOutbox() + if actor, _ := activitypub.FingerActor(follow); actor.Id != "" { + if err := followActivity.MakeRequestOutbox(); err != nil { + return util.MakeError(err, "AdminFollow") } } var redirect string + actor, _ = webfinger.GetActorFromPath(ctx.Path(), "/"+config.Key+"/") if actor.Name != "main" { - redirect = "/" + actor.Name + redirect = actor.Name } return ctx.Redirect("/"+config.Key+"/"+redirect, http.StatusSeeOther) diff --git a/routes/util.go b/routes/util.go index 94337bc..ed5dfb7 100644 --- a/routes/util.go +++ b/routes/util.go @@ -201,19 +201,19 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { break case "Follow": - var validActor bool - var validLocalActor bool - - validActor = (activity.Object.Actor != "") - validLocalActor = (activity.Actor.Id == actor.Id) + validActor := (activity.Object.Actor != "") + validLocalActor := (activity.Actor.Id == actor.Id) var rActivity activitypub.Activity + if validActor && validLocalActor { rActivity = activity.AcceptFollow() rActivity, err = rActivity.SetActorFollowing() + if err != nil { return util.MakeError(err, "ParseOutboxRequest") } + if err := activity.MakeRequestInbox(); err != nil { return util.MakeError(err, "ParseOutboxRequest") } @@ -221,11 +221,13 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { actor, _ := activitypub.GetActorFromDB(config.Domain) webfinger.FollowingBoards, err = actor.GetFollowing() + if err != nil { return util.MakeError(err, "ParseOutboxRequest") } webfinger.Boards, err = webfinger.GetBoardCollection() + if err != nil { return util.MakeError(err, "ParseOutboxRequest") } diff --git a/routes/webfinger.go b/routes/webfinger.go index 495e6c7..3d5fa63 100644 --- a/routes/webfinger.go +++ b/routes/webfinger.go @@ -24,7 +24,7 @@ func Webfinger(c *fiber.Ctx) error { if len(actorDomain) < 2 { c.Status(fiber.StatusBadRequest) - return c.Send([]byte("accpets only subject form of acct:board@instance")) + return c.Send([]byte("accepts only subject form of acct:board@instance")) } if actorDomain[0] == "main" { -- cgit v1.2.3