Archived
1

Improve logging, handle mentions

This commit is contained in:
Jan-Lukas Else 2020-03-10 15:40:04 +01:00
parent bea069bcc1
commit 057a9df07a
2 changed files with 45 additions and 21 deletions

View File

@ -192,10 +192,8 @@ func (a *Actor) NewFollower(iri string, inbox string) error {
func (a *Actor) RemoveFollower(iri string) error { func (a *Actor) RemoveFollower(iri string) error {
if _, ok := a.followers[iri]; ok { if _, ok := a.followers[iri]; ok {
delete(a.followers, iri) delete(a.followers, iri)
fmt.Println("Removed follower: ", iri)
return a.save() return a.save()
} }
fmt.Println(iri, "is not following")
return nil return nil
} }

64
http.go
View File

@ -10,6 +10,7 @@ import (
"net/url" "net/url"
"os" "os"
"regexp" "regexp"
"strings"
"time" "time"
"willnorris.com/go/webmention" "willnorris.com/go/webmention"
) )
@ -81,14 +82,6 @@ func Serve() {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
f, err := os.OpenFile(storage+slash+"actors"+slash+actor.Name+slash+"inbox", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer func() { _ = f.Close() }()
_, _ = f.Write(b)
_, _ = f.WriteString("\n---\n")
switch activity["type"] { switch activity["type"] {
case "Follow": case "Follow":
actor.Accept(activity) actor.Accept(activity)
@ -106,19 +99,23 @@ func Serve() {
case "Create": case "Create":
{ {
if object, ok := activity["object"].(map[string]interface{}); ok { if object, ok := activity["object"].(map[string]interface{}); ok {
inReplyTo, ok := object["inReplyTo"].(string) inReplyTo, hasReplyToString := object["inReplyTo"].(string)
id, ok2 := object["id"].(string) id, hasId := object["id"].(string)
if ok && ok2 && len(inReplyTo) > 0 && len(id) > 0 { if hasReplyToString && hasId && len(inReplyTo) > 0 && len(id) > 0 {
// It's an ActivityPub reply
fmt.Println("Received reply to:", inReplyTo)
webmentionClient := webmention.New(nil) webmentionClient := webmention.New(nil)
endpoint, err := webmentionClient.DiscoverEndpoint(inReplyTo) sendWebmention(webmentionClient, actor, id, inReplyTo)
if err != nil || len(endpoint) < 1 { } else if hasId && len(id) > 0 {
// May be a mention
webmentionClient := webmention.New(nil)
dl, err := webmentionClient.DiscoverLinks(id, "")
if err != nil {
return return
} }
_, err = webmentionClient.SendWebmention(endpoint, id, inReplyTo) // Send Webmentions
if err != nil { for _, link := range dl {
log.Println("Sending webmention to " + inReplyTo + " failed") sendWebmention(webmentionClient, actor, id, link)
} else {
log.Println("Sent webmention to " + inReplyTo)
} }
} }
} }
@ -127,9 +124,11 @@ func Serve() {
{ {
if object, ok := activity["object"].(string); ok && len(object) > 0 && activity["actor"] == object { if object, ok := activity["object"].(string); ok && len(object) > 0 && activity["actor"] == object {
_ = actor.RemoveFollower(object) _ = actor.RemoveFollower(object)
fmt.Println("Deleted", object)
} }
} }
default:
// Log inbox request
logInbox(actor, b)
} }
// Return 201 // Return 201
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
@ -144,3 +143,30 @@ func Serve() {
log.Fatal(http.ListenAndServe(":8081", nil)) log.Fatal(http.ListenAndServe(":8081", nil))
} }
func logInbox(actor *Actor, b []byte) {
f, err := os.OpenFile(storage+slash+"actors"+slash+actor.Name+slash+"inbox", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return
}
defer func() { _ = f.Close() }()
_, _ = f.Write(b)
_, _ = f.WriteString("\n---\n")
}
func sendWebmention(client *webmention.Client, actor *Actor, mentioningLink, mentionedLink string) {
if !strings.Contains(mentionedLink, actor.iri) {
// Not mention of blog
return
}
endpoint, err := client.DiscoverEndpoint(mentionedLink)
if err != nil || len(endpoint) < 1 {
return
}
_, err = client.SendWebmention(endpoint, mentioningLink, mentionedLink)
if err != nil {
log.Println("Sending webmention to " + mentionedLink + " failed")
return
}
log.Println("Sent webmention to " + mentionedLink)
}