-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
75 lines (63 loc) · 2 KB
/
settings.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
package actions
import (
"fmt"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/validate"
)
// UpdateUserSettings happens when users updates their settings
type UpdateUserSettings struct {
Model *models.UpdateUserSettings
}
// Initialize the model
func (input *UpdateUserSettings) Initialize() interface{} {
input.Model = new(models.UpdateUserSettings)
input.Model.Avatar = &models.ImageUpload{}
return input.Model
}
// IsAuthorized returns true if current user is authorized to perform this action
func (input *UpdateUserSettings) IsAuthorized(user *models.User, services *app.Services) bool {
return user != nil
}
// Validate is current model is valid
func (input *UpdateUserSettings) Validate(user *models.User, services *app.Services) *validate.Result {
result := validate.Success()
if input.Model.Name == "" {
result.AddFieldFailure("name", "Name is required.")
}
if input.Model.AvatarType < 1 || input.Model.AvatarType > 3 {
result.AddFieldFailure("avatarType", "Invalid avatar type.")
}
if len(input.Model.Name) > 50 {
result.AddFieldFailure("name", "Name must have less than 50 characters.")
}
input.Model.Avatar.BlobKey = user.AvatarBlobKey
messages, err := validate.ImageUpload(input.Model.Avatar, validate.ImageUploadOpts{
IsRequired: input.Model.AvatarType == models.AvatarTypeCustom,
MinHeight: 50,
MinWidth: 50,
ExactRatio: true,
MaxKilobytes: 100,
})
if err != nil {
return validate.Error(err)
}
result.AddFieldFailure("avatar", messages...)
if input.Model.Settings != nil {
for k, v := range input.Model.Settings {
ok := false
for _, e := range models.AllNotificationEvents {
if e.UserSettingsKeyName == k {
ok = true
if !e.Validate(v) {
result.AddFieldFailure("settings", fmt.Sprintf("Settings %s has an invalid value %s.", k, v))
}
}
}
if ok == false {
result.AddFieldFailure("settings", fmt.Sprintf("Unknown settings named %s.", k))
}
}
}
return result
}