From 23759d526a4245b89a28ae06e51cec16e4fe448e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Thu, 6 Oct 2022 14:47:22 +0200 Subject: [PATCH] feat: settings option to opt in to leaderboards --- repositories/user.go | 1 + routes/settings.go | 22 ++++++++++++++++++++++ views/settings.tpl.html | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/repositories/user.go b/repositories/user.go index 3c36e12..4f1781b 100644 --- a/repositories/user.go +++ b/repositories/user.go @@ -175,6 +175,7 @@ func (r *UserRepository) Update(user *models.User) (*models.User, error) { "reset_token": user.ResetToken, "location": user.Location, "reports_weekly": user.ReportsWeekly, + "public_leaderboard": user.PublicLeaderboard, } result := r.db.Model(user).Updates(updateMap) diff --git a/routes/settings.go b/routes/settings.go index 7ea56b8..2752c3f 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -146,6 +146,8 @@ func (h *SettingsHandler) dispatchAction(action string) action { return h.actionAddLanguageMapping case "update_sharing": return h.actionUpdateSharing + case "update_leaderboard": + return h.actionUpdateLeaderboard case "toggle_wakatime": return h.actionSetWakatimeApiKey case "import_wakatime": @@ -252,6 +254,26 @@ func (h *SettingsHandler) actionResetApiKey(w http.ResponseWriter, r *http.Reque return http.StatusOK, msg, "" } +func (h *SettingsHandler) actionUpdateLeaderboard(w http.ResponseWriter, r *http.Request) (int, string, string) { + if h.config.IsDev() { + loadTemplates() + } + + var err error + user := middlewares.GetPrincipal(r) + defer h.userSrvc.FlushCache() + + user.PublicLeaderboard, err = strconv.ParseBool(r.PostFormValue("enable_leaderboard")) + + if err != nil { + return http.StatusBadRequest, "", "invalid input" + } + if _, err := h.userSrvc.Update(user); err != nil { + return http.StatusInternalServerError, "", "internal sever error" + } + return http.StatusOK, "settings updated", "" +} + func (h *SettingsHandler) actionUpdateSharing(w http.ResponseWriter, r *http.Request) (int, string, string) { if h.config.IsDev() { loadTemplates() diff --git a/views/settings.tpl.html b/views/settings.tpl.html index a6da053..d548893 100644 --- a/views/settings.tpl.html +++ b/views/settings.tpl.html @@ -380,6 +380,46 @@
+ +
+
+
+ Public Leaderboard +

+ Opt in to get listed in the public leaderboard. It shows aggregated statistics from the past 7 days of your coding. +

+
+ +
+ + +
+
+ +
+
+ +
+
+
+
+ +
+ +
+
+ +
+
+
+