2021-02-17 15:07:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-03-06 18:29:15 +00:00
|
|
|
"net/url"
|
2021-02-17 15:07:42 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
|
2021-03-03 17:19:55 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2021-02-17 15:07:42 +00:00
|
|
|
"github.com/vcraescu/go-paginator"
|
|
|
|
)
|
|
|
|
|
|
|
|
type webmentionPaginationAdapter struct {
|
|
|
|
config *webmentionsRequestConfig
|
|
|
|
nums int64
|
2021-06-06 12:39:42 +00:00
|
|
|
db *database
|
2021-02-17 15:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *webmentionPaginationAdapter) Nums() (int64, error) {
|
|
|
|
if p.nums == 0 {
|
2021-06-06 12:39:42 +00:00
|
|
|
nums, _ := p.db.countWebmentions(p.config)
|
2021-02-17 15:07:42 +00:00
|
|
|
p.nums = int64(nums)
|
|
|
|
}
|
|
|
|
return p.nums, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *webmentionPaginationAdapter) Slice(offset, length int, data interface{}) error {
|
|
|
|
modifiedConfig := *p.config
|
|
|
|
modifiedConfig.offset = offset
|
|
|
|
modifiedConfig.limit = length
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
wms, err := p.db.getWebmentions(&modifiedConfig)
|
2021-02-17 15:07:42 +00:00
|
|
|
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&wms).Elem())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) webmentionAdmin(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 15:07:42 +00:00
|
|
|
pageNoString := chi.URLParam(r, "page")
|
|
|
|
pageNo, _ := strconv.Atoi(pageNoString)
|
2021-03-06 18:29:15 +00:00
|
|
|
var status webmentionStatus = ""
|
|
|
|
switch webmentionStatus(r.URL.Query().Get("status")) {
|
|
|
|
case webmentionStatusVerified:
|
|
|
|
status = webmentionStatusVerified
|
|
|
|
case webmentionStatusApproved:
|
|
|
|
status = webmentionStatusApproved
|
|
|
|
}
|
2021-05-24 08:09:37 +00:00
|
|
|
sourcelike := r.URL.Query().Get("source")
|
|
|
|
p := paginator.New(&webmentionPaginationAdapter{config: &webmentionsRequestConfig{
|
|
|
|
status: status,
|
|
|
|
sourcelike: sourcelike,
|
2021-06-06 12:39:42 +00:00
|
|
|
}, db: a.db}, 10)
|
2021-02-17 15:07:42 +00:00
|
|
|
p.SetPage(pageNo)
|
|
|
|
var mentions []*mention
|
|
|
|
err := p.Results(&mentions)
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-02-17 15:07:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Navigation
|
|
|
|
var hasPrev, hasNext bool
|
|
|
|
var prevPage, nextPage int
|
|
|
|
var prevPath, nextPath string
|
|
|
|
hasPrev, _ = p.HasPrev()
|
|
|
|
if hasPrev {
|
|
|
|
prevPage, _ = p.PrevPage()
|
|
|
|
} else {
|
|
|
|
prevPage, _ = p.Page()
|
|
|
|
}
|
|
|
|
if prevPage < 2 {
|
|
|
|
prevPath = webmentionPath
|
|
|
|
} else {
|
|
|
|
prevPath = fmt.Sprintf("%s/page/%d", webmentionPath, prevPage)
|
|
|
|
}
|
|
|
|
hasNext, _ = p.HasNext()
|
|
|
|
if hasNext {
|
|
|
|
nextPage, _ = p.NextPage()
|
|
|
|
} else {
|
|
|
|
nextPage, _ = p.Page()
|
|
|
|
}
|
|
|
|
nextPath = fmt.Sprintf("%s/page/%d", webmentionPath, nextPage)
|
2021-03-06 18:29:15 +00:00
|
|
|
// Query
|
|
|
|
query := ""
|
|
|
|
params := url.Values{}
|
|
|
|
if status != "" {
|
|
|
|
params.Add("status", string(status))
|
|
|
|
}
|
2021-05-24 08:09:37 +00:00
|
|
|
if sourcelike != "" {
|
|
|
|
params.Add("source", sourcelike)
|
|
|
|
}
|
2021-03-06 18:29:15 +00:00
|
|
|
if len(params) > 0 {
|
|
|
|
query = "?" + params.Encode()
|
|
|
|
}
|
2021-02-17 15:07:42 +00:00
|
|
|
// Render
|
2021-06-06 12:39:42 +00:00
|
|
|
a.render(w, r, templateWebmentionAdmin, &renderData{
|
2021-02-17 15:07:42 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Mentions": mentions,
|
|
|
|
"HasPrev": hasPrev,
|
|
|
|
"HasNext": hasNext,
|
2021-06-10 20:09:50 +00:00
|
|
|
"Prev": prevPath + query,
|
|
|
|
"Next": nextPath + query,
|
2021-02-17 15:07:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) webmentionAdminDelete(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 15:07:42 +00:00
|
|
|
id, err := strconv.Atoi(r.FormValue("mentionid"))
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
2021-02-17 15:07:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
err = a.db.deleteWebmention(id)
|
2021-02-17 15:07:42 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-02-17 15:07:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cache.purge()
|
2021-02-17 15:07:42 +00:00
|
|
|
http.Redirect(w, r, ".", http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) webmentionAdminApprove(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 15:07:42 +00:00
|
|
|
id, err := strconv.Atoi(r.FormValue("mentionid"))
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
2021-02-17 15:07:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
err = a.db.approveWebmention(id)
|
2021-02-17 15:07:42 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-02-17 15:07:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cache.purge()
|
2021-02-17 15:07:42 +00:00
|
|
|
http.Redirect(w, r, ".", http.StatusFound)
|
|
|
|
}
|
2021-05-23 18:11:48 +00:00
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) webmentionAdminReverify(w http.ResponseWriter, r *http.Request) {
|
2021-05-23 18:11:48 +00:00
|
|
|
id, err := strconv.Atoi(r.FormValue("mentionid"))
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
2021-05-23 18:11:48 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
err = a.reverifyWebmention(id)
|
2021-05-23 18:11:48 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-05-23 18:11:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, ".", http.StatusFound)
|
|
|
|
}
|