From 1a80cd3c02ed53f6509ebf7012fc445e5122b628 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 7 Aug 2019 09:00:16 -0400 Subject: [PATCH] Add site-wide navigation on colls when chorus = true This adds a new config value: `chorus` that signifies an instance is more about the Reader view than individual blogs / writers. When enabled, user navigation will show on all pages, including About, Reader, and Privacy (ref T680). It also uses different collection templates that keep the instance-wide navigation at the top of the page, instead of the author's name -- again, branded more for the collective than the individual. Ref T681 --- account.go | 9 +- app.go | 2 + collections.go | 10 +- config/config.go | 1 + page/page.go | 2 + posts.go | 11 +- read.go | 17 +- templates.go | 7 +- templates/base.tmpl | 26 ++- templates/chorus-collection-post.tmpl | 150 +++++++++++++++++ templates/chorus-collection.tmpl | 230 ++++++++++++++++++++++++++ templates/read.tmpl | 5 + templates/user/include/header.tmpl | 10 +- 13 files changed, 464 insertions(+), 16 deletions(-) create mode 100644 templates/chorus-collection-post.tmpl create mode 100644 templates/chorus-collection.tmpl diff --git a/account.go b/account.go index d8ea0df..66a3fa0 100644 --- a/account.go +++ b/account.go @@ -21,6 +21,7 @@ import ( "github.com/writeas/web-core/data" "github.com/writeas/web-core/log" "github.com/writeas/writefreely/author" + "github.com/writeas/writefreely/config" "github.com/writeas/writefreely/page" "html/template" "net/http" @@ -58,11 +59,15 @@ func NewUserPage(app *App, r *http.Request, u *User, title string, flashes []str up.Flashes = flashes up.Path = r.URL.Path up.IsAdmin = u.IsAdmin() - up.CanInvite = app.cfg.App.UserInvites != "" && - (up.IsAdmin || app.cfg.App.UserInvites != "admin") + up.CanInvite = canUserInvite(app.cfg, up.IsAdmin) return up } +func canUserInvite(cfg *config.Config, isAdmin bool) bool { + return cfg.App.UserInvites != "" && + (isAdmin || cfg.App.UserInvites != "admin") +} + func (up *UserPage) SetMessaging(u *User) { //up.NeedsAuth = app.db.DoesUserNeedAuth(u.ID) } diff --git a/app.go b/app.go index 6d6a689..68ba7c7 100644 --- a/app.go +++ b/app.go @@ -317,6 +317,8 @@ func pageForReq(app *App, r *http.Request) page.StaticPage { u = getUserSession(app, r) if u != nil { p.Username = u.Username + p.IsAdmin = u != nil && u.IsAdmin() + p.CanInvite = canUserInvite(app.cfg, p.IsAdmin) } } p.CanViewReader = !app.cfg.App.Private || u != nil diff --git a/collections.go b/collections.go index 1a8ceca..0d2549a 100644 --- a/collections.go +++ b/collections.go @@ -525,6 +525,8 @@ type CollectionPage struct { Username string Collections *[]Collection PinnedPosts *[]PublicPost + IsAdmin bool + CanInvite bool } func (c *CollectionObj) ScriptDisplay() template.JS { @@ -737,6 +739,8 @@ func handleViewCollection(app *App, w http.ResponseWriter, r *http.Request) erro IsCustomDomain: cr.isCustomDomain, IsWelcome: r.FormValue("greeting") != "", } + displayPage.IsAdmin = u != nil && u.IsAdmin() + displayPage.CanInvite = canUserInvite(app.cfg, displayPage.IsAdmin) var owner *User if u != nil { displayPage.Username = u.Username @@ -768,7 +772,11 @@ func handleViewCollection(app *App, w http.ResponseWriter, r *http.Request) erro // TODO: fix this mess of collections inside collections displayPage.PinnedPosts, _ = app.db.GetPinnedPosts(coll.CollectionObj) - err = templates["collection"].ExecuteTemplate(w, "collection", displayPage) + collTmpl := "collection" + if app.cfg.App.Chorus { + collTmpl = "chorus-collection" + } + err = templates[collTmpl].ExecuteTemplate(w, "collection", displayPage) if err != nil { log.Error("Unable to render collection index: %v", err) } diff --git a/config/config.go b/config/config.go index d026a74..9a1db52 100644 --- a/config/config.go +++ b/config/config.go @@ -69,6 +69,7 @@ type ( WebFonts bool `ini:"webfonts"` Landing string `ini:"landing"` SimpleNav bool `ini:"simple_nav"` + Chorus bool `ini:"chorus"` // Users SingleUser bool `ini:"single_user"` diff --git a/page/page.go b/page/page.go index 2af5322..15f09a9 100644 --- a/page/page.go +++ b/page/page.go @@ -28,6 +28,8 @@ type StaticPage struct { Values map[string]string Flashes []string CanViewReader bool + IsAdmin bool + CanInvite bool } // SanitizeHost alters the StaticPage to contain a real hostname. This is diff --git a/posts.go b/posts.go index 07902ce..b1ce20f 100644 --- a/posts.go +++ b/posts.go @@ -1345,15 +1345,24 @@ func viewCollectionPost(app *App, w http.ResponseWriter, r *http.Request) error IsPinned bool IsCustomDomain bool PinnedPosts *[]PublicPost + IsAdmin bool + CanInvite bool }{ PublicPost: p, StaticPage: pageForReq(app, r), IsOwner: cr.isCollOwner, IsCustomDomain: cr.isCustomDomain, } + tp.IsAdmin = u != nil && u.IsAdmin() + tp.CanInvite = canUserInvite(app.cfg, tp.IsAdmin) tp.PinnedPosts, _ = app.db.GetPinnedPosts(coll) tp.IsPinned = len(*tp.PinnedPosts) > 0 && PostsContains(tp.PinnedPosts, p) - if err := templates["collection-post"].ExecuteTemplate(w, "post", tp); err != nil { + + postTmpl := "collection-post" + if app.cfg.App.Chorus { + postTmpl = "chorus-collection-post" + } + if err := templates[postTmpl].ExecuteTemplate(w, "post", tp); err != nil { log.Error("Error in collection-post template: %v", err) } } diff --git a/read.go b/read.go index 683f124..fa350fa 100644 --- a/read.go +++ b/read.go @@ -48,6 +48,8 @@ type readPublication struct { CurrentPage int TotalPages int SelTopic string + IsAdmin bool + CanInvite bool } func initLocalTimeline(app *App) { @@ -198,11 +200,16 @@ func showLocalTimeline(app *App, w http.ResponseWriter, r *http.Request, page in } d := &readPublication{ - pageForReq(app, r), - &posts, - page, - ttlPages, - tag, + StaticPage: pageForReq(app, r), + Posts: &posts, + CurrentPage: page, + TotalPages: ttlPages, + SelTopic: tag, + } + if app.cfg.App.Chorus { + u := getUserSession(app, r) + d.IsAdmin = u != nil && u.IsAdmin() + d.CanInvite = canUserInvite(app.cfg, d.IsAdmin) } err := templates["read"].ExecuteTemplate(w, "base", d) diff --git a/templates.go b/templates.go index 7a45c45..6e9a008 100644 --- a/templates.go +++ b/templates.go @@ -64,11 +64,14 @@ func initTemplate(parentDir, name string) { filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"), filepath.Join(parentDir, templatesDir, "base.tmpl"), } - if name == "collection" || name == "collection-tags" { + if name == "collection" || name == "collection-tags" || name == "chorus-collection" { // These pages list out collection posts, so we also parse templatesDir + "include/posts.tmpl" files = append(files, filepath.Join(parentDir, templatesDir, "include", "posts.tmpl")) } - if name == "collection" || name == "collection-tags" || name == "collection-post" || name == "post" { + if name == "chorus-collection" || name == "chorus-collection-post" { + files = append(files, filepath.Join(parentDir, templatesDir, "user", "include", "header.tmpl")) + } + if name == "collection" || name == "collection-tags" || name == "collection-post" || name == "post" || name == "chorus-collection" || name == "chorus-collection-post" { files = append(files, filepath.Join(parentDir, templatesDir, "include", "post-render.tmpl")) } templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...)) diff --git a/templates/base.tmpl b/templates/base.tmpl index c1f17ad..41b0f2a 100644 --- a/templates/base.tmpl +++ b/templates/base.tmpl @@ -13,14 +13,38 @@
-

