mirror of https://github.com/jlelse/GoBlog
Custom webmention sending implementation and other improvements
parent
6dd99289ad
commit
3a866fb3c0
@ -1,27 +0,0 @@
|
||||
{{ define "title" }}
|
||||
<title>{{ with .Blog.Photos.Title }}{{ . }} - {{ end }}{{ .Blog.Title }}</title>
|
||||
{{ end }}
|
||||
|
||||
{{ define "main" }}
|
||||
<main>
|
||||
{{ with .Blog.Photos.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||
{{ with .Blog.Photos.Description }}{{ md . }}{{ end }}
|
||||
{{ if (or .Blog.Photos.Title .Blog.Photos.Description) }}
|
||||
<hr>
|
||||
{{ end }}
|
||||
{{ $blog := .Blog }}
|
||||
{{ range $i, $post := .Data.Posts }}
|
||||
{{ include "photosummary" $blog $post }}
|
||||
{{ end }}
|
||||
{{ if .Data.HasPrev }}
|
||||
<p><a href="{{ .Data.Prev }}">{{ string .Blog.Lang "prev" }}</a></p>
|
||||
{{ end }}
|
||||
{{ if .Data.HasNext }}
|
||||
<p><a href="{{ .Data.Next }}">{{ string .Blog.Lang "next" }}</a></p>
|
||||
{{ end }}
|
||||
</main>
|
||||
{{ end }}
|
||||
|
||||
{{ define "photos" }}
|
||||
{{ template "base" . }}
|
||||
{{ end }}
|
@ -1,23 +0,0 @@
|
||||
{{ define "title" }}{{ end }}
|
||||
|
||||
{{ define "main" }}
|
||||
<main class=h-entry>
|
||||
<article>
|
||||
<data class="u-url hide" value="{{ absolute .Data.Path }}"></data>
|
||||
{{ with title .Data }}<h1 class=p-name>{{ . }}</h1>{{ end }}
|
||||
{{ include "postmeta" . }}
|
||||
{{ if .Data.Content }}
|
||||
<div class=e-content>
|
||||
{{ content .Data }}
|
||||
{{ with p .Data "link" }}
|
||||
<p><a class="u-bookmark-of" href="{{ . }}" target="_blank" rel="noopener">{{ . }}</a></p>
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ end }}
|
||||
</article>
|
||||
</main>
|
||||
{{ end }}
|
||||
|
||||
{{ define "postbasic" }}
|
||||
{{ template "base" . }}
|
||||
{{ end }}
|
@ -0,0 +1,131 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/tomnomnom/linkheader"
|
||||
)
|
||||
|
||||
func (p *post) sendWebmentions() error {
|
||||
links := []string{}
|
||||
contentLinks, err := allLinksFromHTML(strings.NewReader(string(p.html())), p.fullURL())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
links = append(links, contentLinks...)
|
||||
links = append(links, p.firstParameter(appConfig.Micropub.LikeParam), p.firstParameter(appConfig.Micropub.ReplyParam), p.firstParameter(appConfig.Micropub.BookmarkParam))
|
||||
for _, link := range links {
|
||||
if link == "" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(link, appConfig.Server.PublicAddress) {
|
||||
// Save mention directly
|
||||
createWebmention(p.fullURL(), link)
|
||||
continue
|
||||
}
|
||||
endpoint := discoverEndpoint(link)
|
||||
if endpoint == "" {
|
||||
continue
|
||||
}
|
||||
_, err = sendWebmention(endpoint, p.fullURL(), link)
|
||||
if err != nil {
|
||||
log.Println("Sending webmention to " + link + " failed")
|
||||
continue
|
||||
}
|
||||
log.Println("Sent webmention to " + link)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendWebmention(endpoint, source, target string) (*http.Response, error) {
|
||||
req, err := http.NewRequest(http.MethodPost, endpoint, strings.NewReader(url.Values{
|
||||
"source": []string{source},
|
||||
"target": []string{target},
|
||||
}.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set(contentType, contentTypeWWWForm)
|
||||
req.Header.Set(userAgent, appUserAgent)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if code := res.StatusCode; code < 200 || 300 <= code {
|
||||
return res, fmt.Errorf("response error: %v", res.StatusCode)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func discoverEndpoint(urlStr string) string {
|
||||
doRequest := func(method, urlStr string) string {
|
||||
req, err := http.NewRequest(method, urlStr, nil)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
req.Header.Set(userAgent, appUserAgent)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if code := resp.StatusCode; code < 200 || 300 <= code {
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
endpoint, err := extractEndpoint(resp)
|
||||
if err != nil || endpoint == "" {
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue