Fix some annoyances with the webmention admin

This commit is contained in:
Jan-Lukas Else 2021-11-08 23:12:52 +01:00
parent 42e218746b
commit 6c096837d6
4 changed files with 40 additions and 44 deletions

View File

@ -110,7 +110,7 @@ func (*goBlog) redirectToHttps(w http.ResponseWriter, r *http.Request) {
const ( const (
paginationPath = "/page/{page:[0-9-]+}" paginationPath = "/page/{page:[0-9-]+}"
feedPath = ".{feed:rss|json|atom}" feedPath = ".{feed:(rss|json|atom)}"
) )
func (a *goBlog) buildRouter() (http.Handler, error) { func (a *goBlog) buildRouter() (http.Handler, error) {

View File

@ -63,9 +63,7 @@ func (a *goBlog) webmentionsRouter(r chi.Router) {
r.Use(a.authMiddleware) r.Use(a.authMiddleware)
r.Get("/", a.webmentionAdmin) r.Get("/", a.webmentionAdmin)
r.Get(paginationPath, a.webmentionAdmin) r.Get(paginationPath, a.webmentionAdmin)
r.Post("/delete", a.webmentionAdminDelete) r.Post("/{action:(delete|approve|reverify)}", a.webmentionAdminAction)
r.Post("/approve", a.webmentionAdminApprove)
r.Post("/reverify", a.webmentionAdminReverify)
}) })
} }

View File

@ -6,15 +6,20 @@
<main> <main>
<h1>{{ string .Blog.Lang "webmentions" }}</h1> <h1>{{ string .Blog.Lang "webmentions" }}</h1>
{{ $blog := .Blog }} {{ $blog := .Blog }}
{{ $current := .Data.Current }}
{{ range $i, $mention := .Data.Mentions }} {{ range $i, $mention := .Data.Mentions }}
<div class="p"> <div id="mention-{{ $mention.ID }}" class="p">
<p> <p>
From: <a href="{{ $mention.Source }}" target="_blank" rel="noopener noreferrer">{{ $mention.Source }}</a><br/> From: <a href="{{ $mention.Source }}" target="_blank" rel="noopener noreferrer">{{ $mention.Source }}</a><br/>
To: <a href="{{ $mention.Target }}" target="_blank">{{ $mention.Target }}</a><br/> To: <a href="{{ $mention.Target }}" target="_blank">{{ $mention.Target }}</a><br/>
Created: {{ unixtodate $mention.Created }} Created: {{ unixtodate $mention.Created }}<br/><br/>
{{ if $mention.Author }}{{ $mention.Author }}<br/>{{ end }}
{{ with $mention.Title }}<b>{{.}}</b><br/>{{ end }}
{{ with $mention.Content }}<i>{{.}}</i>{{ end }}
</p> </p>
<form method="post"> <form method="post">
<input type="hidden" name="mentionid" value="{{ $mention.ID }}"> <input type="hidden" name="mentionid" value="{{ $mention.ID }}">
<input type="hidden" name="redir" value="{{ $current }}#mention-{{ $mention.ID }}">
{{ if eq $mention.Status "verified" }} {{ if eq $mention.Status "verified" }}
<input type="submit" formaction="/webmention/approve" value="{{ string $blog.Lang "approve" }}"> <input type="submit" formaction="/webmention/approve" value="{{ string $blog.Lang "approve" }}">
{{ end }} {{ end }}

View File

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