Skip to content

Commit

Permalink
refactor: view models (resolve #634)
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Apr 1, 2024
1 parent 724265f commit 6149b0f
Show file tree
Hide file tree
Showing 18 changed files with 6,391 additions and 6,324 deletions.
12,489 changes: 6,252 additions & 6,237 deletions coverage/coverage.out

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions models/view/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package view

import (
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
)

type BasicViewModel interface {
SetError(string)
SetSuccess(string)
Expand All @@ -10,6 +15,29 @@ type Messages struct {
Error string
}

type SharedViewModel struct {
Messages
LeaderboardEnabled bool
InvitesEnabled bool
}

type SharedLoggedInViewModel struct {
SharedViewModel
User *models.User
ApiKey string
}

func NewSharedViewModel(c *conf.Config, messages *Messages) SharedViewModel {
vm := SharedViewModel{
LeaderboardEnabled: c.App.LeaderboardEnabled,
InvitesEnabled: c.Security.InviteCodes,
}
if messages != nil {
vm.Messages = *messages
}
return vm
}

func (m *Messages) SetError(message string) {
m.Error = message
}
Expand Down
9 changes: 4 additions & 5 deletions models/view/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ type Newsbox struct {
}

type HomeViewModel struct {
Messages
TotalHours int
TotalUsers int
LeaderboardEnabled bool
Newsbox *Newsbox
SharedViewModel
TotalHours int
TotalUsers int
Newsbox *Newsbox
}

func (s *HomeViewModel) WithSuccess(m string) *HomeViewModel {
Expand Down
2 changes: 1 addition & 1 deletion models/view/imprint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package view

type ImprintViewModel struct {
Messages
SharedViewModel
HtmlText string
}

Expand Down
19 changes: 8 additions & 11 deletions models/view/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import (
)

type LeaderboardViewModel struct {
Messages
User *models.User
By string
Key string
Items []*models.LeaderboardItemRanked
TopKeys []string
UserLanguages map[string][]string
IntervalLabel string
LeaderboardEnabled bool
ApiKey string
PageParams *utils.PageParams
SharedLoggedInViewModel
By string
Key string
Items []*models.LeaderboardItemRanked
TopKeys []string
UserLanguages map[string][]string
IntervalLabel string
PageParams *utils.PageParams
}

func (s *LeaderboardViewModel) WithSuccess(m string) *LeaderboardViewModel {
Expand Down
2 changes: 1 addition & 1 deletion models/view/login.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package view

type LoginViewModel struct {
Messages
SharedViewModel
TotalUsers int
AllowSignup bool
InviteCode string
Expand Down
11 changes: 4 additions & 7 deletions models/view/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (
)

type ProjectsViewModel struct {
Messages
User *models.User
Projects []*models.ProjectStats
LeaderboardEnabled bool
ApiKey string
PageParams *utils.PageParams
maxCount int64
SharedLoggedInViewModel
Projects []*models.ProjectStats
PageParams *utils.PageParams
maxCount int64
}

func (s *ProjectsViewModel) LangIcon(lang string) string {
Expand Down
6 changes: 1 addition & 5 deletions models/view/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

type SettingsViewModel struct {
Messages
User *models.User
SharedLoggedInViewModel
LanguageMappings []*models.LanguageMapping
Aliases []*SettingsVMCombinedAlias
Labels []*SettingsVMCombinedLabel
Expand All @@ -16,9 +15,6 @@ type SettingsViewModel struct {
DataRetentionMonths int
UserFirstData time.Time
SupportContact string
LeaderboardEnabled bool
ApiKey string
InvitesEnabled bool
InviteLink string
}

Expand Down
8 changes: 2 additions & 6 deletions models/view/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@ import (
)

type SummaryViewModel struct {
Messages
SharedLoggedInViewModel
*models.Summary
*models.SummaryParams
User *models.User
AvatarURL string
EditorColors map[string]string
LanguageColors map[string]string
OSColors map[string]string
ApiKey string
RawQuery string
UserFirstData time.Time
DataRetentionMonths int
LeaderboardEnabled bool
InvitesEnabled bool
}

func (s SummaryViewModel) UserDataExpiring() bool {
cfg := conf.Get()
return cfg.Subscriptions.Enabled &&
cfg.App.DataRetentionMonths > 0 &&
!s.UserFirstData.IsZero() &&
!s.User.HasActiveSubscription() &&
!s.SharedLoggedInViewModel.User.HasActiveSubscription() &&
time.Now().AddDate(0, -cfg.App.DataRetentionMonths, 0).After(s.UserFirstData)
}

Expand Down
8 changes: 4 additions & 4 deletions routes/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func (h *HomeHandler) buildViewModel(r *http.Request, w http.ResponseWriter) *vi
}

vm := &view.HomeViewModel{
TotalHours: totalHours,
TotalUsers: totalUsers,
Newsbox: &newsbox,
LeaderboardEnabled: h.config.App.LeaderboardEnabled,
SharedViewModel: view.NewSharedViewModel(h.config, nil),
TotalHours: totalHours,
TotalUsers: totalUsers,
Newsbox: &newsbox,
}
return routeutils.WithSessionMessages(vm, r, w)
}
4 changes: 3 additions & 1 deletion routes/imprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ func (h *ImprintHandler) GetImprint(w http.ResponseWriter, r *http.Request) {
}

func (h *ImprintHandler) buildViewModel(r *http.Request) *view.ImprintViewModel {
return &view.ImprintViewModel{}
return &view.ImprintViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, nil),
}
}
35 changes: 21 additions & 14 deletions routes/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (h *LeaderboardHandler) buildViewModel(r *http.Request, w http.ResponseWrit
if err != nil {
conf.Log().Request(r).Error("error while fetching general leaderboard items - %v", err)
return &view.LeaderboardViewModel{
Messages: view.Messages{Error: criticalError}, LeaderboardEnabled: true,
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, &view.Messages{Error: criticalError}),
},
}
}

Expand All @@ -93,7 +95,9 @@ func (h *LeaderboardHandler) buildViewModel(r *http.Request, w http.ResponseWrit
if err != nil {
conf.Log().Request(r).Error("error while fetching general leaderboard items - %v", err)
return &view.LeaderboardViewModel{
Messages: view.Messages{Error: criticalError}, LeaderboardEnabled: true,
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, &view.Messages{Error: criticalError}),
},
}
}

Expand Down Expand Up @@ -127,8 +131,9 @@ func (h *LeaderboardHandler) buildViewModel(r *http.Request, w http.ResponseWrit
}
} else {
return &view.LeaderboardViewModel{
Messages: view.Messages{Error: fmt.Sprintf("unsupported aggregation '%s'", byParam)},
LeaderboardEnabled: true,
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, &view.Messages{Error: fmt.Sprintf("unsupported aggregation '%s'", byParam)}),
},
}
}
}
Expand All @@ -141,16 +146,18 @@ func (h *LeaderboardHandler) buildViewModel(r *http.Request, w http.ResponseWrit
leaderboard.FilterEmpty()

vm := &view.LeaderboardViewModel{
User: user,
By: byParam,
Key: keyParam,
Items: leaderboard,
UserLanguages: userLanguages,
TopKeys: topKeys,
IntervalLabel: h.leaderboardService.GetDefaultScope().GetHumanReadable(),
LeaderboardEnabled: true,
ApiKey: apiKey,
PageParams: pageParams,
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, nil),
User: user,
ApiKey: apiKey,
},
By: byParam,
Key: keyParam,
Items: leaderboard,
UserLanguages: userLanguages,
TopKeys: topKeys,
IntervalLabel: h.leaderboardService.GetDefaultScope().GetHumanReadable(),
PageParams: pageParams,
}
return routeutils.WithSessionMessages(vm, r, w)
}
7 changes: 4 additions & 3 deletions routes/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,10 @@ func (h *LoginHandler) buildViewModel(r *http.Request, w http.ResponseWriter) *v
numUsers, _ := h.userSrvc.Count()

