Skip to content

Commit

Permalink
Stop fetching premium status multiple times in same function
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed May 5, 2024
1 parent 28be6da commit 0f8b9cc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
18 changes: 12 additions & 6 deletions tts_commands/src/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use tts_core::{
constants::OPTION_SEPERATORS,
opt_ext::OptionTryUnwrap,
require_guild,
structs::{ApplicationContext, Command, CommandResult, Context, TTSMode},
structs::{ApplicationContext, Command, CommandResult, Context, IsPremium, TTSMode},
traits::PoiseContextExt as _,
translations::GetTextContextExt,
};
Expand Down Expand Up @@ -128,14 +128,20 @@ pub async fn tts(
async fn _tts(ctx: Context<'_>, author: &serenity::User, message: &str) -> CommandResult {
let attachment = {
let data = ctx.data();
let (voice, mode) = data.parse_user_or_guild(author.id, ctx.guild_id()).await?;
let guild_info = if let Some(guild_id) = ctx.guild_id() {
Some((guild_id, data.is_premium_simple(guild_id).await?))
} else {
None
};

let guild_row;
let translation_lang = if let Some(guild_id) = ctx.guild_id() {
let is_premium = data.is_premium_simple(guild_id).await?;
let (voice, mode) = data
.parse_user_or_guild_with_premium(author.id, guild_info)
.await?;

let guild_row;
let translation_lang = if let Some((guild_id, is_premium)) = guild_info {
guild_row = data.guilds_db.get(guild_id.into()).await?;
guild_row.target_lang(is_premium)
guild_row.target_lang(IsPremium::from(is_premium))
} else {
None
};
Expand Down
6 changes: 3 additions & 3 deletions tts_core/src/database_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use typesize::derive::TypeSize;

use poise::serenity_prelude::{ChannelId, GuildId, RoleId, UserId};

use crate::structs::TTSMode;
use crate::structs::{IsPremium, TTSMode};

const MAX_VOICE_LENGTH: usize = 20;

Expand Down Expand Up @@ -67,10 +67,10 @@ pub struct GuildRow {
}

impl GuildRow {
pub fn target_lang(&self, is_premium: bool) -> Option<&str> {
pub fn target_lang(&self, is_premium: IsPremium) -> Option<&str> {
if let Some(target_lang) = &self.target_lang
&& self.to_translate()
&& is_premium
&& is_premium.into()
{
Some(target_lang.as_str())
} else {
Expand Down
6 changes: 6 additions & 0 deletions tts_core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ macro_rules! bool_enum {
$false_value,
}

impl From<$name> for bool {
fn from(value: $name) -> bool {
value == $name::$true_value
}
}

impl From<bool> for $name {
fn from(value: bool) -> Self {
if value {
Expand Down
27 changes: 20 additions & 7 deletions tts_core/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use typesize::derive::TypeSize;
use poise::serenity_prelude::{self as serenity};
use serenity::small_fixed_array::{FixedArray, FixedString};

use crate::{analytics, database};
use crate::{analytics, bool_enum, database};

macro_rules! into_static_display {
($struct:ident) => {
Expand Down Expand Up @@ -105,6 +105,8 @@ impl JoinVCToken {
}
}

bool_enum!(IsPremium(No | Yes));

pub enum FailurePoint {
NotSubscribed(serenity::UserId),
PremiumUser,
Expand Down Expand Up @@ -291,14 +293,25 @@ impl Data {
author_id: serenity::UserId,
guild_id: Option<serenity::GuildId>,
) -> Result<(Cow<'static, str>, TTSMode)> {
let (user_row, premium_check_res) = tokio::try_join!(
self.userinfo_db.get(author_id.into()),
self.premium_check(guild_id)
)?;
let info = if let Some(guild_id) = guild_id {
Some((guild_id, self.is_premium_simple(guild_id).await?))
} else {
None
};

let guild_is_premium = premium_check_res.is_none();
let mut guild_row = None;
self.parse_user_or_guild_with_premium(author_id, info).await
}

pub async fn parse_user_or_guild_with_premium(
&self,
author_id: serenity::UserId,
guild_info: Option<(serenity::GuildId, bool)>,
) -> Result<(Cow<'static, str>, TTSMode)> {
let user_row = self.userinfo_db.get(author_id.into()).await?;
let (guild_id, guild_is_premium) =
guild_info.map_or((None, false), |(id, p)| (Some(id), p));

let mut guild_row = None;
let mut mode = {
let user_mode = if guild_is_premium {
user_row.premium_voice_mode
Expand Down
9 changes: 5 additions & 4 deletions tts_events/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tts_core::{
errors,
opt_ext::OptionTryUnwrap,
require,
structs::{Data, FrameworkContext, JoinVCToken, Result, TTSMode},
structs::{Data, FrameworkContext, IsPremium, JoinVCToken, Result, TTSMode},
traits::SongbirdManagerExt,
};

Expand Down Expand Up @@ -47,6 +47,7 @@ async fn process_tts_msg(
Ok(())
);

let is_premium = data.is_premium_simple(guild_id).await?;
let (voice, mode) = {
if let Some(channel_id) = to_autojoin {
let join_vc_lock = JoinVCToken::acquire(&data, guild_id);
Expand All @@ -72,8 +73,9 @@ async fn process_tts_msg(
};

let (voice, mode) = data
.parse_user_or_guild(message.author.id, Some(guild_id))
.parse_user_or_guild_with_premium(message.author.id, Some((guild_id, is_premium)))
.await?;

let nickname_row = data
.nickname_db
.get([guild_id.into(), message.author.id.into()])
Expand All @@ -97,7 +99,6 @@ async fn process_tts_msg(
(voice, mode)
};

let is_premium = data.is_premium_simple(guild_id).await?;
let speaking_rate = data.speaking_rate(message.author.id, mode).await?;
let url = prepare_url(
data.config.tts_service.clone(),
Expand All @@ -106,7 +107,7 @@ async fn process_tts_msg(
mode,
&speaking_rate,
&guild_row.msg_length.to_arraystring(),
guild_row.target_lang(is_premium),
guild_row.target_lang(IsPremium::from(is_premium)),
);

let call_lock = if let Some(call) = data.songbird.get(guild_id) {
Expand Down

0 comments on commit 0f8b9cc

Please sign in to comment.