This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
225 lines (178 loc) · 6.09 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package web
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/eveisesi/skillz"
"github.com/eveisesi/skillz/internal/user/v2"
"github.com/gertd/go-pluralize"
"github.com/gobuffalo/buffalo"
"github.com/pkg/errors"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
)
var printer = message.NewPrinter(language.English)
func (s *Service) userPageMeta(ctx context.Context, user *skillz.User) (string, *Meta) {
var meta = new(Meta)
if user.Character != nil {
name := pluralize.NewClient().Plural(user.Character.Name)
meta.Title = fmt.Sprintf("%s Skillboard %s", name, titleSuffix)
}
if user.Character != nil && user.Meta != nil && user.QueueSummary != nil {
meta.Description = fmt.Sprintf(
"%s is a character in the MMORPG Eve Online. They have amassed %s skillpoints and they're currently training %d skills in their skill queue. Click to learn more about this character",
user.Character.Name,
printer.Sprintf("%v", number.Decimal(user.Meta.TotalSP)),
len(user.QueueSummary.Queue),
)
}
return "meta", meta
}
func (s *Service) userSettingsMeta(ctx context.Context, user *skillz.User) (string, string) {
name := pluralize.NewClient().Plural(user.Character.Name)
title := fmt.Sprintf("%s Skillboard Settings %s", name, titleSuffix)
return "title", title
}
var userSettingsPageTitle = func(name string) (string, string) {
name = pluralize.NewClient().Plural(name)
return "title", fmt.Sprintf("%s Settings %s", name, titleSuffix)
}
func (s *Service) userHandler(c buffalo.Context) error {
var ctx = c.Request().Context()
userID := c.Param("userID")
u, err := s.user.User(ctx, userID)
if err != nil && !errors.Is(err, user.ErrUserNotFound) {
return c.Error(http.StatusInternalServerError, err)
}
if errors.Is(err, user.ErrUserNotFound) {
c.Flash().Add("danger", err.Error())
return c.Redirect(http.StatusFound, "rootPath()")
}
settings := u.Settings
if settings != nil {
if settings.Visibility == skillz.VisibilityPrivate {
sessionUser := c.Data()[keyAuthenticatedUser]
if sessionUser == nil {
s.flashDanger(c, "User Not Found")
return c.Redirect(http.StatusFound, "rootPath()")
}
authenticatedUser := sessionUser.(*skillz.User)
if authenticatedUser.ID != userID {
s.flashDanger(c, "User Not Found")
return c.Redirect(http.StatusFound, "rootPath()")
}
}
if settings.Visibility == skillz.VisibilityToken {
token := c.Param("token")
if token != "" {
if token != "" && token != settings.VisibilityToken {
s.flashDanger(c, "User Not Found")
return c.Redirect(http.StatusFound, "rootPath()")
}
} else {
sessionUser := c.Data()[keyAuthenticatedUser]
if sessionUser == nil {
s.flashDanger(c, "User Not Found")
return c.Redirect(http.StatusFound, "rootPath()")
}
authenticatedUser, ok := sessionUser.(*skillz.User)
if !ok {
s.flashDanger(c, "User Not Found")
return c.Redirect(http.StatusFound, "rootPath()")
}
if authenticatedUser.ID != userID {
s.flashDanger(c, "User Not Found")
return c.Redirect(http.StatusFound, "rootPath()")
}
}
}
}
if u.IsNew {
c.Set("title", fmt.Sprintf("Welcome to Skillboard.Evie %s", titleSuffix))
return c.Render(http.StatusOK, s.renderer.HTML("user/welcome.plush.html"))
}
u, err = s.user.LoadUserAll(ctx, userID)
if err != nil && !errors.Is(err, user.ErrUserNotFound) {
return c.Error(http.StatusInternalServerError, err)
}
if u.Character != nil {
c.Set(s.userPageMeta(ctx, u))
}
c.Set("user", u)
jsonSkillGrouped, err := json.Marshal(u.SkillsGrouped)
if err != nil {
s.flashDanger(c, "Failed to encoding user data to json")
return c.Redirect(http.StatusFound, "rootPath()")
}
c.Set("jsonSkillGrouped", string(jsonSkillGrouped))
return c.Render(http.StatusOK, s.renderer.HTML("user/index.plush.html", "user/layout.plush.html"))
}
func (s *Service) userSettingsHandler(c buffalo.Context) error {
var ctx = c.Request().Context()
user := c.Data()[keyAuthenticatedUser].(*skillz.User)
if user == nil {
s.logger.Debug("user is missing from session")
return c.Redirect(http.StatusFound, "rootPath()")
}
c.Set(s.userSettingsMeta(ctx, user))
c.Set("checked", func(b bool) string {
o := ""
if b {
o = `checked="on"`
}
return o
})
c.Set("visibilities", skillz.AllVisibilities)
return c.Render(http.StatusOK, s.renderer.HTML("user/settings.plush.html"))
}
func (s *Service) postUserSettingsHandler(c buffalo.Context) error {
var ctx = c.Request().Context()
user := c.Data()[keyAuthenticatedUser].(*skillz.User)
if user == nil {
return c.Redirect(http.StatusFound, "rootPath()")
}
settings := new(skillz.UserSettings)
settings.VisibilityToken = user.Settings.VisibilityToken
err := c.Bind(settings)
if err != nil {
s.flashDanger(c, "failed to process form. Please try again")
return c.Redirect(http.StatusFound, "usersSettingsPath()")
}
if !settings.Visibility.Valid() {
s.flashDanger(c, "Invalid value for Visibilty. Please try again")
return c.Redirect(http.StatusFound, "usersSettingsPath()")
}
err = s.user.CreateUserSettings(ctx, user.ID, settings)
if err != nil {
return err
}
user.Settings = settings
c.Set(userSettingsPageTitle(user.Character.Name))
s.flashSuccess(c, "Settings updated successfully")
return c.Redirect(http.StatusFound, "usersSettingsPath()")
}
func (s *Service) deleteUserSettingsHandler(c buffalo.Context) error {
var r = c.Request()
var ctx = r.Context()
var form = r.Form
if !form.Has("confirmed") {
return c.Redirect(http.StatusFound, "usersSettingsPath()")
}
if form.Get("confirmed") != "true" {
return c.Redirect(http.StatusFound, "usersSettingsPath()")
}
user := c.Data()[keyAuthenticatedUser].(*skillz.User)
if user == nil {
return c.Redirect(http.StatusFound, "rootPath()")
}
err := s.user.DeleteUser(ctx, user)
if err != nil {
s.flashDanger(c, "Failed to delete user. Please contact the maintainer")
return c.Redirect(http.StatusFound, "usersSettingsPath()")
}
c.Session().Clear()
s.flashSuccess(c, "You account has successfully been deleted. Goodbye :-(")
return c.Redirect(http.StatusFound, "rootPath()")
}