Skip to content

Commit 27cd44e

Browse files
committed
Add an audit log for guild dashboard
1 parent b5b65be commit 27cd44e

File tree

14 files changed

+119
-28
lines changed

14 files changed

+119
-28
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.cakeyfox.common
2+
3+
enum class LogType(val value: String) {
4+
UNKNOWN_UPDATE("0"),
5+
UPDATE_GENERAL_SETTINGS("1"),
6+
UPDATE_WELCOMER_SETTINGS("2"),
7+
UPDATE_AUTO_ROLE_SETTINGS("3"),
8+
UPDATE_INVITE_BLOCKER_SETTINGS("4"),
9+
UPDATE_SERVER_LOGS_SETTINGS("5"),
10+
UPDATE_YOUTUBE_SETTINGS("6"),
11+
UPDATE_TWITCH_SETTINGS("7"),
12+
UPDATE_PREMIUM_SETTINGS("8"),
13+
UPDATE_PARTNERSHIP_SETTINGS("9");
14+
15+
companion object {
16+
private val valueMap = entries.associateBy(LogType::value)
17+
private val rawMap = entries.associateBy { it.name }
18+
19+
fun fromDb(value: String): LogType {
20+
return valueMap[value] ?: rawMap[value] ?: UNKNOWN_UPDATE
21+
}
22+
}
23+
}

