2020-11-06 17:45:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-11-16 13:18:14 +00:00
|
|
|
"errors"
|
2020-11-06 17:45:31 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-01-30 18:37:26 +00:00
|
|
|
"reflect"
|
2020-11-06 17:45:31 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-01-30 18:37:26 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/vcraescu/go-paginator"
|
2020-11-06 17:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type webmentionStatus string
|
|
|
|
|
|
|
|
const (
|
|
|
|
webmentionStatusVerified webmentionStatus = "verified"
|
|
|
|
webmentionStatusApproved webmentionStatus = "approved"
|
2021-01-30 18:37:26 +00:00
|
|
|
|
|
|
|
webmentionPath = "/webmention"
|
2020-11-06 17:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mention struct {
|
|
|
|
ID int
|
|
|
|
Source string
|
|
|
|
Target string
|
|
|
|
Created int64
|
|
|
|
Title string
|
|
|
|
Content string
|
|
|
|
Author string
|
2021-01-30 18:37:26 +00:00
|
|
|
Status webmentionStatus
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 11:36:14 +00:00
|
|
|
func initWebmention() error {
|
2020-11-17 21:10:13 +00:00
|
|
|
// Add hooks
|
|
|
|
hookFunc := func(p *post) {
|
2021-01-15 20:56:46 +00:00
|
|
|
if p.Status == statusPublished {
|
|
|
|
p.sendWebmentions()
|
|
|
|
}
|
2020-11-17 21:10:13 +00:00
|
|
|
}
|
|
|
|
postHooks[postPostHook] = append(postHooks[postPostHook], hookFunc)
|
|
|
|
postHooks[postUpdateHook] = append(postHooks[postUpdateHook], hookFunc)
|
|
|
|
postHooks[postDeleteHook] = append(postHooks[postDeleteHook], hookFunc)
|
|
|
|
// Start verifier
|
2020-11-25 11:36:14 +00:00
|
|
|
return initWebmentionQueue()
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleWebmention(w http.ResponseWriter, r *http.Request) {
|
2020-11-16 13:18:14 +00:00
|
|
|
m, err := extractMention(r)
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, err.Error(), http.StatusBadRequest)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-24 10:00:16 +00:00
|
|
|
if !isAllowedHost(httptest.NewRequest(http.MethodGet, m.Target, nil), appConfig.Server.publicHostname) {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, "target not allowed", http.StatusBadRequest)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-25 11:36:14 +00:00
|
|
|
if err = queueMention(m); err != nil {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
_, _ = fmt.Fprint(w, "Webmention accepted")
|
|
|
|
}
|
|
|
|
|
2020-11-16 13:18:14 +00:00
|
|
|
func extractMention(r *http.Request) (*mention, error) {
|
|
|
|
if !strings.Contains(r.Header.Get(contentType), contentTypeWWWForm) {
|
|
|
|
return nil, errors.New("Unsupported Content-Type")
|
|
|
|
}
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
source := r.Form.Get("source")
|
2020-11-25 11:36:14 +00:00
|
|
|
target := unescapedPath(r.Form.Get("target"))
|
2020-11-16 13:18:14 +00:00
|
|
|
if source == "" || target == "" || !isAbsoluteURL(source) || !isAbsoluteURL(target) {
|
|
|
|
return nil, errors.New("Invalid request")
|
|
|
|
}
|
|
|
|
return &mention{
|
2020-11-25 11:36:14 +00:00
|
|
|
Source: source,
|
|
|
|
Target: target,
|
|
|
|
Created: time.Now().Unix(),
|
2020-11-16 13:18:14 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:45:31 +00:00
|
|
|
func webmentionAdmin(w http.ResponseWriter, r *http.Request) {
|
2021-01-30 18:37:26 +00:00
|
|
|
pageNoString := chi.URLParam(r, "page")
|
|
|
|
pageNo, _ := strconv.Atoi(pageNoString)
|
|
|
|
p := paginator.New(&webmentionPaginationAdapter{config: &webmentionsRequestConfig{}}, 10)
|
|
|
|
p.SetPage(pageNo)
|
|
|
|
var mentions []*mention
|
|
|
|
err := p.Results(&mentions)
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-30 18:37:26 +00:00
|
|
|
// 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()
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
2021-01-30 18:37:26 +00:00
|
|
|
nextPath = fmt.Sprintf("%s/page/%d", webmentionPath, nextPage)
|
|
|
|
// Render
|
2020-11-06 17:45:31 +00:00
|
|
|
render(w, "webmentionadmin", &renderData{
|
2021-01-30 18:37:26 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Mentions": mentions,
|
|
|
|
"HasPrev": hasPrev,
|
|
|
|
"HasNext": hasNext,
|
|
|
|
"Prev": slashIfEmpty(prevPath),
|
|
|
|
"Next": slashIfEmpty(nextPath),
|
2020-11-06 17:45:31 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func webmentionAdminDelete(w http.ResponseWriter, r *http.Request) {
|
2020-12-19 10:06:55 +00:00
|
|
|
id, err := strconv.Atoi(r.FormValue("mentionid"))
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, err.Error(), http.StatusBadRequest)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = deleteWebmention(id)
|
|
|
|
if err != nil {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-22 12:20:48 +00:00
|
|
|
purgeCache()
|
2021-01-23 16:24:47 +00:00
|
|
|
http.Redirect(w, r, ".", http.StatusFound)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func webmentionAdminApprove(w http.ResponseWriter, r *http.Request) {
|
2020-12-19 10:06:55 +00:00
|
|
|
id, err := strconv.Atoi(r.FormValue("mentionid"))
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, err.Error(), http.StatusBadRequest)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = approveWebmention(id)
|
|
|
|
if err != nil {
|
2020-12-24 09:09:34 +00:00
|
|
|
serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-22 12:20:48 +00:00
|
|
|
purgeCache()
|
2021-01-23 16:24:47 +00:00
|
|
|
http.Redirect(w, r, ".", http.StatusFound)
|
2020-11-06 17:45:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func webmentionExists(source, target string) bool {
|
|
|
|
result := 0
|
2020-11-25 11:36:14 +00:00
|
|
|
row, err := appDbQueryRow("select exists(select 1 from webmentions where source = ? and target = ?)", source, target)
|
2020-11-09 15:40:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err = row.Scan(&result); err != nil {
|
2020-11-06 17:45:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return result == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func createWebmention(source, target string) (err error) {
|
2020-11-25 11:36:14 +00:00
|
|
|
return queueMention(&mention{
|
|
|
|
Source: source,
|
|
|
|
Target: unescapedPath(target),
|
|
|
|
Created: time.Now().Unix(),
|
|
|
|
})
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteWebmention(id int) error {
|
2020-11-25 11:36:14 +00:00
|
|
|
_, err := appDbExec("delete from webmentions where id = @id", sql.Named("id", id))
|
2020-11-06 17:45:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func approveWebmention(id int) error {
|
2020-11-09 15:40:12 +00:00
|
|
|
_, err := appDbExec("update webmentions set status = ? where id = ?", webmentionStatusApproved, id)
|
2020-11-06 17:45:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type webmentionsRequestConfig struct {
|
2021-01-30 18:37:26 +00:00
|
|
|
target string
|
|
|
|
status webmentionStatus
|
|
|
|
asc bool
|
|
|
|
offset, limit int
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 18:37:26 +00:00
|
|
|
type webmentionPaginationAdapter struct {
|
|
|
|
config *webmentionsRequestConfig
|
|
|
|
nums int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *webmentionPaginationAdapter) Nums() (int64, error) {
|
|
|
|
if p.nums == 0 {
|
|
|
|
nums, _ := countWebmentions(p.config)
|
|
|
|
p.nums = int64(nums)
|
|
|
|
}
|
|
|
|
return p.nums, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *webmentionPaginationAdapter) Slice(offset, length int, data interface{}) error {
|
|
|
|
if reflect.TypeOf(data).Kind() != reflect.Ptr {
|
|
|
|
panic("data has to be a pointer")
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiedConfig := *p.config
|
|
|
|
modifiedConfig.offset = offset
|
|
|
|
modifiedConfig.limit = length
|
|
|
|
|
|
|
|
wms, err := getWebmentions(&modifiedConfig)
|
|
|
|
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&wms).Elem())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildWebmentionsQuery(config *webmentionsRequestConfig) (query string, args []interface{}) {
|
|
|
|
args = []interface{}{}
|
2020-11-09 15:40:12 +00:00
|
|
|
filter := ""
|
2020-11-06 17:45:31 +00:00
|
|
|
if config != nil {
|
2020-11-09 15:40:12 +00:00
|
|
|
if config.target != "" && config.status != "" {
|
|
|
|
filter = "where target = @target and status = @status"
|
2020-11-17 19:01:02 +00:00
|
|
|
args = append(args, sql.Named("target", unescapedPath(config.target)), sql.Named("status", config.status))
|
2020-11-09 15:40:12 +00:00
|
|
|
} else if config.target != "" {
|
|
|
|
filter = "where target = @target"
|
|
|
|
args = append(args, sql.Named("target", config.target))
|
|
|
|
} else if config.status != "" {
|
|
|
|
filter = "where status = @status"
|
|
|
|
args = append(args, sql.Named("status", config.status))
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 15:58:44 +00:00
|
|
|
order := "desc"
|
|
|
|
if config.asc {
|
|
|
|
order = "asc"
|
|
|
|
}
|
2021-01-30 18:37:26 +00:00
|
|
|
query = "select id, source, target, created, title, content, author, status from webmentions " + filter + " order by created " + order
|
|
|
|
if config.limit != 0 || config.offset != 0 {
|
|
|
|
query += " limit @limit offset @offset"
|
|
|
|
args = append(args, sql.Named("limit", config.limit), sql.Named("offset", config.offset))
|
|
|
|
}
|
|
|
|
return query, args
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWebmentions(config *webmentionsRequestConfig) ([]*mention, error) {
|
|
|
|
mentions := []*mention{}
|
|
|
|
query, args := buildWebmentionsQuery(config)
|
|
|
|
rows, err := appDbQuery(query, args...)
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
m := &mention{}
|
2021-01-30 18:37:26 +00:00
|
|
|
err = rows.Scan(&m.ID, &m.Source, &m.Target, &m.Created, &m.Title, &m.Content, &m.Author, &m.Status)
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mentions = append(mentions, m)
|
|
|
|
}
|
|
|
|
return mentions, nil
|
|
|
|
}
|
2021-01-30 18:37:26 +00:00
|
|
|
|
|
|
|
func countWebmentions(config *webmentionsRequestConfig) (count int, err error) {
|
|
|
|
query, params := buildWebmentionsQuery(config)
|
|
|
|
query = "select count(*) from (" + query + ")"
|
|
|
|
row, err := appDbQueryRow(query, params...)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = row.Scan(&count)
|
|
|
|
return
|
|
|
|
}
|