Skip to content

Commit

Permalink
Show posts in reply to webmention in interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Nov 21, 2024
1 parent 1fc817c commit e5e0de2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
14 changes: 11 additions & 3 deletions postsDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ type postsRequestConfig struct {
publishedBefore time.Time
randomOrder bool
priorityOrder bool
ascendingOrder bool
fetchWithoutParams bool // fetch posts without parameters
fetchParams []string // only fetch these parameters
withoutRenderedTitle bool // fetch posts without rendered title
Expand Down Expand Up @@ -535,10 +536,17 @@ func buildPostsQuery(c *postsRequestConfig, selection string) (query string, arg
queryBuilder.WriteString(" order by ")
if c.randomOrder {
queryBuilder.WriteString("random()")
} else if c.priorityOrder {
queryBuilder.WriteString("priority desc, published desc")
} else {
queryBuilder.WriteString("published desc")
if c.priorityOrder {
queryBuilder.WriteString("priority desc, published")
} else {
queryBuilder.WriteString("published")
}
if c.ascendingOrder {
queryBuilder.WriteString(" asc")
} else {
queryBuilder.WriteString(" desc")
}
}
// Limit & Offset
if c.limit != 0 || c.offset != 0 {
Expand Down
25 changes: 24 additions & 1 deletion uiComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,37 @@ func (a *goBlog) renderInteractions(hb *htmlbuilder.HtmlBuilder, rd *renderData)
hb.WriteEscaped(mention.Content)
hb.WriteElementClose("i")
}
if len(mention.Replies) > 0 {
hb.WriteElementOpen("ul")
for _, reply := range mention.Replies {
hb.WriteElementOpen("li")
hb.WriteElementOpen("a", "href", reply.Path, "target", "_blank")
hb.WriteEscaped(cmp.Or(a.cfg.User.Name, a.getFullAddress(reply.Path)))
hb.WriteElementClose("a")
if reply.RenderedTitle != "" {
hb.WriteUnescaped(" ")
hb.WriteElementOpen("strong")
hb.WriteEscaped(reply.RenderedTitle)
hb.WriteElementClose("strong")
}
if summary := a.postSummary(reply); summary != "" {
hb.WriteUnescaped(" ")
hb.WriteElementOpen("i")
hb.WriteEscaped(summary)
hb.WriteElementClose("i")
}
hb.WriteElementClose("li")
}
hb.WriteElementClose("ul")
}
if len(mention.Submentions) > 0 {
renderMentions(mention.Submentions)
}
hb.WriteElementClose("li")
}
hb.WriteElementClose("ul")
}
renderMentions(a.db.getWebmentionsByAddress(rd.Canonical))
renderMentions(a.getWebmentionsByAddress(rd.Canonical))
// Show form to send a webmention
hb.WriteElementOpen("form", "class", "fw p", "method", "post", "action", "/webmention")
hb.WriteElementOpen("label", "for", "wm-source", "class", "p")
Expand Down
27 changes: 21 additions & 6 deletions webmention.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type mention struct {
Author string
Status webmentionStatus
Submentions []*mention
Replies []*post
}

