diff options
author | FChannel <> | 2021-07-15 13:49:29 -0700 |
---|---|---|
committer | FChannel <> | 2021-07-15 13:49:29 -0700 |
commit | 22ee823b6e9200317337941c2ad41ca9df9dd855 (patch) | |
tree | 8ff9518bb529ff476bf4d667ea3f72b4cba970a2 | |
parent | 26f59904f0f1d7a908969ad0f5846448b0e7451b (diff) |
added auto follow for boards
-rwxr-xr-x | Server | bin | 0 -> 11195223 bytes | |||
-rw-r--r-- | client.go | 1 | ||||
-rw-r--r-- | database.go | 26 | ||||
-rw-r--r-- | databaseschema.psql | 4 | ||||
-rw-r--r-- | follow.go | 46 | ||||
-rw-r--r-- | main.go | 83 | ||||
-rw-r--r-- | outboxPost.go | 18 | ||||
-rw-r--r-- | static/manage.html | 4 |
8 files changed, 134 insertions, 48 deletions
Binary files differ @@ -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 @@ -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 +} @@ -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 }} - <div id="following" class="popup-box" style="margin-bottom: 25px; margin-top: 5px; padding: 12px;"> <h4 style="margin: 0; margin-bottom: 5px;">Subscribed</h4> - <form id="follow-form" action="/{{ .Key }}/{{ .Board.Name }}/follow" method="post" enctype="application/x-www-form-urlencoded"> + <a href="/autosubscribe?board={{ .Board.Name }}">{{ if .AutoSubscribe }}[Auto Subscribe On]{{ else }}[Auto Subscribe Off]{{ end }}</a> + <form id="follow-form" action="/{{ .Key }}/{{ .Board.Name }}/follow" method="post" enctype="application/x-www-form-urlencoded" style="margin-top: 5px;"> <input id="follow" name="follow" style="margin-bottom: 12px;" placeholder="https://fchan.xyz/g"></input> <input type="submit" value="Subscribe"><br> <input type="hidden" name="actor" value="{{ $board.Actor.Id }}"> |