use sfr_core as sc;
use sfr_slack_api as ssa;
use sfr_types as st;
use crate::handler::oauth::{OauthHandler, OauthHandlerTrait};
use crate::{ResponseError, SlashCommandResponse};
use axum::async_trait;
use sc::SlashCommandBody;
use ssa::Client as SlackClient;
#[async_trait]
pub trait SlashCommandHandlerTrait: Send + Sync {
async fn handle_slash_command(
&self,
client: &SlackClient,
body: SlashCommandBody,
) -> Result<st::SlashCommandResponse, ResponseError>;
}
#[derive(Clone)]
pub(crate) struct SlashCommandHandler<T>(T)
where
T: SlashCommandHandlerTrait;
impl<T> SlashCommandHandler<T>
where
T: SlashCommandHandlerTrait,
{
pub fn new(inner: T) -> Self {
Self(inner)
}
pub async fn handle<OH>(
&self,
client: reqwest::Client,
oauth: &OauthHandler<OH>,
body: SlashCommandBody,
) -> Result<impl axum::response::IntoResponse, ResponseError>
where
OH: OauthHandlerTrait,
{
tracing::info!("slash command: start to process");
let token = oauth.take_oauth_token_from_team_id(&body.team_id).await?;
let client = SlackClient::new(client, token);
let resp: SlashCommandResponse = self.0.handle_slash_command(&client, body).await?.into();
Ok(resp)
}
}
impl<T> std::ops::Deref for SlashCommandHandler<T>
where
T: SlashCommandHandlerTrait,
{
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}