From 76c5baef6ae8f5520ab40fef48532b6b5759384f Mon Sep 17 00:00:00 2001
From: r <r@freesoftwareextremist.com>
Date: Sat, 3 Apr 2021 09:22:43 +0000
Subject: Add option for user CSS

---
 model/settings.go       | 2 ++
 renderer/model.go       | 1 +
 service/service.go      | 4 ++++
 service/transport.go    | 2 ++
 templates/header.tmpl   | 3 +++
 templates/settings.tmpl | 6 ++++++
 6 files changed, 18 insertions(+)

diff --git a/model/settings.go b/model/settings.go
index 6d17901..c4e8aec 100644
--- a/model/settings.go
+++ b/model/settings.go
@@ -11,6 +11,7 @@ type Settings struct {
 	FluorideMode         bool   `json:"fluoride_mode"`
 	DarkMode             bool   `json:"dark_mode"`
 	AntiDopamineMode     bool   `json:"anti_dopamine_mode"`
+	CSS                  string `json:"css"`
 }
 
 func NewSettings() *Settings {
@@ -25,5 +26,6 @@ func NewSettings() *Settings {
 		FluorideMode:         false,
 		DarkMode:             false,
 		AntiDopamineMode:     false,
+		CSS:                  "",
 	}
 }
diff --git a/renderer/model.go b/renderer/model.go
index d6cd6f3..2e6c40f 100644
--- a/renderer/model.go
+++ b/renderer/model.go
@@ -14,6 +14,7 @@ type Context struct {
 	CSRFToken        string
 	UserID           string
 	AntiDopamineMode bool
+	UserCSS          string
 	Referrer         string
 }
 
diff --git a/service/service.go b/service/service.go
index a29345e..5d80c28 100644
--- a/service/service.go
+++ b/service/service.go
@@ -64,6 +64,7 @@ func (s *service) authenticate(c *client, sid string, csrf string, ref string, t
 			CSRFToken:        c.s.CSRFToken,
 			UserID:           c.s.UserID,
 			AntiDopamineMode: sett.AntiDopamineMode,
+			UserCSS:          sett.CSS,
 			Referrer:         ref,
 		}
 	}()
@@ -888,6 +889,9 @@ func (s *service) SaveSettings(c *client, settings *model.Settings) (err error)
 	default:
 		return errInvalidArgument
 	}
+	if len(settings.CSS) > 1<<20 {
+		return errInvalidArgument
+	}
 	sess, err := s.sessionRepo.Get(c.s.ID)
 	if err != nil {
 		return
diff --git a/service/transport.go b/service/transport.go
index 50bf9be..f448cc3 100644
--- a/service/transport.go
+++ b/service/transport.go
@@ -463,6 +463,7 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler {
 		fluorideMode := c.r.FormValue("fluoride_mode") == "true"
 		darkMode := c.r.FormValue("dark_mode") == "true"
 		antiDopamineMode := c.r.FormValue("anti_dopamine_mode") == "true"
+		css := c.r.FormValue("css")
 
 		settings := &model.Settings{
 			DefaultVisibility:    visibility,
@@ -475,6 +476,7 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler {
 			FluorideMode:         fluorideMode,
 			DarkMode:             darkMode,
 			AntiDopamineMode:     antiDopamineMode,
+			CSS:                  css,
 		}
 
 		err := s.SaveSettings(c, settings)
diff --git a/templates/header.tmpl b/templates/header.tmpl
index df2b6af..1abb6dd 100644
--- a/templates/header.tmpl
+++ b/templates/header.tmpl
@@ -25,6 +25,9 @@
 	{{if $.Ctx.FluorideMode}}
 	<script src="/static/fluoride.js"></script>
 	{{end}}
+	{{if $.Ctx.UserCSS}}
+	<style>{{$.Ctx.UserCSS}}</style>
+	{{end}}
 </head>
 <body {{if $.Ctx.DarkMode}}class="dark"{{end}}>
 {{end}}
diff --git a/templates/settings.tmpl b/templates/settings.tmpl
index 4a72b6a..8aee4e2 100644
--- a/templates/settings.tmpl
+++ b/templates/settings.tmpl
@@ -63,6 +63,12 @@
 		<input id="dark-mode" name="dark_mode" type="checkbox" value="true" {{if .Settings.DarkMode}}checked{{end}}>
 		<label for="dark-mode"> Use dark theme </label>
 	</div>
+	<div class="settings-form-field">
+		<label for="css"> Custom CSS: </label>
+	</div>
+	<div>
+		<textarea id="css" name="css" cols="80" rows="8">{{.Settings.CSS}}</textarea>
+	</div>
 	<button type="submit"> Save </button>
 </form>
 
-- 
cgit v1.2.3