slack_morphism/models/blocks/
view.rs

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::blocks::kit::SlackBlock;
use crate::blocks::{SlackBlockId, SlackBlockPlainText, SlackBlockPlainTextOnly};
use crate::SlackCallbackId;
use crate::*;
use rsb_derive::Builder;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
use std::collections::HashMap;

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum SlackView {
    #[serde(rename = "home")]
    Home(SlackHomeView),
    #[serde(rename = "modal")]
    Modal(SlackModalView),
}

#[serde_as]
#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackHomeView {
    pub blocks: Vec<SlackBlock>,
    #[serde(default)]
    #[serde_as(as = "serde_with::NoneAsEmptyString")]
    pub private_metadata: Option<String>,
    #[serde(default)]
    #[serde_as(as = "serde_with::NoneAsEmptyString")]
    pub callback_id: Option<SlackCallbackId>,
    #[serde(default)]
    #[serde_as(as = "serde_with::NoneAsEmptyString")]
    pub external_id: Option<String>,
}

#[serde_as]
#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackModalView {
    pub title: SlackBlockPlainTextOnly,
    pub blocks: Vec<SlackBlock>,
    pub close: Option<SlackBlockPlainTextOnly>,
    pub submit: Option<SlackBlockPlainTextOnly>,
    #[serde(default)]
    #[serde_as(as = "serde_with::NoneAsEmptyString")]
    pub private_metadata: Option<String>,
    #[serde(default)]
    #[serde_as(as = "serde_with::NoneAsEmptyString")]
    pub callback_id: Option<SlackCallbackId>,
    pub clear_on_close: Option<bool>,
    pub notify_on_close: Option<bool>,
    pub hash: Option<String>,
    #[serde(default)]
    #[serde_as(as = "serde_with::NoneAsEmptyString")]
    pub external_id: Option<String>,
}

#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackStatefulView {
    #[serde(flatten)]
    pub state_params: SlackStatefulStateParams,
    #[serde(flatten)]
    pub view: SlackView,
}

#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackStatefulStateParams {
    pub id: SlackViewId,
    pub team_id: SlackTeamId,
    pub state: Option<SlackViewState>,
    pub hash: String,
    pub previous_view_id: Option<SlackViewId>,
    pub root_view_id: Option<SlackViewId>,
    pub app_id: Option<SlackAppId>,
    pub bot_id: Option<SlackBotId>,
}

#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackViewState {
    pub values: HashMap<SlackBlockId, HashMap<SlackActionId, SlackViewStateValue>>,
}

#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackViewStateValue {
    #[serde(rename = "type")]
    pub action_type: SlackActionType,
    pub value: Option<String>,
    pub selected_date: Option<String>,
    pub selected_time: Option<String>,
    pub selected_date_time: Option<SlackDateTime>,
    pub selected_conversation: Option<SlackConversationId>,
    pub selected_channel: Option<SlackChannelId>,
    pub selected_user: Option<SlackUserId>,
    pub selected_option: Option<SlackViewStateValueSelectedOption>,
    pub selected_conversations: Option<Vec<SlackConversationId>>,
    pub selected_users: Option<Vec<SlackUserId>>,
    pub selected_options: Option<Vec<SlackViewStateValueSelectedOption>>,
}

pub type SlackActionState = SlackViewState;
pub type SlackActionStateValue = SlackViewStateValue;

#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackViewStateValueSelectedOption {
    pub text: SlackBlockPlainText,
    pub value: String,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(tag = "response_action")]
pub enum SlackViewSubmissionResponse {
    #[serde(rename = "clear")]
    Clear(SlackViewSubmissionClearResponse),
    #[serde(rename = "update")]
    Update(SlackViewSubmissionUpdateResponse),
    #[serde(rename = "push")]
    Push(SlackViewSubmissionPushResponse),
    #[serde(rename = "errors")]
    Errors(SlackViewSubmissionErrorsResponse),
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackViewSubmissionClearResponse {}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackViewSubmissionUpdateResponse {
    pub view: SlackView,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackViewSubmissionPushResponse {
    pub view: SlackView,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Builder)]
pub struct SlackViewSubmissionErrorsResponse {
    pub errors: HashMap<String, String>,
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_slack_view_submission_clear_response_serialization() {
        let output = serde_json::to_string(&SlackViewSubmissionResponse::Clear(
            SlackViewSubmissionClearResponse::new(),
        ))
        .unwrap();
        assert_eq!(output, r#"{"response_action":"clear"}"#);
    }

    #[test]
    fn test_slack_api_apps_manifest_create_request() {
        let payload = include_str!("./fixtures/slack_home_view.json");
        let model: SlackHomeView = serde_json::from_str(payload).unwrap();
        assert!(model.private_metadata.is_none());
        assert_eq!(
            model.callback_id,
            Some(SlackCallbackId::from("test-callback-id"))
        );
    }
}