diff --git a/cmd/owl/web/handler.go b/cmd/owl/web/handler.go index a54a1c6..253b6fe 100644 --- a/cmd/owl/web/handler.go +++ b/cmd/owl/web/handler.go @@ -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")) diff --git a/cmd/owl/web/webmention_test.go b/cmd/owl/web/webmention_test.go index 96324d4..d85c2fb 100644 --- a/cmd/owl/web/webmention_test.go +++ b/cmd/owl/web/webmention_test.go @@ -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) +}