Skip to content

Commit

Permalink
feat(styles): implement ban functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
vednoc committed Jul 13, 2021
1 parent 48b8e22 commit 12fac37
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions handlers/server.go
Expand Up @@ -82,6 +82,8 @@ func Initialize() {
app.Get("/edit/:id", jwtware.Protected, style.EditGet)
app.Post("/edit/:id", jwtware.Protected, style.EditPost)
app.Post("/style/:id/promote", jwtware.Protected, style.Promote)
app.Get("/styles/ban/:id", jwtware.Protected, style.BanGet)
app.Post("/styles/ban/:id", jwtware.Protected, style.BanPost)
app.Get("/oauth_settings/:id?", jwtware.Protected, oauthprovider.OAuthSettingsGet)
app.Post("/oauth_settings/:id?", jwtware.Protected, oauthprovider.OAuthSettingsPost)
app.Get("/user/ban/:id", jwtware.Protected, user.Ban)
Expand Down
83 changes: 83 additions & 0 deletions handlers/style/ban.go
@@ -0,0 +1,83 @@
package style

import (
"log"

"github.com/gofiber/fiber/v2"

"userstyles.world/handlers/jwt"
"userstyles.world/models"
"userstyles.world/modules/database"
"userstyles.world/search"
)

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 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,
})
}

// Delete from database.
q := new(models.Style)
if err = database.Conn.Delete(q, "styles.id = ?", id).Error; err != nil {
log.Printf("Failed to delete style, err: %#+v\n", err)
c.Status(fiber.StatusInternalServerError)
return c.Render("err", fiber.Map{
"Title": "Internal server error",
"User": u,
})
}

// Delete from search index.
if err = search.DeleteStyle(s.ID); err != nil {
log.Printf("Couldn't delete style %d failed, err: %s", s.ID, err.Error())
}

return c.Redirect("/account", fiber.StatusSeeOther)
}
4 changes: 4 additions & 0 deletions views/icons/ban.html
@@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" width="{{ . }}" height="{{ . }}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="10"></circle>
<line x1="4.93" y1="4.93" x2="19.07" y2="19.07"></line>
</svg>
15 changes: 15 additions & 0 deletions views/style/ban.html
@@ -0,0 +1,15 @@
<div class="wrapper py:l">
<section class="ta:c">
<h1>{{ .Title }}</h1>
<p>This action is irreversible.</p>
</section>
<section class="limit mt:l mx:a">
<form class="form-wrapper" method="post" action="/styles/ban/{{ .Style.ID }}">
<label class="mb:m">Are you sure you want to ban "{{ .Style.Name }}"?</label>
<div>
<button class="btn primary mr:s" type="submit">Confirm</button>
<a class="fg:1" href="/style/{{ .Style.ID }}">Cancel</a>
</div>
</form>
</section>
</div>
6 changes: 6 additions & 0 deletions views/style/view.html
Expand Up @@ -35,6 +35,12 @@ <h1 class="mb:m">{{ .Style.Name -}}
{{ end }}

<span class="actions flex ml:a">
{{ if .User.IsModOrAdmin }}
<a
href="/styles/ban/{{ .Style.ID }}"
class="btn icon mr:s"
>{{ template "icons/ban" 16 }} Ban</a>
{{ end }}
{{ if eq .User.ID .Style.UserID }}
<a
href="/delete/{{ .Style.ID }}"
Expand Down

0 comments on commit 12fac37

Please sign in to comment.