Skip to content

Commit 14f5c8d

Browse files
committed
refactor(status): add tokio task for updating channel's names
1 parent 0136217 commit 14f5c8d

11 files changed

Lines changed: 164 additions & 92 deletions

File tree

rustmail/src/commands/anonreply/text_command/anonreply.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::modules::update_thread_status_ui;
12
use crate::prelude::config::*;
23
use crate::prelude::db::*;
34
use crate::prelude::errors::*;
@@ -8,7 +9,6 @@ use chrono::Utc;
89
use serenity::all::{Context, GuildId, Message, UserId};
910
use std::collections::HashMap;
1011
use std::sync::Arc;
11-
use crate::modules::update_thread_status_ui;
1212

1313
pub async fn anonreply(
1414
ctx: Context,
@@ -79,7 +79,13 @@ pub async fn anonreply(
7979
ticket_status.last_message_by = TicketAuthor::Staff;
8080
ticket_status.last_message_at = Utc::now().timestamp();
8181
update_thread_status_db(&thread.id, &ticket_status, db_pool).await?;
82-
update_thread_status_ui(&ctx, &ticket_status).await?;
82+
83+
tokio::spawn({
84+
let ctx = ctx.clone();
85+
async move {
86+
let _ = update_thread_status_ui(&ctx, &ticket_status).await;
87+
}
88+
});
8389

8490
let _ = msg.delete(&ctx.http).await;
8591

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 = format!("🔵・{}・0m", user.name);
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 = format!("🔵・{}・0m", user.name);
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/commands/release/slash_command/release.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::modules::update_thread_status_ui;
12
use crate::prelude::commands::*;
23
use crate::prelude::config::*;
34
use crate::prelude::db::*;
@@ -83,29 +84,45 @@ impl RegistrableCommand for ReleaseCommand {
8384
if thread_name == thread.user_name {
8485
return Err(ModmailError::Command(CommandError::TicketAlreadyReleased));
8586
}
86-
87-
tokio::spawn(async move {
88-
let _ = rename_channel_with_timeout(
89-
&ctx,
90-
&config,
91-
thread_id,
92-
thread.user_name.clone(),
93-
None,
94-
Some(&command),
95-
)
96-
.await;
97-
98-
let mut params = std::collections::HashMap::new();
99-
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
10087

101-
let response = MessageBuilder::system_message(&ctx, &config)
102-
.translated_content("release.confirmation", Some(&params), None, None)
103-
.await
104-
.to_channel(command.channel_id)
105-
.build_interaction_message_followup()
88+
tokio::spawn({
89+
let db_pool = db_pool.clone();
90+
91+
async move {
92+
let mut ticket_status = match get_thread_status(&thread.id, &db_pool).await
93+
{
94+
Some(status) => status,
95+
None => {
96+
return;
97+
}
98+
};
99+
ticket_status.taken_by = None;
100+
let _ = update_thread_status_db(
101+
&thread.id.to_string(),
102+
&ticket_status,
103+
&db_pool,
104+
)
106105
.await;
107106

108-
let _ = command.create_followup(ctx.clone(), response).await;
107+
tokio::spawn({
108+
let ctx = ctx.clone();
109+
async move {
110+
let _ = update_thread_status_ui(&ctx, &ticket_status).await;
111+
}
112+
});
113+
114+
let mut params = std::collections::HashMap::new();
115+
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
116+
117+
let response = MessageBuilder::system_message(&ctx, &config)
118+
.translated_content("release.confirmation", Some(&params), None, None)
119+
.await
120+
.to_channel(command.channel_id)
121+
.build_interaction_message_followup()
122+
.await;
123+
124+
let _ = command.create_followup(ctx.clone(), response).await;
125+
}
109126
});
110127

111128
Ok(())

rustmail/src/commands/release/text_command/release.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::modules::update_thread_status_ui;
12
use crate::prelude::commands::*;
23
use crate::prelude::config::*;
34
use crate::prelude::db::*;
@@ -39,26 +40,36 @@ pub async fn release(
3940

4041
let config_clone = config.clone();
4142

42-
tokio::spawn(async move {
43-
let _ = rename_channel_with_timeout(
44-
&ctx,
45-
&config_clone,
46-
thread_id,
47-
thread.user_name.clone(),
48-
Some(&msg),
49-
None,
50-
)
51-
.await;
43+
tokio::spawn({
44+
let db_pool = db_pool.clone();
5245

53-
let mut params = std::collections::HashMap::new();
54-
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
46+
async move {
47+
let mut ticket_status = match get_thread_status(&thread.id, &db_pool).await {
48+
Some(status) => status,
49+
None => {
50+
return;
51+
}
52+
};
53+
ticket_status.taken_by = None;
54+
let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
5555

56-
let _ = MessageBuilder::system_message(&ctx, &config_clone)
57-
.translated_content("release.confirmation", Some(&params), None, None)
58-
.await
59-
.to_channel(msg.channel_id)
60-
.send(true)
61-
.await;
56+
tokio::spawn({
57+
let ctx = ctx.clone();
58+
async move {
59+
let _ = update_thread_status_ui(&ctx, &ticket_status).await;
60+
}
61+
});
62+
63+
let mut params = std::collections::HashMap::new();
64+
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
65+
66+
let _ = MessageBuilder::system_message(&ctx, &config_clone)
67+
.translated_content("release.confirmation", Some(&params), None, None)
68+
.await
69+
.to_channel(msg.channel_id)
70+
.send(true)
71+
.await;
72+
}
6273
});
6374

6475
Ok(())

rustmail/src/commands/reply/slash_command/reply.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::modules::update_thread_status_ui;
12
use crate::prelude::commands::*;
23
use crate::prelude::config::*;
34
use crate::prelude::db::*;
@@ -14,7 +15,6 @@ use serenity::all::{
1415
};
1516
use std::collections::HashMap;
1617
use std::sync::Arc;
17-
use crate::modules::update_thread_status_ui;
1818

1919
pub struct ReplyCommand;
2020

@@ -168,7 +168,13 @@ impl RegistrableCommand for ReplyCommand {
168168
ticket_status.last_message_by = TicketAuthor::Staff;
169169
ticket_status.last_message_at = Utc::now().timestamp();
170170
update_thread_status_db(&thread.id, &ticket_status, db_pool).await?;
171-
update_thread_status_ui(&ctx, &ticket_status).await?;
171+
172+
tokio::spawn({
173+
let ctx = ctx.clone();
174+
async move {
175+
let _ = update_thread_status_ui(&ctx, &ticket_status).await;
176+
}
177+
});
172178

173179
let mut sr = MessageBuilder::begin_staff_reply(
174180
&ctx,

rustmail/src/commands/reply/text_command/reply.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::modules::update_thread_status_ui;
12
use crate::prelude::config::*;
23
use crate::prelude::db::*;
34
use crate::prelude::errors::*;
@@ -8,7 +9,6 @@ use chrono::Utc;
89
use serenity::all::{Context, GuildId, Message, UserId};
910
use std::collections::HashMap;
1011
use std::sync::Arc;
11-
use crate::modules::update_thread_status_ui;
1212

1313
pub async fn reply(
1414
ctx: Context,
@@ -51,7 +51,13 @@ pub async fn reply(
5151
ticket_status.last_message_by = TicketAuthor::Staff;
5252
ticket_status.last_message_at = Utc::now().timestamp();
5353
update_thread_status_db(&thread.id, &ticket_status, db_pool).await?;
54-
update_thread_status_ui(&ctx, &ticket_status).await?;
54+
55+
tokio::spawn({
56+
let ctx = ctx.clone();
57+
async move {
58+
let _ = update_thread_status_ui(&ctx, &ticket_status).await;
59+
}
60+
});
5561

5662
let _ = msg.delete(&ctx.http).await;
5763

rustmail/src/commands/take/slash_command/take.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::modules::update_thread_status_ui;
12
use crate::prelude::commands::*;
23
use crate::prelude::config::*;
34
use crate::prelude::db::*;
@@ -84,28 +85,40 @@ impl RegistrableCommand for TakeCommand {
8485
return Err(ModmailError::Command(CommandError::TicketAlreadyTaken));
8586
}
8687

87-
tokio::spawn(async move {
88-
let _ = rename_channel_with_timeout(
89-
&ctx,
90-
&config,
91-
thread_id,
92-
format!("🔵-{}", command.user.name.clone()),
93-
None,
94-
Some(&command),
95-
)
96-
.await;
97-
98-
let mut params = std::collections::HashMap::new();
99-
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
100-
101-
let response = MessageBuilder::system_message(&ctx, &config)
102-
.translated_content("take.confirmation", Some(&params), None, None)
103-
.await
104-
.to_channel(command.channel_id)
105-
.build_interaction_message_followup()
106-
.await;
107-
108-
let _ = command.create_followup(ctx.clone(), response).await;
88+
tokio::spawn({
89+
let config = config.clone();
90+
let db_pool = db_pool.clone();
91+
92+
async move {
93+
let mut ticket_status = match get_thread_status(&thread.id, &db_pool).await
94+
{
95+
Some(status) => status,
96+
None => {
97+
return;
98+
}
99+
};
100+
ticket_status.taken_by = Some(command.user.id.to_string());
101+
let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
102+
103+
tokio::spawn({
104+
let ctx = ctx.clone();
105+
async move {
106+
let _ = update_thread_status_ui(&ctx, &ticket_status).await;
107+
}
108+
});
109+
110+
let mut params = std::collections::HashMap::new();
111+
params.insert("staff".to_string(), format!("<@{}>", command.user.id));
112+
113+
let response = MessageBuilder::system_message(&ctx, &config)
114+
.translated_content("take.confirmation", Some(&params), None, None)
115+
.await
116+
.to_channel(command.channel_id)
117+
.build_interaction_message_followup()
118+
.await;
119+
120+
let _ = command.create_followup(ctx.clone(), response).await;
121+
}
109122
});
110123

111124
Ok(())

rustmail/src/commands/take/text_command/take.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::modules::update_thread_status_ui;
12
use crate::prelude::commands::*;
23
use crate::prelude::config::*;
34
use crate::prelude::db::*;
@@ -36,29 +37,38 @@ pub async fn take(
3637
if thread_name == format!("🔵-{}", msg.author.name) {
3738
return Err(ModmailError::Command(CommandError::TicketAlreadyTaken));
3839
}
39-
40+
4041
let config_clone = config.clone();
41-
42-
tokio::spawn(async move {
43-
let _ = rename_channel_with_timeout(
44-
&ctx,
45-
&config_clone,
46-
thread_id,
47-
format!("🔵-{}", msg.author.name.clone()),
48-
Some(&msg),
49-
None,
50-
)
51-
.await;
5242

53-
let mut params = std::collections::HashMap::new();
54-
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
43+
tokio::spawn({
44+
let db_pool = db_pool.clone();
45+
async move {
46+
let mut ticket_status = match get_thread_status(&thread.id, &db_pool).await {
47+
Some(status) => status,
48+
None => {
49+
return;
50+
}
51+
};
52+
ticket_status.taken_by = Some(msg.author.id.to_string());
53+
let _ = update_thread_status_db(&thread.id, &ticket_status, &db_pool).await;
54+
55+
tokio::spawn({
56+
let ctx = ctx.clone();
57+
async move {
58+
let _ = update_thread_status_ui(&ctx, &ticket_status).await;
59+
}
60+
});
61+
62+
let mut params = std::collections::HashMap::new();
63+
params.insert("staff".to_string(), format!("<@{}>", msg.author.id));
5564

56-
let _ = MessageBuilder::system_message(&ctx, &config_clone)
57-
.translated_content("take.confirmation", Some(&params), None, None)
58-
.await
59-
.to_channel(msg.channel_id)
60-
.send(true)
61-
.await;
65+
let _ = MessageBuilder::system_message(&ctx, &config_clone)
66+
.translated_content("take.confirmation", Some(&params), None, None)
67+
.await
68+
.to_channel(msg.channel_id)
69+
.send(true)
70+
.await;
71+
}
6272
});
6373

6474
Ok(())

rustmail/src/i18n/language/en.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use crate::commands::help;
2+
use crate::errors::ModmailError::Discord;
13
use crate::prelude::errors::*;
4+
use std::thread::current;
25

36
pub fn load_english_messages(dict: &mut ErrorDictionary) {
47
dict.messages.insert(
@@ -892,7 +895,7 @@ pub fn load_english_messages(dict: &mut ErrorDictionary) {
892895
);
893896
dict.messages.insert(
894897
"take.confirmation".to_string(),
895-
DictionaryMessage::new("The ticket is now taken by {staff}."),
898+
DictionaryMessage::new("The ticket is now taken by {staff}.\nDue to **Discord's API**, the channel name change may take up to **10 minutes**."),
896899
);
897900
dict.messages.insert(
898901
"take.timeout".to_string(),
@@ -919,7 +922,7 @@ pub fn load_english_messages(dict: &mut ErrorDictionary) {
919922
);
920923
dict.messages.insert(
921924
"release.confirmation".to_string(),
922-
DictionaryMessage::new("The ticket has been released by {staff}."),
925+
DictionaryMessage::new("The ticket has been released by {staff}.\nDue to **Discord's API**, the channel name change may take up to **10 minutes**."),
923926
);
924927
dict.messages.insert(
925928
"slash_command.help_command_argument_desc".to_string(),

0 commit comments

Comments
 (0)