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
5 changes: 5 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ embed_color = "ffb800"
[logs]
show_log_on_edit = true
show_log_on_delete = true

[updates]
enabled = true
check_interval_hours = 6
notify_channel_id = 14043597305635899
2 changes: 2 additions & 0 deletions crates/rustmail/src/api/handler/bot/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub async fn handle_get_config(
notifications: config.notifications.clone(),
reminders: config.reminders.clone(),
logs: config.logs.clone(),
updates: config.updates.clone(),
};

Ok(Json(response))
Expand Down Expand Up @@ -83,6 +84,7 @@ pub async fn handle_update_config(
notifications: update.notifications,
reminders: update.reminders,
logs: update.logs,
updates: update.updates,
db_pool: None,
error_handler: None,
thread_locks: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
Expand Down
2 changes: 2 additions & 0 deletions crates/rustmail/src/api/handler/bot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod statistics;
pub mod status;
pub mod stop;
pub mod tickets;
pub mod version;

pub use config::*;
pub use profile::*;
Expand All @@ -15,3 +16,4 @@ pub use statistics::*;
pub use status::*;
pub use stop::*;
pub use tickets::*;
pub use version::*;
59 changes: 59 additions & 0 deletions crates/rustmail/src/api/handler/bot/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::modules::update_checker::{
CURRENT_VERSION, LAST_UPDATE_CHECK_KEY, LATEST_KNOWN_VERSION_KEY, LATEST_RELEASE_URL_KEY,
is_newer,
};
use crate::prelude::db::*;
use crate::prelude::types::*;
use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use rustmail_types::VersionInfo;
use std::sync::Arc;
use tokio::sync::Mutex;

pub async fn handle_get_version(
State(bot_state): State<Arc<Mutex<BotState>>>,
) -> Result<Json<VersionInfo>, StatusCode> {
let (pool, check_enabled) = {
let state = bot_state.lock().await;

let check_enabled = state
.config
.as_ref()
.map(|c| c.updates.enabled)
.unwrap_or(false);

(state.db_pool.clone(), check_enabled)
};

let Some(pool) = pool else {
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};

let latest = get_system_metadata(LATEST_KNOWN_VERSION_KEY, &pool)
.await
.ok()
.flatten();
let release_url = get_system_metadata(LATEST_RELEASE_URL_KEY, &pool)
.await
.ok()
.flatten();
let last_checked = get_system_metadata(LAST_UPDATE_CHECK_KEY, &pool)
.await
.ok()
.flatten();

let update_available = latest
.as_deref()
.map(|latest| is_newer(latest, CURRENT_VERSION))
.unwrap_or(false);

Ok(Json(VersionInfo {
current: CURRENT_VERSION.to_string(),
latest,
update_available,
release_url,
check_enabled,
last_checked,
}))
}
1 change: 1 addition & 0 deletions crates/rustmail/src/api/routes/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn create_bot_router(bot_state: Arc<Mutex<BotState>>) -> Router<Arc<Mutex<Bo
.route("/config", get(handle_get_config))
.route("/statistics", get(handle_statistics))
.route("/profile", get(handle_get_profile))
.route("/version", get(handle_get_version))
.layer(axum::middleware::from_fn_with_state(
bot_state.clone(),
move |state, jar, req, next| {
Expand Down
1 change: 1 addition & 0 deletions crates/rustmail/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub async fn run_bot(
registry.register_command(CategoryCommand);
registry.register_command(RenameCommand);
registry.register_command(BaninfoCommand);
registry.register_command(VersionCommand);

let registry = Arc::new(registry);

Expand Down
1 change: 1 addition & 0 deletions crates/rustmail/src/commands/edit/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mod tests {
language: LanguageConfig::default(),
reminders: ReminderConfig::default(),
error_handling: ErrorHandlingConfig::default(),
updates: UpdateConfig::default(),
db_pool: None,
error_handler: None,
thread_locks: Arc::new(Mutex::new(Default::default())),
Expand Down
2 changes: 2 additions & 0 deletions crates/rustmail/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod reply;
pub mod snippet;
pub mod status;
pub mod take;
pub mod version;

pub use add_reminder::*;
pub use add_staff::*;
Expand Down Expand Up @@ -62,6 +63,7 @@ pub use reply::*;
pub use snippet::*;
pub use status::*;
pub use take::*;
pub use version::*;

pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

Expand Down
20 changes: 20 additions & 0 deletions crates/rustmail/src/commands/version/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::prelude::config::*;
use crate::prelude::i18n::*;
use crate::prelude::modules::*;
use std::collections::HashMap;

pub async fn build_version_content(config: &Config) -> String {
let mut params = HashMap::new();
params.insert("version".to_string(), CURRENT_VERSION.to_string());
params.insert("repository".to_string(), REPOSITORY_URL.to_string());

get_translated_message(
config,
"version_command.current",
Some(&params),
None,
None,
None,
)
.await
}
7 changes: 7 additions & 0 deletions crates/rustmail/src/commands/version/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod common;
pub mod slash_command;
pub mod text_command;

pub use common::*;
pub use slash_command::*;
pub use text_command::*;
3 changes: 3 additions & 0 deletions crates/rustmail/src/commands/version/slash_command/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod version;

pub use version::*;
75 changes: 75 additions & 0 deletions crates/rustmail/src/commands/version/slash_command/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::prelude::commands::*;
use crate::prelude::config::*;
use crate::prelude::errors::*;
use crate::prelude::handlers::*;
use crate::prelude::i18n::*;
use crate::prelude::utils::*;
use serenity::FutureExt;
use serenity::all::{CommandInteraction, CommandType, Context, CreateCommand, ResolvedOption};
use std::sync::Arc;

pub struct VersionCommand;

#[async_trait::async_trait]
impl RegistrableCommand for VersionCommand {
fn as_community(&self) -> Option<&dyn CommunityRegistrable> {
None
}

fn name(&self) -> &'static str {
"version"
}

fn doc<'a>(&self, config: &'a Config) -> BoxFuture<'a, String> {
async move { get_translated_message(config, "help.version", None, None, None, None).await }
.boxed()
}

fn register(&self, config: &Config) -> BoxFuture<'_, Vec<CreateCommand>> {
let config = config.clone();

Box::pin(async move {
let cmd_desc = get_translated_message(
&config,
"slash_command.version_command_desc",
None,
None,
None,
None,
)
.await;

vec![
CreateCommand::new(self.name()).description(cmd_desc),
CreateCommand::new(self.name()).kind(CommandType::User),
]
})
}

fn run(
&self,
ctx: &Context,
command: &CommandInteraction,
_options: &[ResolvedOption<'_>],
config: &Config,
_handler: Arc<InteractionHandler>,
) -> BoxFuture<'_, ModmailResult<()>> {
let ctx = ctx.clone();
let command = command.clone();
let config = config.clone();

Box::pin(async move {
defer_response(&ctx, &command).await?;

let content = build_version_content(&config).await;

let _ = MessageBuilder::system_message(&ctx, &config)
.content(content)
.to_channel(command.channel_id)
.send_interaction_followup(&command, false)
.await;

Ok(())
})
}
}
3 changes: 3 additions & 0 deletions crates/rustmail/src/commands/version/text_command/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod version;

pub use version::*;
24 changes: 24 additions & 0 deletions crates/rustmail/src/commands/version/text_command/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::prelude::commands::*;
use crate::prelude::config::*;
use crate::prelude::errors::*;
use crate::prelude::handlers::*;
use crate::prelude::utils::*;
use serenity::all::{Context, Message};
use std::sync::Arc;

pub async fn version(
ctx: Context,
msg: Message,
config: &Config,
_handler: Arc<GuildMessagesHandler>,
) -> ModmailResult<()> {
let content = build_version_content(config).await;

let _ = MessageBuilder::system_message(&ctx, config)
.content(content)
.to_channel(msg.channel_id)
.send(false)
.await;

Ok(())
}
Loading