func (a *goBlog) initWebmention() {
Expand Down Expand Up @@ -208,7 +209,7 @@ func (db *database) approveWebmentionId(id int) error {
}

func (a *goBlog) reverifyWebmentionId(id int) error {
m, err := a.db.getWebmentions(&webmentionsRequestConfig{
m, err := a.getWebmentions(&webmentionsRequestConfig{
id: id,
limit: 1,
})
Expand All @@ -229,6 +230,7 @@ type webmentionsRequestConfig struct {
asc bool
offset, limit int
submentions bool
replies bool
}

func buildWebmentionsQuery(config *webmentionsRequestConfig) (query string, args []any) {
Expand Down Expand Up @@ -267,10 +269,10 @@ func buildWebmentionsQuery(config *webmentionsRequestConfig) (query string, args
return queryBuilder.String(), args
}

func (db *database) getWebmentions(config *webmentionsRequestConfig) ([]*mention, error) {
func (a *goBlog) getWebmentions(config *webmentionsRequestConfig) ([]*mention, error) {
mentions := []*mention{}
query, args := buildWebmentionsQuery(config)
rows, err := db.Query(query, args...)
rows, err := a.db.Query(query, args...)
if err != nil {
return nil, err
}
Expand All @@ -285,7 +287,7 @@ func (db *database) getWebmentions(config *webmentionsRequestConfig) ([]*mention
m.Url = m.Source
}
if config.submentions {
m.Submentions, err = db.getWebmentions(&webmentionsRequestConfig{
m.Submentions, err = a.getWebmentions(&webmentionsRequestConfig{
target: m.Source,
submentions: false, // prevent infinite recursion
asc: config.asc,
Expand All @@ -295,20 +297,33 @@ func (db *database) getWebmentions(config *webmentionsRequestConfig) ([]*mention
return nil, err
}
}
if config.replies {
m.Replies, err = a.getPosts(&postsRequestConfig{
parameter: a.cfg.Micropub.ReplyParam,
parameterValue: m.Source,
visibility: []postVisibility{visibilityPublic, visibilityUnlisted},
fetchParams: []string{"title", "summary"},
ascendingOrder: true,
})
if err != nil {
return nil, err
}
}
mentions = append(mentions, m)
}
return mentions, nil
}

func (db *database) getWebmentionsByAddress(address string) []*mention {
func (a *goBlog) getWebmentionsByAddress(address string) []*mention {
if address == "" {
return nil
}
mentions, _ := db.getWebmentions(&webmentionsRequestConfig{
mentions, _ := a.getWebmentions(&webmentionsRequestConfig{
target: address,
status: webmentionStatusApproved,
asc: true,
submentions: true,
replies: true,
})
return mentions
}
Expand Down
8 changes: 4 additions & 4 deletions webmentionAdmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ type webmentionPaginationAdapter struct {
config *webmentionsRequestConfig
nums int64
getNums sync.Once
db *database
a *goBlog
}

var _ paginator.Adapter = (*webmentionPaginationAdapter)(nil)

func (p *webmentionPaginationAdapter) Nums() (int64, error) {
p.getNums.Do(func() {
p.nums = int64(noError(p.db.countWebmentions(p.config)))
p.nums = int64(noError(p.a.db.countWebmentions(p.config)))
})
return p.nums, nil
}
Expand All @@ -33,7 +33,7 @@ func (p *webmentionPaginationAdapter) Slice(offset, length int, data any) error
modifiedConfig.offset = offset
modifiedConfig.limit = length

wms, err := p.db.getWebmentions(&modifiedConfig)
wms, err := p.a.getWebmentions(&modifiedConfig)
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&wms).Elem())
return err
}
Expand All @@ -50,7 +50,7 @@ func (a *goBlog) webmentionAdmin(w http.ResponseWriter, r *http.Request) {
p := paginator.New(&webmentionPaginationAdapter{config: &webmentionsRequestConfig{
status: status,
sourcelike: sourcelike,
}, db: a.db}, 5)
}, a: a}, 5)
p.SetPage(stringToInt(chi.URLParam(r, "page")))
var mentions []*mention
err := p.Results(&mentions)
Expand Down
14 changes: 7 additions & 7 deletions webmention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_webmentions(t *testing.T) {
Author: "Test-Author",
}, webmentionStatusVerified)

mentions, err := app.db.getWebmentions(&webmentionsRequestConfig{
mentions, err := app.getWebmentions(&webmentionsRequestConfig{
sourcelike: "example.xyz",
})
require.NoError(t, err)
Expand All @@ -45,24 +45,24 @@ func Test_webmentions(t *testing.T) {
exists := app.db.webmentionExists(&mention{Source: "Https://Example.net/test", Target: "Https://Example.com/TÄst"})
assert.True(t, exists)

mentions = app.db.getWebmentionsByAddress("https://example.com/täst")
mentions = app.getWebmentionsByAddress("https://example.com/täst")
assert.Len(t, mentions, 0)

mentions = app.db.getWebmentionsByAddress("")
mentions = app.getWebmentionsByAddress("")
assert.Len(t, mentions, 0)

mentions, err = app.db.getWebmentions(&webmentionsRequestConfig{
mentions, err = app.getWebmentions(&webmentionsRequestConfig{
sourcelike: "example.net",
})
require.NoError(t, err)
if assert.Len(t, mentions, 1) {
_ = app.db.approveWebmentionId(mentions[0].ID)
}

mentions = app.db.getWebmentionsByAddress("https://example.com/täst")
mentions = app.getWebmentionsByAddress("https://example.com/täst")
assert.Len(t, mentions, 1)

mentions = app.db.getWebmentionsByAddress("https://example.com/t%C3%A4st")
mentions = app.getWebmentionsByAddress("https://example.com/t%C3%A4st")
assert.Len(t, mentions, 1)

err = app.db.deleteWebmention(&mention{
Expand All @@ -71,7 +71,7 @@ func Test_webmentions(t *testing.T) {
})
assert.NoError(t, err)

mentions = app.db.getWebmentionsByAddress("https://example.com/täst")
mentions = app.getWebmentionsByAddress("https://example.com/täst")
assert.Len(t, mentions, 0)

}

0 comments on commit e5e0de2

Please sign in to comment.