Skip to content

Commit

Permalink
refactor(style): separate notifications from email
Browse files Browse the repository at this point in the history
  • Loading branch information
vednoc committed Sep 24, 2023
1 parent a78cf24 commit f45173b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 51 deletions.
6 changes: 3 additions & 3 deletions handlers/review/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"userstyles.world/handlers/jwt"
"userstyles.world/models"
"userstyles.world/modules/cache"
"userstyles.world/modules/database"
"userstyles.world/modules/log"
)

Expand Down Expand Up @@ -92,16 +93,15 @@ func createForm(c *fiber.Ctx) error {
return c.Render("err", fiber.Map{})
}

// Create a notification.
notification := models.Notification{
n := models.Notification{
Kind: models.KindReview,
TargetID: int(s.UserID),
UserID: int(u.ID),
StyleID: i,
ReviewID: int(r.ID),
}

if err = notification.Create(); err != nil {
if err = models.CreateNotification(database.Conn, &n); err != nil {
log.Warn.Printf("Failed to add notification to review %d: %s\n", r.ID, err)
}

Expand Down
2 changes: 1 addition & 1 deletion handlers/review/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func removeForm(c *fiber.Ctx) error {
UserID: int(u.ID),
StyleID: sid,
}
if err = n.Create(); err != nil {
if err = models.CreateNotification(database.Conn, &n); err != nil {
c.Locals("Title", "Failed to add notification")
return c.Status(fiber.StatusNotFound).Render("err", fiber.Map{})
}
Expand Down
77 changes: 39 additions & 38 deletions handlers/style/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func BanPost(c *fiber.Ctx) error {
}
id := c.Params("id")

// Check if style exists.
s, err := models.GetStyleByID(id)
style, err := models.GetStyleByID(id)
if err != nil {
c.Status(fiber.StatusNotFound)
return c.Render("err", fiber.Map{
Expand All @@ -77,18 +76,34 @@ func BanPost(c *fiber.Ctx) error {
})
}

// Initialize modlog data.
logEntry := models.Log{
user, err := storage.FindUser(style.UserID)
if err != nil {
c.Status(fiber.StatusInternalServerError)
return c.Render("err", fiber.Map{
"Title": "Internal server error",
"User": u,
})
}

event := models.Log{
UserID: u.ID,
Username: u.Username,
Kind: models.LogRemoveStyle,
TargetUserName: s.Username,
TargetData: s.Name,
TargetUserName: style.Username,
TargetData: style.Name,
Reason: strings.TrimSpace(c.FormValue("reason")),
Message: strings.TrimSpace(c.FormValue("message")),
Censor: c.FormValue("censor") == "on",
}

notification := models.Notification{
Kind: models.KindBannedStyle,
TargetID: int(event.ID),
UserID: int(user.ID),
StyleID: int(style.ID),
}

// INSERT INTO `logs`
err = database.Conn.Transaction(func(tx *gorm.DB) error {
if err = storage.DeleteUserstyle(tx, i); err != nil {
return err
Expand All @@ -99,7 +114,10 @@ func BanPost(c *fiber.Ctx) error {
if err = storage.DeleteSearchData(tx, i); err != nil {
return err
}
if err = models.CreateLog(tx, &logEntry); err != nil {
if err = models.CreateLog(tx, &event); err != nil {
return err
}
if err = models.CreateNotification(tx, &notification); err != nil {
return err
}
return models.RemoveStyleCode(id)
Expand All @@ -114,38 +132,21 @@ func BanPost(c *fiber.Ctx) error {

cache.Code.Remove(i)

go func(style *models.APIStyle, entry models.Log) {
user, 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
}
go sendRemovalEmail(user, style, event)

// Add notification to database.
notification := models.Notification{
Seen: false,
Kind: models.KindBannedStyle,
TargetID: int(entry.ID),
UserID: int(user.ID),
StyleID: int(style.ID),
}

if err := notification.Create(); err != nil {
log.Warn.Printf("Failed to create a notification for ban removal %d: %v\n", style.ID, err)
}

args := fiber.Map{
"User": user,
"Style": style,
"Log": entry,
"Link": config.BaseURL + "/modlog#id-" + strconv.Itoa(int(entry.ID)),
}
return c.Redirect("/modlog", fiber.StatusSeeOther)
}

title := "Your style has been removed"
if err := email.Send("style/ban", user.Email, title, args); err != nil {
log.Warn.Printf("Failed to email author for style %d: %s\n", style.ID, err)
}
}(s, logEntry)
func sendRemovalEmail(user *storage.User, style *models.APIStyle, entry models.Log) {
args := fiber.Map{
"User": user,
"Style": style,
"Log": entry,
"Link": config.BaseURL + "/modlog#id-" + strconv.Itoa(int(entry.ID)),
}

return c.Redirect("/modlog", fiber.StatusSeeOther)
title := "Your style has been removed"
if err := email.Send("style/ban", user.Email, title, args); err != nil {
log.Warn.Printf("Failed to email author for style %d: %s\n", style.ID, err)
}
}
11 changes: 4 additions & 7 deletions handlers/style/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,17 @@ func Promote(c *fiber.Ctx) error {
if !style.Featured {
go sendPromotionEmail(style, user, u.Username)

// Create a notification.
notification := models.Notification{
n := models.Notification{
Seen: false,
Kind: models.KindStylePromotion,
TargetID: int(style.UserID),
UserID: int(u.ID),
StyleID: id,
}

go func(notification models.Notification) {
if err := notification.Create(); err != nil {
log.Warn.Printf("Failed to create a notification for %d, err: %v", id, err.Error())
}
}(notification)
if err := models.CreateNotification(database.Conn, &n); err != nil {
log.Warn.Printf("Failed to create a notification for %d: %s\n", id, err)
}
}

return c.Redirect("/style/"+p, fiber.StatusSeeOther)
Expand Down
5 changes: 3 additions & 2 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Notification struct {
ReviewID int `gorm:"default:null"`
}

func (n Notification) Create() error {
return db().Create(&n).Error
// CreateNotification inserts a new notification.
func CreateNotification(db *gorm.DB, n *Notification) error {
return db.Create(&n).Error
}
10 changes: 10 additions & 0 deletions modules/storage/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ func FindUsersCreatedOn(date time.Time) ([]User, error) {

return res, nil
}

// FindUser returns a user.
func FindUser(id uint) (u *User, err error) {
err = database.Conn.Find(&u, "id = ?", id).Error
if err != nil {
return nil, err
}

return u, err
}

0 comments on commit f45173b

Please sign in to comment.