vm := &view.LoginViewModel{
TotalUsers: int(numUsers),
AllowSignup: h.config.IsDev() || h.config.Security.AllowSignup,
InviteCode: r.URL.Query().Get("invite"),
SharedViewModel: view.NewSharedViewModel(h.config, nil),
TotalUsers: int(numUsers),
AllowSignup: h.config.IsDev() || h.config.Security.AllowSignup,
InviteCode: r.URL.Query().Get("invite"),
}
return routeutils.WithSessionMessages(vm, r, w)
}
19 changes: 12 additions & 7 deletions routes/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,22 @@ func (h *ProjectsHandler) buildViewModel(r *http.Request, w http.ResponseWriter)
if err != nil {
conf.Log().Request(r).Error("error while fetching project stats for '%s' - %v", user.ID, err)
return &view.ProjectsViewModel{
Messages: view.Messages{Error: criticalError},
LeaderboardEnabled: h.config.App.LeaderboardEnabled,
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, &view.Messages{Error: criticalError}),
User: user,
ApiKey: user.ApiKey,
},
}
}

vm := &view.ProjectsViewModel{
User: user,
Projects: projects,
LeaderboardEnabled: h.config.App.LeaderboardEnabled,
ApiKey: user.ApiKey,
PageParams: pageParams,
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, nil),
User: user,
ApiKey: user.ApiKey,
},
Projects: projects,
PageParams: pageParams,
}
return routeutils.WithSessionMessages(vm, r, w)
}
33 changes: 26 additions & 7 deletions routes/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,13 @@ func (h *SettingsHandler) buildViewModel(r *http.Request, w http.ResponseWriter,
aliases, err := h.aliasSrvc.GetByUser(user.ID)
if err != nil {
conf.Log().Request(r).Error("error while building alias map - %v", err)
return &view.SettingsViewModel{Messages: view.Messages{Error: criticalError}, LeaderboardEnabled: h.config.App.LeaderboardEnabled}
return &view.SettingsViewModel{
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, &view.Messages{Error: criticalError}),
User: user,
ApiKey: user.ApiKey,
},
}
}
aliasMap := make(map[string][]*models.Alias)
for _, a := range aliases {
Expand Down Expand Up @@ -835,7 +841,13 @@ func (h *SettingsHandler) buildViewModel(r *http.Request, w http.ResponseWriter,
labelMap, err := h.projectLabelSrvc.GetByUserGroupedInverted(user.ID)
if err != nil {
conf.Log().Request(r).Error("error while building settings project label map - %v", err)
return &view.SettingsViewModel{Messages: view.Messages{Error: criticalError}, LeaderboardEnabled: h.config.App.LeaderboardEnabled}
return &view.SettingsViewModel{
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, &view.Messages{Error: criticalError}),
User: user,
ApiKey: user.ApiKey,
},
}
}

