1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! The trait representing the interface that acts as a slash command.

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;

/// The trait representing the interface that acts as a slash command.
#[async_trait]
pub trait SlashCommandHandlerTrait: Send + Sync {
    /// Handles [`SlashCommandBody`].
    async fn handle_slash_command(
        &self,
        client: &SlackClient,
        body: SlashCommandBody,
    ) -> Result<st::SlashCommandResponse, ResponseError>;
}

/// The new type to wrap [`SlashCommandHandlerTrait`].
#[derive(Clone)]
pub(crate) struct SlashCommandHandler<T>(T)
where
    T: SlashCommandHandlerTrait;

impl<T> SlashCommandHandler<T>
where
    T: SlashCommandHandlerTrait,
{
    /// The constructor.
    pub fn new(inner: T) -> Self {
        Self(inner)
    }

    /// The wrapper function for a slash command.

    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
    }
}