From 0dd256579a89e6ed8eb14584925b13dae6854961 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Mon, 24 May 2021 10:09:37 +0200 Subject: [PATCH] Add option to query webmentions with source (using like) --- webmention.go | 23 ++++++++++++++--------- webmentionAdmin.go | 9 ++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/webmention.go b/webmention.go index 494ea22..f5d0a19 100644 --- a/webmention.go +++ b/webmention.go @@ -129,6 +129,7 @@ func reverifyWebmention(id int) error { type webmentionsRequestConfig struct { target string status webmentionStatus + sourcelike string id int asc bool offset, limit int @@ -138,17 +139,21 @@ func buildWebmentionsQuery(config *webmentionsRequestConfig) (query string, args args = []interface{}{} filter := "" if config != nil { - if config.target != "" && config.status != "" { - filter = "where target = @target and status = @status" - args = append(args, sql.Named("target", unescapedPath(config.target)), sql.Named("status", config.status)) - } else if config.target != "" { - filter = "where target = @target" + filter = "where 1 = 1" + if config.target != "" { + filter += " and target = @target" args = append(args, sql.Named("target", config.target)) - } else if config.status != "" { - filter = "where status = @status" + } + if config.status != "" { + filter += " and status = @status" args = append(args, sql.Named("status", config.status)) - } else if config.id != 0 { - filter = "where id = @id" + } + if config.sourcelike != "" { + filter += " and source like @sourcelike" + args = append(args, sql.Named("sourcelike", "%"+config.sourcelike+"%")) + } + if config.id != 0 { + filter += " and id = @id" args = append(args, sql.Named("id", config.id)) } } diff --git a/webmentionAdmin.go b/webmentionAdmin.go index 6302e38..7eaa935 100644 --- a/webmentionAdmin.go +++ b/webmentionAdmin.go @@ -44,7 +44,11 @@ func webmentionAdmin(w http.ResponseWriter, r *http.Request) { case webmentionStatusApproved: status = webmentionStatusApproved } - p := paginator.New(&webmentionPaginationAdapter{config: &webmentionsRequestConfig{status: status}}, 10) + sourcelike := r.URL.Query().Get("source") + p := paginator.New(&webmentionPaginationAdapter{config: &webmentionsRequestConfig{ + status: status, + sourcelike: sourcelike, + }}, 10) p.SetPage(pageNo) var mentions []*mention err := p.Results(&mentions) @@ -80,6 +84,9 @@ func webmentionAdmin(w http.ResponseWriter, r *http.Request) { if status != "" { params.Add("status", string(status)) } + if sourcelike != "" { + params.Add("source", sourcelike) + } if len(params) > 0 { query = "?" + params.Encode() }