Support help that is hidden in less explicit mode.

This commit is contained in:
Condorra 2022-12-25 01:42:51 +11:00
parent 887b69340f
commit e5e6c45e81
3 changed files with 34 additions and 4 deletions

View File

@ -164,6 +164,18 @@ impl DBTrans {
Ok(()) Ok(())
} }
pub async fn get_session_model(self: &Self, session: &ListenerSession) -> DResult<Option<Session>> {
match self.pg_trans()?
.query_opt("SELECT details FROM sessions WHERE session = $1", &[&session.session])
.await? {
None => Ok(None),
Some(row) =>
Ok(Some(serde_json::from_value(
row.get("details")
)?))
}
}
pub async fn commit(mut self: Self) -> DResult<()> { pub async fn commit(mut self: Self) -> DResult<()> {
let trans_opt = self.with_trans_mut(|t| std::mem::replace(t, None)); let trans_opt = self.with_trans_mut(|t| std::mem::replace(t, None));
for trans in trans_opt { for trans in trans_opt {

View File

@ -4,6 +4,7 @@ use crate::db::{DBTrans, DBPool};
use ansi_macro::ansi; use ansi_macro::ansi;
use phf::phf_map; use phf::phf_map;
use async_trait::async_trait; use async_trait::async_trait;
use crate::models::session::Session;
mod parsing; mod parsing;
mod ignore; mod ignore;
@ -11,6 +12,7 @@ mod help;
pub struct VerbContext<'l> { pub struct VerbContext<'l> {
session: &'l ListenerSession, session: &'l ListenerSession,
session_dat: &'l mut Session,
trans: &'l DBTrans trans: &'l DBTrans
} }
@ -48,7 +50,12 @@ static ALWAYS_AVAILABLE_COMMANDS: UserVerbRegistry = phf_map! {
pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DResult<()> { pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DResult<()> {
let (cmd, params) = parsing::parse_command_name(msg); let (cmd, params) = parsing::parse_command_name(msg);
let trans = pool.start_transaction().await?; let trans = pool.start_transaction().await?;
let mut session_dat = match trans.get_session_model(session).await? {
None => { return Ok(()) }
Some(v) => v
};
let handler_opt = ALWAYS_AVAILABLE_COMMANDS.get(cmd); let handler_opt = ALWAYS_AVAILABLE_COMMANDS.get(cmd);
let ctx = VerbContext { session, trans: &trans, session_dat: &mut session_dat };
match handler_opt { match handler_opt {
None => { None => {
@ -59,7 +66,7 @@ pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DRes
).await?; ).await?;
} }
Some(handler) => { Some(handler) => {
match handler.handle(&VerbContext { session, trans: &trans }, cmd, params).await { match handler.handle(&ctx, cmd, params).await {
Ok(()) => {} Ok(()) => {}
Err(UserError(err_msg)) => { Err(UserError(err_msg)) => {
trans.queue_for_session(session, &(err_msg + "\r\n")).await?; trans.queue_for_session(session, &(err_msg + "\r\n")).await?;

View File

@ -19,13 +19,24 @@ static HELP_PAGES: phf::Map<&'static str, &'static str> = phf_map! {
\t<bold>help register<reset> will tell you about the register command.") \t<bold>help register<reset> will tell you about the register command.")
}; };
static EXPLICIT_HELP_PAGES: phf::Map<&'static str, &'static str> = phf_map! {
"fuck" =>
ansi!("Type <bold>fuck <lt>name><reset> to fuck someone. It only works if \
they have consented.")
};
pub struct Verb; pub struct Verb;
#[async_trait] #[async_trait]
impl UserVerb for Verb { impl UserVerb for Verb {
async fn handle(self: &Self, ctx: &VerbContext, _verb: &str, remaining: &str) -> UResult<()> { async fn handle(self: &Self, ctx: &VerbContext, _verb: &str, remaining: &str) -> UResult<()> {
let help = HELP_PAGES.get(remaining).ok_or( let mut help = None;
if !ctx.session_dat.less_explicit_mode {
help = help.or_else(|| EXPLICIT_HELP_PAGES.get(remaining))
}
help = help.or_else(|| HELP_PAGES.get(remaining));
let help_final = help.ok_or(
UserError("No help available on that".to_string()))?; UserError("No help available on that".to_string()))?;
ctx.trans.queue_for_session(ctx.session, &(help.to_string() + "\r\n")).await?; ctx.trans.queue_for_session(ctx.session, &(help_final.to_string() + "\r\n")).await?;
Ok(()) Ok(())
} }
} }