{{.SiteName}}

+ {{ if .SimpleNav }} {{end}}
diff --git a/templates/chorus-collection-post.tmpl b/templates/chorus-collection-post.tmpl new file mode 100644 index 0000000..392ebe6 --- /dev/null +++ b/templates/chorus-collection-post.tmpl @@ -0,0 +1,150 @@ +{{define "post"}} + + + + + {{.PlainDisplayTitle}} {{localhtml "title dash" .Language.String}} {{.Collection.DisplayTitle}} + + + + + + + + + {{if gt .Views 1}} + {{end}} + + + + + + + {{if gt (len .Images) 0}}{{else}}{{end}} + + + + + + + {{range .Images}}{{else}}{{end}} + + {{if .Collection.StyleSheet}}{{end}} + + + {{if .Collection.RenderMathJax}} + + {{template "mathjax" . }} + {{end}} + + + {{template "highlighting" .}} + + + + +
+ + {{template "user-navigation" .}} + +
{{if .IsScheduled}}

Scheduled

{{end}}{{if .Title.String}}

{{.FormattedDisplayTitle}}

{{end}}{{/* TODO: check format: if .Collection.Format.ShowDates*/}}
{{.HTMLContent}}
+ + {{ if .Collection.ShowFooterBranding }} + + {{ end }} + + + {{if .Collection.CanShowScript}} + {{range .Collection.ExternalScripts}}{{end}} + {{if .Collection.Script}}{{end}} + {{end}} + +{{end}} diff --git a/templates/chorus-collection.tmpl b/templates/chorus-collection.tmpl new file mode 100644 index 0000000..06695b1 --- /dev/null +++ b/templates/chorus-collection.tmpl @@ -0,0 +1,230 @@ +{{define "collection"}} + + + + + {{.DisplayTitle}}{{if not .SingleUser}} — {{.SiteName}}{{end}} + + + + + {{if gt .CurrentPage 1}}{{end}} + {{if lt .CurrentPage .TotalPages}}{{end}} + {{if not .IsPrivate}}{{end}} + + + + + + + + + + + + + + + + + {{if .StyleSheet}}{{end}} + + + {{if .RenderMathJax}} + + {{template "mathjax" .}} + {{end}} + + + {{template "highlighting" . }} + + + + {{template "user-navigation" .}} + +
+

