<commit_before>package com.mrpowergamerbr.loritta.website.views.subviews.configure

import com.mrpowergamerbr.loritta.Loritta
import com.mrpowergamerbr.loritta.oauth2.TemmieDiscordAuth
import com.mrpowergamerbr.loritta.userdata.ServerConfig
import com.mrpowergamerbr.loritta.utils.*
import com.mrpowergamerbr.loritta.website.LorittaWebsite
import com.mrpowergamerbr.loritta.website.views.subviews.ProtectedView
import net.dv8tion.jda.core.entities.Guild
import org.jooby.Request
import org.jooby.Response

abstract class ConfigureView : ProtectedView() {
	override fun handleRender(req: Request, res: Response, path: String, variables: MutableMap<String, Any?>): Boolean {
		return path.matches(Regex("^/dashboard/configure/[0-9]+"))
	}

	override fun renderProtected(req: Request, res: Response, path: String, variables: MutableMap<String, Any?>, discordAuth: TemmieDiscordAuth): String {
		val userIdentification = req.ifGet<TemmieDiscordAuth.UserIdentification>("userIdentification").get()
		val split = path.split("/");
		if (4 > split.size) {
			return "Servidor não encontrado!"
		}
		val guildId = split[3]

		val temmieGuild = discordAuth.getUserGuilds().firstOrNull { it.id == guildId }
		val jdaGuild = lorittaShards.getGuildById(guildId)
		val serverConfig = loritta.getServerConfigForGuild(guildId)

		if (jdaGuild == null) {
			return "Eu não estou neste servidor ou as minhas shards ainda não reiniciaram!"
		}

		val id = userIdentification.id
		val member = jdaGuild.getMemberById(id)
		var canAccessDashboardViaPermission = false

		if (member != null) {
			val lorittaUser = GuildLorittaUser(member, serverConfig, loritta.getOrCreateLorittaProfile(id.toLong()))

			canAccessDashboardViaPermission = lorittaUser.hasPermission(LorittaPermission.ALLOW_ACCESS_TO_DASHBOARD)
		}

		val canBypass = userIdentification.id == Loritta.config.ownerId || canAccessDashboardViaPermission
		if ((!canBypass) && (temmieGuild == null || !LorittaWebsite.canManageGuild(temmieGuild))) {
			return "Você não tem permissão!"
		}

		variables["guild"] = jdaGuild
		variables["serverConfig"] = serverConfig
		variables["serverConfigJson"] = gson.toJson(WebsiteUtils.getServerConfigAsJson(jdaGuild, serverConfig, userIdentification))
		return renderConfiguration(req, res, path, variables, discordAuth, jdaGuild, serverConfig)
	}

	abstract fun renderConfiguration(req: Request, res: Response, path: String, variables: MutableMap<String, Any?>, discordAuth: TemmieDiscordAuth, guild: Guild, serverConfig: ServerConfig): String
}<commit_msg>Evitar fazer um query a API do Discord apenas para pegar se o usuário pode administrar ou gerenciar o servidor<commit_after>package com.mrpowergamerbr.loritta.website.views.subviews.configure

import com.mrpowergamerbr.loritta.Loritta
import com.mrpowergamerbr.loritta.oauth2.TemmieDiscordAuth
import com.mrpowergamerbr.loritta.userdata.ServerConfig
import com.mrpowergamerbr.loritta.utils.*
import com.mrpowergamerbr.loritta.website.views.subviews.ProtectedView
import net.dv8tion.jda.core.Permission
import net.dv8tion.jda.core.entities.Guild
import org.jooby.Request
import org.jooby.Response

abstract class ConfigureView : ProtectedView() {
	override fun handleRender(req: Request, res: Response, path: String, variables: MutableMap<String, Any?>): Boolean {
		return path.matches(Regex("^/dashboard/configure/[0-9]+"))
	}

	override fun renderProtected(req: Request, res: Response, path: String, variables: MutableMap<String, Any?>, discordAuth: TemmieDiscordAuth): String {
		val userIdentification = req.ifGet<TemmieDiscordAuth.UserIdentification>("userIdentification").get()
		val split = path.split("/");
		if (4 > split.size) {
			return "Servidor não encontrado!"
		}
		val guildId = split[3]

		val temmieGuild = discordAuth.getUserGuilds().firstOrNull { it.id == guildId }
		val jdaGuild = lorittaShards.getGuildById(guildId)
		val serverConfig = loritta.getServerConfigForGuild(guildId)

		if (jdaGuild == null) {
			return "Eu não estou neste servidor ou as minhas shards ainda não reiniciaram!"
		}

		val id = userIdentification.id
		val member = jdaGuild.getMemberById(id)
		var canAccessDashboardViaPermission = false

		if (member != null) {
			val lorittaUser = GuildLorittaUser(member, serverConfig, loritta.getOrCreateLorittaProfile(id.toLong()))

			canAccessDashboardViaPermission = lorittaUser.hasPermission(LorittaPermission.ALLOW_ACCESS_TO_DASHBOARD)
		}

		val canBypass = userIdentification.id == Loritta.config.ownerId || canAccessDashboardViaPermission
		if (!canBypass && !(member.hasPermission(Permission.ADMINISTRATOR) || member.hasPermission(Permission.MANAGE_SERVER))) {
			return "Você não tem permissão!"
		}

		variables["guild"] = jdaGuild
		variables["serverConfig"] = serverConfig
		variables["serverConfigJson"] = gson.toJson(WebsiteUtils.getServerConfigAsJson(jdaGuild, serverConfig, userIdentification))
		return renderConfiguration(req, res, path, variables, discordAuth, jdaGuild, serverConfig)
	}

	abstract fun renderConfiguration(req: Request, res: Response, path: String, variables: MutableMap<String, Any?>, discordAuth: TemmieDiscordAuth, guild: Guild, serverConfig: ServerConfig): String
}
{'fraction_non_alphanumeric': 0.10324199150906986, 'fraction_numerical': 0.0025086839058278657, 'mean_word_length': 6.228730822873082, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2897750', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1655, 'n_words': 344}