From b7acd39051a293289c182f2a9166f681bbe8b07f Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 9 Sep 2019 22:04:20 +0200 Subject: [PATCH 01/69] Add Cache-Control headers on AP endpoints Includes: * AP Collection fetching via canonical URL * AP Collection fetching via API * AP Post fetching via canonical URL * AP Post fetching via API Ref T693 --- activitypub.go | 10 ++++++++++ collections.go | 1 + posts.go | 2 ++ 3 files changed, 13 insertions(+) diff --git a/activitypub.go b/activitypub.go index 997609d..f436a87 100644 --- a/activitypub.go +++ b/activitypub.go @@ -37,6 +37,8 @@ import ( const ( // TODO: delete. don't use this! apCustomHandleDefault = "blog" + + apCacheTime = time.Minute ) type RemoteUser struct { @@ -84,6 +86,7 @@ func handleFetchCollectionActivities(app *App, w http.ResponseWriter, r *http.Re p := c.PersonObject() + setCacheControl(w, apCacheTime) return impart.RenderActivityJSON(w, p, http.StatusOK) } @@ -137,6 +140,7 @@ func handleFetchCollectionOutbox(app *App, w http.ResponseWriter, r *http.Reques ocp.OrderedItems = append(ocp.OrderedItems, *a) } + setCacheControl(w, apCacheTime) return impart.RenderActivityJSON(w, ocp, http.StatusOK) } @@ -183,6 +187,7 @@ func handleFetchCollectionFollowers(app *App, w http.ResponseWriter, r *http.Req ocp.OrderedItems = append(ocp.OrderedItems, f.ActorID) } */ + setCacheControl(w, apCacheTime) return impart.RenderActivityJSON(w, ocp, http.StatusOK) } @@ -219,6 +224,7 @@ func handleFetchCollectionFollowing(app *App, w http.ResponseWriter, r *http.Req // Return outbox page ocp := activitystreams.NewOrderedCollectionPage(accountRoot, "following", 0, p) ocp.OrderedItems = []interface{}{} + setCacheControl(w, apCacheTime) return impart.RenderActivityJSON(w, ocp, http.StatusOK) } @@ -703,3 +709,7 @@ func unmarshalActor(actorResp []byte, actor *activitystreams.Person) error { return nil } + +func setCacheControl(w http.ResponseWriter, ttl time.Duration) { + w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", ttl.Seconds())) +} diff --git a/collections.go b/collections.go index 997d4d7..a13aad2 100644 --- a/collections.go +++ b/collections.go @@ -730,6 +730,7 @@ func handleViewCollection(app *App, w http.ResponseWriter, r *http.Request) erro if strings.Contains(r.Header.Get("Accept"), "application/activity+json") { ac := c.PersonObject() ac.Context = []interface{}{activitystreams.Namespace} + setCacheControl(w, apCacheTime) return impart.RenderActivityJSON(w, ac, http.StatusOK) } diff --git a/posts.go b/posts.go index 2f3606f..625410b 100644 --- a/posts.go +++ b/posts.go @@ -1034,6 +1034,7 @@ func fetchPost(app *App, w http.ResponseWriter, r *http.Request) error { p.Collection = &CollectionObj{Collection: *coll} po := p.ActivityObject() po.Context = []interface{}{activitystreams.Namespace} + setCacheControl(w, apCacheTime) return impart.RenderActivityJSON(w, po, http.StatusOK) } @@ -1359,6 +1360,7 @@ Are you sure it was ever here?`, p.extractData() ap := p.ActivityObject() ap.Context = []interface{}{activitystreams.Namespace} + setCacheControl(w, apCacheTime) return impart.RenderActivityJSON(w, ap, http.StatusOK) } else { p.extractData() From caca8f0ae26f586e4736eeefd01eef0e6ed80808 Mon Sep 17 00:00:00 2001 From: Rob Loranger Date: Thu, 3 Oct 2019 09:47:08 -0700 Subject: [PATCH 02/69] show timestamps in local date/locale this adds a helper script to rewrite all time elements with a proper datetime attribute into the users locale via the browser navigator.language. collection, collection-post and chorus-collection-post templates now include this script --- static/js/localdate.js | 9 +++++++++ templates/chorus-collection-post.tmpl | 1 + templates/collection-post.tmpl | 1 + templates/collection.tmpl | 1 + 4 files changed, 12 insertions(+) create mode 100644 static/js/localdate.js diff --git a/static/js/localdate.js b/static/js/localdate.js new file mode 100644 index 0000000..19fa502 --- /dev/null +++ b/static/js/localdate.js @@ -0,0 +1,9 @@ +function toLocalDate(el) { + var d = new Date(el.getAttribute("datetime")); + el.textContent = d.toLocaleDateString(navigator.language || "en-US", { year: 'numeric', month: 'long', day: 'numeric' }); +} + +var $dates = document.querySelectorAll("time"); +for (var i=0; i < $dates.length; i++) { + toLocalDate($dates[i]); +} diff --git a/templates/chorus-collection-post.tmpl b/templates/chorus-collection-post.tmpl index bab2e31..e60a435 100644 --- a/templates/chorus-collection-post.tmpl +++ b/templates/chorus-collection-post.tmpl @@ -90,6 +90,7 @@ article time.dt-published { {{range .Collection.ExternalScripts}}{{end}} {{if .Collection.Script}}{{end}} {{end}} + {{end}} {{if .Collection.Script}}{{end}} {{end}} + + {{end}} {{end}} + + {{end}}