/
ban.go
170 lines (148 loc) · 4.8 KB
/
ban.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
package style
import (
"strconv"
"strings"
"github.com/gofiber/fiber/v2"
"userstyles.world/handlers/jwt"
"userstyles.world/models"
"userstyles.world/modules/config"
"userstyles.world/modules/database"
"userstyles.world/modules/log"
"userstyles.world/search"
"userstyles.world/utils"
)
func BanGet(c *fiber.Ctx) error {
u, _ := jwt.User(c)
// Check if logged-in user has permissions.
if !u.IsModOrAdmin() {
c.Status(fiber.StatusUnauthorized)
return c.Render("err", fiber.Map{
"Title": "Can't do that",
"User": u,
})
}
// Check if style exists.
s, err := models.GetStyleByID(c.Params("id"))
if err != nil {
c.Status(fiber.StatusNotFound)
return c.Render("err", fiber.Map{
"Title": "Style not found",
"User": u,
})
}
return c.Render("style/ban", fiber.Map{
"Title": "Confirm ban",
"User": u,
"Style": s,
})
}
func sendBanEmail(baseURL string, user *models.User, style *models.APIStyle, modLogID uint) error {
modLogEntry := baseURL + "/modlog#entry_" + strconv.Itoa(int(modLogID))
partPlain := utils.NewPart().
SetBody("Hi " + user.Username + ",\n" +
"We'd like to notice you about a recent action from our moderation team:\n\n" +
"Your style \"" + style.Name + "\" has been removed from our platform.\n" +
"You can check for more information about this action on the modlog: " + modLogEntry + "\n\n" +
"If you'd like to come in touch with us, please email us at feedback@userstyles.world\n" +
"Regards,\n" + "The Moderation Team")
partHTML := utils.NewPart().
SetBody("<p>Hi " + user.Username + ",</p>\n" +
"<br>" +
"<p>We'd like to notice you about a recent action from our moderation team:</p>\n" +
"<br><br>" +
"<p>Your style \"<b>" + style.Name + "</b>\" has been removed from our platform.</p>\n" +
"<p>You can check for more information about this action on the " +
"<a target=\"_blank\" clicktracking=\"off\" href=\"" + modLogEntry + "\">Modlog</a>.</p>\n" +
"<p>If you'd like to come in touch with us, " +
"please email us at <a href=\"mailto:feedback@userstyles.world\">feedback@userstyles.world</a>.<p>\n" +
"<br><br>" +
"<p>Regards,</p>\n" + "<p>The Moderation Team</p>").
SetContentType("text/html")
err := utils.NewEmail().
SetTo(user.Email).
SetSubject("Moderation notice").
AddPart(*partPlain).
AddPart(*partHTML).
SendEmail(config.IMAPServer)
if err != nil {
return err
}
return nil
}
func BanPost(c *fiber.Ctx) error {
u, _ := jwt.User(c)
id := c.Params("id")
// Check if logged-in user has permissions.
if !u.IsModOrAdmin() {
c.Status(fiber.StatusUnauthorized)
return c.Render("err", fiber.Map{
"Title": "Can't do that",
"User": u,
})
}
// Check if style exists.
s, err := models.GetStyleByID(id)
if err != nil {
c.Status(fiber.StatusNotFound)
return c.Render("err", fiber.Map{
"Title": "Style not found",
"User": u,
})
}
// Initialize modlog data.
logEntry := models.Log{
UserID: u.ID,
Username: u.Username,
Kind: models.LogRemoveStyle,
TargetUserName: s.Username,
TargetData: s.Name,
Reason: strings.TrimSpace(c.FormValue("reason")),
Censor: c.FormValue("censor") == "on",
}
// Add banned style log entry.
modlog := new(models.Log)
if err := modlog.AddLog(&logEntry); err != nil {
log.Warn.Printf("Failed to add style %d to ModLog: %s", s.ID, err.Error())
return c.Render("err", fiber.Map{
"Title": "Internal server error.",
"User": u,
})
}
// Delete from database.
q := new(models.Style)
if err = database.Conn.Delete(q, "styles.id = ?", id).Error; err != nil {
log.Warn.Printf("Failed to delete style %d: %s\n", s.ID, err.Error())
c.Status(fiber.StatusInternalServerError)
return c.Render("err", fiber.Map{
"Title": "Internal server error",
"User": u,
})
}
go func(baseURL string, style *models.APIStyle, modLogID uint) {
// Add notification to database.
notification := models.Notification{
Seen: false,
Kind: models.KindBannedStyle,
TargetID: int(modLogID),
UserID: int(u.ID),
StyleID: int(s.ID),
}
if err := notification.Create(); err != nil {
log.Warn.Printf("Failed to create a notification for ban removal %d: %v\n", style.ID, err)
}
// Delete from search index.
if err = search.DeleteStyle(style.ID); err != nil {
log.Warn.Printf("Failed to delete style %d: %s", style.ID, err.Error())
}
targetUser, err := models.FindUserByID(strconv.Itoa(int(style.UserID)))
if err != nil {
log.Warn.Printf("Failed to find user %d: %s", style.UserID, err.Error())
return
}
// Notify the author about style removal.
if err := sendBanEmail(baseURL, targetUser, style, modLogID); err != nil {
log.Warn.Printf("Failed to mail author for style %d: %s", style.ID, err.Error())
}
}(c.BaseURL(), s, logEntry.ID)
return c.Redirect("/modlog", fiber.StatusSeeOther)
}