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"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type webmentionStatus string
|
|
|
|
|
|
|
|
const (
|
|
|
|
webmentionStatusNew webmentionStatus = "new"
|
|
|
|
webmentionStatusRenew webmentionStatus = "renew"
|
|
|
|
webmentionStatusVerified webmentionStatus = "verified"
|
|
|
|
webmentionStatusApproved webmentionStatus = "approved"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mention struct {
|
|
|
|
ID int
|
|
|
|
Source string
|
|
|
|
Target string
|
|
|
|
Created int64
|
|
|
|
Title string
|
|
|
|
Content string
|
|
|
|
Author string
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
verified, err := getWebmentions(&webmentionsRequestConfig{
|
|
|
|
status: webmentionStatusVerified,
|
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
approved, err := getWebmentions(&webmentionsRequestConfig{
|
|
|
|
status: webmentionStatusApproved,
|
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
render(w, "webmentionadmin", &renderData{
|
|
|
|
Data: map[string][]*mention{
|
|
|
|
"Verified": verified,
|
|
|
|
"Approved": approved,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2020-12-19 10:06:55 +00:00
|
|
|
http.Redirect(w, r, "/webmention", 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()
|
2020-12-19 10:06:55 +00:00
|
|
|
http.Redirect(w, r, "/webmention", 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 {
|
|
|
|
target string
|
|
|
|
status webmentionStatus
|
2020-11-11 15:58:44 +00:00
|
|
|
asc bool
|
2020-11-06 17:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getWebmentions(config *webmentionsRequestConfig) ([]*mention, error) {
|
|
|
|
mentions := []*mention{}
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
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"
|
|
|
|
}
|
2020-11-16 14:46:16 +00:00
|
|
|
rows, err = appDbQuery("select id, source, target, created, title, content, author from webmentions "+filter+" order by created "+order, args...)
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
m := &mention{}
|
2020-11-16 14:46:16 +00:00
|
|
|
err = rows.Scan(&m.ID, &m.Source, &m.Target, &m.Created, &m.Title, &m.Content, &m.Author)
|
2020-11-06 17:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mentions = append(mentions, m)
|
|
|
|
}
|
|
|
|
return mentions, nil
|
|
|
|
}
|