Skip to content

Commit

Permalink
Merge branch improve-search
Browse files Browse the repository at this point in the history
  • Loading branch information
vednoc committed Sep 24, 2023
2 parents c704560 + f45173b commit 82958d5
Show file tree
Hide file tree
Showing 21 changed files with 178 additions and 178 deletions.
17 changes: 11 additions & 6 deletions handlers/core/search.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"fmt"
"strings"
"time"

Expand All @@ -19,12 +20,16 @@ func Search(c *fiber.Ctx) error {
c.Locals("Canonical", "search")

keyword := strings.TrimSpace(c.Query("q"))
if len(keyword) < 3 {
c.Locals("Error", "Keywords need to be in a group of three or more characters.")
return c.Render("core/search", fiber.Map{})
}
c.Locals("Keyword", keyword)

category := strings.TrimSpace(c.Query("category"))
c.Locals("Category", category)

query := keyword
if category != "" {
query += fmt.Sprintf(" category:%s", category)
}

page, err := models.IsValidPage(c.Query("page"))
if err != nil || page < 1 {
c.Locals("Title", "Invalid page size")
Expand All @@ -36,7 +41,7 @@ func Search(c *fiber.Ctx) error {

t := time.Now()

total, err := storage.TotalSearchStyles(keyword, sort)
total, err := storage.TotalSearchStyles(query, sort)
if err != nil {
log.Database.Println(err)
c.Locals("Title", "Failed to count userstyles")
Expand All @@ -50,7 +55,7 @@ func Search(c *fiber.Ctx) error {
}
c.Locals("Pagination", p)

s, err := storage.FindSearchStyles(keyword, p.SortStyles(), page)
s, err := storage.FindSearchStyles(query, p.SortStyles(), page)
if err != nil {
log.Database.Println(err)
c.Locals("Title", "Failed to search for userstyles")
Expand Down
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
121 changes: 56 additions & 65 deletions handlers/style/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"

"userstyles.world/handlers/jwt"
"userstyles.world/models"
Expand All @@ -13,6 +14,7 @@ import (
"userstyles.world/modules/database"
"userstyles.world/modules/email"
"userstyles.world/modules/log"
"userstyles.world/modules/storage"
)

func BanGet(c *fiber.Ctx) error {
Expand Down Expand Up @@ -65,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 @@ -75,87 +76,77 @@ 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",
}

// 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 style from database.
q := new(models.Style)
if err = database.Conn.Delete(q, "styles.id = ?", s.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,
})
notification := models.Notification{
Kind: models.KindBannedStyle,
TargetID: int(event.ID),
UserID: int(user.ID),
StyleID: int(style.ID),
}

// Delete stats from database.
if err = new(models.Stats).Delete(s.ID); err != nil {
log.Warn.Printf("Failed to delete stats for style %d: %s\n", s.ID, err.Error())
c.Status(fiber.StatusInternalServerError)
// INSERT INTO `logs`
err = database.Conn.Transaction(func(tx *gorm.DB) error {
if err = storage.DeleteUserstyle(tx, i); err != nil {
return err
}
if err = models.DeleteStats(tx, i); err != nil {
return err
}
if err = storage.DeleteSearchData(tx, i); err != nil {
return err
}
if err = models.CreateLog(tx, &event); err != nil {
return err
}
if err = models.CreateNotification(tx, &notification); err != nil {
return err
}
return models.RemoveStyleCode(id)
})
if err != nil {
log.Database.Printf("Failed to remove %d: %s\n", i, err)
return c.Render("err", fiber.Map{
"Title": "Internal server error",
"Title": "Failed to remove userstyle",
"User": u,
})
}

if err = models.RemoveStyleCode(strconv.Itoa(int(s.ID))); err != nil {
log.Warn.Printf("kind=removecode id=%v err=%q\n", s.ID, err)
}

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)
}
}
38 changes: 17 additions & 21 deletions handlers/style/delete.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package style

import (
"strconv"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"

"userstyles.world/handlers/jwt"
"userstyles.world/models"
"userstyles.world/modules/cache"
"userstyles.world/modules/database"
"userstyles.world/modules/log"
"userstyles.world/modules/storage"
)

func DeleteGet(c *fiber.Ctx) error {
Expand Down Expand Up @@ -67,30 +67,26 @@ func DeletePost(c *fiber.Ctx) error {
})
}

// Delete style 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())
return c.Render("err", fiber.Map{
"Title": "Internal server error",
"User": u,
})
}

// Delete stats from database.
if err = new(models.Stats).Delete(s.ID); err != nil {
log.Warn.Printf("Failed to delete stats for style %d: %s\n", s.ID, err.Error())
c.Status(fiber.StatusInternalServerError)
err = database.Conn.Transaction(func(tx *gorm.DB) error {
if err = storage.DeleteUserstyle(tx, i); err != nil {
return err
}
if err = models.DeleteStats(tx, i); err != nil {
return err
}
if err = storage.DeleteSearchData(tx, i); err != nil {
return err
}
return models.RemoveStyleCode(id)
})
if err != nil {
log.Database.Printf("Failed to delete %d: %s\n", i, err)
return c.Render("err", fiber.Map{
"Title": "Internal server error",
"Title": "Failed to remove userstyle",
"User": u,
})
}

if err = models.RemoveStyleCode(strconv.Itoa(int(s.ID))); err != nil {
log.Warn.Printf("kind=removecode id=%v err=%q\n", s.ID, err)
}

cache.Code.Remove(i)

return c.Redirect("/user/"+u.Username, fiber.StatusSeeOther)
Expand Down
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
2 changes: 1 addition & 1 deletion handlers/style/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func GetStylePage(c *fiber.Ctx) error {
"User": u,
"Title": data.Name,
"Style": data,
"URL": c.BaseURL() + c.Path(),
"URL": c.BaseURL() + "/style/" + id,
"Slug": slug,
"Canonical": "style/" + id + "/" + slug,
"RenderMeta": true,
Expand Down
4 changes: 2 additions & 2 deletions handlers/user/ban.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/config"
"userstyles.world/modules/database"
"userstyles.world/modules/email"
"userstyles.world/modules/log"
)
Expand Down Expand Up @@ -105,8 +106,7 @@ func ConfirmBan(c *fiber.Ctx) error {
}

// Add banned user log entry.
modlog := new(models.Log)
if err := modlog.AddLog(&logEntry); err != nil {
if err := models.CreateLog(database.Conn, &logEntry); err != nil {
log.Warn.Printf("Failed to add user %d to ModLog: %s\n", targetUser.ID, err.Error())
return c.Render("err", fiber.Map{
"Title": "Internal server error",
Expand Down
13 changes: 3 additions & 10 deletions models/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@ type APILog struct {
TargetUserName string
}

// AddLog adds a new log to the database.
func (*Log) AddLog(logEntry *Log) (err error) {
err = db().
Model(modelLog).
Create(logEntry).
Error
if err != nil {
return errors.ErrFailedLogAddition
}
return nil
// CreateLog inserts a new log entry into the database.
func CreateLog(db *gorm.DB, log *Log) (err error) {
return db.Model(modelLog).Create(log).Error
}

// GetLogOfKind returns all the logs of the specified kind and
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
}
Loading

0 comments on commit 82958d5

Please sign in to comment.