/
users.go
106 lines (100 loc) · 3.21 KB
/
users.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
package actions
import (
"fmt"
"regexp"
"strings"
"github.com/IEEESBITBA/Curso-de-Python-Sistemas/models"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop/v5"
"github.com/pkg/errors"
)
// UserSettingsGet render page with user settings :)
func UserSettingsGet(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
topics := &models.Topics{}
if err := tx.All(topics); err != nil {
return errors.WithStack(err)
}
c.Set("topics", topics)
return c.Render(200, r.HTML("users/settings.plush.html"))
}
// UserSettingsPost handles event when user changes setting by submitting setting form
func UserSettingsPost(c buffalo.Context) error {
tx := c.Value("tx").(*pop.Connection)
userDB := c.Value("current_user").(*models.User)
user := &models.User{}
if err := c.Bind(user); err != nil {
return errors.WithStack(err)
}
s, err := sanitizeNick(user.Nick)
emptyIfClean := hasBadWord(s)
if emptyIfClean != "" {
c.Logger().Infof("Bad word inserted by user %s: %s", user.Email, emptyIfClean)
c.Flash().Add("warning", "@#%$*!!")
return c.Redirect(302, "userSettingsPath()")
}
if err != nil {
c.Flash().Add("danger", T.Translate(c, "user-settings-nick-invalid"))
return c.Redirect(302, "userSettingsPath()")
}
c.Session().Set("code_theme", user.Theme)
userDB.Nick = s
if err := tx.UpdateColumns(userDB, "nick"); err != nil {
return errors.WithStack(err)
}
c.Flash().Add("success", T.Translate(c, "user-settings-edit-success"))
return c.Redirect(302, "userSettingsPath()") //return c.Redirect(302, "topicGetPath()", render.Data{"forum_title":f.Title, "cat_title":c.Param("cat_title"),
// "tid":c.Param("tid")})
}
// sanitizeNick don't want bad characters that break urls or weird stuff.
func sanitizeNick(s string) (string, error) {
s = strings.TrimSpace(s)
if len(s) > 10 {
return "", errors.New("nick too long")
}
re := regexp.MustCompile(fmt.Sprintf("[0-9a-zA-Z]{%d}", len(s)))
if !re.MatchString(s) {
return s, errors.New("nick contains non alphanumeric char")
}
return s, nil
}
// UsersSettingsRemoveTopicSubscription handles user clicking remove subscription button
func UsersSettingsRemoveTopicSubscription(c buffalo.Context) error {
var unsub []string
usr := new(models.User)
tx := c.Value("tx").(*pop.Connection)
if err := tx.Find(usr, c.Param("uid")); err != nil {
return errors.WithStack(err)
}
if c.Param("tid") == "all" {
for _, sub := range usr.Subscriptions {
topic := new(models.Topic)
if err := tx.Find(topic, sub); err != nil {
continue
}
unsub = append(unsub, topic.ID.String())
}
if len(unsub) == 0 {
c.Flash().Add("warning", T.Translate(c, "topic-unsubscribe-empty"))
return c.Redirect(302, "/u")
}
} else {
unsub = append(unsub, c.Param("tid"))
}
for _, id := range unsub {
topic := new(models.Topic)
if err := tx.Find(topic, id); err != nil {
return errors.WithStack(err)
}
usr.RemoveSubscription(topic.ID)
if err := tx.UpdateColumns(usr, "subscriptions"); err != nil {
return errors.WithStack(err)
}
topic.RemoveSubscriber(usr.ID)
if err := tx.UpdateColumns(topic, "subscribers"); err != nil {
return errors.WithStack(err)
}
}
c.Flash().Add("success", T.Translate(c, "topic-unsubscribe-success"))
return c.Redirect(302, "/u")
}