From 660a09475ea21b37c9396cdcfd5eb8fa929b0b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Thu, 17 Feb 2022 09:53:37 +0100 Subject: [PATCH] chore: include avatar rendering into wakapi itself --- config.default.yml | 3 ++- config/config.go | 2 +- go.mod | 1 + go.sum | 2 ++ main.go | 2 ++ routes/api/avatar.go | 35 +++++++++++++++++++++++++++++++++++ routes/login.go | 3 +-- version.txt | 2 +- views/login.tpl.html | 6 +++--- views/signup.tpl.html | 2 +- 10 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 routes/api/avatar.go diff --git a/config.default.yml b/config.default.yml index 4b60091..a6fcaa7 100644 --- a/config.default.yml +++ b/config.default.yml @@ -23,7 +23,8 @@ app: # url template for user avatar images (to be used with services like gravatar or dicebear) # available variable placeholders are: username, username_hash, email, email_hash - avatar_url_template: https://avatars.dicebear.com/api/pixel-art-neutral/{username_hash}.svg + # defaults to wakapi's internal avatar rendering powered by https://codeberg.org/Codeberg/avatars + avatar_url_template: api/avatar/{username_hash}.svg db: host: # leave blank when using sqlite3 diff --git a/config/config.go b/config/config.go index 07dec4d..a4b5680 100644 --- a/config/config.go +++ b/config/config.go @@ -69,7 +69,7 @@ type appConfig struct { ImportBatchSize int `yaml:"import_batch_size" default:"50" env:"WAKAPI_IMPORT_BATCH_SIZE"` InactiveDays int `yaml:"inactive_days" default:"7" env:"WAKAPI_INACTIVE_DAYS"` CountCacheTTLMin int `yaml:"count_cache_ttl_min" default:"30" env:"WAKAPI_COUNT_CACHE_TTL_MIN"` - AvatarURLTemplate string `yaml:"avatar_url_template" default:"https://avatars.dicebear.com/api/pixel-art-neutral/{username_hash}.svg"` + AvatarURLTemplate string `yaml:"avatar_url_template" default:"api/avatar/{username_hash}.svg"` CustomLanguages map[string]string `yaml:"custom_languages"` Colors map[string]map[string]string `yaml:"-"` } diff --git a/go.mod b/go.mod index 80c78c5..f9c6624 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/muety/wakapi go 1.16 require ( + codeberg.org/Codeberg/avatars v0.0.0-20211228163022-8da63012fe69 github.com/BurntSushi/toml v0.4.1 // indirect github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac diff --git a/go.sum b/go.sum index 9b16a63..5d316f7 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +codeberg.org/Codeberg/avatars v0.0.0-20211228163022-8da63012fe69 h1:/XvI42KX57UTpeIOIt7IfM+pmEFTL8FGtiIUGcGDOIU= +codeberg.org/Codeberg/avatars v0.0.0-20211228163022-8da63012fe69/go.mod h1:ML/htpPRb3+owhkm4+qG2ZrXnk5WXaQLASOZ5GLCPi8= github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw= diff --git a/main.go b/main.go index c0c56c3..96f7cbd 100644 --- a/main.go +++ b/main.go @@ -180,6 +180,7 @@ func main() { summaryApiHandler := api.NewSummaryApiHandler(userService, summaryService) metricsHandler := api.NewMetricsHandler(userService, summaryService, heartbeatService, keyValueService) diagnosticsHandler := api.NewDiagnosticsApiHandler(userService, diagnosticsService) + avatarHandler := api.NewAvatarHandler() // Compat Handlers wakatimeV1StatusBarHandler := wtV1Routes.NewStatusBarHandler(userService, summaryService) @@ -237,6 +238,7 @@ func main() { heartbeatApiHandler.RegisterRoutes(apiRouter) metricsHandler.RegisterRoutes(apiRouter) diagnosticsHandler.RegisterRoutes(apiRouter) + avatarHandler.RegisterRoutes(apiRouter) wakatimeV1StatusBarHandler.RegisterRoutes(apiRouter) wakatimeV1AllHandler.RegisterRoutes(apiRouter) wakatimeV1SummariesHandler.RegisterRoutes(apiRouter) diff --git a/routes/api/avatar.go b/routes/api/avatar.go new file mode 100644 index 0000000..f5e3818 --- /dev/null +++ b/routes/api/avatar.go @@ -0,0 +1,35 @@ +package api + +import ( + "codeberg.org/Codeberg/avatars" + "github.com/gorilla/mux" + conf "github.com/muety/wakapi/config" + "net/http" +) + +type AvatarHandler struct { + config *conf.Config +} + +func NewAvatarHandler() *AvatarHandler { + return &AvatarHandler{config: conf.Get()} +} + +func (h *AvatarHandler) RegisterRoutes(router *mux.Router) { + r := router.PathPrefix("/avatar/{hash}.svg").Subrouter() + r.Path("").Methods(http.MethodGet).HandlerFunc(h.Get) +} + +func (h *AvatarHandler) Get(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + if vars["hash"] == "" { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte{}) + return + } + + w.Header().Set("Content-Type", "image/svg+xml") + w.WriteHeader(http.StatusOK) + w.Write([]byte(avatars.MakeMaleAvatar(vars["hash"]))) +} diff --git a/routes/login.go b/routes/login.go index 4d159f6..0542321 100644 --- a/routes/login.go +++ b/routes/login.go @@ -299,12 +299,11 @@ func (h *LoginHandler) PostResetPassword(w http.ResponseWriter, r *http.Request) func (h *LoginHandler) buildViewModel(r *http.Request) *view.LoginViewModel { numUsers, _ := h.userSrvc.Count() - allowSignup := h.config.Security.AllowSignup return &view.LoginViewModel{ Success: r.URL.Query().Get("success"), Error: r.URL.Query().Get("error"), TotalUsers: int(numUsers), - AllowSignup: allowSignup, + AllowSignup: h.config.IsDev() || h.config.Security.AllowSignup, } } diff --git a/version.txt b/version.txt index e3a4f19..fae692e 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2.2.0 \ No newline at end of file +2.2.1 \ No newline at end of file diff --git a/views/login.tpl.html b/views/login.tpl.html index 4d68ecf..fe3938d 100644 --- a/views/login.tpl.html +++ b/views/login.tpl.html @@ -31,13 +31,13 @@ Forgot password?
- {{ if eq .AllowSignup true }} + {{ if .AllowSignup }} {{ else }} - - + + {{ end }} diff --git a/views/signup.tpl.html b/views/signup.tpl.html index bcd4e74..6c46f31 100644 --- a/views/signup.tpl.html +++ b/views/signup.tpl.html @@ -83,7 +83,7 @@ - {{ if eq .AllowSignup true }} + {{ if .AllowSignup }}