combinedLabels := make([]*view.SettingsVMCombinedLabel, 0)
Expand All @@ -857,7 +869,13 @@ func (h *SettingsHandler) buildViewModel(r *http.Request, w http.ResponseWriter,
projects, err := routeutils.GetEffectiveProjectsList(user, h.heartbeatSrvc, h.aliasSrvc)
if err != nil {
conf.Log().Request(r).Error("error while fetching projects - %v", err)
return &view.SettingsViewModel{Messages: view.Messages{Error: criticalError}, LeaderboardEnabled: h.config.App.LeaderboardEnabled}
return &view.SettingsViewModel{
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, &view.Messages{Error: criticalError}),
User: user,
ApiKey: user.ApiKey,
},
}
}

// subscriptions
Expand All @@ -878,19 +896,20 @@ func (h *SettingsHandler) buildViewModel(r *http.Request, w http.ResponseWriter,
inviteLink := condition.TernaryOperator[bool, string](inviteCode == "", "", fmt.Sprintf("%s/signup?invite=%s", h.config.Server.GetPublicUrl(), inviteCode))

vm := &view.SettingsViewModel{
User: user,
SharedLoggedInViewModel: view.SharedLoggedInViewModel{
SharedViewModel: view.NewSharedViewModel(h.config, nil),
User: user,
ApiKey: user.ApiKey,
},
LanguageMappings: mappings,
Aliases: combinedAliases,
Labels: combinedLabels,
Projects: projects,
ApiKey: user.ApiKey,
UserFirstData: firstData,
SubscriptionPrice: subscriptionPrice,
SupportContact: h.config.App.SupportContact,
DataRetentionMonths: h.config.App.DataRetentionMonths,
LeaderboardEnabled: h.config.App.LeaderboardEnabled,
InviteLink: inviteLink,
InvitesEnabled: h.config.Security.InviteCodes,
}
return routeutils.WithSessionMessages(vm, r, w)
}
Expand Down
Loading

0 comments on commit 6149b0f

Please sign in to comment.