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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! # Input Block
//!
//! [slack api docs 🔗]
//!
//! A block that collects information from users -
//!
//! Read [slack's guide to using modals 🔗]
//! to learn how input blocks pass information to your app.
//!
//! [slack api docs 🔗]: https://api.slack.com/reference/block-kit/blocks#input
//! [slack's guide to using modals 🔗]: https://api.slack.com/surfaces/modals/using#gathering_input

use serde::{Deserialize, Serialize};
use validator::Validate;

use crate::{compose::text,
            convert,
            elems::{select, Radio, TextInput},
            val_helpr::ValidationResult};

/// # Input Block
///
/// [slack api docs 🔗]
///
/// A block that collects information from users -
///
/// Read [slack's guide to using modals 🔗]
/// to learn how input blocks pass information to your app.
///
/// [slack api docs 🔗]: https://api.slack.com/reference/block-kit/blocks#input
/// [slack's guide to using modals 🔗]: https://api.slack.com/surfaces/modals/using#gathering_input
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize, Validate)]
pub struct Contents<'a> {
  #[validate(custom = "validate::label")]
  label: text::Text,

  element: InputElement<'a>,

  #[serde(skip_serializing_if = "Option::is_none")]
  #[validate(length(max = 255))]
  block_id: Option<String>,

  #[serde(skip_serializing_if = "Option::is_none")]
  #[validate(custom = "validate::hint")]
  hint: Option<text::Text>,

  #[serde(skip_serializing_if = "Option::is_none")]
  dispatch_action: Option<bool>,

  #[serde(skip_serializing_if = "Option::is_none")]
  optional: Option<bool>,
}

