Skip to content

Commit 4b7e624

Browse files
authored
Merge pull request #146 from Rustmail/135-convert-recover-command-to-slash-command
feat(commands): add complete slash command for recover command
2 parents 2f46d8d + 148e883 commit 4b7e624

12 files changed

Lines changed: 108 additions & 4 deletions

File tree

src/commands/delete/slash_command/delete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::commands::delete::common::{
33
update_message_numbers,
44
};
55
use crate::config::Config;
6-
use crate::errors::{common, MessageError, ModmailError, ModmailResult};
6+
use crate::errors::{MessageError, ModmailError, ModmailResult, common};
77
use crate::i18n::get_translated_message;
88
use crate::utils::command::defer_response::defer_response_ephemeral;
99
use crate::utils::message::message_builder::MessageBuilder;

src/commands/delete/text_command/delete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::commands::delete::common::{
33
get_thread_info, send_delete_message, update_message_numbers,
44
};
55
use crate::config::Config;
6-
use crate::errors::{common, ModmailResult};
6+
use crate::errors::{ModmailResult, common};
77
use serenity::all::{Context, Message};
88

99
pub async fn delete(ctx: &Context, msg: &Message, config: &Config) -> ModmailResult<()> {

src/commands/recover/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod slash_command;
2+
pub mod text_command;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod recover;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use crate::config::Config;
2+
use crate::errors::{ModmailResult, common};
3+
use crate::i18n::get_translated_message;
4+
use crate::modules::message_recovery::recover_missing_messages;
5+
use crate::utils::command::defer_response::defer_response;
6+
use crate::utils::message::message_builder::MessageBuilder;
7+
use serenity::all::{CommandInteraction, Context, CreateCommand, ResolvedOption};
8+
use std::collections::HashMap;
9+
10+
pub async fn register(config: &Config) -> CreateCommand {
11+
let cmd_desc = get_translated_message(
12+
config,
13+
"slash_command.recover_command_description",
14+
None,
15+
None,
16+
None,
17+
None,
18+
)
19+
.await;
20+
21+
CreateCommand::new("recover").description(cmd_desc)
22+
}
23+
24+
pub async fn run(
25+
ctx: &Context,
26+
command: &CommandInteraction,
27+
_options: &[ResolvedOption<'_>],
28+
config: &Config,
29+
) -> ModmailResult<()> {
30+
let _ = config
31+
.db_pool
32+
.as_ref()
33+
.ok_or_else(common::database_connection_failed)?;
34+
35+
defer_response(&ctx, &command).await?;
36+
37+
let mut params = HashMap::new();
38+
params.insert("user".to_string(), command.user.name.clone());
39+
40+
let response = MessageBuilder::system_message(ctx, config)
41+
.translated_content(
42+
"recovery.started",
43+
Some(&params),
44+
Some(command.user.id),
45+
command.guild_id.map(|g| g.get()),
46+
)
47+
.await
48+
.to_channel(command.channel_id)
49+
.build_interaction_message_followup()
50+
.await;
51+
52+
let _ = command.create_followup(&ctx.http, response).await;
53+
54+
let ctx_clone = ctx.clone();
55+
let config_clone = config.clone();
56+
let channel_id = command.channel_id;
57+
let command_clone = command.clone();
58+
59+
tokio::spawn(async move {
60+
let recovery_results = recover_missing_messages(&ctx_clone, &config_clone).await;
61+
62+
let total_recovered: u32 = recovery_results.iter().map(|r| r.recovered_count).sum();
63+
let successful_threads = recovery_results.iter().filter(|r| r.success).count();
64+
let failed_threads = recovery_results.len() - successful_threads;
65+
66+
let mut params = HashMap::new();
67+
params.insert("total".to_string(), total_recovered.to_string());
68+
params.insert("threads".to_string(), successful_threads.to_string());
69+
params.insert("failed".to_string(), failed_threads.to_string());
70+
71+
let response = MessageBuilder::system_message(&ctx_clone, &config_clone)
72+
.translated_content("recovery.summary", Some(&params), None, None)
73+
.await
74+
.to_channel(channel_id)
75+
.build_interaction_message_followup()
76+
.await;
77+
78+
let _ = command_clone
79+
.create_followup(&ctx_clone.http, response)
80+
.await;
81+
});
82+
83+
Ok(())
84+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod recover;

src/commands/recover.rs renamed to src/commands/recover/text_command/recover.rs

File renamed without changes.

src/handlers/guild_interaction_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::commands::force_close::slash_command::force_close;
77
use crate::commands::id::slash_command::id;
88
use crate::commands::move_thread::slash_command::move_thread;
99
use crate::commands::new_thread::slash_command::new_thread;
10+
use crate::commands::recover::slash_command::recover;
1011
use crate::commands::remove_staff::slash_command::remove_staff;
1112
use crate::commands::reply::slash_command::reply;
1213
use crate::config::Config;
@@ -90,6 +91,9 @@ impl EventHandler for InteractionHandler {
9091
"delete" => {
9192
delete::run(&ctx, &command, &command.data.options(), &self.config).await
9293
}
94+
"recover" => {
95+
recover::run(&ctx, &command, &command.data.options(), &self.config).await
96+
}
9397
_ => Err(ModmailError::Command(CommandError::UnknownSlashCommand(
9498
command.data.name.clone(),
9599
))),

src/handlers/guild_messages_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::commands::force_close::text_command::force_close::force_close;
99
use crate::commands::id::text_command::id::id;
1010
use crate::commands::move_thread::text_command::move_thread::move_thread;
1111
use crate::commands::new_thread::text_command::new_thread::new_thread;
12-
use crate::commands::recover::recover;
12+
use crate::commands::recover::text_command::recover::recover;
1313
use crate::commands::remove_staff::text_command::remove_staff::remove_staff;
1414
use crate::commands::reply::text_command::reply::reply;
1515
use crate::config::Config;
@@ -20,7 +20,7 @@ use crate::db::operations::{
2020
};
2121
use crate::db::operations::{get_thread_channel_by_user_id, thread_exists, update_message_content};
2222
use crate::db::threads::get_thread_by_user_id;
23-
use crate::errors::{common, ModmailResult};
23+
use crate::errors::{ModmailResult, common};
2424
use crate::i18n::get_translated_message;
2525
use crate::utils::message::message_builder::MessageBuilder;
2626
use crate::utils::thread::get_thread_lock::get_thread_lock;

src/handlers/ready_handler.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::commands::force_close::slash_command::force_close;
77
use crate::commands::id::slash_command::id;
88
use crate::commands::move_thread::slash_command::move_thread;
99
use crate::commands::new_thread::slash_command::new_thread;
10+
use crate::commands::recover::slash_command::recover;
1011
use crate::commands::remove_staff::slash_command::remove_staff;
1112
use crate::commands::reply::slash_command::reply;
1213
use crate::config::Config;
@@ -66,6 +67,7 @@ impl EventHandler for ReadyHandler {
6667
force_close::register(&self.config).await,
6768
reply::register(&self.config).await,
6869
delete::register(&self.config).await,
70+
recover::register(&self.config).await,
6971
],
7072
)
7173
.await;

0 commit comments

Comments
 (0)