From e5e0de22bd01422055a6c919403cdb57d894da58 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Thu, 21 Nov 2024 09:54:02 +0100 Subject: [PATCH] Show posts in reply to webmention in interactions --- postsDb.go | 14 +++++++++++--- uiComponents.go | 25 ++++++++++++++++++++++++- webmention.go | 27 +++++++++++++++++++++------ webmentionAdmin.go | 8 ++++---- webmention_test.go | 14 +++++++------- 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/postsDb.go b/postsDb.go index 0fa5345e..ba6b6b91 100644 --- a/postsDb.go +++ b/postsDb.go @@ -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 @@ -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 { diff --git a/uiComponents.go b/uiComponents.go index 77d48c81..7dd96ed9 100644 --- a/uiComponents.go +++ b/uiComponents.go @@ -350,6 +350,29 @@ 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) } @@ -357,7 +380,7 @@ func (a *goBlog) renderInteractions(hb *htmlbuilder.HtmlBuilder, rd *renderData) } 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") diff --git a/webmention.go b/webmention.go index 9a4a7871..89eed242 100644 --- a/webmention.go +++ b/webmention.go @@ -35,6 +35,7 @@ type mention struct { Author string Status webmentionStatus Submentions []*mention + Replies []*post } func (a *goBlog) initWebmention() { @@ -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, }) @@ -229,6 +230,7 @@ type webmentionsRequestConfig struct { asc bool offset, limit int submentions bool + replies bool } func buildWebmentionsQuery(config *webmentionsRequestConfig) (query string, args []any) { @@ -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 } @@ -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, @@ -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 } diff --git a/webmentionAdmin.go b/webmentionAdmin.go index 7e795e22..8a33591b 100644 --- a/webmentionAdmin.go +++ b/webmentionAdmin.go @@ -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 } @@ -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 } @@ -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) diff --git a/webmention_test.go b/webmention_test.go index 71c98b64..b2c58476 100644 --- a/webmention_test.go +++ b/webmention_test.go @@ -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) @@ -45,13 +45,13 @@ 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) @@ -59,10 +59,10 @@ func Test_webmentions(t *testing.T) { _ = 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{ @@ -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) }