foxy/src/main/kotlin/net/cakeyfox/foxy/website/routes/api/v1/dashboard/guild/PostAutoRoleSettingsRoute.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import mu.KotlinLogging
1010
import net.cakeyfox.common.Constants
1111
import net.cakeyfox.common.FoxyLocale
1212
import net.cakeyfox.foxy.utils.BaseRoute
13+
import net.cakeyfox.common.LogType
1314
import net.cakeyfox.foxy.website.FoxyWebsite
1415
import net.cakeyfox.foxy.website.utils.RouteUtils.checkPermissions
1516
import net.cakeyfox.foxy.website.utils.RouteUtils.htmxRedirect
@@ -24,7 +25,7 @@ class PostAutoRoleSettingsRoute(val server: FoxyWebsite) : BaseRoute("/api/v1/se
2425
?: return htmxRedirect(context.call, Constants.INVITE_LINK)
2526

2627
try {
27-
checkPermissions(server, context, locale, context.call) ?: return
28+
val result = checkPermissions(server, context, locale, context.call) ?: return
2829
fun Parameters.getBoolean(name: String) = this[name] == "on"
2930

3031
val params = context.call.receiveParameters()
@@ -40,6 +41,11 @@ class PostAutoRoleSettingsRoute(val server: FoxyWebsite) : BaseRoute("/api/v1/se
4041
autoRoleModule.isEnabled = params.getBoolean("enableAutoRole")
4142
autoRoleModule.roles.addAll(allRoles)
4243
}
44+
server.foxy.database.guild.addLogToGuild(
45+
guildId,
46+
result.user.id,
47+
LogType.UPDATE_AUTO_ROLE_SETTINGS.value
48+
)
4349

4450
context.call.response.headers.append("HX-Refresh", "true")
4551
context.call.respondText("", ContentType.Text.Html)

foxy/src/main/kotlin/net/cakeyfox/foxy/website/routes/api/v1/dashboard/guild/PostGeneralSettingsRoute.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import mu.KotlinLogging
1010
import net.cakeyfox.common.Constants
1111
import net.cakeyfox.common.FoxyLocale
1212
import net.cakeyfox.foxy.utils.BaseRoute
13+
import net.cakeyfox.common.LogType
1314
import net.cakeyfox.foxy.website.FoxyWebsite
1415
import net.cakeyfox.foxy.website.utils.RouteUtils.checkPermissions
1516
import net.cakeyfox.foxy.website.utils.RouteUtils.htmxRedirect
@@ -26,7 +27,7 @@ class PostGeneralSettingsRoute(val server: FoxyWebsite) :
2627
?: return htmxRedirect(context.call, Constants.INVITE_LINK)
2728

2829
try {
29-
checkPermissions(server, context, locale, context.call) ?: return
30+
val result = checkPermissions(server, context, locale, context.call) ?: return
3031

3132
val params = context.call.receiveParameters()
3233
fun Parameters.getBoolean(name: String) = this[name] == "on"
@@ -53,6 +54,11 @@ class PostGeneralSettingsRoute(val server: FoxyWebsite) :
5354
blockedChannels.addAll(finalBlocked)
5455
}
5556
}
57+
server.foxy.database.guild.addLogToGuild(
58+
guildId,
59+
result.user.id,
60+
LogType.UPDATE_GENERAL_SETTINGS.value
61+
)
5662

5763
context.call.response.headers.append("HX-Refresh", "true")
5864
context.call.respondText("", ContentType.Text.Html)

foxy/src/main/kotlin/net/cakeyfox/foxy/website/routes/api/v1/dashboard/guild/PostServerLogsSettingsRoute.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import mu.KotlinLogging
99
import net.cakeyfox.common.Constants
1010
import net.cakeyfox.common.FoxyLocale
1111
import net.cakeyfox.foxy.utils.BaseRoute
12+
import net.cakeyfox.common.LogType
1213
import net.cakeyfox.foxy.website.FoxyWebsite
1314
import net.cakeyfox.foxy.website.utils.RouteUtils.checkPermissions
1415
import net.cakeyfox.foxy.website.utils.RouteUtils.htmxRedirect
@@ -19,11 +20,11 @@ class PostServerLogsSettingsRoute(val server: FoxyWebsite) : BaseRoute("/api/v1/
1920
override suspend fun handle(context: RoutingContext, locale: FoxyLocale) {
2021
val guildId = context.call.parameters["guildId"] ?: return
2122

22-
val guildData = server.foxy.database.guild.getGuildOrNull(guildId)
23+
server.foxy.database.guild.getGuildOrNull(guildId)
2324
?: return htmxRedirect(context.call, Constants.INVITE_LINK)
2425

2526
try {
26-
checkPermissions(server, context, locale, context.call) ?: return
27+
val result = checkPermissions(server, context, locale, context.call) ?: return
2728

2829
val params = context.call.receiveParameters()
2930
fun Parameters.getBoolean(name: String) = this[name] == "on"
@@ -55,6 +56,11 @@ class PostServerLogsSettingsRoute(val server: FoxyWebsite) : BaseRoute("/api/v1/
5556
}
5657
}
5758
}
59+
server.foxy.database.guild.addLogToGuild(
60+
guildId,
61+
result.user.id,
62+
LogType.UPDATE_SERVER_LOGS_SETTINGS.value
63+
)
5864

5965
context.call.response.headers.append("HX-Refresh", "true")
6066
context.call.respondText("", ContentType.Text.Html)

foxy/src/main/kotlin/net/cakeyfox/foxy/website/routes/api/v1/dashboard/guild/PostWelcomerRoute.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.serialization.json.Json
99
import mu.KotlinLogging
1010
import net.cakeyfox.common.FoxyLocale
1111
import net.cakeyfox.foxy.utils.BaseRoute
12+
import net.cakeyfox.common.LogType
1213
import net.cakeyfox.foxy.website.FoxyWebsite
1314
import net.cakeyfox.foxy.website.utils.RouteUtils.checkPermissions
1415
import net.cakeyfox.serializable.data.utils.DiscordMessageBody
@@ -23,7 +24,7 @@ class PostWelcomerRoute(val server: FoxyWebsite) :
2324
val guildId = context.call.parameters["guildId"] ?: return
2425

2526
try {
26-
checkPermissions(server, context, locale, context.call) ?: return
27+
val result = checkPermissions(server, context, locale, context.call) ?: return
2728

2829
val params = context.call.receiveParameters()
2930
fun Parameters.getBoolean(name: String) = this[name] == "on"
@@ -90,6 +91,12 @@ class PostWelcomerRoute(val server: FoxyWebsite) :
9091
guildJoinLeaveModule.dmWelcomeMessage = body["dmSettings"]
9192
}
9293

94+
server.foxy.database.guild.addLogToGuild(
95+
guildId,
96+
result.user.id,
97+
LogType.UPDATE_WELCOMER_SETTINGS.value
98+
)
99+
93100
context.call.response.headers.append("HX-Refresh", "true")
94101
context.call.respondText("", ContentType.Text.Html)
95102
} catch (e: Exception) {

foxy/src/main/kotlin/net/cakeyfox/foxy/website/routes/api/v1/dashboard/guild/PostYouTubeAddChannelRoute.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.ktor.server.response.respondText
66
import io.ktor.server.routing.RoutingContext
77
import net.cakeyfox.common.FoxyLocale
88
import net.cakeyfox.foxy.utils.BaseRoute
9+
import net.cakeyfox.common.LogType
910
import net.cakeyfox.foxy.website.FoxyWebsite
1011
import net.cakeyfox.foxy.website.utils.RouteUtils
1112
import net.cakeyfox.foxy.website.utils.RouteUtils.checkPermissions
@@ -23,7 +24,7 @@ class PostYouTubeAddChannelRoute(val server: FoxyWebsite) :
2324
val channelQuery = context.call.receive<String>()
2425
if (guildData == null) return
2526

26-
checkPermissions(server, context, locale, context.call) ?: return
27+
val result = checkPermissions(server, context, locale, context.call) ?: return
2728
val channelInfo = server.foxy.youtubeManager.getChannelInfo(channelQuery)
2829
?.items?.get(0) ?: return
2930

@@ -37,6 +38,11 @@ class PostYouTubeAddChannelRoute(val server: FoxyWebsite) :
3738
"",
3839
""
3940
)
41+
server.foxy.database.guild.addLogToGuild(
42+
guildId,
43+
result.user.id,
44+
LogType.UPDATE_YOUTUBE_SETTINGS.value
45+
)
4046
}
4147

4248
RouteUtils.removeFormLock(server, guildId)