impl<'a> Contents<'a> {
  /// Create an Input Block from a text Label and interactive element.
  ///
  /// # Arguments
  ///
  /// - `label` - A label that appears above an input element in the form of
  ///     a [text object 🔗] that must have type of `plain_text`.
  ///     Maximum length for the text in this field is 2000 characters.
  ///
  /// - `element` - An interactive `block_element` that will be used to gather
  ///     the input for this block.
  ///     For the kinds of Elements supported by
  ///     Input blocks, see the `InputElement` enum.
  ///     For info about Block Elements in general,
  ///     see the `elems` module.
  ///
  /// [text object 🔗]: https://api.slack.com/reference/messaging/composition-objects#text
  ///
  /// # Example
  /// ```
  /// use slack_blocks::elems::Select;
  /// use slack_blocks::blocks;
  ///
  /// let label = "On a scale from 1 - 5, how angsty are you?";
  /// let input = Select::from_placeholder_and_action_id("Pick a channel...", "ABC123")
  ///     .choose_from_public_channels();
  ///
  /// let block = blocks::input::Contents::from_label_and_element(label, input);
  ///
  /// // < send to slack API >
  /// ```
  pub fn from_label_and_element(label: impl Into<text::Plain>,
                                element: impl Into<InputElement<'a>>)
                                -> Self {
    Contents { label: label.into().into(),
               element: element.into(),
               block_id: None,
               hint: None,
               dispatch_action: None,
               optional: None }
  }

  /// Set a unique `block_id` to identify this instance of an Input Block.
  ///
  /// # Arguments
  ///
  /// - `block_id` - A string acting as a unique identifier for a block.
  ///     You can use this `block_id` when you receive an interaction
  ///     payload to [identify the source of the action 🔗].
  ///     If not specified, one will be generated.
  ///     Maximum length for this field is 255 characters.
  ///     `block_id` should be unique for each message and each iteration of a message.
  ///     If a message is updated, use a new `block_id`.
  ///
  /// [identify the source of the action 🔗]: https://api.slack.com/interactivity/handling#payloads
  ///
  /// # Example
  /// ```
  /// use slack_blocks::elems::Select;
  /// use slack_blocks::blocks;
  ///
  /// let label = "On a scale from 1 - 5, how angsty are you?";
  /// let input = Select::from_placeholder_and_action_id("Pick a channel...", "ABC123")
  ///     .choose_from_public_channels();
  ///
  /// let block = blocks::input
  ///     ::Contents
  ///     ::from_label_and_element(label, input)
  ///     .with_block_id("angst_rating_12345");
  ///
  /// // < send to slack API >
  /// ```
  pub fn with_block_id(mut self, block_id: impl ToString) -> Self {
    self.block_id = Some(block_id.to_string());
    self
  }

  /// Set the `hint` on this Input Block that appears below
  /// an input element in a lighter grey.
  ///
  /// # Arguments
  ///
  /// - `hint` - An optional hint that appears below an input element
  ///     in a lighter grey.
  ///     It must be a a [text object 🔗] with a `type` of `plain_text`.
  ///     Maximum length for the `text` in this field is 2000 characters.
  ///
  /// [text object 🔗]: https://api.slack.com/reference/messaging/composition-objects#text
  ///
  /// # Example
  /// ```
  /// use slack_blocks::elems::Select;
  /// use slack_blocks::blocks;
  ///
  /// # use std::error::Error;
  /// # pub fn main() -> Result<(), Box<dyn Error>> {
  /// let label = "On a scale from 1 - 5, how angsty are you?";
  /// let input = Select::from_placeholder_and_action_id("Pick a channel...", "ABC123")
  ///     .choose_from_public_channels();
  ///
  /// let block = blocks::input
  ///     ::Contents
  ///     ::from_label_and_element(label, input)
  ///     .with_hint("PSST hey! Don't let them know how angsty you are!");
  ///
  /// // < send to slack API >
  /// # Ok(())
  /// # }
  /// ```
  pub fn with_hint(mut self, hint: impl Into<text::Plain>) -> Self {
    self.hint = Some(hint.into().into());
    self
  }

  /// Set whether or not this input is Optional.
  ///
  /// # Arguments
  /// - `optionality` - A boolean that indicates whether the input
  ///     element may be empty when a user submits the modal.
  ///     Defaults to false.
  ///
  /// # Example
  /// ```
  /// use slack_blocks::elems::Select;
  /// use slack_blocks::blocks;
  ///
  /// let label = "On a scale from 1 - 5, how angsty are you?";
  /// let input = Select::from_placeholder_and_action_id("Pick a channel...", "ABC123")
  ///     .choose_from_public_channels();
  ///
  /// let block = blocks::input
  ///     ::Contents
  ///     ::from_label_and_element(label, input)
  ///     .with_hint("PSST hey! Don't even answer that!")
  ///     .with_optional(true);
  ///
  /// // < send to slack API >
  /// ```
  pub fn with_optional(mut self, optionality: bool) -> Self {
    self.optional = Some(optionality);
    self
  }

  /// Will allow the elements in this block to dispatch block_actions payloads. Defaults to false.
  pub fn dispatch_block_actions(mut self) -> Self {
    self.dispatch_action = Some(true);
    self
  }

  /// Validate that this Input block agrees with Slack's model requirements
  ///
  /// # Errors
  /// - If `from_label_and_element` was passed a Text object longer
  ///     than 2000 chars
  /// - If `with_hint` was called with a block id longer
  ///     than 2000 chars
  /// - If `with_block_id` was called with a block id longer
  ///     than 256 chars
  ///
  /// # Example
  /// ```
  /// use slack_blocks::elems::Select;
  /// use slack_blocks::blocks;
  ///
  /// let label = "On a scale from 1 - 5, how angsty are you?";
  /// let input = Select::from_placeholder_and_action_id("Pick a channel...", "ABC123")
  ///     .choose_from_public_channels();
  /// let long_string = std::iter::repeat(' ').take(2001).collect::<String>();
  ///
  /// let block = blocks::input
  ///     ::Contents
  ///     ::from_label_and_element(label, input)
  ///     .with_block_id(long_string);
  ///
  /// assert_eq!(true, matches!(block.validate(), Err(_)));
  ///
  /// // < send to slack API >
  /// ```
  pub fn validate(&self) -> ValidationResult {
    Validate::validate(self)
  }
}

/// Enum representing the [`BlockElement` 🔗] types
/// supported by InputElement.
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum InputElement<'a> {
  #[serde(rename = "channels_select")]
  SelectPublicChannel(select::PublicChannel<'a>),
  Checkboxes,
  DatePicker,
  MultiSelect,
  TextInput(TextInput<'a>),
  Radio(Radio<'a>),
}

use select::PublicChannel as SelectPublicChannel;
convert! {
    impl<'_> From<SelectPublicChannel> for InputElement
        => |contents| InputElement::SelectPublicChannel(contents)
}

convert!(impl<'a> From<Radio<'a>> for InputElement<'a> => |r| InputElement::Radio(r));
convert!(impl<'a> From<TextInput<'a>> for InputElement<'a> => |r| InputElement::TextInput(r));

mod validate {
  use crate::{compose::text,
              val_helpr::{below_len, ValidatorResult}};

  pub(super) fn label(text: &text::Text) -> ValidatorResult {
    below_len("Input Label", 2000, text.as_ref())
  }

  pub(super) fn hint(text: &text::Text) -> ValidatorResult {
    below_len("Input Hint", 2000, text.as_ref())
  }
}