Skip to content

Commit

Permalink
feat(modlog): create modlog prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusted committed Jul 8, 2021
1 parent adfb0cf commit 664f3eb
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 0 deletions.
34 changes: 34 additions & 0 deletions handlers/core/modlog.go
@@ -0,0 +1,34 @@
package core

import (
"github.com/gofiber/fiber/v2"
"userstyles.world/handlers/jwt"
"userstyles.world/models"
)

// GetModLog renders the modlog view.
// It will pass trough the relevant information from the database.
func GetModLog(c *fiber.Ctx) error {
u, _ := jwt.User(c)

bannedUsers, err := models.GetLogOfKind(models.LogBanUser)
if err != nil {
return c.Render("err", fiber.Map{
"Title": "Internal Server error",
"User": u,
})
}

removedStyles, err := models.GetLogOfKind(models.LogRemoveStyle)
if err != nil {
return c.Render("err", fiber.Map{
"Title": "Internal Server error",
"User": u,
})
}

return c.Render("core/modlog", fiber.Map{
"BannedUsers": bannedUsers,
"RemovedStyles": removedStyles,
})
}
1 change: 1 addition & 0 deletions handlers/server.go
Expand Up @@ -68,6 +68,7 @@ func Initialize() {
app.Get("/user/:name", user.Profile)
app.Get("~:name", user.Profile)
app.Get("/docs/:document", core.GetDocs)
app.Get("/modlog", core.GetModLog)

app.Get("/logout", jwtware.Protected, user.Logout)
app.Get("/account", jwtware.Protected, user.Account)
Expand Down
55 changes: 55 additions & 0 deletions models/log.go
@@ -0,0 +1,55 @@
package models

import (
"gorm.io/gorm"
"userstyles.world/modules/database"
"userstyles.world/modules/errors"
)

type LogKind = uint8

const (
LogBanUser LogKind = iota + 1
LogRemoveStyle
)

// Log struct has all the relavant information for a log entry
type Log struct {
gorm.Model
Username string
UserID uint
Reason string
Kind LogKind
TargetData string
TargetUserName string
}

// AddLog adds a new log to the database.
func AddLog(log Log) (err error) {
err = database.Conn.
Debug().
Model(Log{}).
Create(log).
Error
if err != nil {
return errors.ErrFailedLogAddition
}
return nil
}

// GetLogOfKind returns all the logs of the specified kind and
// select the correct user Author.
func GetLogOfKind(kind LogKind) (q *[]Log, err error) {
err = database.Conn.
Debug().
Model(Log{}).
Select("logs.*, u.id, u.username").
Joins("join users u on u.id = logs.user_id").
Where("kind = ?", kind).
Find(&q).
Error
if err != nil {
return nil, errors.ErrFailedLogRetrieval
}
return q, nil
}
20 changes: 20 additions & 0 deletions modules/database/init/init.go
Expand Up @@ -25,6 +25,7 @@ var tables = []struct {
{"stats", &models.Stats{}},
{"oauths", &models.OAuth{}},
{"histories", &models.History{}},
{"logs", &models.Log{}},
}

func connect() (*gorm.DB, error) {
Expand Down Expand Up @@ -200,6 +201,22 @@ func seed() {
},
}

Logs := []models.Log{
{
UserID: 1,
Reason: "I like to abuse powers.",
Kind: models.LogBanUser,
TargetUserName: "gusted",
},
{
UserID: 1,
Reason: "My style is superior",
Kind: models.LogRemoveStyle,
TargetUserName: "gusted",
TargetData: "Black-Discord",
},
}

if config.DB_RANDOM_DATA != "false" {
amount, _ := strconv.Atoi(config.DB_RANDOM_DATA)
s, u := generateData(amount)
Expand All @@ -216,4 +233,7 @@ func seed() {
for i := range OAuths {
database.Conn.Create(&OAuths[i])
}
for i := range Logs {
database.Conn.Create(&Logs[i])
}
}
9 changes: 9 additions & 0 deletions modules/errors/errors.go
Expand Up @@ -77,6 +77,15 @@ var (

// ErrNoStyleStats errors that the given style doesn't have any styles yet.
ErrNoStyleStats = errors.New("style doesn't have stats yet")

// ErrFailedLogRetrieval errors that it couldn't retrieve all the logs.
ErrFailedLogRetrieval = errors.New("failed to find all logs")

// ErrFailedLogAddition errors that it couldn't add the log.
ErrFailedLogAddition = errors.New("failed to add the log")

// ErrOnlyRemovedStyle errors that this function only allows to remove style kind.
ErrOnlyRemovedStyle = errors.New("only remove style kind is allowed")
)

// UnexpectedSigningMethod errors that a unexpected jwt signing method was used.
Expand Down
83 changes: 83 additions & 0 deletions views/core/modlog.html
@@ -0,0 +1,83 @@
<div class="wrapper py:l">
<style type="text/css" media="screen">
table {
width: 100%;
padding: 1rem;
border-spacing: 0;
border-radius: var(--br-2);
background-color: var(--bg-2);
}
table thead {
top: 0;
position: sticky;
text-align: left;
background-color: var(--bg-2);
}
table tr > * { padding: 0.5rem }
tbody tr:nth-child(2n-1) { background-color: var(--bg-3) }
</style>

<section class="mt:m ta:c">
<h1>Read-Only Mod log</h1>
<p class="fg:3">This is a transpancy modlog that is read-only and list all of the mod actions.</p>
<p class="fg:3">All names are displayed as their Username and not their Displayname.</p>
</section>

<section class="users mt:l">
<h2 class="td:d">Banned users</h2>
<p class="fg:3 mb:m">{{ len .BannedUsers }} banned users in total.</p>

{{ if ge (len .BannedUsers) 1 }}
<table>
<thead>
<th>Moderator</th>
<th>Time of Entry</th>
<th>Banned user</th>
<th>Reason</th>
</thead>
<tbody>
{{ range .BannedUsers }}
<tr>
<td><a href="/user/{{ .Username }}">{{ .Username }}</a></td>
<td>{{ .CreatedAt | Date }}</td>
<td>{{ .TargetUserName }}</td>
<td>{{ .Reason }}</td>
</tr>
{{ end }}
</tbody>
</table>
{{ else }}
<p class="ta:c">No banned users found.</p>
{{ end }}
</section>

<section class="users mt:l">
<h2 class="td:d">Removed styles</h2>
<p class="fg:3 mb:m">{{ len .RemovedStyles }} removed styles in total.</p>

{{ if ge (len .RemovedStyles) 1 }}
<table>
<thead>
<th>Moderator</th>
<th>Time of Entry</th>
<th>Removed Style</th>
<th>Owner of removed style</th>
<th>Reason</th>
</thead>
<tbody>
{{ range .RemovedStyles }}
<tr>
<td><a href="/user/{{ .Username }}">{{ .Username }}</a></td>
<td>{{ .CreatedAt | Date }}</td>
<td>{{ .TargetData }}</td>
<td><a href="/user/{{ .TargetUserName }}">{{ .TargetUserName }}</a></td>
<td>{{ .Reason }}</td>
</tr>
{{ end }}
</tbody>
</table>
{{ else }}
<p class="ta:c">No removed styles found.</p>
{{ end }}
</section>
</div>

0 comments on commit 664f3eb

Please sign in to comment.