Some refactoring and tests

This commit is contained in:
Jan-Lukas Else 2023-12-26 11:39:19 +01:00
parent 56d3e22afa
commit 313faf550c
4 changed files with 61 additions and 25 deletions

2
go.mod
View File

@ -121,7 +121,7 @@ require (
github.com/valyala/fastjson v1.6.4 // indirect
go.mau.fi/util v0.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.15.0 // indirect

4
go.sum
View File

@ -308,8 +308,8 @@ golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=

View File

@ -313,40 +313,32 @@ func (p *post) getChannel() string {
return p.Blog + "/" + p.Section
}
func (a *goBlog) addReplyTitleAndContext(p *post) {
if replyLink := p.firstParameter(a.cfg.Micropub.ReplyParam); replyLink != "" {
addTitle := p.firstParameter(a.cfg.Micropub.ReplyTitleParam) == "" && a.getBlogFromPost(p).addReplyTitle
addContext := p.firstParameter(a.cfg.Micropub.ReplyContextParam) == "" && a.getBlogFromPost(p).addReplyContext
func (a *goBlog) addTitleAndContext(p *post, linkParam, titleParam, contextParam string, addTitleFlag, addContextFlag bool) {
if link := p.firstParameter(linkParam); link != "" {
addTitle := p.firstParameter(titleParam) == "" && addTitleFlag
addContext := p.firstParameter(contextParam) == "" && addContextFlag
if !addTitle && !addContext {
return
}
if mf, err := a.parseMicroformats(replyLink, true); err == nil {
if mf, err := a.parseMicroformats(link, true); err == nil {
if addTitle && mf.Title != "" {
p.addParameter(a.cfg.Micropub.ReplyTitleParam, mf.Title)
p.addParameter(titleParam, mf.Title)
}
if addContext && mf.Content != "" {
p.addParameter(a.cfg.Micropub.ReplyContextParam, mf.Content)
p.addParameter(contextParam, mf.Content)
}
}
}
}
func (a *goBlog) addReplyTitleAndContext(p *post) {
bc := a.getBlogFromPost(p)
a.addTitleAndContext(p, a.cfg.Micropub.ReplyParam, a.cfg.Micropub.ReplyTitleParam, a.cfg.Micropub.ReplyContextParam, bc.addReplyTitle, bc.addReplyContext)
}
func (a *goBlog) addLikeTitleAndContext(p *post) {
if likeLink := p.firstParameter(a.cfg.Micropub.LikeParam); likeLink != "" {
addTitle := p.firstParameter(a.cfg.Micropub.LikeTitleParam) == "" && a.getBlogFromPost(p).addLikeTitle
addContext := p.firstParameter(a.cfg.Micropub.LikeContextParam) == "" && a.getBlogFromPost(p).addLikeContext
if !addTitle && !addContext {
return
}
if mf, err := a.parseMicroformats(likeLink, true); err == nil {
if addTitle && mf.Title != "" {
p.addParameter(a.cfg.Micropub.LikeTitleParam, mf.Title)
}
if addContext && mf.Content != "" {
p.addParameter(a.cfg.Micropub.LikeContextParam, mf.Content)
}
}
}
bc := a.getBlogFromPost(p)
a.addTitleAndContext(p, a.cfg.Micropub.LikeParam, a.cfg.Micropub.LikeTitleParam, a.cfg.Micropub.LikeContextParam, bc.addLikeTitle, bc.addLikeContext)
}
// Public because of rendering

44
postsFuncs_test.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_AddReplyTitleAndContext(t *testing.T) {
app := createMicropubTestEnv(t)
bc := app.cfg.Blogs[app.cfg.DefaultBlog]
bc.addReplyContext = true
bc.addReplyTitle = true
fhc := newFakeHttpClient()
app.httpClient = fhc.Client
fhc.setFakeResponse(http.StatusOK, `
<!doctypehtml>
<title>Microformats Entry Example</title>
<article class=h-entry>
<h1 class=p-name>My First Microformats Post</h1>
<div class=e-content>
<p>This is the main content of my post. It can contain <a href=#>links</a>, <strong>bold</strong> text, and more.</div>
</article>
`)
err := app.createPost(&post{
Path: "/testpost",
Parameters: map[string][]string{
"replylink": {"https://example.com"},
},
Content: "Test",
})
require.NoError(t, err)
p, err := app.getPost("/testpost")
require.NoError(t, err)
assert.Equal(t, "My First Microformats Post", p.firstParameter("replytitle"))
assert.Equal(t, "This is the main content of my post. It can contain links, bold text, and more.", p.firstParameter("replycontext"))
}