From 43bc4f2e5b4cbceff04f807a550d7fe802580f35 Mon Sep 17 00:00:00 2001 From: Kieran Klukas <92754843+kcoderhtml@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:46:00 -0500 Subject: [PATCH] feat: add redacted user information retrieval for leaderboard --- models/compat/wakatime/v1/user.go | 25 +++++++++++++++++++++++++ routes/compat/wakatime/v1/leaders.go | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/models/compat/wakatime/v1/user.go b/models/compat/wakatime/v1/user.go index cd303612..62fc59dc 100644 --- a/models/compat/wakatime/v1/user.go +++ b/models/compat/wakatime/v1/user.go @@ -48,6 +48,31 @@ func NewFromUser(user *models.User) *User { avatarURL = fmt.Sprintf("%s%s/%s", cfg.Server.GetPublicUrl(), cfg.Server.BasePath, avatarURL) } + return &User{ + ID: user.ID, + DisplayName: user.ID, + Email: user.Email, + TimeZone: tz, + Username: user.ID, + CreatedAt: user.CreatedAt, + ModifiedAt: user.CreatedAt, + Photo: avatarURL, + } +} + +func RedactedFromUser(user *models.User) *User { + cfg := config.Get() + tz, _ := time.Now().Zone() + if user.Location != "" { + tz = user.Location + } + + avatarURL := user.AvatarURL(cfg.App.AvatarURLTemplate) + + if !strings.HasPrefix(avatarURL, "http") { + avatarURL = fmt.Sprintf("%s%s/%s", cfg.Server.GetPublicUrl(), cfg.Server.BasePath, avatarURL) + } + return &User{ DisplayName: user.ID, TimeZone: tz, diff --git a/routes/compat/wakatime/v1/leaders.go b/routes/compat/wakatime/v1/leaders.go index 61120c56..065c809b 100644 --- a/routes/compat/wakatime/v1/leaders.go +++ b/routes/compat/wakatime/v1/leaders.go @@ -163,7 +163,7 @@ func (h *LeadersHandler) buildViewModel(globalLeaderboard, languageLeaderboard m } }), }, - User: v1.NewFromUser(entry.User), + User: v1.RedactedFromUser(entry.User), }) }