Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/rustmail/src/commands/add_reminder/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use chrono::Local;
use serenity::all::{ChannelId, CommandInteraction, Context, GuildId, Message, RoleId, UserId};
use sqlx::SqlitePool;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use std::time::Duration;
use tokio::select;
use tokio::sync::watch::Receiver;
Expand Down Expand Up @@ -109,6 +109,9 @@ pub async fn send_register_confirmation_from_command(
.await;
}

/// One timer per reminder id.
static REMINDER_TIMERS: LazyLock<KeyedTasks<i64>> = LazyLock::new(KeyedTasks::new);

pub fn spawn_reminder(
reminder: &Reminder,
reminder_id: Option<i64>,
Expand All @@ -123,7 +126,7 @@ pub fn spawn_reminder(
let reminder = reminder.clone();
let mut shutdown_rx = shutdown.as_ref().clone();

tokio::spawn(async move {
REMINDER_TIMERS.spawn(reminder.id, async move {
let now = Local::now().timestamp();
let delay_duration = if reminder.data.trigger_time > now {
reminder.data.trigger_time - now
Expand Down
2 changes: 1 addition & 1 deletion crates/rustmail/src/commands/delete/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub async fn delete_dm_message(ctx: &Context, user_id: i64, message_ids: &Messag
if let Some(dm_msg_id) = &message_ids.dm_message_id
&& let Ok(msg_id) = dm_msg_id.parse::<u64>()
{
if let Ok(user) = ctx.http.get_user(UserId::new(user_id as u64)).await {
if let Ok(user) = UserId::new(user_id as u64).to_user(ctx).await {
if let Ok(dm_channel) = user.create_dm_channel(&ctx.http).await {
match dm_channel.message(&ctx.http, msg_id).await {
Ok(dm_message) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl RegistrableCommand for NewThreadCommand {
}
};

let user = match ctx.http.get_user(user_id).await {
let user = match user_id.to_user(&ctx).await {
Ok(user) => user,
Err(_) => return Err(ModmailError::Discord(DiscordError::UserNotFound)),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn new_thread(

let user_id = user_id.unwrap();

let user = match ctx.http.get_user(user_id).await {
let user = match user_id.to_user(&ctx).await {
Ok(user) => user,
Err(_) => return Err(ModmailError::Discord(DiscordError::UserNotFound)),
};
Expand Down
11 changes: 5 additions & 6 deletions crates/rustmail/src/commands/release/slash_command/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ impl RegistrableCommand for ReleaseCommand {
}
};
ticket_status.taken_by = None;
let _ = update_thread_status_db(
&thread.id.to_string(),
&ticket_status,
&db_pool,
)
.await;
if let Err(e) =
update_thread_status_db(&thread.id, &ticket_status, &db_pool).await
{
eprintln!("Failed to update thread status for {}: {e:?}", thread.id);
}

let applied = update_thread_status_ui(&ctx, &ticket_status)
.await
Expand Down
5 changes: 4 additions & 1 deletion crates/rustmail/src/commands/release/text_command/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ pub async fn release(
}
};
ticket_status.taken_by = None;
let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
if let Err(e) = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await
{
eprintln!("Failed to update thread status for {}: {e:?}", thread.id);
}

let applied = update_thread_status_ui(&ctx, &ticket_status)
.await
Expand Down
6 changes: 5 additions & 1 deletion crates/rustmail/src/commands/rename/slash_command/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ impl RegistrableCommand for RenameCommand {
};

ticket_status.label = new_label.clone();
let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
if let Err(e) =
update_thread_status_db(&thread.id, &ticket_status, &db_pool).await
{
eprintln!("Failed to update thread status for {}: {e:?}", thread.id);
}

let has_label = new_label.is_some();
let applied = update_thread_status_ui(&ctx, &ticket_status)
Expand Down
4 changes: 3 additions & 1 deletion crates/rustmail/src/commands/rename/text_command/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ pub async fn rename_ticket(
ticket_status.label = Some(new_label.clone());
}

let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
if let Err(e) = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await {
eprintln!("Failed to update thread status for {}: {e:?}", thread.id);
}

let has_label = ticket_status.label.is_some();
let applied = update_thread_status_ui(&ctx, &ticket_status)
Expand Down
6 changes: 5 additions & 1 deletion crates/rustmail/src/commands/take/slash_command/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ impl RegistrableCommand for TakeCommand {
}
};
ticket_status.taken_by = Some(command.user.id.to_string());
let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
if let Err(e) =
update_thread_status_db(&thread.id, &ticket_status, &db_pool).await
{
eprintln!("Failed to update thread status for {}: {e:?}", thread.id);
}

let applied = update_thread_status_ui(&ctx, &ticket_status)
.await
Expand Down
5 changes: 4 additions & 1 deletion crates/rustmail/src/commands/take/text_command/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ pub async fn take(
}
};
ticket_status.taken_by = Some(msg.author.id.to_string());
let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
if let Err(e) = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await
{
eprintln!("Failed to update thread status for {}: {e:?}", thread.id);
}

let applied = update_thread_status_ui(&ctx, &ticket_status)
.await
Expand Down
5 changes: 4 additions & 1 deletion crates/rustmail/src/db/operations/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ pub async fn init_database() -> Result<SqlitePool, sqlx::Error> {
.busy_timeout(Duration::from_secs(5));

let pool = SqlitePoolOptions::new()
.max_connections(30)
// SQLite has a single writer; extra connections only add lock contention.
.max_connections(10)
// Fail fast instead of piling up callers for 30s when the pool is exhausted.
.acquire_timeout(Duration::from_secs(10))
.connect_with(connect_options)
.await?;

Expand Down
4 changes: 2 additions & 2 deletions crates/rustmail/src/db/operations/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn insert_staff_message(
let user_id = staff_user_id.get() as i64;

let user_name = staff_user_id
.to_user(&ctx.http)
.to_user(ctx)
.await
.map(|user| user.name)
.unwrap_or_else(|_| "Unknown".to_string());
Expand Down Expand Up @@ -273,7 +273,7 @@ pub async fn insert_internal_message(
let user_name = msg
.author
.id
.to_user(&ctx.http)
.to_user(ctx)
.await
.map(|user| user.name)
.unwrap_or_else(|_| "Unknown".to_string());
Expand Down
Loading