Archived
1

Add support for Delete of actor and Unfollow

This commit is contained in:
Jan-Lukas Else 2020-03-01 18:20:05 +01:00
parent dc13138cd8
commit 03df5b5916
2 changed files with 46 additions and 28 deletions

View File

@ -190,16 +190,13 @@ func (a *Actor) NewFollower(iri string, inbox string) error {
}
func (a *Actor) RemoveFollower(iri string) error {
delete(a.followers, iri)
return a.save()
}
// batchSend sends a batch of http posts to a list of recipients
func (a *Actor) batchSend(activity map[string]interface{}, recipients []string) (err error) {
for _, v := range recipients {
_ = a.signedHTTPPost(activity, v)
if _, ok := a.followers[iri]; ok {
delete(a.followers, iri)
fmt.Println("Removed follower: ", iri)
return a.save()
}
return
fmt.Println(iri, "is not following")
return nil
}
// send to followers sends a batch of http posts to each one of the followers
@ -210,7 +207,9 @@ func (a *Actor) sendToFollowers(activity map[string]interface{}) {
recipients[i] = inbox.(string)
i++
}
_ = a.batchSend(activity, recipients)
for _, v := range recipients {
_ = a.signedHTTPPost(activity, v)
}
return
}

55
http.go
View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"log"
@ -65,29 +66,47 @@ func Serve() {
defer func() { _ = f.Close() }()
_, _ = f.Write(b)
_, _ = f.WriteString("\n---\n")
if activity["type"] == "Follow" {
switch activity["type"] {
case "Follow":
actor.Accept(activity)
} else if activity["type"] == "Undo" {
// TODO: Implement unfollow
} else if activity["type"] == "Create" {
object, ok := activity["object"].(map[string]interface{})
if ok {
inReplyTo, ok := object["inReplyTo"].(string)
id, ok2 := object["id"].(string)
if ok && ok2 && len(inReplyTo) > 0 && len(id) > 0 {
webmentionClient := webmention.New(nil)
endpoint, err := webmentionClient.DiscoverEndpoint(inReplyTo)
if err != nil || len(endpoint) < 1 {
return
case "Undo":
{
if object, ok := activity["object"].(map[string]interface{}); ok {
if objectType, ok := object["type"].(string); ok && objectType == "Follow" {
if iri, ok := object["actor"].(string); ok && iri == activity["actor"] {
_ = actor.RemoveFollower(iri)
fmt.Println(iri, "unfollowed")
}
}
_, err = webmentionClient.SendWebmention(endpoint, id, inReplyTo)
if err != nil {
log.Println("Sending webmention to " + inReplyTo + " failed")
} else {
log.Println("Sent webmention to " + inReplyTo)
}
}
case "Create":
{
if object, ok := activity["object"].(map[string]interface{}); ok {
inReplyTo, ok := object["inReplyTo"].(string)
id, ok2 := object["id"].(string)
if ok && ok2 && len(inReplyTo) > 0 && len(id) > 0 {
webmentionClient := webmention.New(nil)
endpoint, err := webmentionClient.DiscoverEndpoint(inReplyTo)
if err != nil || len(endpoint) < 1 {
return
}
_, err = webmentionClient.SendWebmention(endpoint, id, inReplyTo)
if err != nil {
log.Println("Sending webmention to " + inReplyTo + " failed")
} else {
log.Println("Sent webmention to " + inReplyTo)
}
}
}
}
case "Delete":
{
if object, ok := activity["object"].(string); ok && len(object) > 0 && activity["actor"] == object {
_ = actor.RemoveFollower(object)
fmt.Println("Deleted", object)
}
}
}
// Return 201
w.WriteHeader(http.StatusCreated)