From 6c096837d64247f312f873c9604f0d9ceaca952e Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Mon, 8 Nov 2021 23:12:52 +0100 Subject: [PATCH] Fix some annoyances with the webmention admin --- http.go | 2 +- httpRouters.go | 4 +- templates/webmentionadmin.gohtml | 9 ++++- webmentionAdmin.go | 69 ++++++++++++++------------------ 4 files changed, 40 insertions(+), 44 deletions(-) diff --git a/http.go b/http.go index 0c87f1a..d375676 100644 --- a/http.go +++ b/http.go @@ -110,7 +110,7 @@ func (*goBlog) redirectToHttps(w http.ResponseWriter, r *http.Request) { const ( paginationPath = "/page/{page:[0-9-]+}" - feedPath = ".{feed:rss|json|atom}" + feedPath = ".{feed:(rss|json|atom)}" ) func (a *goBlog) buildRouter() (http.Handler, error) { diff --git a/httpRouters.go b/httpRouters.go index 221380f..25af67f 100644 --- a/httpRouters.go +++ b/httpRouters.go @@ -63,9 +63,7 @@ func (a *goBlog) webmentionsRouter(r chi.Router) { r.Use(a.authMiddleware) r.Get("/", a.webmentionAdmin) r.Get(paginationPath, a.webmentionAdmin) - r.Post("/delete", a.webmentionAdminDelete) - r.Post("/approve", a.webmentionAdminApprove) - r.Post("/reverify", a.webmentionAdminReverify) + r.Post("/{action:(delete|approve|reverify)}", a.webmentionAdminAction) }) } diff --git a/templates/webmentionadmin.gohtml b/templates/webmentionadmin.gohtml index e4895c2..eaadcd5 100644 --- a/templates/webmentionadmin.gohtml +++ b/templates/webmentionadmin.gohtml @@ -6,15 +6,20 @@

{{ string .Blog.Lang "webmentions" }}

{{ $blog := .Blog }} + {{ $current := .Data.Current }} {{ range $i, $mention := .Data.Mentions }} -
+

From: {{ $mention.Source }}
To: {{ $mention.Target }}
- Created: {{ unixtodate $mention.Created }} + Created: {{ unixtodate $mention.Created }}

+ {{ if $mention.Author }}{{ $mention.Author }}
{{ end }} + {{ with $mention.Title }}{{.}}
{{ end }} + {{ with $mention.Content }}{{.}}{{ end }}

+ {{ if eq $mention.Status "verified" }} {{ end }} diff --git a/webmentionAdmin.go b/webmentionAdmin.go index ac923c1..58e2362 100644 --- a/webmentionAdmin.go +++ b/webmentionAdmin.go @@ -17,6 +17,8 @@ type webmentionPaginationAdapter struct { db *database } +var _ paginator.Adapter = &webmentionPaginationAdapter{} + func (p *webmentionPaginationAdapter) Nums() (int64, error) { if p.nums == 0 { nums, _ := p.db.countWebmentions(p.config) @@ -36,8 +38,7 @@ func (p *webmentionPaginationAdapter) Slice(offset, length int, data interface{} } func (a *goBlog) webmentionAdmin(w http.ResponseWriter, r *http.Request) { - pageNoString := chi.URLParam(r, "page") - pageNo, _ := strconv.Atoi(pageNoString) + pageNo, _ := strconv.Atoi(chi.URLParam(r, "page")) var status webmentionStatus = "" switch webmentionStatus(r.URL.Query().Get("status")) { case webmentionStatusVerified: @@ -49,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}, 10) + }, db: a.db}, 5) p.SetPage(pageNo) var mentions []*mention err := p.Results(&mentions) @@ -59,8 +60,8 @@ func (a *goBlog) webmentionAdmin(w http.ResponseWriter, r *http.Request) { } // Navigation var hasPrev, hasNext bool - var prevPage, nextPage int - var prevPath, nextPath string + var prevPage, currentPage, nextPage int + var prevPath, currentPath, nextPath string hasPrev, _ = p.HasPrev() if hasPrev { prevPage, _ = p.PrevPage() @@ -72,6 +73,8 @@ func (a *goBlog) webmentionAdmin(w http.ResponseWriter, r *http.Request) { } else { prevPath = fmt.Sprintf("%s/page/%d", webmentionPath, prevPage) } + currentPage, _ = p.Page() + currentPath = fmt.Sprintf("%s/page/%d", webmentionPath, currentPage) hasNext, _ = p.HasNext() if hasNext { nextPage, _ = p.NextPage() @@ -98,51 +101,41 @@ func (a *goBlog) webmentionAdmin(w http.ResponseWriter, r *http.Request) { "HasPrev": hasPrev, "HasNext": hasNext, "Prev": prevPath + query, + "Current": currentPath + query, "Next": nextPath + query, }, }) } -func (a *goBlog) webmentionAdminDelete(w http.ResponseWriter, r *http.Request) { +func (a *goBlog) webmentionAdminAction(w http.ResponseWriter, r *http.Request) { + action := chi.URLParam(r, "action") + if action != "delete" && action != "approve" && action != "reverify" { + a.serveError(w, r, "Invalid action", http.StatusBadRequest) + return + } id, err := strconv.Atoi(r.FormValue("mentionid")) if err != nil { a.serveError(w, r, err.Error(), http.StatusBadRequest) return } - err = a.db.deleteWebmention(id) + switch action { + case "delete": + err = a.db.deleteWebmention(id) + case "approve": + err = a.db.approveWebmention(id) + case "reverify": + err = a.reverifyWebmention(id) + } if err != nil { a.serveError(w, r, err.Error(), http.StatusInternalServerError) return } - a.cache.purge() - http.Redirect(w, r, ".", http.StatusFound) -} - -func (a *goBlog) webmentionAdminApprove(w http.ResponseWriter, r *http.Request) { - id, err := strconv.Atoi(r.FormValue("mentionid")) - if err != nil { - a.serveError(w, r, err.Error(), http.StatusBadRequest) - return - } - err = a.db.approveWebmention(id) - if err != nil { - a.serveError(w, r, err.Error(), http.StatusInternalServerError) - return - } - a.cache.purge() - http.Redirect(w, r, ".", http.StatusFound) -} - -func (a *goBlog) webmentionAdminReverify(w http.ResponseWriter, r *http.Request) { - id, err := strconv.Atoi(r.FormValue("mentionid")) - if err != nil { - a.serveError(w, r, err.Error(), http.StatusBadRequest) - return - } - err = a.reverifyWebmention(id) - if err != nil { - a.serveError(w, r, err.Error(), http.StatusInternalServerError) - return - } - http.Redirect(w, r, ".", http.StatusFound) + if action == "delete" || action == "approve" { + a.cache.purge() + } + redirectTo := r.FormValue("redir") + if redirectTo == "" { + redirectTo = "." + } + http.Redirect(w, r, redirectTo, http.StatusFound) }