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 (
paginationPath = "/page/{page:[0-9-]+}"
feedPath = ".{feed:rss|json|atom}"
feedPath = ".{feed:(rss|json|atom)}"
)
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.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)
})
}

View File

@ -6,15 +6,20 @@
<main>
<h1>{{ string .Blog.Lang "webmentions" }}</h1>
{{ $blog := .Blog }}
{{ $current := .Data.Current }}
{{ range $i, $mention := .Data.Mentions }}
<div class="p">
<div id="mention-{{ $mention.ID }}" class="p">
<p>
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/>
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>
<form method="post">
<input type="hidden" name="mentionid" value="{{ $mention.ID }}">
<input type="hidden" name="redir" value="{{ $current }}#mention-{{ $mention.ID }}">
{{ if eq $mention.Status "verified" }}
<input type="submit" formaction="/webmention/approve" value="{{ string $blog.Lang "approve" }}">
{{ end }}

View File

@ -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)
}