Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add User Donation Keys #2311

Draft
wants to merge 8 commits into
base: development
Choose a base branch
from
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ class Loritta(discordConfig: GeneralDiscordConfig, discordInstanceConfig: Genera
BannedUsers,
ProfileDesigns,
ProfileDesignsPayments,
DailyProfileShopItems
DailyProfileShopItems,
UserDonationKeys
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import com.mrpowergamerbr.loritta.commands.AbstractCommand
import com.mrpowergamerbr.loritta.commands.CommandContext
import com.mrpowergamerbr.loritta.dao.DonationKey
import com.mrpowergamerbr.loritta.dao.GuildProfile
import com.mrpowergamerbr.loritta.dao.UserDonationKey
import com.mrpowergamerbr.loritta.network.Databases
import com.mrpowergamerbr.loritta.tables.GuildProfiles
import com.mrpowergamerbr.loritta.tables.Profiles
import com.mrpowergamerbr.loritta.utils.*
import com.mrpowergamerbr.loritta.utils.Constants
import com.mrpowergamerbr.loritta.utils.LorittaShards
import com.mrpowergamerbr.loritta.utils.gson
import com.mrpowergamerbr.loritta.utils.locale.BaseLocale
import com.mrpowergamerbr.loritta.utils.loritta
import com.mrpowergamerbr.loritta.utils.lorittaShards
import com.mrpowergamerbr.loritta.utils.lorittaSupervisor
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
Expand All @@ -26,8 +32,12 @@ import net.perfectdreams.loritta.utils.ClusterOfflineException
import net.perfectdreams.loritta.utils.payments.PaymentGateway
import net.perfectdreams.loritta.utils.payments.PaymentReason
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update

class LoriServerListConfigCommand : AbstractCommand("lslc", category = CommandCategory.MAGIC) {
override fun getDescription(locale: BaseLocale): String {
Expand Down Expand Up @@ -187,7 +197,7 @@ class LoriServerListConfigCommand : AbstractCommand("lslc", category = CommandCa
return
}

if (arg0 == "generate_key" && arg1 != null && arg2 != null) {
if (arg0 == "generate_guild_key" && arg1 != null && arg2 != null) {
transaction(Databases.loritta) {
DonationKey.new {
this.userId = arg1.toLong()
Expand All @@ -198,12 +208,29 @@ class LoriServerListConfigCommand : AbstractCommand("lslc", category = CommandCa

context.reply(
LorittaReply(
"Key criada com sucesso!"
"Guild Key criada com sucesso!"
)
)
return
}

if (arg0 == "generate_user_key" && arg1 != null && arg2 != null) {
transaction(Databases.loritta) {
UserDonationKey.new {
this.userId = arg1.toLong()
this.expiresAt = System.currentTimeMillis() + 2_764_800_000
this.value = arg2.toDouble()
}
}

context.reply(
LorittaReply(
"User Key criada com sucesso!"
)
)
return
}

if (arg0 == "inspect_donations" && arg1 != null) {
val id = arg1.toLong()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mrpowergamerbr.loritta.dao

import com.mrpowergamerbr.loritta.tables.UserDonationKeys
import org.jetbrains.exposed.dao.LongEntity
import org.jetbrains.exposed.dao.LongEntityClass
import org.jetbrains.exposed.dao.id.EntityID

class UserDonationKey(id: EntityID<Long>) : LongEntity(id) {
companion object : LongEntityClass<UserDonationKey>(UserDonationKeys)

var userId by UserDonationKeys.userId
var value by UserDonationKeys.value
var expiresAt by UserDonationKeys.expiresAt
var metadata by UserDonationKeys.metadata
val activeIn by Profile optionalReferencedOn UserDonationKeys.activeIn

/**
* Returns if the key is still active
*/
fun isActive() = expiresAt >= System.currentTimeMillis()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mrpowergamerbr.loritta.tables

import com.mrpowergamerbr.loritta.utils.exposed.rawJsonb
import com.mrpowergamerbr.loritta.utils.gson
import org.jetbrains.exposed.dao.id.LongIdTable

object UserDonationKeys : LongIdTable() {
val activeIn = optReference("active_in", Profiles)
val userId = long("user")
val value = double("value")
val expiresAt = long("expires_at")
val metadata = rawJsonb("metadata", gson).nullable()
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mrpowergamerbr.loritta.commands.vanilla.magic.*
import com.mrpowergamerbr.loritta.dao.*
import com.mrpowergamerbr.loritta.network.Databases
import com.mrpowergamerbr.loritta.profile.ProfileDesignManager
import com.mrpowergamerbr.loritta.tables.UserDonationKeys
import com.mrpowergamerbr.loritta.utils.Constants
import com.mrpowergamerbr.loritta.utils.config.*
import com.mrpowergamerbr.loritta.utils.locale.*
Expand All @@ -32,7 +33,6 @@ import net.perfectdreams.loritta.commands.vanilla.administration.*
import net.perfectdreams.loritta.commands.vanilla.economy.*
import net.perfectdreams.loritta.commands.vanilla.magic.*
import net.perfectdreams.loritta.commands.vanilla.social.*
import net.perfectdreams.loritta.dao.Payment
import net.perfectdreams.loritta.platform.discord.commands.DiscordCommandMap
import net.perfectdreams.loritta.platform.discord.plugin.JVMPluginManager
import net.perfectdreams.loritta.platform.discord.utils.*
Expand All @@ -41,7 +41,6 @@ import net.perfectdreams.loritta.utils.*
import net.perfectdreams.loritta.utils.config.*
import net.perfectdreams.loritta.utils.extensions.readImage
import net.perfectdreams.loritta.utils.locale.DebugLocales
import net.perfectdreams.loritta.utils.payments.PaymentReason
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import java.awt.image.BufferedImage
Expand Down Expand Up @@ -701,11 +700,9 @@ abstract class LorittaDiscord(var discordConfig: GeneralDiscordConfig, var disco
}

fun _getActiveMoneyFromDonations(userId: Long): Double {
return Payment.find {
(Payments.expiresAt greaterEq System.currentTimeMillis()) and
(Payments.reason eq PaymentReason.DONATION) and
(Payments.userId eq userId)
}.sumByDouble { it.money.toDouble() }
return UserDonationKey.find { UserDonationKeys.activeIn eq userId and (UserDonationKeys.expiresAt greaterEq System.currentTimeMillis()) }
.toList()
.sumByDouble { it.value }
}

fun launchMessageJob(event: Event, block: suspend CoroutineScope.() -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@ import com.google.gson.JsonParser
import com.mrpowergamerbr.loritta.dao.ServerConfig
import com.mrpowergamerbr.loritta.website.LoriWebCode
import com.mrpowergamerbr.loritta.website.WebsiteAPIException
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.*
import io.ktor.application.ApplicationCall
import io.ktor.http.HttpStatusCode
import io.ktor.request.receiveText
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.AutorolePayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.CustomBadgePayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.DailyMultiplierPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.EconomyPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.GeneralConfigPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.LevelPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.MiscellaneousPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.ModerationPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.ResetXpPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.RssFeedsPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.TimersPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.TwitchPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.TwitterPayload
import com.mrpowergamerbr.loritta.website.views.subviews.api.config.types.YouTubePayload
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.request.*
import net.dv8tion.jda.api.entities.Guild
import net.perfectdreams.loritta.platform.discord.LorittaDiscord
import net.perfectdreams.loritta.utils.ActionType
Expand Down Expand Up @@ -63,7 +76,6 @@ class PatchServerConfigRoute(loritta: LorittaDiscord) : RequiresAPIGuildAuthRout
"miscellaneous" to MiscellaneousPayload::class.java,
"economy" to EconomyPayload::class.java,
"timers" to TimersPayload::class.java,
"premium" to PremiumKeyPayload::class.java,
"badge" to CustomBadgePayload::class.java,
"daily_multiplier" to DailyMultiplierPayload::class.java,
"level" to LevelPayload::class.java,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import com.google.gson.JsonParser
import com.mrpowergamerbr.loritta.Loritta
import com.mrpowergamerbr.loritta.dao.Background
import com.mrpowergamerbr.loritta.dao.ProfileDesign
import com.mrpowergamerbr.loritta.dao.UserDonationKey
import com.mrpowergamerbr.loritta.tables.UserDonationKeys
import com.mrpowergamerbr.loritta.utils.Constants
import com.mrpowergamerbr.loritta.utils.lorittaShards
import com.mrpowergamerbr.loritta.utils.networkbans.ApplyBansTask
import com.mrpowergamerbr.loritta.website.LoriWebCode
import com.mrpowergamerbr.loritta.website.WebsiteAPIException
Expand All @@ -18,18 +21,19 @@ import kotlinx.serialization.json.Json
import mu.KotlinLogging
import net.perfectdreams.loritta.platform.discord.LorittaDiscord
import net.perfectdreams.loritta.serializable.UserIdentification
import net.perfectdreams.loritta.tables.*
import net.perfectdreams.loritta.tables.BackgroundPayments
import net.perfectdreams.loritta.tables.Backgrounds
import net.perfectdreams.loritta.tables.BannedIps
import net.perfectdreams.loritta.tables.ProfileDesigns
import net.perfectdreams.loritta.tables.ProfileDesignsPayments
import net.perfectdreams.loritta.website.routes.BaseRoute
import net.perfectdreams.loritta.website.session.LorittaJsonWebSession
import net.perfectdreams.loritta.website.utils.WebsiteUtils
import net.perfectdreams.loritta.website.utils.extensions.respondJson
import net.perfectdreams.loritta.website.utils.extensions.trueIp
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import kotlin.collections.firstOrNull
import kotlin.collections.map
import kotlin.collections.set
import kotlin.collections.toSet

class GetSelfInfoRoute(loritta: LorittaDiscord) : BaseRoute(loritta, "/api/v1/users/@me/{sections?}") {
companion object {
Expand Down Expand Up @@ -118,6 +122,23 @@ class GetSelfInfoRoute(loritta: LorittaDiscord) : BaseRoute(loritta, "/api/v1/us
)
}

if ("userdonationkeys" in sections) {
val userDonationKeys = loritta.newSuspendedTransaction {
UserDonationKey.find { UserDonationKeys.userId eq userIdentification.id.toLong() and (UserDonationKeys.expiresAt greaterEq System.currentTimeMillis()) }
.toList()
}

payload["userDonationKeys"] = userDonationKeys.map {
jsonObject(
"id" to it.id.value,
"value" to it.value,
"expiresAt" to it.expiresAt,
"user" to WebsiteUtils.transformToJson(lorittaShards.retrieveUserById(it.userId)!!),
"activeIn" to loritta.newSuspendedTransaction { it.activeIn }
)
}.toJsonArray()
}

if ("settings" in sections) {
val settings = loritta.newSuspendedTransaction {
profile.settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mrpowergamerbr.loritta.tables.Reminders
import com.mrpowergamerbr.loritta.tables.Reputations
import com.mrpowergamerbr.loritta.tables.ShipEffects
import com.mrpowergamerbr.loritta.tables.StoredMessages
import com.mrpowergamerbr.loritta.tables.UserDonationKeys
import com.mrpowergamerbr.loritta.utils.Constants
import io.ktor.application.*
import io.ktor.sessions.*
Expand Down Expand Up @@ -44,11 +45,16 @@ class PostDeleteDataRoute(loritta: LorittaDiscord) : RequiresAPIDiscordLoginRout
Dailies.receivedById eq userId
}

logger.info { "Deleting $userId's donation keys..." }
logger.info { "Deleting $userId's server donation keys..." }
DonationKeys.deleteWhere {
DonationKeys.userId eq userId
}

logger.info { "Deleting $userId's user donation keys..." }
UserDonationKeys.deleteWhere {
UserDonationKeys.userId eq userId
}

logger.info { "Deleting $userId's guild profiles..." }
GuildProfiles.deleteWhere {
GuildProfiles.userId eq userId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object ConfigTransformers {
TwitchConfigTransformer,
TwitterConfigTransformer,
TextChannelsTransformer,
UserDonationKeysTransformer,
GuildDonationKeysTransformer,
ActiveDonationKeysTransformer,
GuildInfoTransformer,
GeneralConfigTransformer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import com.google.gson.JsonObject
import com.mrpowergamerbr.loritta.dao.DonationKey
import com.mrpowergamerbr.loritta.dao.ServerConfig
import com.mrpowergamerbr.loritta.tables.DonationKeys
import net.perfectdreams.loritta.website.utils.WebsiteUtils
import com.mrpowergamerbr.loritta.utils.loritta
import com.mrpowergamerbr.loritta.utils.lorittaShards
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import net.dv8tion.jda.api.entities.Guild
import net.perfectdreams.loritta.website.session.LorittaJsonWebSession
import net.perfectdreams.loritta.website.utils.WebsiteUtils
import org.jetbrains.exposed.sql.and

object UserDonationKeysTransformer : ConfigTransformer {
object GuildDonationKeysTransformer : ConfigTransformer {
override val payloadType: String = "userkeys"
override val configKey: String = "donationKeys"

Expand Down