From 6a0f664b565716ad08301e7699d6c0393dbba977 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sun, 22 May 2022 13:02:48 -0700 Subject: following pass through --- activitypub/activity.go | 49 +++++++++++++++------------------ activitypub/actor.go | 19 ++++++++----- activitypub/object.go | 4 +-- activitypub/util.go | 2 +- main.go | 6 ++--- routes/actor.go | 38 ++++++++++++-------------- routes/admin.go | 72 +++++++++---------------------------------------- routes/util.go | 12 +++++---- routes/webfinger.go | 2 +- 9 files changed, 77 insertions(+), 127 deletions(-) diff --git a/activitypub/activity.go b/activitypub/activity.go index 1650f14..036a5ce 100644 --- a/activitypub/activity.go +++ b/activitypub/activity.go @@ -218,47 +218,42 @@ func (activity Activity) Report(reason string) (bool, error) { func (activity Activity) SetActorFollower() (Activity, error) { var query string - alreadyFollow, err := activity.Actor.IsAlreadyFollower(activity.Object.Actor) + alreadyFollower, err := activity.Actor.IsAlreadyFollower(activity.Object.Actor) if err != nil { return activity, util.MakeError(err, "SetFollower") } - activity.Type = "Reject" if activity.Actor.Id == activity.Object.Actor { + activity.Type = "Reject" return activity, nil } - if alreadyFollow { + if alreadyFollower { query = `delete from follower where id=$1 and follower=$2` - activity.Summary = activity.Object.Actor + " Unfollow " + activity.Actor.Id - if _, err := config.DB.Exec(query, activity.Actor.Id, activity.Object.Actor); err != nil { return activity, util.MakeError(err, "SetFollower") } activity.Type = "Accept" + activity.Summary = activity.Object.Actor + " Unfollow " + activity.Actor.Id return activity, util.MakeError(err, "SetFollower") } query = `insert into follower (id, follower) values ($1, $2)` - activity.Summary = activity.Object.Actor + " Follow " + activity.Actor.Id - if _, err := config.DB.Exec(query, activity.Actor.Id, activity.Object.Actor); err != nil { return activity, util.MakeError(err, "SetFollower") } activity.Type = "Accept" + activity.Summary = activity.Object.Actor + " Follow " + activity.Actor.Id + return activity, nil } func (activity Activity) SetActorFollowing() (Activity, error) { - var query string - - alreadyFollowing := false - alreadyFollower := false objActor, _ := GetActor(activity.Object.Actor) - following, err := objActor.GetFollowing() + alreadyFollowing, err := objActor.IsAlreadyFollowing(activity.Actor.Id) if err != nil { return activity, util.MakeError(err, "SetActorFollowing") @@ -277,11 +272,7 @@ func (activity Activity) SetActorFollowing() (Activity, error) { return activity, util.MakeError(err, "SetActorFollowing") } - for _, e := range following { - if e.Id == activity.Actor.Id { - alreadyFollowing = true - } - } + alreadyFollower := false for _, e := range remoteActorFollowerCol.Items { if e.Id == activity.Object.Actor { @@ -289,44 +280,45 @@ func (activity Activity) SetActorFollowing() (Activity, error) { } } - activity.Type = "Reject" - if activity.Actor.Id == activity.Object.Actor { + activity.Type = "Reject" return activity, nil } - if alreadyFollowing && alreadyFollower { - query = `delete from following where id=$1 and following=$2` - activity.Summary = activity.Object.Actor + " Unfollowing " + activity.Actor.Id + var query string + if alreadyFollowing && alreadyFollower { if res, err := activity.Actor.IsLocal(); err == nil && !res { go activity.Actor.DeleteCache() - } else { + } else if err != nil { return activity, util.MakeError(err, "SetActorFollowing") } + query = `delete from following where id=$1 and following=$2` if _, err := config.DB.Exec(query, activity.Object.Actor, activity.Actor.Id); err != nil { return activity, util.MakeError(err, "SetActorFollowing") } activity.Type = "Accept" + activity.Summary = activity.Object.Actor + " Unfollowing " + activity.Actor.Id return activity, nil } if !alreadyFollowing && !alreadyFollower { - - query = `insert into following (id, following) values ($1, $2)` - activity.Summary = activity.Object.Actor + " Following " + activity.Actor.Id - if res, err := activity.Actor.IsLocal(); err == nil && !res { go activity.Actor.WriteCache() + } else if err != nil { + return activity, util.MakeError(err, "SetActorFollowing") } + + query = `insert into following (id, following) values ($1, $2)` if _, err := config.DB.Exec(query, activity.Object.Actor, activity.Actor.Id); err != nil { return activity, util.MakeError(err, "SetActorFollowing") } activity.Type = "Accept" + activity.Summary = activity.Object.Actor + " Following " + activity.Actor.Id return activity, nil } @@ -354,9 +346,11 @@ func (activity Activity) MakeFollowingReq() (bool, error) { } defer resp.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) var respActivity Activity + err = json.Unmarshal(body, &respActivity) return respActivity.Type == "Accept", util.MakeError(err, "MakeFollowingReq") @@ -422,7 +416,6 @@ func (activity Activity) MakeRequestOutbox() error { } req, err := http.NewRequest("POST", activity.Actor.Outbox, bytes.NewBuffer(j)) - if err != nil { return util.MakeError(err, "MakeRequestOutbox") } diff --git a/activitypub/actor.go b/activitypub/actor.go index bc7ba8a..aa1081f 100644 --- a/activitypub/actor.go +++ b/activitypub/actor.go @@ -32,6 +32,10 @@ func (actor Actor) AddFollower(follower string) error { } func (actor Actor) ActivitySign(signature string) (string, error) { + if actor.PublicKey.Id == "" { + actor, _ = GetActorFromDB(actor.Id) + } + var file string query := `select file from publicKeyPem where id=$1 ` @@ -101,7 +105,7 @@ func (actor Actor) AutoFollow() error { return util.MakeError(err, "AutoFollow") } - follower, err := nActor.GetFollow() + follower, err := nActor.GetFollower() if err != nil { return util.MakeError(err, "AutoFollow") @@ -494,7 +498,7 @@ func (actor Actor) GetCollectionTypeLimit(nType string, limit int) (Collection, return nColl, nil } -func (actor Actor) GetFollow() ([]ObjectBase, error) { +func (actor Actor) GetFollower() ([]ObjectBase, error) { var followerCollection []ObjectBase query := `select follower from follower where id=$1` @@ -596,7 +600,7 @@ func (actor Actor) GetFollowersResp(ctx *fiber.Ctx) error { return util.MakeError(err, "GetFollowersResp") } - following.Items, err = actor.GetFollow() + following.Items, err = actor.GetFollower() if err != nil { return util.MakeError(err, "GetFollowersResp") @@ -798,7 +802,7 @@ func (actor Actor) IsAlreadyFollowing(follow string) (bool, error) { } func (actor Actor) IsAlreadyFollower(follow string) (bool, error) { - followers, err := actor.GetFollow() + followers, err := actor.GetFollower() if err != nil { return false, util.MakeError(err, "IsAlreadyFollower") @@ -898,7 +902,7 @@ func (actor Actor) SendToFollowers(activity Activity) error { } activity.Actor = &nActor - followers, err := nActor.GetFollow() + followers, err := nActor.GetFollower() if err != nil { return util.MakeError(err, "SendToFollowers") @@ -949,10 +953,13 @@ func (actor Actor) Verify(signature string, verify string) error { sig, _ := base64.StdEncoding.DecodeString(signature) if actor.PublicKey.PublicKeyPem == "" { - _actor, err := FingerActor(actor.Id) + // TODO: this should be Finger but its not getting PublicKeyPem atm + _actor, err := GetActorFromDB(actor.Id) + if err != nil { return util.MakeError(err, "Verify") } + actor = _actor } diff --git a/activitypub/object.go b/activitypub/object.go index 29a17e4..59ea904 100644 --- a/activitypub/object.go +++ b/activitypub/object.go @@ -104,7 +104,7 @@ func (obj ObjectBase) DeleteAndRepliesRequest() error { activity.Actor.Id = nObj.OrderedItems[0].Actor activity.Object = &nObj.OrderedItems[0] objActor, _ := GetActor(nObj.OrderedItems[0].Actor) - followers, err := objActor.GetFollow() + followers, err := objActor.GetFollower() if err != nil { return util.MakeError(err, "DeleteAndRepliesRequest") @@ -263,7 +263,7 @@ func (obj ObjectBase) DeleteRequest() error { } activity.Actor = &actor objActor, _ := GetActor(nObj.Actor) - followers, err := objActor.GetFollow() + followers, err := objActor.GetFollower() if err != nil { return util.MakeError(err, "DeleteRequest") } diff --git a/activitypub/util.go b/activitypub/util.go index 9dfa1ae..f53d8e5 100644 --- a/activitypub/util.go +++ b/activitypub/util.go @@ -356,7 +356,7 @@ func GetActorFromDB(id string) (Actor, error) { if nActor.Id != "" && nActor.PublicKey.PublicKeyPem == "" { if err := CreatePublicKeyFromPrivate(&nActor, publicKeyPem); err != nil { - return nActor, util.MakeError(err, "") + return nActor, util.MakeError(err, "GetActorFromDB") } } diff --git a/main.go b/main.go index 039ee61..cc167ed 100644 --- a/main.go +++ b/main.go @@ -62,11 +62,11 @@ func main() { app.Post("/verify", routes.AdminVerify) app.Post("/auth", routes.AdminAuth) app.All("/"+config.Key+"/", routes.AdminIndex) - app.Post("/"+config.Key+"/follow", routes.AdminFollow) + app.Get("/"+config.Key+"/follow", routes.AdminFollow) app.Post("/"+config.Key+"/addboard", routes.AdminAddBoard) app.Get("/"+config.Key+"/postnews", routes.AdminPostNews) app.Get("/"+config.Key+"/newsdelete", routes.AdminNewsDelete) - app.Post("/"+config.Key+"/:actor/follow", routes.AdminActorIndex) + app.All("/"+config.Key+"/:actor/follow", routes.AdminFollow) app.Get("/"+config.Key+"/:actor", routes.AdminActorIndex) app.Get("/news", routes.NewsGet) @@ -88,7 +88,7 @@ func main() { // Board actor app.Get("/:actor/catalog", routes.CatalogGet) app.Post("/:actor/inbox", routes.ActorInbox) - app.Post("/:actor/outbox", routes.ActorOutbox) + app.All("/:actor/outbox", routes.ActorOutbox) app.Get("/:actor/following", routes.ActorFollowing) app.All("/:actor/followers", routes.ActorFollowers) app.Get("/:actor/reported", routes.ActorReported) 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