jlelse
/
jsonpub
Archived
1
Fork 0

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 {
if _, ok := a.followers[iri]; ok {
delete(a.followers, iri)
fmt.Println("Removed follower: ", iri)
return a.save()
}
fmt.Println(iri, "is not following")
return nil
}

64
http.go
View File

@ -10,6 +10,7 @@ import (
"net/url"
"os"
"regexp"
"strings"
"time"
"willnorris.com/go/webmention"
)
@ -81,14 +82,6 @@ func Serve() {
w.WriteHeader(http.StatusNotFound)
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"] {
case "Follow":
actor.Accept(activity)
@ -106,19 +99,23 @@ func Serve() {
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 {
inReplyTo, hasReplyToString := object["inReplyTo"].(string)
id, hasId := object["id"].(string)
if hasReplyToString && hasId && len(inReplyTo) > 0 && len(id) > 0 {
// It's an ActivityPub reply
fmt.Println("Received reply to:", inReplyTo)
webmentionClient := webmention.New(nil)
endpoint, err := webmentionClient.DiscoverEndpoint(inReplyTo)
if err != nil || len(endpoint) < 1 {
sendWebmention(webmentionClient, actor, id, inReplyTo)
} else if hasId && len(id) > 0 {
// May be a mention
webmentionClient := webmention.New(nil)
dl, err := webmentionClient.DiscoverLinks(id, "")
if err != nil {
return
}
_, err = webmentionClient.SendWebmention(endpoint, id, inReplyTo)
if err != nil {
log.Println("Sending webmention to " + inReplyTo + " failed")
} else {
log.Println("Sent webmention to " + inReplyTo)
// Send Webmentions
for _, link := range dl {
sendWebmention(webmentionClient, actor, id, link)
}
}
}
@ -127,9 +124,11 @@ func Serve() {
{
if object, ok := activity["object"].(string); ok && len(object) > 0 && activity["actor"] == object {
_ = actor.RemoveFollower(object)
fmt.Println("Deleted", object)
}
}
default:
// Log inbox request
logInbox(actor, b)
}
// Return 201
w.WriteHeader(http.StatusCreated)
@ -144,3 +143,30 @@ func Serve() {
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)
}