forked from NyaaPantsu/nyaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
172 lines (157 loc) · 5.49 KB
/
profile.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
package userController
import (
"strconv"
"time"
"net/http"
"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/notifications"
"github.com/NyaaPantsu/nyaa/models/users"
"github.com/NyaaPantsu/nyaa/templates"
"github.com/NyaaPantsu/nyaa/utils/cookies"
"github.com/NyaaPantsu/nyaa/utils/crypto"
"github.com/NyaaPantsu/nyaa/utils/email"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
"github.com/NyaaPantsu/nyaa/utils/validator"
"github.com/NyaaPantsu/nyaa/utils/validator/user"
"github.com/gin-gonic/gin"
)
// UserProfileHandler : Getting User Profile
func UserProfileHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
Ts, _ := publicSettings.GetTfuncAndLanguageFromRequest(c)
messages := msg.GetMessages(c)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser == nil {
currentUser := router.GetUser(c)
follow := c.Request.URL.Query()["followed"]
unfollow := c.Request.URL.Query()["unfollowed"]
deleteVar := c.Request.URL.Query()["delete"]
if (deleteVar != nil) && (currentUser.CurrentOrAdmin(userProfile.ID)) {
_, err := userProfile.Delete(currentUser)
if err == nil && currentUser.CurrentUserIdentical(userProfile.ID) {
cookies.Clear(c)
}
templates.Static(c, "site/static/delete_success.jet.html")
} else {
if follow != nil {
messages.AddInfof("infos", Ts("user_followed_msg"), userProfile.Username)
}
if unfollow != nil {
messages.AddInfof("infos", Ts("user_unfollowed_msg"), userProfile.Username)
}
userProfile.ParseSettings()
templates.UserProfile(c, userProfile)
}
} else {
c.Status(http.StatusNotFound)
}
}
// UserDetailsHandler : Getting User Profile Details View
func UserDetailsHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
currentUser := router.GetUser(c)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser == nil && currentUser.CurrentOrAdmin(userProfile.ID) {
b := userValidator.UserForm{}
c.Bind(&b)
availableLanguages := publicSettings.GetAvailableLanguages()
userProfile.ParseSettings()
templates.UserProfileEdit(c, userProfile, b, availableLanguages)
} else {
c.Status(http.StatusNotFound)
}
}
// UserProfileFormHandler : Getting View User Profile Update
func UserProfileFormHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
currentUser := router.GetUser(c)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser != nil || !currentUser.CurrentOrAdmin(userProfile.ID) || userProfile.ID == 0 {
c.Status(http.StatusNotFound)
return
}
userProfile.ParseSettings()
messages := msg.GetMessages(c)
userForm := userValidator.UserForm{}
userSettingsForm := userValidator.UserSettingsForm{}
if len(c.PostForm("email")) > 0 {
if !userValidator.EmailValidation(c.PostForm("email")) {
messages.AddErrorT("email", "email_not_valid")
}
}
if len(c.PostForm("username")) > 0 {
if !userValidator.ValidateUsername(c.PostForm("username")) {
messages.AddErrorT("username", "username_illegal")
}
}
if !messages.HasErrors() {
c.Bind(&userForm)
c.Bind(&userSettingsForm)
if !currentUser.HasAdmin() {
userForm.Username = userProfile.Username
userForm.Status = userProfile.Status
} else {
if userProfile.Status != userForm.Status && userForm.Status == 2 {
messages.AddErrorT("errors", "elevating_user_error")
}
}
validator.ValidateForm(&userForm, messages)
if !messages.HasErrors() {
if userForm.Email != userProfile.Email {
email.SendVerificationToUser(currentUser, userForm.Email)
messages.AddInfoTf("infos", "email_changed", userForm.Email)
userForm.Email = userProfile.Email // reset, it will be set when user clicks verification
}
user, _, err := users.UpdateFromRequest(c, &userForm, &userSettingsForm, currentUser, uint(id))
if err != nil {
messages.Error(err)
}
if userForm.Email != user.Email {
// send verification to new email and keep old
email.SendVerificationToUser(user, userForm.Email)
}
if !messages.HasErrors() {
messages.AddInfoT("infos", "profile_updated")
}
}
}
availableLanguages := publicSettings.GetAvailableLanguages()
templates.UserProfileEdit(c, userProfile, userForm, availableLanguages)
}
// UserNotificationsHandler : Controller to show user notifications
func UserNotificationsHandler(c *gin.Context) {
currentUser := router.GetUser(c)
if currentUser.ID > 0 {
messages := msg.GetMessages(c)
if c.Request.URL.Query()["clear"] != nil {
notifications.DeleteAllNotifications(currentUser.ID)
messages.AddInfoT("infos", "notifications_cleared")
currentUser.Notifications = []models.Notification{}
}
templates.UserProfileNotifications(c, currentUser)
} else {
c.Status(http.StatusNotFound)
}
}
// UserAPIKeyResetHandler : Controller to reset user api key
func UserAPIKeyResetHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
currentUser := router.GetUser(c)
messages := msg.GetMessages(c)
userProfile, _, errorUser := users.FindForAdmin(uint(id))
if errorUser != nil || !currentUser.CurrentOrAdmin(userProfile.ID) || userProfile.ID == 0 {
c.Status(http.StatusNotFound)
return
}
userProfile.APIToken, _ = crypto.GenerateRandomToken32()
userProfile.APITokenExpiry = time.Unix(0, 0)
_, errorUser = userProfile.UpdateRaw()
if errorUser != nil {
messages.Error(errorUser)
} else {
messages.AddInfoT("infos", "profile_updated")
}
UserProfileHandler(c)
}