From 41c63c0688475d5212ce2262b1be248bf438a9ad Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Thu, 3 Jun 2021 02:44:35 -0700 Subject: cleaned up file names --- follow.go | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 follow.go (limited to 'follow.go') diff --git a/follow.go b/follow.go new file mode 100644 index 0000000..667c6bd --- /dev/null +++ b/follow.go @@ -0,0 +1,222 @@ +package main + +import "net/http" +import "database/sql" +import _ "github.com/lib/pq" +import "encoding/json" + +func GetActorFollowing(w http.ResponseWriter, db *sql.DB, id string) { + var following Collection + + following.AtContext.Context = "https://www.w3.org/ns/activitystreams" + following.Type = "Collection" + following.TotalItems, _ = GetActorFollowTotal(db, id) + following.Items = GetActorFollowingDB(db, id) + + enc, _ := json.MarshalIndent(following, "", "\t") + w.Header().Set("Content-Type", activitystreams) + w.Write(enc) +} + +func GetActorFollowers(w http.ResponseWriter, db *sql.DB, id string) { + var following Collection + + following.AtContext.Context = "https://www.w3.org/ns/activitystreams" + following.Type = "Collection" + _, following.TotalItems = GetActorFollowTotal(db, id) + following.Items = GetActorFollowDB(db, id) + + enc, _ := json.MarshalIndent(following, "", "\t") + w.Header().Set("Content-Type", activitystreams) + w.Write(enc) +} + + +func GetActorFollowingDB(db *sql.DB, id string) []ObjectBase { + var followingCollection []ObjectBase + query := `select following from following where id=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error with following db query") + + defer rows.Close() + + for rows.Next() { + var obj ObjectBase + + err := rows.Scan(&obj.Id) + + CheckError(err, "error with following db scan") + + followingCollection = append(followingCollection, obj) + } + + return followingCollection +} + +func GetActorFollowDB(db *sql.DB, id string) []ObjectBase { + var followerCollection []ObjectBase + + query := `select follower from follower where id=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error with follower db query") + + defer rows.Close() + + for rows.Next() { + var obj ObjectBase + + err := rows.Scan(&obj.Id) + + CheckError(err, "error with followers db scan") + + followerCollection = append(followerCollection, obj) + } + + return followerCollection +} + +func GetActorFollowTotal(db *sql.DB, id string) (int, int) { + var following int + var followers int + + query := `select count(following) from following where id=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error with following total db query") + + defer rows.Close() + + for rows.Next() { + err := rows.Scan(&following) + + CheckError(err, "error with following total db scan") + } + + query = `select count(follower) from follower where id=$1` + + rows, err = db.Query(query, id) + + CheckError(err, "error with followers total db query") + + defer rows.Close() + + for rows.Next() { + err := rows.Scan(&followers) + + CheckError(err, "error with followers total db scan") + } + + return following, followers +} + +func AcceptFollow(activity Activity) Activity { + var accept Activity + accept.AtContext.Context = activity.AtContext.Context + accept.Type = "Accept" + accept.Actor = activity.Object.Actor + var nObj ObjectBase + var nActor Actor + accept.Object = &nObj + accept.Object.Actor = &nActor + accept.Object.Actor = activity.Actor + var nNested NestedObjectBase + var mActor Actor + accept.Object.Object = &nNested + accept.Object.Object.Actor = &mActor + accept.Object.Object.Actor = activity.Object.Actor + accept.Object.Object.Type = "Follow" + accept.To = append(accept.To, activity.Object.Actor.Id) + + return accept +} + +func RejectActivity(activity Activity) Activity { + var accept Activity + accept.AtContext.Context = activity.AtContext.Context + accept.Type = "Reject" + var nObj ObjectBase + var nActor Actor + accept.Object = &nObj + accept.Object.Actor = &nActor + accept.Actor = activity.Object.Actor + accept.Object.Actor = activity.Actor + var nNested NestedObjectBase + var mActor Actor + accept.Object.Object = &nNested + accept.Object.Object.Actor = &mActor + accept.Object.Object.Actor = activity.Object.Actor + accept.Object.Object.Type = "Follow" + accept.To = append(accept.To, activity.Actor.Id) + + return accept +} + +func SetActorFollowerDB(db *sql.DB, activity Activity) Activity { + var query string + alreadyFollow := false + followers := GetActorFollowDB(db, activity.Actor.Id) + + for _, e := range followers { + if e.Id == activity.Object.Actor.Id { + alreadyFollow = true + } + } + if alreadyFollow { + query = `delete from follower where id=$1 and follower=$2` + activity.Summary = activity.Object.Actor.Id + " Unfollow " + activity.Actor.Id + } else { + query = `insert into follower (id, follower) values ($1, $2)` + activity.Summary = activity.Object.Actor.Id + " Follow " + activity.Actor.Id + } + + _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor.Id) + + if CheckError(err, "error with follower db insert/delete") != nil { + activity.Type = "Reject" + return activity + } + + activity.Type = "Accept" + return activity +} + +func SetActorFollowingDB(db *sql.DB, activity Activity) Activity { + var query string + alreadyFollow := false + following := GetActorFollowingDB(db, activity.Object.Actor.Id) + + for _, e := range following { + if e.Id == activity.Actor.Id { + alreadyFollow = true + } + } + + if alreadyFollow { + query = `delete from following where id=$1 and following=$2` + activity.Summary = activity.Object.Actor.Id + " Unfollowing " + activity.Actor.Id + if !IsActorLocal(db, activity.Actor.Id) { + go DeleteActorCache(db, activity.Actor.Id) + } + } else { + query = `insert into following (id, following) values ($1, $2)` + activity.Summary = activity.Object.Actor.Id + " Following " + activity.Actor.Id + if !IsActorLocal(db, activity.Actor.Id) { + go WriteActorToCache(db, activity.Actor.Id) + } + } + + _, err := db.Exec(query, activity.Object.Actor.Id, activity.Actor.Id) + + if CheckError(err, "error with following db insert/delete") != nil { + activity.Type = "Reject" + return activity + } + + activity.Type = "Accept" + return activity +} -- cgit v1.2.3 From ef7eb7330018c84a44fb24711982c25f51749d2e Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Thu, 24 Jun 2021 23:45:18 -0700 Subject: changed activitystream formating for objects to better align with pleromas actor field --- follow.go | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'follow.go') diff --git a/follow.go b/follow.go index 667c6bd..d62e3b8 100644 --- a/follow.go +++ b/follow.go @@ -118,19 +118,17 @@ func AcceptFollow(activity Activity) Activity { var accept Activity accept.AtContext.Context = activity.AtContext.Context accept.Type = "Accept" - accept.Actor = activity.Object.Actor + var nActor Actor + accept.Actor = &nActor + accept.Actor.Id = activity.Object.Actor var nObj ObjectBase - var nActor Actor accept.Object = &nObj - accept.Object.Actor = &nActor - accept.Object.Actor = activity.Actor + accept.Object.Actor = activity.Actor.Id var nNested NestedObjectBase - var mActor Actor accept.Object.Object = &nNested - accept.Object.Object.Actor = &mActor accept.Object.Object.Actor = activity.Object.Actor accept.Object.Object.Type = "Follow" - accept.To = append(accept.To, activity.Object.Actor.Id) + accept.To = append(accept.To, activity.Object.Actor) return accept } @@ -140,15 +138,11 @@ func RejectActivity(activity Activity) Activity { accept.AtContext.Context = activity.AtContext.Context accept.Type = "Reject" var nObj ObjectBase - var nActor Actor accept.Object = &nObj - accept.Object.Actor = &nActor - accept.Actor = activity.Object.Actor - accept.Object.Actor = activity.Actor + accept.Actor.Id = activity.Object.Actor + accept.Object.Actor = activity.Actor.Id var nNested NestedObjectBase - var mActor Actor accept.Object.Object = &nNested - accept.Object.Object.Actor = &mActor accept.Object.Object.Actor = activity.Object.Actor accept.Object.Object.Type = "Follow" accept.To = append(accept.To, activity.Actor.Id) @@ -162,19 +156,20 @@ func SetActorFollowerDB(db *sql.DB, activity Activity) Activity { followers := GetActorFollowDB(db, activity.Actor.Id) for _, e := range followers { - if e.Id == activity.Object.Actor.Id { + if e.Id == activity.Object.Actor { alreadyFollow = true } } + if alreadyFollow { query = `delete from follower where id=$1 and follower=$2` - activity.Summary = activity.Object.Actor.Id + " Unfollow " + activity.Actor.Id + activity.Summary = activity.Object.Actor + " Unfollow " + activity.Actor.Id } else { query = `insert into follower (id, follower) values ($1, $2)` - activity.Summary = activity.Object.Actor.Id + " Follow " + activity.Actor.Id + activity.Summary = activity.Object.Actor + " Follow " + activity.Actor.Id } - _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor.Id) + _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor) if CheckError(err, "error with follower db insert/delete") != nil { activity.Type = "Reject" @@ -188,7 +183,7 @@ func SetActorFollowerDB(db *sql.DB, activity Activity) Activity { func SetActorFollowingDB(db *sql.DB, activity Activity) Activity { var query string alreadyFollow := false - following := GetActorFollowingDB(db, activity.Object.Actor.Id) + following := GetActorFollowingDB(db, activity.Object.Actor) for _, e := range following { if e.Id == activity.Actor.Id { @@ -198,19 +193,19 @@ func SetActorFollowingDB(db *sql.DB, activity Activity) Activity { if alreadyFollow { query = `delete from following where id=$1 and following=$2` - activity.Summary = activity.Object.Actor.Id + " Unfollowing " + activity.Actor.Id + activity.Summary = activity.Object.Actor + " Unfollowing " + activity.Actor.Id if !IsActorLocal(db, activity.Actor.Id) { go DeleteActorCache(db, activity.Actor.Id) } } else { query = `insert into following (id, following) values ($1, $2)` - activity.Summary = activity.Object.Actor.Id + " Following " + activity.Actor.Id + activity.Summary = activity.Object.Actor + " Following " + activity.Actor.Id if !IsActorLocal(db, activity.Actor.Id) { go WriteActorToCache(db, activity.Actor.Id) } } - _, err := db.Exec(query, activity.Object.Actor.Id, activity.Actor.Id) + _, err := db.Exec(query, activity.Object.Actor, activity.Actor.Id) if CheckError(err, "error with following db insert/delete") != nil { activity.Type = "Reject" -- cgit v1.2.3 From 80bcce22a2368c3911137dec97d6744dee328809 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Fri, 25 Jun 2021 14:55:18 -0700 Subject: more cleaning up to better intergrate with plemroma standards. can verify signatures better --- follow.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'follow.go') diff --git a/follow.go b/follow.go index d62e3b8..7c56136 100644 --- a/follow.go +++ b/follow.go @@ -139,6 +139,8 @@ func RejectActivity(activity Activity) Activity { accept.Type = "Reject" var nObj ObjectBase accept.Object = &nObj + var nActor Actor + accept.Actor = &nActor accept.Actor.Id = activity.Object.Actor accept.Object.Actor = activity.Actor.Id var nNested NestedObjectBase -- cgit v1.2.3