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 package web
import ( import (
"fmt"
"h4kor/owl-blogs" "h4kor/owl-blogs"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"strings" "strings"
@ -103,20 +105,41 @@ func userWebmentionHandler(repo *owl.Repository) func(http.ResponseWriter, *http
return 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], "/") parts := strings.Split(target[0], "/")
if len(parts) < 2 { if len(parts) < 2 {
aliasPost = tryAlias(target[0])
if aliasPost == nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Not found")) w.Write([]byte("Not found"))
return return
} }
}
postId := parts[len(parts)-2] postId := parts[len(parts)-2]
post, err := user.GetPost(postId) foundPost, err := user.GetPost(postId)
if err != nil { if err != nil && aliasPost == nil {
aliasPost = tryAlias(target[0])
if aliasPost == nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Post not found")) w.Write([]byte("Post not found"))
return return
} }
err = post.AddIncomingWebmention(source[0]) }
if aliasPost != nil {
foundPost = *aliasPost
}
err = foundPost.AddIncomingWebmention(source[0])
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Unable to process webmention")) w.Write([]byte("Unable to process webmention"))

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -159,3 +160,28 @@ func TestValidationOfTarget(t *testing.T) {
assertStatus(t, rr, http.StatusBadRequest) 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)
}