2021-04-07 11:29:21 +02:00
|
|
|
package webmention
|
|
|
|
|
|
|
|
import (
|
2021-04-09 18:04:04 +02:00
|
|
|
"brainbaking.com/go-jamming/app/mf"
|
|
|
|
"brainbaking.com/go-jamming/app/webmention/recv"
|
|
|
|
"brainbaking.com/go-jamming/app/webmention/send"
|
2021-04-18 15:39:29 +02:00
|
|
|
"brainbaking.com/go-jamming/db"
|
2021-04-09 21:00:54 +02:00
|
|
|
"github.com/gorilla/mux"
|
2021-04-07 11:29:21 +02:00
|
|
|
"net/http"
|
|
|
|
|
2021-04-09 18:04:04 +02:00
|
|
|
"brainbaking.com/go-jamming/common"
|
|
|
|
"brainbaking.com/go-jamming/rest"
|
2021-04-07 11:29:21 +02:00
|
|
|
)
|
|
|
|
|
2021-04-13 09:10:32 +02:00
|
|
|
var (
|
|
|
|
httpClient = &rest.HttpClient{}
|
|
|
|
)
|
2021-04-09 18:00:40 +02:00
|
|
|
|
2021-04-18 15:39:29 +02:00
|
|
|
func HandleGet(repo db.MentionRepo) http.HandlerFunc {
|
2021-04-09 21:00:54 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-04-11 13:03:41 +02:00
|
|
|
domain := mux.Vars(r)["domain"]
|
2021-04-18 15:39:29 +02:00
|
|
|
rest.Json(w, repo.GetAll(domain))
|
2021-04-09 21:00:54 +02:00
|
|
|
}
|
2021-04-07 11:44:58 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:39:29 +02:00
|
|
|
func HandlePut(conf *common.Config, repo db.MentionRepo) http.HandlerFunc {
|
2021-04-09 21:00:54 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-04-09 18:00:40 +02:00
|
|
|
since := getSinceQueryParam(r)
|
|
|
|
domain := mux.Vars(r)["domain"]
|
|
|
|
|
2021-04-09 18:04:04 +02:00
|
|
|
snder := &send.Sender{
|
2021-04-09 18:00:40 +02:00
|
|
|
RestClient: httpClient,
|
|
|
|
Conf: conf,
|
2021-04-18 15:39:29 +02:00
|
|
|
Repo: repo,
|
2021-04-09 18:00:40 +02:00
|
|
|
}
|
|
|
|
go snder.Send(domain, since)
|
|
|
|
rest.Accept(w)
|
|
|
|
}
|
2021-04-07 11:44:58 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 18:00:40 +02:00
|
|
|
func getSinceQueryParam(r *http.Request) string {
|
2021-04-20 20:08:07 +02:00
|
|
|
sinceParam := r.URL.Query()["since"]
|
2021-04-09 18:00:40 +02:00
|
|
|
since := ""
|
|
|
|
if len(sinceParam) > 0 {
|
|
|
|
since = sinceParam[0]
|
|
|
|
}
|
|
|
|
return since
|
|
|
|
}
|
2021-04-08 09:54:47 +02:00
|
|
|
|
2021-04-18 15:39:29 +02:00
|
|
|
func HandlePost(conf *common.Config, repo db.MentionRepo) http.HandlerFunc {
|
2021-04-09 21:00:54 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
if !validate(r, r.Header, conf) {
|
|
|
|
rest.BadRequest(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
target := r.FormValue("target")
|
|
|
|
if !isValidTargetUrl(target, httpClient) {
|
|
|
|
rest.BadRequest(w)
|
|
|
|
return
|
|
|
|
}
|
2021-04-07 17:31:23 +02:00
|
|
|
|
2021-04-09 21:00:54 +02:00
|
|
|
wm := mf.Mention{
|
|
|
|
Source: r.FormValue("source"),
|
|
|
|
Target: target,
|
|
|
|
}
|
|
|
|
recv := &recv.Receiver{
|
|
|
|
RestClient: httpClient,
|
|
|
|
Conf: conf,
|
2021-04-18 15:39:29 +02:00
|
|
|
Repo: repo,
|
2021-04-09 21:00:54 +02:00
|
|
|
}
|
2021-04-07 17:31:23 +02:00
|
|
|
|
2021-04-09 21:00:54 +02:00
|
|
|
go recv.Receive(wm)
|
|
|
|
rest.Accept(w)
|
|
|
|
}
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|