aboutsummaryrefslogtreecommitdiff
path: root/processors
diff options
context:
space:
mode:
authorBobby <[email protected]>2025-07-12 20:01:55 +0530
committerBobby <[email protected]>2025-07-12 20:01:55 +0530
commitc97589bd1ae1d2366c4fb070264d26c8b8d8b7c5 (patch)
tree7940dede46aa6571d892065ed3f95b676ed20610 /processors
parenta896b3fe27579993c0cb761832242d806d3e9438 (diff)
downloadimageboard-c97589bd1ae1d2366c4fb070264d26c8b8d8b7c5.tar.xz
imageboard-c97589bd1ae1d2366c4fb070264d26c8b8d8b7c5.zip
add cookie based preferences
Diffstat (limited to 'processors')
-rw-r--r--processors/preferences.go76
-rw-r--r--processors/processors.go1
2 files changed, 77 insertions, 0 deletions
diff --git a/processors/preferences.go b/processors/preferences.go
new file mode 100644
index 0000000..a96d1cb
--- /dev/null
+++ b/processors/preferences.go
@@ -0,0 +1,76 @@
+package processors
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+type SitePreferences struct {
+ SidebarWidth string `json:"sidebar_width"`
+ MainContentWidth string `json:"main_content_width"`
+ H1FontSize string `json:"h1_font_size"`
+ BodyFontSize string `json:"body_font_size"`
+ SmallFontSize string `json:"small_font_size"`
+ PostsPerPage int `json:"posts_per_page"`
+}
+
+func PreferencesContextProcessor(context *fiber.Ctx) error {
+ defaultPreferences := SitePreferences{
+ SidebarWidth: "180px",
+ MainContentWidth: "1200px",
+ H1FontSize: "16px",
+ BodyFontSize: "13px",
+ SmallFontSize: "11px",
+ PostsPerPage: 42,
+ }
+
+ preferences := defaultPreferences
+
+ preferencesCookie := context.Cookies("preferences")
+ if preferencesCookie != "" {
+ _ = json.Unmarshal([]byte(preferencesCookie), &preferences)
+ }
+
+ bytes, err := json.Marshal(preferences)
+ if err == nil {
+ context.Cookie(&fiber.Cookie{
+ Name: "preferences",
+ Value: string(bytes),
+ Path: "/",
+ SameSite: fiber.CookieSameSiteLaxMode,
+ })
+ }
+
+ context.Locals("Preferences", preferences)
+ context.Locals("PreferencesCSS", preferencesToCSS(preferences))
+ return context.Next()
+}
+
+func preferencesToCSS(preferences SitePreferences) string {
+ return fmt.Sprintf(`
+ <style>
+ main {
+ width: %s;
+ }
+ body {
+ font-size: %s;
+ }
+ h1 {
+ font-size: %s;
+ }
+ small {
+ font-size: %s;
+ }
+ .sidebar {
+ width: %s;
+ }
+ </style>`,
+ preferences.MainContentWidth,
+ preferences.BodyFontSize,
+ preferences.H1FontSize,
+ preferences.SmallFontSize,
+ preferences.SidebarWidth,
+ )
+}
diff --git a/processors/processors.go b/processors/processors.go
index e060abe..bbd23fe 100644
--- a/processors/processors.go
+++ b/processors/processors.go
@@ -6,4 +6,5 @@ func Initialize(app *fiber.App) {
app.Use(RequestContextProcessor)
app.Use(MetaContextProcessor)
app.Use(SidebarContextProcessor)
+ app.Use(PreferencesContextProcessor)
}