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 +- 4 files changed, 37 insertions(+), 37 deletions(-) (limited to 'activitypub') 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") } } -- cgit v1.2.3