2020-11-16 17:34:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2021-02-27 07:31:06 +00:00
|
|
|
"github.com/thoas/go-funk"
|
2020-11-16 17:34:29 +00:00
|
|
|
"github.com/tomnomnom/linkheader"
|
2021-06-28 20:17:18 +00:00
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
2020-11-16 17:34:29 +00:00
|
|
|
)
|
|
|
|
|
2021-07-23 05:02:49 +00:00
|
|
|
const postParamWebmention = "webmention"
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) sendWebmentions(p *post) error {
|
2021-07-14 13:44:57 +00:00
|
|
|
if p.Status != statusPublished && p.Status != statusUnlisted {
|
|
|
|
// Not published or unlisted
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
if wm := a.cfg.Webmention; wm != nil && wm.DisableSending {
|
2021-04-23 17:36:57 +00:00
|
|
|
// Just ignore the mentions
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-23 05:02:49 +00:00
|
|
|
if pp, ok := p.Parameters[postParamWebmention]; ok && len(pp) > 0 && pp[0] == "false" {
|
|
|
|
// Ignore this post
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-16 17:34:29 +00:00
|
|
|
links := []string{}
|
2021-07-19 14:32:45 +00:00
|
|
|
contentLinks, err := allLinksFromHTML(strings.NewReader(string(a.postHtml(p, false))), a.fullPostURL(p))
|
2020-11-16 17:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
links = append(links, contentLinks...)
|
2021-07-30 13:43:13 +00:00
|
|
|
if mpc := a.cfg.Micropub; mpc != nil {
|
|
|
|
links = append(links, p.firstParameter(a.cfg.Micropub.LikeParam), p.firstParameter(a.cfg.Micropub.ReplyParam), p.firstParameter(a.cfg.Micropub.BookmarkParam))
|
|
|
|
}
|
2021-02-27 07:31:06 +00:00
|
|
|
for _, link := range funk.UniqString(links) {
|
2020-11-16 17:34:29 +00:00
|
|
|
if link == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-27 07:31:06 +00:00
|
|
|
// Internal mention
|
2021-06-06 12:39:42 +00:00
|
|
|
if strings.HasPrefix(link, a.cfg.Server.PublicAddress) {
|
2020-11-16 17:34:29 +00:00
|
|
|
// Save mention directly
|
2021-06-06 12:39:42 +00:00
|
|
|
if err := a.createWebmention(a.fullPostURL(p), link); err != nil {
|
2021-02-08 17:51:07 +00:00
|
|
|
log.Println("Failed to create webmention:", err.Error())
|
|
|
|
}
|
2020-11-16 17:34:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-27 07:31:06 +00:00
|
|
|
// External mention
|
2021-07-27 10:51:08 +00:00
|
|
|
if a.isPrivate() {
|
2021-02-27 07:31:06 +00:00
|
|
|
// Private mode, don't send external mentions
|
|
|
|
continue
|
|
|
|
}
|
2021-07-14 13:44:57 +00:00
|
|
|
// Send webmention
|
2021-06-19 06:37:16 +00:00
|
|
|
endpoint := a.discoverEndpoint(link)
|
2020-11-16 17:34:29 +00:00
|
|
|
if endpoint == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-19 06:37:16 +00:00
|
|
|
if err = a.sendWebmention(endpoint, a.fullPostURL(p), link); err != nil {
|
2020-11-16 17:34:29 +00:00
|
|
|
log.Println("Sending webmention to " + link + " failed")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Println("Sent webmention to " + link)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
func (a *goBlog) sendWebmention(endpoint, source, target string) error {
|
2020-11-16 17:34:29 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, endpoint, strings.NewReader(url.Values{
|
|
|
|
"source": []string{source},
|
|
|
|
"target": []string{target},
|
|
|
|
}.Encode()))
|
|
|
|
if err != nil {
|
2021-04-23 17:36:57 +00:00
|
|
|
return err
|
2020-11-16 17:34:29 +00:00
|
|
|
}
|
2021-06-18 12:32:03 +00:00
|
|
|
req.Header.Set(contentType, contenttype.WWWForm)
|
2020-11-16 17:34:29 +00:00
|
|
|
req.Header.Set(userAgent, appUserAgent)
|
2021-06-19 06:37:16 +00:00
|
|
|
res, err := a.httpClient.Do(req)
|
2020-11-16 17:34:29 +00:00
|
|
|
if err != nil {
|
2021-04-23 17:36:57 +00:00
|
|
|
return err
|
2020-11-16 17:34:29 +00:00
|
|
|
}
|
2021-03-31 07:29:52 +00:00
|
|
|
defer res.Body.Close()
|
|
|
|
_, _ = io.Copy(io.Discard, res.Body)
|
2020-11-16 17:34:29 +00:00
|
|
|
if code := res.StatusCode; code < 200 || 300 <= code {
|
2021-04-23 17:36:57 +00:00
|
|
|
return fmt.Errorf("response error: %v", res.StatusCode)
|
2020-11-16 17:34:29 +00:00
|
|
|
}
|
2021-04-23 17:36:57 +00:00
|
|
|
return nil
|
2020-11-16 17:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
func (a *goBlog) discoverEndpoint(urlStr string) string {
|
2020-11-16 17:34:29 +00:00
|
|
|
doRequest := func(method, urlStr string) string {
|
|
|
|
req, err := http.NewRequest(method, urlStr, nil)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
req.Header.Set(userAgent, appUserAgent)
|
2021-06-19 06:37:16 +00:00
|
|
|
resp, err := a.httpClient.Do(req)
|
2020-11-16 17:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2021-03-31 07:29:52 +00:00
|
|
|
defer resp.Body.Close()
|
2020-11-16 17:34:29 +00:00
|
|
|
if code := resp.StatusCode; code < 200 || 300 <= code {
|
2021-03-31 07:29:52 +00:00
|
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
2020-11-16 17:34:29 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
endpoint, err := extractEndpoint(resp)
|
|
|
|
if err != nil || endpoint == "" {
|
2021-03-31 07:29:52 +00:00
|
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
2020-11-16 17:34:29 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if urls, err := resolveURLReferences(urlStr, endpoint); err == nil && len(urls) > 0 && urls[0] != "" {
|
|
|
|
return urls[0]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
headEndpoint := doRequest(http.MethodHead, urlStr)
|
|
|
|
if headEndpoint != "" {
|
|
|
|
return headEndpoint
|
|
|
|
}
|
|
|
|
getEndpoint := doRequest(http.MethodGet, urlStr)
|
|
|
|
if getEndpoint != "" {
|
|
|
|
return getEndpoint
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractEndpoint(resp *http.Response) (string, error) {
|
|
|
|
// first check http link headers
|
|
|
|
if endpoint := wmEndpointHTTPLink(resp.Header); endpoint != "" {
|
|
|
|
return endpoint, nil
|
|
|
|
}
|
|
|
|
// then look in the HTML body
|
|
|
|
endpoint, err := wmEndpointHTMLLink(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return endpoint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func wmEndpointHTTPLink(headers http.Header) string {
|
|
|
|
links := linkheader.ParseMultiple(headers[http.CanonicalHeaderKey("Link")]).FilterByRel("webmention")
|
|
|
|
for _, link := range links {
|
|
|
|
if u := link.URL; u != "" {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func wmEndpointHTMLLink(r io.Reader) (string, error) {
|
|
|
|
doc, err := goquery.NewDocumentFromReader(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
href, _ := doc.Find("a[href][rel=webmention],link[href][rel=webmention]").Attr("href")
|
|
|
|
return href, nil
|
|
|
|
}
|