From 13eb51913e24ed00e647c89be1a35e035192c194 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 19 Aug 2020 09:28:44 -0400 Subject: [PATCH 1/4] Support Web Monetization via backend attribute This supports a new `monetization_pointer` collection attribute. When present, we include the `monetization` meta tag on all collection pages. --- collections.go | 3 +++ database.go | 22 ++++++++++++++++++++++ posts.go | 2 ++ templates/chorus-collection-post.tmpl | 1 + templates/chorus-collection.tmpl | 1 + templates/collection-post.tmpl | 1 + templates/collection-tags.tmpl | 1 + templates/collection.tmpl | 1 + templates/include/post-render.tmpl | 6 ++++++ 9 files changed, 38 insertions(+) diff --git a/collections.go b/collections.go index edde677..ae75729 100644 --- a/collections.go +++ b/collections.go @@ -552,6 +552,7 @@ type CollectionPage struct { IsOwner bool CanPin bool Username string + Monetization string Collections *[]Collection PinnedPosts *[]PublicPost IsAdmin bool @@ -829,6 +830,7 @@ func handleViewCollection(app *App, w http.ResponseWriter, r *http.Request) erro // Add more data // TODO: fix this mess of collections inside collections displayPage.PinnedPosts, _ = app.db.GetPinnedPosts(coll.CollectionObj, isOwner) + displayPage.Monetization = app.db.GetCollectionAttribute(coll.ID, "monetization_pointer") collTmpl := "collection" if app.cfg.App.Chorus { @@ -947,6 +949,7 @@ func handleViewCollectionTag(app *App, w http.ResponseWriter, r *http.Request) e // Add more data // TODO: fix this mess of collections inside collections displayPage.PinnedPosts, _ = app.db.GetPinnedPosts(coll.CollectionObj, isOwner) + displayPage.Monetization = app.db.GetCollectionAttribute(coll.ID, "monetization_pointer") err = templates["collection-tags"].ExecuteTemplate(w, "collection-tags", displayPage) if err != nil { diff --git a/database.go b/database.go index c764340..a8feb8a 100644 --- a/database.go +++ b/database.go @@ -2162,6 +2162,28 @@ func (db *datastore) CollectionHasAttribute(id int64, attr string) bool { return true } +func (db *datastore) GetCollectionAttribute(id int64, attr string) string { + var v string + err := db.QueryRow("SELECT value FROM collectionattributes WHERE collection_id = ? AND attribute = ?", id, attr).Scan(&v) + switch { + case err == sql.ErrNoRows: + return "" + case err != nil: + log.Error("Couldn't SELECT value in getCollectionAttribute for attribute '%s': %v", attr, err) + return "" + } + return v +} + +func (db *datastore) SetCollectionAttribute(id int64, attr, v string) error { + _, err := db.Exec("INSERT INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?)", id, attr, v) + if err != nil { + log.Error("Unable to INSERT into collectionattributes: %v", err) + return err + } + return nil +} + // DeleteAccount will delete the entire account for userID func (db *datastore) DeleteAccount(userID int64) error { // Get all collections diff --git a/posts.go b/posts.go index 4c8c76e..8d60650 100644 --- a/posts.go +++ b/posts.go @@ -1476,6 +1476,7 @@ Are you sure it was ever here?`, IsOwner bool IsPinned bool IsCustomDomain bool + Monetization string PinnedPosts *[]PublicPost IsFound bool IsAdmin bool @@ -1493,6 +1494,7 @@ Are you sure it was ever here?`, tp.CanInvite = canUserInvite(app.cfg, tp.IsAdmin) tp.PinnedPosts, _ = app.db.GetPinnedPosts(coll, p.IsOwner) tp.IsPinned = len(*tp.PinnedPosts) > 0 && PostsContains(tp.PinnedPosts, p) + tp.Monetization = app.db.GetCollectionAttribute(coll.ID, "monetization_pointer") if !postFound { w.WriteHeader(http.StatusNotFound) diff --git a/templates/chorus-collection-post.tmpl b/templates/chorus-collection-post.tmpl index dcea457..22f2d8f 100644 --- a/templates/chorus-collection-post.tmpl +++ b/templates/chorus-collection-post.tmpl @@ -29,6 +29,7 @@ {{range .Images}}{{else}}{{end}} + {{template "collection-meta" .}} {{if .Collection.StyleSheet}}{{end}} {{end}} {{end}} {{if .Collection.RenderMathJax}} diff --git a/templates/collection-tags.tmpl b/templates/collection-tags.tmpl index b7c92c8..e2f8962 100644 --- a/templates/collection-tags.tmpl +++ b/templates/collection-tags.tmpl @@ -29,6 +29,7 @@ + {{template "collection-meta" .}} {{if .Collection.StyleSheet}}{{end}} {{if .Collection.RenderMathJax}} diff --git a/templates/collection.tmpl b/templates/collection.tmpl index d39c58c..42664e7 100644 --- a/templates/collection.tmpl +++ b/templates/collection.tmpl @@ -27,6 +27,7 @@ + {{template "collection-meta" .}} {{if .StyleSheet}}{{end}} {{if .RenderMathJax}} diff --git a/templates/include/post-render.tmpl b/templates/include/post-render.tmpl index 81fd33e..c4ed082 100644 --- a/templates/include/post-render.tmpl +++ b/templates/include/post-render.tmpl @@ -1,4 +1,10 @@ +{{define "collection-meta"}} + {{if .Monetization -}} + + {{- end}} +{{end}} + {{define "highlighting"}}