foxy/src/main/kotlin/net/cakeyfox/foxy/website/routes/api/v1/dashboard/guild/PostYouTubeRemoveChannelRoute.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.ktor.server.response.respond
55
import io.ktor.server.routing.RoutingContext
66
import net.cakeyfox.common.FoxyLocale
77
import net.cakeyfox.foxy.utils.BaseRoute
8+
import net.cakeyfox.common.LogType
89
import net.cakeyfox.foxy.website.FoxyWebsite
910
import net.cakeyfox.foxy.website.utils.RouteUtils.checkPermissions
1011

@@ -17,12 +18,17 @@ class PostYouTubeRemoveChannelRoute(val server: FoxyWebsite) :
1718

1819
if (guildData == null) return
1920

20-
checkPermissions(server, context, locale, context.call) ?: return
21+
val result = checkPermissions(server, context, locale, context.call) ?: return
2122

2223
val followedChannel = guildData.followedYouTubeChannels.find { it.channelId == channelId }
2324

2425
if (followedChannel != null) {
2526
server.foxy.database.youtube.removeChannelFromGuild(guildId, channelId)
27+
server.foxy.database.guild.addLogToGuild(
28+
guildId,
29+
result.user.id,
30+
LogType.UPDATE_YOUTUBE_SETTINGS.value
31+
)
2632
}
2733

2834
context.call.respond(HttpStatusCode.OK)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package net.cakeyfox.foxy.website.routes.partials.dashboard.guild
2+
3+
import frontend.htmx.partials.renderAuditLog
4+
import frontend.htmx.partials.renderServerLogsPartial
5+
import io.ktor.server.htmx.hx
6+
import io.ktor.server.routing.Routing
7+
import io.ktor.server.routing.get
8+
import io.ktor.server.routing.route
9+
import io.ktor.server.sessions.get
10+
import io.ktor.server.sessions.sessions
11+
import io.ktor.utils.io.ExperimentalKtorApi
12+
import mu.KotlinLogging
13+
import net.cakeyfox.common.FoxyLocale
14+
import net.cakeyfox.foxy.website.FoxyWebsite
15+
import net.cakeyfox.foxy.website.utils.RouteUtils
16+
import net.cakeyfox.foxy.website.utils.RouteUtils.checkSession
17+
import net.cakeyfox.foxy.website.utils.RouteUtils.respondWithPage
18+
import net.cakeyfox.serializable.data.website.UserSession
19+
20+
class GetServerAuditLog {
21+
@OptIn(ExperimentalKtorApi::class)
22+
fun Routing.getServerAuditLogRoute(server: FoxyWebsite) {
23+
route("/{lang}/partials/{guildId}/logs") {
24+
hx.get {
25+
val guildId = call.parameters["guildId"] ?: return@get
26+
checkSession(call, server, call.sessions.get<UserSession>()) ?: return@get
27+
val lang = call.parameters["lang"] ?: return@get
28+
val locale = FoxyLocale(lang)
29+
val guild = server.foxy.database.guild.getGuildOrNull(guildId)!!
30+
31+
try {
32+
33+
respondWithPage(call) {
34+
renderAuditLog(
35+
locale,
36+
guild.dashboardLogs
37+
)
38+
}
39+
} catch (e: Exception) {
40+
println(e.message)
41+
}
42+
}
43+
}
44+
}
45+
}

foxy/src/main/kotlin/net/cakeyfox/foxy/website/utils/RouteManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import net.cakeyfox.foxy.website.routes.pages.GetHomePage
2727
import net.cakeyfox.foxy.website.routes.pages.GetPremiumPage
2828
import net.cakeyfox.foxy.website.routes.pages.GetSupportPage
2929
import net.cakeyfox.foxy.website.routes.pages.GetTermsOfServiceRoute
30+
import net.cakeyfox.foxy.website.routes.partials.dashboard.guild.GetServerAuditLog
3031
import net.cakeyfox.foxy.website.routes.partials.dashboard.guild.GetServerAutoRoleSettingsRoute
3132
import net.cakeyfox.foxy.website.routes.partials.dashboard.guild.GetServerGeneralSettingsPartial
3233
import net.cakeyfox.foxy.website.routes.partials.dashboard.guild.GetServerListPartial
@@ -76,6 +77,7 @@ fun Application.registerAllRoutes(server: FoxyWebsite) {
7677
GetServerAutoRoleSettingsRoute().apply { getAutoRoleSettings(server) }
7778
GetServerLogsSettingsRoute().apply { getServerLogsSettingsRoute(server) }
7879
GetGenericUserInventoryRoute().apply { getUserBackgroundInventory(server) }
80+
GetServerAuditLog().apply { getServerAuditLogRoute(server) }
7981

8082
// ==[OAUTH]==
8183
OAuthManager(server).apply { oauthRoutes() }

foxy/src/main/styles-dashboard/_cirno-card.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
margin-top: 20px;
99
padding: 20px;
1010
border-radius: 4px;
11-
margin-left: 24rem;
1211
margin-bottom: 1rem;
1312
font-family: sans-serif;
1413
background-color: var(--reimu-feature-card-background);

0 commit comments

Comments
 (0)