{{.DisplayTitle}}

+ {{if .Description}}

{{.Description}}

{{end}} + {{/*if not .Public/*}} + + {{/*end*/}} + {{if .PinnedPosts}} + {{end}} +
+ + {{if .Posts}}
{{else}}
{{end}} + + {{if .IsWelcome}} +
+

Welcome, {{.Username}}!

+

This is your new blog.

+

Start writing, or customize your blog.

+

Check out our writing guide to see what else you can do, and get in touch anytime with questions or feedback.

+
+ {{end}} + + {{template "posts" .}} + + {{if gt .TotalPages 1}}{{end}} + + {{if .Posts}}
{{else}}{{end}} + + {{if .ShowFooterBranding }} + + {{ end }} + + + {{if .CanShowScript}} + {{range .ExternalScripts}}{{end}} + {{if .Script}}{{end}} + {{end}} + + + +{{end}} diff --git a/templates/read.tmpl b/templates/read.tmpl index e7527bc..28d2fd1 100644 --- a/templates/read.tmpl +++ b/templates/read.tmpl @@ -65,11 +65,16 @@ } body#collection header nav { display: inline !important; + } + body#collection header nav:not(#full-nav):not(#user-nav) { margin: 0 0 0 1em !important; } header nav#user-nav { margin-left: 0 !important; } + body#collection header nav.tabs a:first-child { + margin-left: 1em; + } {{end}} {{define "body-attrs"}}id="collection"{{end}} diff --git a/templates/user/include/header.tmpl b/templates/user/include/header.tmpl index 5f29646..1ebc6ac 100644 --- a/templates/user/include/header.tmpl +++ b/templates/user/include/header.tmpl @@ -1,5 +1,5 @@ {{define "user-navigation"}} - +
{{if .SingleUser}}