From 22ee823b6e9200317337941c2ad41ca9df9dd855 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Thu, 15 Jul 2021 13:49:29 -0700 Subject: added auto follow for boards --- Server | Bin 0 -> 11195223 bytes client.go | 1 + database.go | 26 ++++++++++++++++ databaseschema.psql | 4 ++- follow.go | 46 +++++++++++++++++++++++++++++ main.go | 83 ++++++++++++++++++++++++---------------------------- outboxPost.go | 18 ++++++++++++ static/manage.html | 4 +-- 8 files changed, 134 insertions(+), 48 deletions(-) create mode 100755 Server diff --git a/Server b/Server new file mode 100755 index 0000000..4977d41 Binary files /dev/null and b/Server differ diff --git a/client.go b/client.go index a46f2a1..af83a28 100644 --- a/client.go +++ b/client.go @@ -64,6 +64,7 @@ type AdminPage struct { Domain string IsLocal bool PostBlacklist []PostBlacklist + AutoSubscribe bool } type Report struct { diff --git a/database.go b/database.go index 0e53142..1b2ec3d 100644 --- a/database.go +++ b/database.go @@ -1649,3 +1649,29 @@ func DeleteRegexBlacklistDB(db *sql.DB, id int) { CheckError(err, "error with delete from postblacklist") } + +func GetActorAutoSubscribeDB(db *sql.DB, id string) bool{ + query := `select autosubscribe from actor where id=$1` + + rows, err:= db.Query(query, id) + + CheckError(err, "error with getting actor auto subscribe status from db") + + var subscribed bool + defer rows.Close() + rows.Next() + rows.Scan(&subscribed) + + + return subscribed +} + +func SetActorAutoSubscribeDB(db *sql.DB, id string) { + current := GetActorAutoSubscribeDB(db, id) + + query := `update actor set autosubscribe=$1 where id=$2` + + _, err := db.Exec(query, !current, id) + + CheckError(err, "error with set actor auto subscribe status from db") +} diff --git a/databaseschema.psql b/databaseschema.psql index 8c0c0d4..954aa2e 100644 --- a/databaseschema.psql +++ b/databaseschema.psql @@ -230,4 +230,6 @@ ALTER TABLE cacheactivitystream ADD COLUMN IF NOT EXISTS sensitive boolean defau CREATE TABLE IF NOT EXISTS postblacklist( id serial primary key, regex varchar(200) -); \ No newline at end of file +); + +ALTER TABLE actor ADD COLUMN IF NOT EXISTS autosubscribe boolean default false; \ No newline at end of file diff --git a/follow.go b/follow.go index 7c56136..b0f0515 100644 --- a/follow.go +++ b/follow.go @@ -217,3 +217,49 @@ func SetActorFollowingDB(db *sql.DB, activity Activity) Activity { activity.Type = "Accept" return activity } + +func AutoFollow(db *sql.DB, actor string) { + following := GetActorFollowingDB(db, actor) + follower := GetActorFollowDB(db, actor) + + isFollowing := false + + for _, e := range follower { + for _, k := range following { + if e.Id == k.Id { + isFollowing = true + } + } + + if !isFollowing && e.Id != Domain { + followActivity := MakeFollowActivity(db, actor, e.Id) + + if FingerActor(e.Id).Id != "" { + MakeActivityRequestOutbox(db, followActivity) + } + } + } +} + +func MakeFollowActivity(db *sql.DB, actor string, follow string) Activity { + var followActivity Activity + + followActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" + followActivity.Type = "Follow" + + var obj ObjectBase + var nactor Actor + if actor == Domain { + nactor = GetActorFromDB(db, actor) + } else { + nactor = FingerActor(actor) + } + + followActivity.Actor = &nactor + followActivity.Object = &obj + + followActivity.Object.Actor = follow + followActivity.To = append(followActivity.To, follow) + + return followActivity +} diff --git a/main.go b/main.go index c3e436f..d36fec5 100644 --- a/main.go +++ b/main.go @@ -447,7 +447,6 @@ func main() { http.HandleFunc("/" + *Key + "/", func(w http.ResponseWriter, r *http.Request) { id, _ := GetPasswordFromSession(r) - actor := GetActorFromPath(db, r.URL.Path, "/" + *Key + "/") if actor.Id == "" { @@ -475,26 +474,7 @@ func main() { if follow || adminFollow { r.ParseForm() - - var followActivity Activity - - followActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" - followActivity.Type = "Follow" - - - var obj ObjectBase - var nactor Actor - if r.FormValue("actor") == Domain { - nactor = GetActorFromDB(db, r.FormValue("actor")) - } else { - nactor = FingerActor(r.FormValue("actor")) - } - - followActivity.Actor = &nactor - followActivity.Object = &obj - - followActivity.Object.Actor = r.FormValue("follow") - followActivity.To = append(followActivity.To, r.FormValue("follow")) + followActivity := MakeFollowActivity(db, r.FormValue("actor"), r.FormValue("follow")) if followActivity.Actor.Id == Domain && !IsActorLocal(db, followActivity.Object.Actor) { w.Write([]byte("main board can only follow local boards. Create a new board and then follow outside boards from it.")) @@ -566,6 +546,7 @@ func main() { adminData.Board.Post.Actor = actor.Id + adminData.AutoSubscribe = GetActorAutoSubscribeDB(db, actor.Id); t.ExecuteTemplate(w, "layout", adminData) @@ -609,15 +590,10 @@ func main() { http.HandleFunc("/" + *Key + "/addboard", func(w http.ResponseWriter, r *http.Request) { - id, _ := GetPasswordFromSession(r) - actor := GetActorFromDB(db, Domain) - - if id == "" || (id != actor.Id && id != Domain) { - t := template.Must(template.ParseFiles("./static/verify.html")) - t.Execute(w, "") - return + if !HasValidation(w, r, actor) { + return } var newActorActivity Activity @@ -654,14 +630,9 @@ func main() { http.HandleFunc("/" + *Key + "/postnews", func(w http.ResponseWriter, r *http.Request) { - id, _ := GetPasswordFromSession(r) - actor := GetActorFromDB(db, Domain) - - if id == "" || (id != actor.Id && id != Domain) { - t := template.Must(template.ParseFiles("./static/verify.html")) - t.Execute(w, "") + if !HasValidation(w, r, actor) { return } @@ -677,14 +648,9 @@ func main() { http.HandleFunc("/" + *Key + "/newsdelete/", func(w http.ResponseWriter, r *http.Request){ - id, _ := GetPasswordFromSession(r) - actor := GetActorFromDB(db, Domain) - - if id == "" || (id != actor.Id && id != Domain) { - t := template.Must(template.ParseFiles("./static/verify.html")) - t.Execute(w, "") + if !HasValidation(w, r, actor) { return } @@ -1219,12 +1185,9 @@ func main() { http.HandleFunc("/blacklist", func(w http.ResponseWriter, r *http.Request) { - id, _ := GetPasswordFromSession(r) - actor := GetActorFromDB(db, Domain) - if id == "" || (id != actor.Id && id != Domain) { - http.Redirect(w, r, "/", http.StatusSeeOther) + if !HasValidation(w, r, actor) { return } @@ -1263,7 +1226,26 @@ func main() { if r.URL.Query().Get("hash") != "" { RouteImages(w, r.URL.Query().Get("hash")) } - }) + }) + + http.HandleFunc("/autosubscribe", func(w http.ResponseWriter, r *http.Request) { + + if !HasValidation(w, r, GetActorFromDB(db, Domain)) { + return + } + + board := r.URL.Query().Get("board") + actor := GetActorByNameFromDB(db, board) + + SetActorAutoSubscribeDB(db, actor.Id) + autoSub := GetActorAutoSubscribeDB(db, actor.Id) + + if autoSub { + AutoFollow(db, actor.Id) + } + + http.Redirect(w, r, "/" + *Key + "/" + board, http.StatusSeeOther) + }) fmt.Println("Server for " + Domain + " running on port " + Port) @@ -2680,3 +2662,14 @@ func IsPostBlacklist(db *sql.DB, comment string) bool { return false } + +func HasValidation(w http.ResponseWriter, r *http.Request, actor Actor) bool { + id, _ := GetPasswordFromSession(r) + + if id == "" || (id != actor.Id && id != Domain) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return false + } + + return true +} diff --git a/outboxPost.go b/outboxPost.go index 45093b8..88c85e1 100644 --- a/outboxPost.go +++ b/outboxPost.go @@ -555,6 +555,24 @@ func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { response := AcceptFollow(activity) response = SetActorFollowerDB(db, response) MakeActivityRequest(db, response) + + alreadyFollow := false + autoSub := GetActorAutoSubscribeDB(db, response.Actor.Id) + following := GetActorFollowingDB(db, response.Actor.Id) + + for _, e := range following { + if e.Id == activity.Actor.Id { + alreadyFollow = true + } + } + + if autoSub && !alreadyFollow { + followActivity := MakeFollowActivity(db, response.Actor.Id, response.Object.Actor) + + if FingerActor(response.Object.Actor).Id != "" { + MakeActivityRequestOutbox(db, followActivity) + } + } } else { fmt.Println("follow request for rejected") response := RejectActivity(activity) diff --git a/static/manage.html b/static/manage.html index e161f18..86b35d4 100644 --- a/static/manage.html +++ b/static/manage.html @@ -20,10 +20,10 @@ {{ $board := .Board }} {{ $key := .Key }} {{ if .IsLocal }} -