diff options
-rw-r--r-- | follow.go | 95 | ||||
-rw-r--r-- | main.go | 4 | ||||
-rw-r--r-- | outboxPost.go | 17 | ||||
-rw-r--r-- | static/manage.html | 2 |
4 files changed, 86 insertions, 32 deletions
@@ -6,7 +6,6 @@ import ( "net/http" _ "github.com/lib/pq" - ) func GetActorFollowing(w http.ResponseWriter, db *sql.DB, id string) { @@ -180,69 +179,111 @@ func IsAlreadyFollower(db *sql.DB, actor string, follow string) bool { return false; } -func SetActorFollowerDB(db *sql.DB, activity Activity) Activity { +func SetActorFollowerDB(db *sql.DB, activity Activity) Activity { var query string alreadyFollow := IsAlreadyFollower(db, activity.Actor.Id, activity.Object.Actor) + activity.Type = "Reject" + if activity.Actor.Id == activity.Object.Actor { + return activity + } + if alreadyFollow { query = `delete from follower where id=$1 and follower=$2` activity.Summary = activity.Object.Actor + " Unfollow " + activity.Actor.Id _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor) - if CheckError(err, "error with follower db insert/delete") != nil { + if CheckError(err, "error with follower db delete") != nil { activity.Type = "Reject" return activity - } + } + + activity.Type = "Accept" + return activity } else { query = `insert into follower (id, follower) values ($1, $2)` - activity.Summary = activity.Object.Actor + " Follow " + activity.Actor.Id - } + activity.Summary = activity.Object.Actor + " Follow " + activity.Actor.Id - _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor) + _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor) + + if CheckError(err, "error with follower db insert") != nil { + activity.Type = "Reject" + return activity + } + + activity.Type = "Accept" + return activity + } - 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 + alreadyFollowing := false + alreadyFollower := false following := GetActorFollowingDB(db, activity.Object.Actor) + actor := FingerActor(activity.Actor.Id) + + remoteActorFollowerCol := GetCollectionFromReq(actor.Followers) + for _, e := range following { if e.Id == activity.Actor.Id { - alreadyFollow = true + alreadyFollowing = true } } + + for _, e := range remoteActorFollowerCol.Items { + if e.Id == activity.Object.Actor { + alreadyFollower = true + } + } + + activity.Type = "Reject" + + if activity.Actor.Id == activity.Object.Actor { + return activity + } - if alreadyFollow { + if alreadyFollowing && alreadyFollower { query = `delete from following where id=$1 and following=$2` activity.Summary = activity.Object.Actor + " Unfollowing " + activity.Actor.Id if !IsActorLocal(db, activity.Actor.Id) { go DeleteActorCache(db, activity.Actor.Id) - } - } else { + } + _, err := db.Exec(query, activity.Object.Actor, activity.Actor.Id) + + if CheckError(err, "error with following db delete") != nil { + activity.Type = "Reject" + return activity + } + + activity.Type = "Accept" + return activity + } + + if !alreadyFollowing && !alreadyFollower { + query = `insert into following (id, following) values ($1, $2)` 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, 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" + if CheckError(err, "error with following db insert") != nil { + activity.Type = "Reject" + return activity + } + + activity.Type = "Accept" return activity - } + } + - activity.Type = "Accept" return activity } @@ -260,9 +301,11 @@ func AutoFollow(db *sql.DB, actor string) { } if !isFollowing && e.Id != Domain && e.Id != actor { - followActivity := MakeFollowActivity(db, actor, e.Id) + followActivity := MakeFollowActivity(db, actor, e.Id) + + nActor := FingerActor(e.Id) - if FingerActor(e.Id).Id != "" { + if nActor.Id != "" { MakeActivityRequestOutbox(db, followActivity) } } @@ -2175,7 +2175,9 @@ func MakeActivityRequest(db *sql.DB, activity Activity) { _, err = RouteProxy(req) - CheckError(err, "error with sending activity resp to") + if err != nil { + fmt.Println("error with sending activity resp to actor " + instance) + } } } } diff --git a/outboxPost.go b/outboxPost.go index 1d658ea..88b9927 100644 --- a/outboxPost.go +++ b/outboxPost.go @@ -101,7 +101,7 @@ func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { var rActivity Activity if validActor && validLocalActor { rActivity = AcceptFollow(activity) - SetActorFollowingDB(db, rActivity) + rActivity = SetActorFollowingDB(db, rActivity) MakeActivityRequest(db, activity) } @@ -565,16 +565,26 @@ func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { MakeActivityRequest(db, response) alreadyFollow := false + alreadyFollowing := false autoSub := GetActorAutoSubscribeDB(db, response.Actor.Id) following := GetActorFollowingDB(db, response.Actor.Id) for _, e := range following { - if e.Id == activity.Actor.Id { + if e.Id == response.Object.Id { alreadyFollow = true } } - if autoSub && !alreadyFollow { + actor := FingerActor(response.Object.Actor) + remoteActorFollowingCol := GetCollectionFromReq(actor.Following) + + for _, e := range remoteActorFollowingCol.Items { + if e.Id == response.Actor.Id { + alreadyFollowing = true + } + } + + if autoSub && !alreadyFollow && alreadyFollowing { followActivity := MakeFollowActivity(db, response.Actor.Id, response.Object.Actor) if FingerActor(response.Object.Actor).Id != "" { @@ -597,7 +607,6 @@ func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { } break } - } func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity Activity) bool { diff --git a/static/manage.html b/static/manage.html index 2647532..3bd621b 100644 --- a/static/manage.html +++ b/static/manage.html @@ -22,7 +22,7 @@ {{ if .IsLocal }} <div id="following" class="popup-box" style="margin-bottom: 25px; margin-top: 5px; padding: 12px;"> <h4 style="margin: 0; margin-bottom: 5px;">Following</h4> - <a href="/autosubscribe?board={{ .Board.Name }}">{{ if .AutoSubscribe }}[Auto Follow On]{{ else }}[Auto Follow Off]{{ end }}</a> + {{ if .AutoSubscribe }}<a title="Auto Follow is On" href="/autosubscribe?board={{ .Board.Name }}">[Toggle Auto Follow Off]{{ else }}<a title="Auto Follow is Off" href="/autosubscribe?board={{ .Board.Name }}">[Toggle Auto Follow On]{{ 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: 5px;" size="35" placeholder="https://fchan.xyz/g"></input> <input type="submit" value="Follow"><br> |