Skip to content

Commit 5c9594b

Browse files
committed
refactor(db): only fetch thread_status entries with active threads
1 parent a598587 commit 5c9594b

4 files changed

Lines changed: 17 additions & 23 deletions

File tree

rustmail/src/commands/new_thread/slash_command/new_thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl RegistrableCommand for NewThreadCommand {
137137
}
138138

139139
let inbox_category_id = ChannelId::new(config.thread.inbox_category_id);
140-
let channel_name = user.name.to_lowercase().replace(" ", "-").to_string();
140+
let channel_name = format!("🔵・{}・0m", user.name);
141141
let mut channel_builder = serenity::all::CreateChannel::new(&channel_name);
142142
channel_builder = channel_builder
143143
.kind(serenity::model::channel::ChannelType::Text)

rustmail/src/commands/new_thread/text_command/new_thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub async fn new_thread(
5858
}
5959

6060
let inbox_category_id = ChannelId::new(config.thread.inbox_category_id);
61-
let channel_name = user.name.to_lowercase().replace(" ", "-").to_string();
61+
let channel_name = format!("🔵・{}・0m", user.name);
6262
let mut channel_builder = serenity::all::CreateChannel::new(&channel_name);
6363
channel_builder = channel_builder
6464
.kind(serenity::model::channel::ChannelType::Text)

rustmail/src/db/operations/threads.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,14 @@ pub async fn is_orphaned_thread_channel(
420420
pub async fn get_all_thread_status(pool: &SqlitePool) -> Vec<TicketState> {
421421
match sqlx::query!(
422422
r#"
423-
SELECT channel_id, owner_id, taken_by, last_message_by, last_message_at
424-
FROM thread_status
423+
SELECT ts.channel_id,
424+
ts.owner_id,
425+
ts.taken_by,
426+
ts.last_message_by,
427+
ts.last_message_at
428+
FROM thread_status ts
429+
JOIN threads t ON ts.thread_id = t.id
430+
WHERE t.status = 1
425431
"#
426432
)
427433
.fetch_all(pool)

rustmail/src/modules/threads_status.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::time::Duration;
21
use crate::prelude::errors::*;
32
use crate::prelude::types::*;
43
use chrono::Utc;
54
use serenity::all::{ChannelId, UserId};
65
use serenity::builder::EditChannel;
76
use serenity::client::Context;
7+
use std::time::Duration;
88
use tokio::time::timeout;
99

1010
pub async fn update_thread_status_ui(ctx: &Context, ticket: &TicketState) -> ModmailResult<()> {
@@ -24,14 +24,7 @@ pub async fn update_thread_status_ui(ctx: &Context, ticket: &TicketState) -> Mod
2424
};
2525

2626
let owner_id = ticket.owner_id.parse().unwrap_or(0);
27-
let owner_name = if owner_id != 0 {
28-
ctx.cache
29-
.user(UserId::new(owner_id))
30-
.map(|u| u.name.clone())
31-
.unwrap_or_else(|| format!("User-{}", owner_id))
32-
} else {
33-
"Unknown".to_string()
34-
};
27+
let owner_name = UserId::new(owner_id).to_user(&ctx.http).await?.name;
3528

3629
let mut name = format!("{color}・{}", owner_name);
3730

@@ -51,25 +44,20 @@ pub async fn update_thread_status_ui(ctx: &Context, ticket: &TicketState) -> Mod
5144

5245
let result = timeout(
5346
Duration::from_secs(2),
54-
channel.edit(&ctx.http, EditChannel::new().name(&name))
55-
).await;
47+
channel.edit(&ctx.http, EditChannel::new().name(&name)),
48+
)
49+
.await;
5650

5751
match result {
5852
Ok(Ok(_)) => Ok(()),
5953

6054
Ok(Err(e)) => {
61-
eprintln!(
62-
"Failed to edit channel {}: {:?}",
63-
ticket.channel_id, e
64-
);
55+
eprintln!("Failed to edit channel {}: {:?}", ticket.channel_id, e);
6556
Err(e.into())
6657
}
6758

6859
Err(_) => {
69-
eprintln!(
70-
"Timeout editing channel {} (skipping)",
71-
ticket.channel_id
72-
);
60+
eprintln!("Timeout editing channel {} (skipping)", ticket.channel_id);
7361
Ok(())
7462
}
7563
}

0 commit comments

Comments
 (0)