diff --git a/owl_test.go b/owl_test.go index 68a478f..5d3b5f4 100644 --- a/owl_test.go +++ b/owl_test.go @@ -6,6 +6,18 @@ import ( "time" ) +type MockMicroformatParser struct{} + +func (*MockMicroformatParser) ParseHEntry(data []byte) (owl.ParsedHEntry, error) { + return owl.ParsedHEntry{Title: "Mock Title"}, nil +} + +type MockHttpRetriever struct{} + +func (*MockHttpRetriever) Get(url string) ([]byte, error) { + return []byte(""), nil +} + func randomName() string { rand.Seed(time.Now().UnixNano()) var letters = []rune("abcdefghijklmnopqrstuvwxyz") diff --git a/post.go b/post.go index 4107741..136676f 100644 --- a/post.go +++ b/post.go @@ -150,12 +150,20 @@ func (post *Post) LoadMeta() error { func (post *Post) AddWebmention(source string) error { hash := sha256.Sum256([]byte(source)) hashStr := base64.URLEncoding.EncodeToString(hash[:]) - data := "source: " + source // Check if file already exists fileName := path.Join(post.WebmentionDir(), hashStr+".yml") if fileExists(fileName) { return nil } + data := "source: " + source + "\n" + html, err := post.user.repo.Retriever.Get(source) + if err == nil { + entry, err := post.user.repo.Parser.ParseHEntry(html) + if err == nil { + data += "title: " + entry.Title + "\n" + } + } + return os.WriteFile(fileName, []byte(data), 0644) } diff --git a/post_test.go b/post_test.go index 1d95559..26b5a32 100644 --- a/post_test.go +++ b/post_test.go @@ -169,7 +169,8 @@ func TestLoadMeta(t *testing.T) { func TestAddWebmentionCreatesFile(t *testing.T) { repo := getTestRepo() - repo.SetAllowRawHtml(true) + repo.Retriever = &MockHttpRetriever{} + repo.Parser = &MockMicroformatParser{} user, _ := repo.CreateUser("testuser") post, _ := user.CreateNewPost("testpost") @@ -185,7 +186,8 @@ func TestAddWebmentionCreatesFile(t *testing.T) { func TestAddWebmentionNotOverwritingFile(t *testing.T) { repo := getTestRepo() - repo.SetAllowRawHtml(true) + repo.Retriever = &MockHttpRetriever{} + repo.Parser = &MockMicroformatParser{} user, _ := repo.CreateUser("testuser") post, _ := user.CreateNewPost("testpost") @@ -211,3 +213,26 @@ func TestAddWebmentionNotOverwritingFile(t *testing.T) { t.Errorf("Expected: %v", content) } } + +func TestAddWebmentionAddsParsedTitle(t *testing.T) { + repo := getTestRepo() + repo.Retriever = &MockHttpRetriever{} + repo.Parser = &MockMicroformatParser{} + user, _ := repo.CreateUser("testuser") + post, _ := user.CreateNewPost("testpost") + + post.AddWebmention("https://example.com") + dir, _ := os.Open(post.WebmentionDir()) + defer dir.Close() + files, _ := dir.Readdirnames(-1) + + if len(files) != 1 { + t.Error("No file created for webmention") + } + + fileContent, _ := os.ReadFile(path.Join(post.WebmentionDir(), files[0])) + if !strings.Contains(string(fileContent), "Mock Title") { + t.Error("File not containing the title.") + t.Errorf("Got: %v", string(fileContent)) + } +} diff --git a/release.sh b/release.sh index 54093d0..1242d34 100755 --- a/release.sh +++ b/release.sh @@ -1,2 +1,2 @@ -docker build . -t git.libove.org/h4kor/owl-blogs +docker build . -t git.libove.org/h4kor/owl-blogs:$1 docker push git.libove.org/h4kor/owl-blogs \ No newline at end of file diff --git a/repository.go b/repository.go index 68cb3d7..c3c8c66 100644 --- a/repository.go +++ b/repository.go @@ -20,6 +20,8 @@ type Repository struct { single_user_mode bool active_user string allow_raw_html bool + Retriever HttpRetriever + Parser MicroformatParser } type RepoConfig struct { @@ -27,7 +29,7 @@ type RepoConfig struct { } func CreateRepository(name string) (Repository, error) { - newRepo := Repository{name: name} + newRepo := Repository{name: name, Parser: OwlMicroformatParser{}, Retriever: OwlHttpRetriever{}} // check if repository already exists if dirExists(newRepo.Dir()) { return Repository{}, fmt.Errorf("Repository already exists") @@ -61,7 +63,7 @@ func CreateRepository(name string) (Repository, error) { func OpenRepository(name string) (Repository, error) { - repo := Repository{name: name} + repo := Repository{name: name, Parser: OwlMicroformatParser{}, Retriever: OwlHttpRetriever{}} if !dirExists(repo.Dir()) { return Repository{}, fmt.Errorf("Repository does not exist: " + repo.Dir()) } diff --git a/repository_test.go b/repository_test.go index 4596efb..590a082 100644 --- a/repository_test.go +++ b/repository_test.go @@ -27,7 +27,7 @@ func TestCannotCreateExistingRepository(t *testing.T) { func TestCanCreateANewUser(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) if _, err := os.Stat(path.Join(user.Dir(), "")); err != nil { t.Error("User directory not created") @@ -36,7 +36,7 @@ func TestCanCreateANewUser(t *testing.T) { func TestCannotRecreateExisitingUser(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() userName := randomUserName() repo.CreateUser(userName) _, err := repo.CreateUser(userName) @@ -47,7 +47,7 @@ func TestCannotRecreateExisitingUser(t *testing.T) { func TestCreateUserAddsVersionFile(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) if _, err := os.Stat(path.Join(user.Dir(), "/meta/VERSION")); err != nil { t.Error("Version file not created") @@ -56,7 +56,7 @@ func TestCreateUserAddsVersionFile(t *testing.T) { func TestCreateUserAddsBaseHtmlFile(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) if _, err := os.Stat(path.Join(user.Dir(), "/meta/base.html")); err != nil { t.Error("Base html file not created") @@ -65,7 +65,7 @@ func TestCreateUserAddsBaseHtmlFile(t *testing.T) { func TestCreateUserAddConfigYml(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) if _, err := os.Stat(path.Join(user.Dir(), "/meta/config.yml")); err != nil { t.Error("Config file not created") @@ -74,7 +74,7 @@ func TestCreateUserAddConfigYml(t *testing.T) { func TestCreateUserAddsPublicFolder(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) if _, err := os.Stat(path.Join(user.Dir(), "/public")); err != nil { t.Error("Public folder not created") @@ -83,7 +83,7 @@ func TestCreateUserAddsPublicFolder(t *testing.T) { func TestCanListRepoUsers(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user1, _ := repo.CreateUser(randomUserName()) user2, _ := repo.CreateUser(randomUserName()) // Create a new post @@ -121,7 +121,7 @@ func TestCannotOpenNonExisitingRepo(t *testing.T) { func TestGetUser(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Get the user user2, err := repo.GetUser(user.Name()) @@ -135,7 +135,7 @@ func TestGetUser(t *testing.T) { func TestCannotGetNonexistingUser(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() _, err := repo.GetUser(randomUserName()) if err == nil { t.Error("No error returned when getting non-existing user") @@ -144,7 +144,7 @@ func TestCannotGetNonexistingUser(t *testing.T) { func TestGetStaticDirOfRepo(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() // Get the user staticDir := repo.StaticDir() if staticDir == "" { @@ -154,7 +154,7 @@ func TestGetStaticDirOfRepo(t *testing.T) { func TestNewRepoGetsStaticFiles(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() if _, err := os.Stat(repo.StaticDir()); err != nil { t.Error("Static directory not found") } @@ -169,7 +169,7 @@ func TestNewRepoGetsStaticFiles(t *testing.T) { func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css")) if err != nil { t.Error("Error opening pico.min.css") @@ -183,7 +183,7 @@ func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) { func TestNewRepoGetsBaseHtml(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() if _, err := os.Stat(path.Join(repo.Dir(), "/base.html")); err != nil { t.Error("Base html file not found") } @@ -191,7 +191,7 @@ func TestNewRepoGetsBaseHtml(t *testing.T) { func TestCanGetRepoTemplate(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() // Get the user template, err := repo.Template() if err != nil { @@ -239,7 +239,7 @@ func TestSingleUserRepoUserUrlPathIsSimple(t *testing.T) { } func TestCanGetMapWithAllPostAliases(t *testing.T) { - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) post, _ := user.CreateNewPost("test-1") @@ -276,7 +276,7 @@ func TestCanGetMapWithAllPostAliases(t *testing.T) { } func TestAliasesHaveCorrectPost(t *testing.T) { - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) post1, _ := user.CreateNewPost("test-1") post2, _ := user.CreateNewPost("test-2") diff --git a/user_test.go b/user_test.go index c2f449c..0f8b5d4 100644 --- a/user_test.go +++ b/user_test.go @@ -11,7 +11,7 @@ import ( func TestCreateNewPostCreatesEntryInPublic(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post user.CreateNewPost("testpost") @@ -26,7 +26,7 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) { func TestCreateNewPostCreatesMediaDir(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post post, _ := user.CreateNewPost("testpost") @@ -49,7 +49,7 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) { func TestCreateNewPostMultipleCalls(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post user.CreateNewPost("testpost") @@ -66,7 +66,7 @@ func TestCreateNewPostMultipleCalls(t *testing.T) { func TestCanListUserPosts(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post user.CreateNewPost("testpost") @@ -83,7 +83,7 @@ func TestCanListUserPosts(t *testing.T) { func TestCannotListUserPostsInSubdirectories(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post user.CreateNewPost("testpost") @@ -120,7 +120,7 @@ func TestCannotListUserPostsInSubdirectories(t *testing.T) { func TestCannotListUserPostsWithoutIndexMd(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post user.CreateNewPost("testpost") @@ -149,7 +149,7 @@ func TestCannotListUserPostsWithoutIndexMd(t *testing.T) { func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post post, _ := user.CreateNewPost("testpost") @@ -170,7 +170,7 @@ func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) { func TestListUsersDraftsExcludedRealWorld(t *testing.T) { // Create a new user - repo, _ := owl.CreateRepository(testRepoName()) + repo := getTestRepo() user, _ := repo.CreateUser(randomUserName()) // Create a new post post, _ := user.CreateNewPost("testpost") diff --git a/webmention.go b/webmention.go index 53dee9a..780fa20 100644 --- a/webmention.go +++ b/webmention.go @@ -13,13 +13,19 @@ type HttpRetriever interface { Get(url string) ([]byte, error) } +type MicroformatParser interface { + ParseHEntry(data []byte) (ParsedHEntry, error) +} + type OwlHttpRetriever struct{} +type OwlMicroformatParser struct{} + type ParsedHEntry struct { Title string } -func (ret *OwlHttpRetriever) Get(url string) ([]byte, error) { +func (OwlHttpRetriever) Get(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { return []byte{}, err @@ -39,7 +45,7 @@ func collectText(n *html.Node, buf *bytes.Buffer) { } } -func ParseHEntry(data []byte) (ParsedHEntry, error) { +func (OwlMicroformatParser) ParseHEntry(data []byte) (ParsedHEntry, error) { doc, err := html.Parse(strings.NewReader(string(data))) if err != nil { return ParsedHEntry{}, err diff --git a/webmention_test.go b/webmention_test.go index c9abf6c..e02d9cf 100644 --- a/webmention_test.go +++ b/webmention_test.go @@ -11,8 +11,8 @@ import ( func TestParseValidHEntry(t *testing.T) { html := []byte("
Foo
") - - entry, err := owl.ParseHEntry(html) + parser := &owl.OwlMicroformatParser{} + entry, err := parser.ParseHEntry(html) if err != nil { t.Errorf("Unable to parse feed: %v", err) @@ -24,8 +24,8 @@ func TestParseValidHEntry(t *testing.T) { func TestParseValidHEntryWithoutTitle(t *testing.T) { html := []byte("
Foo
") - - entry, err := owl.ParseHEntry(html) + parser := &owl.OwlMicroformatParser{} + entry, err := parser.ParseHEntry(html) if err != nil { t.Errorf("Unable to parse feed: %v", err)