-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modlog): create modlog prototype
- Loading branch information
Gusted
committed
Jul 8, 2021
1 parent
adfb0cf
commit 664f3eb
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |