| 1 | use chatsvc::{ChannelPrivacy, Mailbox, NewChannel}; |
| 2 | |
| 3 | use crate::engine::SyncEngine; |
| 4 | use crate::error::{Error, Result}; |
| 5 | use crate::events::CoreEvent; |
| 6 | use crate::remote::Remote; |
| 7 | |
| 8 | pub const MAX_CHANNEL_NAME_LENGTH: usize = 50; |
| 9 | pub const MAX_CHANNEL_DESCRIPTION_LENGTH: usize = 1024; |
| 10 | |
| 11 | impl<R: Remote> SyncEngine<R> { |
| 12 | pub async fn create_channel( |
| 13 | &self, |
| 14 | team_id: &str, |
| 15 | name: &str, |
| 16 | description: &str, |
| 17 | privacy: ChannelPrivacy, |
| 18 | ) -> Result<()> { |
| 19 | let name = name.trim(); |
| 20 | if name.is_empty() { |
| 21 | return Err(Error::Unsupported("a channel needs a name")); |
| 22 | } |
| 23 | let team_thread_id = self |
| 24 | .store |
| 25 | .team_thread_id(team_id)? |
| 26 | .ok_or_else(|| Error::UnknownConversation(team_id.to_owned()))?; |
| 27 | let object_id = self.my_user_id().await?; |
| 28 | let tenant_id = self.my_tenant_id_from_any_chat().await?; |
| 29 | let source = self.folder_source()?; |
| 30 | source |
| 31 | .create_channel( |
| 32 | NewChannel { |
| 33 | team_thread_id: &team_thread_id, |
| 34 | group_id: team_id, |
| 35 | name, |
| 36 | description: description.trim(), |
| 37 | privacy, |
| 38 | }, |
| 39 | Mailbox { |
| 40 | object_id: &object_id, |
| 41 | tenant_id: &tenant_id, |
| 42 | }, |
| 43 | ) |
| 44 | .await?; |
| 45 | self.refresh_teams(true).await; |
| 46 | if let Err(error) = self.refresh_team_layout(source, true).await { |
| 47 | self.report(format!("cannot refresh the team layout: {error}")); |
| 48 | } |
| 49 | let _ = self.events.send(CoreEvent::SidebarChanged); |
| 50 | Ok(()) |
| 51 | } |
| 52 | } |