| 1 | use gpui_kit::*; |
| 2 | use store::TeamRights; |
| 3 | use teams_core::{ChannelPrivacy, MAX_CHANNEL_NAME_LENGTH}; |
| 4 | |
| 5 | use crate::app_state::{AppState, Selection}; |
| 6 | |
| 7 | pub const CREATE_CHANNEL_FAILURE: &str = "We couldn't create the channel."; |
| 8 | pub const EDIT_CHANNEL_FAILURE: &str = "Channel wasn't renamed."; |
| 9 | pub const CHANNEL_NAME_CHARACTERS: &str = |
| 10 | "Channel names can't contain ~ # % & * { } + / \\ : < > ? | ' \" , or .."; |
| 11 | pub const CHANNEL_NAME_EDGES: &str = "Channel names can't start with _ or . and can't end with ."; |
| 12 | pub const CHANNEL_NAME_LENGTH: &str = "Channel names can have up to 50 characters."; |
| 13 | const FORBIDDEN_CHARACTERS: &[char] = &[ |
| 14 | '~', '#', '%', '&', '*', '{', '}', '+', '/', '\\', ':', '<', '>', '?', '|', '\'', '"', ',', |
| 15 | ]; |
| 16 | |
| 17 | /// Why Teams would refuse this channel name, `None` when it may be created. |
| 18 | pub fn channel_name_problem(raw: &str) -> Option<&'static str> { |
| 19 | let name = raw.trim(); |
| 20 | if name.chars().count() > MAX_CHANNEL_NAME_LENGTH { |
| 21 | return Some(CHANNEL_NAME_LENGTH); |
| 22 | } |
| 23 | if name.contains(FORBIDDEN_CHARACTERS) || name.contains("..") { |
| 24 | return Some(CHANNEL_NAME_CHARACTERS); |
| 25 | } |
| 26 | if name.starts_with(['_', '.']) || name.ends_with('.') { |
| 27 | return Some(CHANNEL_NAME_EDGES); |
| 28 | } |
| 29 | None |
| 30 | } |
| 31 | |
| 32 | pub fn can_create_channel(raw: &str) -> bool { |
| 33 | !raw.trim().is_empty() && channel_name_problem(raw).is_none() |
| 34 | } |
| 35 | |
| 36 | pub fn delete_channel_failure(channel_name: &str) -> String { |
| 37 | format!("We couldn't delete the {channel_name} channel. Try deleting it again.") |
| 38 | } |
| 39 | |
| 40 | pub fn channel_deleted_notice(channel_name: &str) -> String { |
| 41 | format!("{channel_name} has been deleted") |
| 42 | } |
| 43 | |
| 44 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 45 | pub enum ChannelKind { |
| 46 | General, |
| 47 | Standard, |
| 48 | Private, |
| 49 | Shared, |
| 50 | } |
| 51 | |
| 52 | impl ChannelKind { |
| 53 | pub fn of(general: bool, membership_type: Option<&str>) -> Self { |
| 54 | match membership_type.map(str::to_ascii_lowercase).as_deref() { |
| 55 | _ if general => ChannelKind::General, |
| 56 | Some("private") => ChannelKind::Private, |
| 57 | Some("shared") => ChannelKind::Shared, |
| 58 | _ => ChannelKind::Standard, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 64 | pub struct ChannelAdmin { |
| 65 | pub can_edit: bool, |
| 66 | pub can_delete: bool, |
| 67 | } |
| 68 | |
| 69 | /// Private channels are managed by team owners here, the channel's own owner role is not loaded. |
| 70 | pub fn channel_admin(rights: TeamRights, kind: ChannelKind) -> ChannelAdmin { |
| 71 | match kind { |
| 72 | ChannelKind::General | ChannelKind::Shared => ChannelAdmin::default(), |
| 73 | ChannelKind::Private => ChannelAdmin { |
| 74 | can_edit: rights.owner, |
| 75 | can_delete: rights.owner, |
| 76 | }, |
| 77 | ChannelKind::Standard => ChannelAdmin { |
| 78 | can_edit: rights.edit_channel, |
| 79 | can_delete: rights.delete_channel, |
| 80 | }, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl AppState { |
| 85 | pub fn team_rights(&self, team_id: &str) -> TeamRights { |
| 86 | self.sidebar |
| 87 | .teams |
| 88 | .iter() |
| 89 | .find(|team| team.team.id == team_id) |
| 90 | .map(|team| team.rights) |
| 91 | .unwrap_or_default() |
| 92 | } |
| 93 | |
| 94 | pub fn team_name(&self, team_id: &str) -> Option<String> { |
| 95 | self.sidebar |
| 96 | .teams |
| 97 | .iter() |
| 98 | .find(|team| team.team.id == team_id) |
| 99 | .map(|team| team.team.name.clone()) |
| 100 | } |
| 101 | |
| 102 | pub fn channel_admin(&self, channel_id: &str) -> ChannelAdmin { |
| 103 | let Some((team, channel)) = self.sidebar.teams.iter().find_map(|team| { |
| 104 | team.channels |
| 105 | .iter() |
| 106 | .find(|channel| channel.id == channel_id) |
| 107 | .map(|channel| (team, channel)) |
| 108 | }) else { |
| 109 | return ChannelAdmin::default(); |
| 110 | }; |
| 111 | let general = self |
| 112 | .store |
| 113 | .team_thread_id(&team.team.id) |
| 114 | .ok() |
| 115 | .flatten() |
| 116 | .is_some_and(|thread_id| thread_id == channel_id); |
| 117 | let kind = ChannelKind::of(general, channel.membership_type.as_deref()); |
| 118 | channel_admin(team.rights, kind) |
| 119 | } |
| 120 | |
| 121 | pub fn channel_is_private(&self, channel_id: &str) -> bool { |
| 122 | self.sidebar |
| 123 | .teams |
| 124 | .iter() |
| 125 | .flat_map(|team| &team.channels) |
| 126 | .find(|channel| channel.id == channel_id) |
| 127 | .is_some_and(|channel| { |
| 128 | ChannelKind::of(false, channel.membership_type.as_deref()) == ChannelKind::Private |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | pub fn load_channel_description( |
| 133 | &self, |
| 134 | channel_id: &str, |
| 135 | cx: &mut Context<Self>, |
| 136 | ) -> Task<Option<String>> { |
| 137 | let Some(engine) = self.engine.clone() else { |
| 138 | return Task::ready(None); |
| 139 | }; |
| 140 | let channel_id = channel_id.to_owned(); |
| 141 | let receiver = |
| 142 | crate::runtime::spawn(async move { engine.channel_description(&channel_id).await }); |
| 143 | cx.background_spawn(async move { receiver.await.ok().and_then(Result::ok) }) |
| 144 | } |
| 145 | |
| 146 | pub fn edit_channel( |
| 147 | &mut self, |
| 148 | channel_id: &str, |
| 149 | name: &str, |
| 150 | description: Option<&str>, |
| 151 | cx: &mut Context<Self>, |
| 152 | ) { |
| 153 | let Some(Some(engine)) = self.chat_action_engine(cx) else { |
| 154 | return; |
| 155 | }; |
| 156 | let (channel_id, name, description) = ( |
| 157 | channel_id.to_owned(), |
| 158 | name.to_owned(), |
| 159 | description.map(str::to_owned), |
| 160 | ); |
| 161 | self.run_channel_action( |
| 162 | EDIT_CHANNEL_FAILURE, |
| 163 | async move { |
| 164 | engine |
| 165 | .edit_channel(&channel_id, &name, description.as_deref()) |
| 166 | .await |
| 167 | }, |
| 168 | |_, _| {}, |
| 169 | None, |
| 170 | cx, |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | pub fn delete_channel(&mut self, channel_id: &str, cx: &mut Context<Self>) { |
| 175 | let Some(Some(engine)) = self.chat_action_engine(cx) else { |
| 176 | return; |
| 177 | }; |
| 178 | let channel_name = self.channel_name(channel_id).unwrap_or_default(); |
| 179 | let general_id = self |
| 180 | .channel_team_id(channel_id) |
| 181 | .and_then(|team_id| self.store.team_thread_id(&team_id).ok().flatten()); |
| 182 | let owned_id = channel_id.to_owned(); |
| 183 | let receiver = crate::runtime::spawn(async move { engine.delete_channel(&owned_id).await }); |
| 184 | let channel_id = channel_id.to_owned(); |
| 185 | cx.spawn(async move |this, cx| { |
| 186 | let deleted = matches!(receiver.await, Ok(Ok(()))); |
| 187 | this.update(cx, |state, cx| { |
| 188 | if !deleted { |
| 189 | state.raise_notice(delete_channel_failure(&channel_name), None, cx); |
| 190 | return; |
| 191 | } |
| 192 | if state.selection == Some(Selection::Channel(channel_id.clone())) |
| 193 | && let Some(general_id) = general_id |
| 194 | { |
| 195 | state.select(Selection::Channel(general_id), cx); |
| 196 | } |
| 197 | state.raise_notice(channel_deleted_notice(&channel_name), None, cx); |
| 198 | }) |
| 199 | .ok(); |
| 200 | }) |
| 201 | .detach(); |
| 202 | } |
| 203 | |
| 204 | pub fn create_channel( |
| 205 | &mut self, |
| 206 | team_id: &str, |
| 207 | name: &str, |
| 208 | description: &str, |
| 209 | privacy: ChannelPrivacy, |
| 210 | cx: &mut Context<Self>, |
| 211 | ) { |
| 212 | let Some(Some(engine)) = self.chat_action_engine(cx) else { |
| 213 | return; |
| 214 | }; |
| 215 | let (team_id, name, description) = |
| 216 | (team_id.to_owned(), name.to_owned(), description.to_owned()); |
| 217 | self.run_channel_action( |
| 218 | CREATE_CHANNEL_FAILURE, |
| 219 | async move { |
| 220 | engine |
| 221 | .create_channel(&team_id, &name, &description, privacy) |
| 222 | .await |
| 223 | }, |
| 224 | |_, _| {}, |
| 225 | None, |
| 226 | cx, |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | #[cfg(test)] |
| 232 | mod tests { |
| 233 | use teams_core::MAX_CHANNEL_NAME_LENGTH; |
| 234 | |
| 235 | use store::TeamRights; |
| 236 | |
| 237 | use super::{ |
| 238 | CHANNEL_NAME_CHARACTERS, CHANNEL_NAME_EDGES, CHANNEL_NAME_LENGTH, ChannelAdmin, |
| 239 | ChannelKind, can_create_channel, channel_admin, channel_name_problem, |
| 240 | }; |
| 241 | |
| 242 | #[test] |
| 243 | fn channel_names_follow_the_teams_rules() { |
| 244 | assert!(can_create_channel(" Planning 2027 ")); |
| 245 | assert!(!can_create_channel(" ")); |
| 246 | assert!(!can_create_channel( |
| 247 | &"a".repeat(MAX_CHANNEL_NAME_LENGTH + 1) |
| 248 | )); |
| 249 | assert_eq!( |
| 250 | channel_name_problem(&"a".repeat(MAX_CHANNEL_NAME_LENGTH + 1)), |
| 251 | Some(CHANNEL_NAME_LENGTH) |
| 252 | ); |
| 253 | assert_eq!(channel_name_problem("Q&A"), Some(CHANNEL_NAME_CHARACTERS)); |
| 254 | assert_eq!(channel_name_problem("a..b"), Some(CHANNEL_NAME_CHARACTERS)); |
| 255 | assert_eq!(channel_name_problem("_drafts"), Some(CHANNEL_NAME_EDGES)); |
| 256 | assert_eq!(channel_name_problem("notes."), Some(CHANNEL_NAME_EDGES)); |
| 257 | assert_eq!(channel_name_problem("Release notes"), None); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn channel_admin_follows_the_team_rights_and_channel_kind() { |
| 262 | let member = TeamRights { |
| 263 | edit_channel: true, |
| 264 | delete_channel: false, |
| 265 | ..TeamRights::default() |
| 266 | }; |
| 267 | assert_eq!( |
| 268 | channel_admin(member, ChannelKind::Standard), |
| 269 | ChannelAdmin { |
| 270 | can_edit: true, |
| 271 | can_delete: false |
| 272 | } |
| 273 | ); |
| 274 | assert_eq!( |
| 275 | channel_admin(member, ChannelKind::Private), |
| 276 | ChannelAdmin::default() |
| 277 | ); |
| 278 | let owner = TeamRights { |
| 279 | owner: true, |
| 280 | edit_channel: true, |
| 281 | delete_channel: true, |
| 282 | ..TeamRights::default() |
| 283 | }; |
| 284 | assert_eq!( |
| 285 | channel_admin(owner, ChannelKind::General), |
| 286 | ChannelAdmin::default() |
| 287 | ); |
| 288 | assert_eq!( |
| 289 | channel_admin(owner, ChannelKind::Private), |
| 290 | ChannelAdmin { |
| 291 | can_edit: true, |
| 292 | can_delete: true |
| 293 | } |
| 294 | ); |
| 295 | assert_eq!( |
| 296 | ChannelKind::of(false, Some("Private")), |
| 297 | ChannelKind::Private |
| 298 | ); |
| 299 | assert_eq!( |
| 300 | ChannelKind::of(true, Some("standard")), |
| 301 | ChannelKind::General |
| 302 | ); |
| 303 | assert_eq!(ChannelKind::of(false, None), ChannelKind::Standard); |
| 304 | } |
| 305 | } |