Add noindex for robots header on non-public posts

This commit is contained in:
Jan-Lukas Else 2023-01-28 22:51:23 +01:00
parent b6a88e6bed
commit efdb2f8038
5 changed files with 25 additions and 22 deletions

View File

@ -381,7 +381,7 @@ func (a *goBlog) apShowFollowers(w http.ResponseWriter, r *http.Request) {
followersCollection.Items.Append(ap.IRI(follower.follower))
}
followersCollection.TotalItems = uint(len(followers))
a.serveAPItem(followersCollection, w, r)
a.serveAPItem(w, r, http.StatusOK, followersCollection)
return
}
a.render(w, r, a.renderActivityPubFollowers, &renderData{

View File

@ -38,8 +38,8 @@ func (a *goBlog) checkActivityStreamsRequest(next http.Handler) http.Handler {
})
}
func (a *goBlog) serveActivityStreamsPost(p *post, w http.ResponseWriter, r *http.Request) {
a.serveAPItem(a.toAPNote(p), w, r)
func (a *goBlog) serveActivityStreamsPost(w http.ResponseWriter, r *http.Request, status int, p *post) {
a.serveAPItem(w, r, status, a.toAPNote(p))
}
func (a *goBlog) toAPNote(p *post) *ap.Note {
@ -159,11 +159,11 @@ func (a *goBlog) toApPerson(blog string) *ap.Person {
return apBlog
}
func (a *goBlog) serveActivityStreams(blog string, w http.ResponseWriter, r *http.Request) {
a.serveAPItem(a.toApPerson(blog), w, r)
func (a *goBlog) serveActivityStreams(w http.ResponseWriter, r *http.Request, status int, blog string) {
a.serveAPItem(w, r, status, a.toApPerson(blog))
}
func (a *goBlog) serveAPItem(item any, w http.ResponseWriter, r *http.Request) {
func (a *goBlog) serveAPItem(w http.ResponseWriter, r *http.Request, status int, item any) {
// Encode
binary, err := jsonld.WithContext(jsonld.IRI(ap.ActivityBaseURI), jsonld.IRI(ap.SecurityContextURI)).Marshal(item)
if err != nil {

6
go.mod
View File

@ -11,7 +11,7 @@ require (
github.com/alecthomas/chroma/v2 v2.4.0
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
github.com/carlmjohnson/requests v0.22.3
github.com/carlmjohnson/requests v0.23.1
// master
github.com/cretz/bine v0.2.1-0.20221201125941-b9d31d9c7866
github.com/dchest/captcha v1.0.0
@ -21,7 +21,7 @@ require (
github.com/elnormous/contenttype v1.0.3
github.com/emersion/go-smtp v0.16.0
github.com/go-ap/activitypub v0.0.0-20221209114049-1ceafda50f9f
github.com/go-ap/client v0.0.0-20221211133508-e3087682099e
github.com/go-ap/client v0.0.0-20230125140507-b30e32f7fa90
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73
github.com/go-chi/chi/v5 v5.0.8
github.com/go-fed/httpsig v1.1.0
@ -41,7 +41,7 @@ require (
github.com/lopezator/migrator v0.3.1
github.com/mattn/go-sqlite3 v1.14.16
github.com/mergestat/timediff v0.0.3
github.com/microcosm-cc/bluemonday v1.0.21
github.com/microcosm-cc/bluemonday v1.0.22
github.com/mmcdole/gofeed v1.1.3
github.com/paulmach/go.geojson v1.4.0
github.com/posener/wstest v1.2.0

12
go.sum
View File

@ -68,8 +68,8 @@ github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b h1:6+ZFm0flnudZzdSE0JxlhR2hKnGPcNB35BjQf4RYQDY=
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
github.com/carlmjohnson/requests v0.22.3 h1:ip16AKXNYuArdw9L5/1mL+mNorlZO5XhkLg617yOumc=
github.com/carlmjohnson/requests v0.22.3/go.mod h1:iTsaX9TdFg2+L4WtZO/HFyDMPEfBnogV3i4A4gjDnvs=
github.com/carlmjohnson/requests v0.23.1 h1:d8FpOZC6We3raxa3cW1oEyK+cNz4XSW8A6wjMkMsbOM=
github.com/carlmjohnson/requests v0.23.1/go.mod h1:i/0+cvgndkM7SPtw911bcbYR4CP+/9vP/+TbcFqPz3A=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
@ -129,8 +129,8 @@ github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-ap/activitypub v0.0.0-20221209114049-1ceafda50f9f h1:UV5kupaU8AP8g8Bbsn53q87XCufW/E8wvnTHDKqjoR4=
github.com/go-ap/activitypub v0.0.0-20221209114049-1ceafda50f9f/go.mod h1:1oVD0h0aPT3OEE1ZoSUoym/UGKzxe+e0y8K2AkQ1Hqs=
github.com/go-ap/client v0.0.0-20221211133508-e3087682099e h1:JS8lV0aQwHg1XVXRyVC4NTzuoVf8vob4vXtu1maF/o0=
github.com/go-ap/client v0.0.0-20221211133508-e3087682099e/go.mod h1:P5qNxXHk44o9OEmHxmjCFjS5jLZa4BZv6h7lHrmC1MA=
github.com/go-ap/client v0.0.0-20230125140507-b30e32f7fa90 h1:6RecS9v9M00TAnGpaAj4cRjGVZzFVuqXWoZrefGBFro=
github.com/go-ap/client v0.0.0-20230125140507-b30e32f7fa90/go.mod h1:P5qNxXHk44o9OEmHxmjCFjS5jLZa4BZv6h7lHrmC1MA=
github.com/go-ap/errors v0.0.0-20221205040414-01c1adfc98ea h1:ywGtLGVjJjMrq4mu35Qmu+NtlhlTk/gTayE6Bb4tQZk=
github.com/go-ap/errors v0.0.0-20221205040414-01c1adfc98ea/go.mod h1:SaTNjEEkp0q+w3pUS1ccyEL/lUrHteORlDq/e21mCc8=
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 h1:GMKIYXyXPGIp+hYiWOhfqK4A023HdgisDT4YGgf99mw=
@ -316,8 +316,8 @@ github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwp
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mergestat/timediff v0.0.3 h1:ucCNh4/ZrTPjFZ081PccNbhx9spymCJkFxSzgVuPU+Y=
github.com/mergestat/timediff v0.0.3/go.mod h1:yvMUaRu2oetc+9IbPLYBJviz6sA7xz8OXMDfhBl7YSI=
github.com/microcosm-cc/bluemonday v1.0.21 h1:dNH3e4PSyE4vNX+KlRGHT5KrSvjeUkoNPwEORjffHJg=
github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM=
github.com/microcosm-cc/bluemonday v1.0.22 h1:p2tT7RNzRdCi0qmwxG+HbqD6ILkmwter1ZwVZn1oTxA=
github.com/microcosm-cc/bluemonday v1.0.22/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mmcdole/gofeed v1.1.3 h1:pdrvMb18jMSLidGp8j0pLvc9IGziX4vbmvVqmLH6z8o=

View File

@ -72,12 +72,16 @@ func (a *goBlog) servePost(w http.ResponseWriter, r *http.Request) {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
status := http.StatusOK
if p.Deleted() {
status = http.StatusGone
}
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
if r.URL.Path == a.getRelativePath(p.Blog, "") {
a.serveActivityStreams(p.Blog, w, r)
a.serveActivityStreams(w, r, status, p.Blog)
return
}
a.serveActivityStreamsPost(p, w, r)
a.serveActivityStreamsPost(w, r, status, p)
return
}
canonical := p.firstParameter("original")
@ -88,11 +92,10 @@ func (a *goBlog) servePost(w http.ResponseWriter, r *http.Request) {
if p.Path == a.getRelativePath(p.Blog, "") {
renderMethod = a.renderStaticHome
}
w.Header().Add("Link", fmt.Sprintf("<%s>; rel=shortlink", a.shortPostURL(p)))
status := http.StatusOK
if p.Deleted() {
status = http.StatusGone
if p.Visibility != visibilityPublic {
w.Header().Set("X-Robots-Tag", "noindex")
}
w.Header().Add("Link", fmt.Sprintf("<%s>; rel=shortlink", a.shortPostURL(p)))
a.renderWithStatusCode(w, r, status, renderMethod, &renderData{
BlogString: p.Blog,
Canonical: canonical,
@ -154,7 +157,7 @@ func (p *postPaginationAdapter) Slice(offset, length int, data any) error {
func (a *goBlog) serveHome(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
a.serveActivityStreams(blog, w, r)
a.serveActivityStreams(w, r, http.StatusOK, blog)
return
}
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{