| 1 | use gpui_kit::*; |
| 2 | use store::TeamRights; |
| 3 | use teams_core::{ChannelPrivacy, MAX_CHANNEL_NAME_LENGTH}; |
| 4 | |
| 5 | use crate::app_state::AppState; |
| 6 | |
| 7 | pub const CREATE_CHANNEL_FAILURE: &str = "We couldn't create the channel."; |
| 8 | pub const CHANNEL_NAME_CHARACTERS: &str = |
| 9 | "Channel names can't contain ~ # % & * { } + / \\ : < > ? | ' \" , or .."; |
| 10 | pub const CHANNEL_NAME_EDGES: &str = "Channel names can't start with _ or . and can't end with ."; |
| 11 | pub const CHANNEL_NAME_LENGTH: &str = "Channel names can have up to 50 characters."; |
| 12 | const FORBIDDEN_CHARACTERS: &[char] = &[ |
| 13 | '~', '#', '%', '&', '*', '{', '}', '+', '/', '\\', ':', '<', '>', '?', '|', '\'', '"', ',', |
| 14 | ]; |
| 15 | |
| 16 | /// Why Teams would refuse this channel name, `None` when it may be created. |
| 17 | pub fn channel_name_problem(raw: &str) -> Option<&'static str> { |
| 18 | let name = raw.trim(); |
| 19 | if name.chars().count() > MAX_CHANNEL_NAME_LENGTH { |
| 20 | return Some(CHANNEL_NAME_LENGTH); |
| 21 | } |
| 22 | if name.contains(FORBIDDEN_CHARACTERS) || name.contains("..") { |
| 23 | return Some(CHANNEL_NAME_CHARACTERS); |
| 24 | } |
| 25 | if name.starts_with(['_', '.']) || name.ends_with('.') { |
| 26 | return Some(CHANNEL_NAME_EDGES); |
| 27 | } |
| 28 | None |
| 29 | } |
| 30 | |
| 31 | pub fn can_create_channel(raw: &str) -> bool { |
| 32 | !raw.trim().is_empty() && channel_name_problem(raw).is_none() |
| 33 | } |
| 34 | |
| 35 | impl AppState { |
| 36 | pub fn team_rights(&self, team_id: &str) -> TeamRights { |
| 37 | self.sidebar |
| 38 | .teams |
| 39 | .iter() |
| 40 | .find(|team| team.team.id == team_id) |
| 41 | .map(|team| team.rights) |
| 42 | .unwrap_or_default() |
| 43 | } |
| 44 | |
| 45 | pub fn team_name(&self, team_id: &str) -> Option<String> { |
| 46 | self.sidebar |
| 47 | .teams |
| 48 | .iter() |
| 49 | .find(|team| team.team.id == team_id) |
| 50 | .map(|team| team.team.name.clone()) |
| 51 | } |
| 52 | |
| 53 | pub fn create_channel( |
| 54 | &mut self, |
| 55 | team_id: &str, |
| 56 | name: &str, |
| 57 | description: &str, |
| 58 | privacy: ChannelPrivacy, |
| 59 | cx: &mut Context<Self>, |
| 60 | ) { |
| 61 | let Some(Some(engine)) = self.chat_action_engine(cx) else { |
| 62 | return; |
| 63 | }; |
| 64 | let (team_id, name, description) = |
| 65 | (team_id.to_owned(), name.to_owned(), description.to_owned()); |
| 66 | self.run_channel_action( |
| 67 | CREATE_CHANNEL_FAILURE, |
| 68 | async move { |
| 69 | engine |
| 70 | .create_channel(&team_id, &name, &description, privacy) |
| 71 | .await |
| 72 | }, |
| 73 | |_, _| {}, |
| 74 | None, |
| 75 | cx, |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #[cfg(test)] |
| 81 | mod tests { |
| 82 | use teams_core::MAX_CHANNEL_NAME_LENGTH; |
| 83 | |
| 84 | use super::{ |
| 85 | CHANNEL_NAME_CHARACTERS, CHANNEL_NAME_EDGES, CHANNEL_NAME_LENGTH, can_create_channel, |
| 86 | channel_name_problem, |
| 87 | }; |
| 88 | |
| 89 | #[test] |
| 90 | fn channel_names_follow_the_teams_rules() { |
| 91 | assert!(can_create_channel(" Planning 2027 ")); |
| 92 | assert!(!can_create_channel(" ")); |
| 93 | assert!(!can_create_channel( |
| 94 | &"a".repeat(MAX_CHANNEL_NAME_LENGTH + 1) |
| 95 | )); |
| 96 | assert_eq!( |
| 97 | channel_name_problem(&"a".repeat(MAX_CHANNEL_NAME_LENGTH + 1)), |
| 98 | Some(CHANNEL_NAME_LENGTH) |
| 99 | ); |
| 100 | assert_eq!(channel_name_problem("Q&A"), Some(CHANNEL_NAME_CHARACTERS)); |
| 101 | assert_eq!(channel_name_problem("a..b"), Some(CHANNEL_NAME_CHARACTERS)); |
| 102 | assert_eq!(channel_name_problem("_drafts"), Some(CHANNEL_NAME_EDGES)); |
| 103 | assert_eq!(channel_name_problem("notes."), Some(CHANNEL_NAME_EDGES)); |
| 104 | assert_eq!(channel_name_problem("Release notes"), None); |
| 105 | } |
| 106 | } |