accept webmention for alias. Resolves #11

This commit is contained in:
Niko Abeler 2022-09-08 20:52:04 +02:00
parent b3cb65bbc1
commit db9dee6232
2 changed files with 58 additions and 9 deletions

View File

@ -1,8 +1,10 @@
package web
import (
"fmt"
"h4kor/owl-blogs"
"net/http"
"net/url"
"os"
"path"
"strings"
@ -103,20 +105,41 @@ func userWebmentionHandler(repo *owl.Repository) func(http.ResponseWriter, *http
return
}
tryAlias := func(target string) *owl.Post {
parsedTarget, _ := url.Parse(target)
aliases, _ := repo.PostAliases()
fmt.Printf("aliases %v", aliases)
fmt.Printf("parsedTarget %v", parsedTarget)
if _, ok := aliases[parsedTarget.Path]; ok {
return aliases[parsedTarget.Path]
}
return nil
}
var aliasPost *owl.Post
parts := strings.Split(target[0], "/")
if len(parts) < 2 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Not found"))
return
aliasPost = tryAlias(target[0])
if aliasPost == nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Not found"))
return
}
}
postId := parts[len(parts)-2]
post, err := user.GetPost(postId)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Post not found"))
return
foundPost, err := user.GetPost(postId)
if err != nil && aliasPost == nil {
aliasPost = tryAlias(target[0])
if aliasPost == nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Post not found"))
return
}
}
err = post.AddIncomingWebmention(source[0])
if aliasPost != nil {
foundPost = *aliasPost
}
err = foundPost.AddIncomingWebmention(source[0])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Unable to process webmention"))

View File

@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"
@ -159,3 +160,28 @@ func TestValidationOfTarget(t *testing.T) {
assertStatus(t, rr, http.StatusBadRequest)
}
func TestAcceptWebmentionForAlias(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1")
content := "---\n"
content += "title: Test\n"
content += "aliases: \n"
content += " - /foo/bar\n"
content += " - /foo/baz\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
target := "https://example.com/foo/bar"
source := "https://example.com"
rr, err := setupWebmentionTest(repo, user, target, source)
if err != nil {
t.Fatal(err)
}
assertStatus(t, rr, http.StatusAccepted)
}