| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use chatsvc::{ChannelEdit, ChannelPrivacy, ExistingChannel, Mailbox, NewChannel}; |
| 4 | use chrono::{Duration, Utc}; |
| 5 | |
| 6 | use crate::engine::SyncEngine; |
| 7 | use crate::error::{Error, Result}; |
| 8 | use crate::events::CoreEvent; |
| 9 | use crate::remote::Remote; |
| 10 | |
| 11 | pub const MAX_CHANNEL_NAME_LENGTH: usize = 50; |
| 12 | pub const MAX_CHANNEL_DESCRIPTION_LENGTH: usize = 1024; |
| 13 | const META_DELETED_CHANNELS: &str = "deleted_channels"; |
| 14 | const DELETED_CHANNEL_GRACE_MINUTES: i64 = 60; |
| 15 | |
| 16 | /// Channel ids deleted here with their deletion time in ms; Graph and CSA keep listing them for minutes. |
| 17 | pub(crate) fn parse_deleted_channels(text: &str) -> HashMap<String, i64> { |
| 18 | serde_json::from_str(text).unwrap_or_default() |
| 19 | } |
| 20 | |
| 21 | pub(crate) fn recent_deleted_channels( |
| 22 | deleted: HashMap<String, i64>, |
| 23 | now_ms: i64, |
| 24 | ) -> HashMap<String, i64> { |
| 25 | let grace_ms = Duration::minutes(DELETED_CHANNEL_GRACE_MINUTES).num_milliseconds(); |
| 26 | deleted |
| 27 | .into_iter() |
| 28 | .filter(|(_, deleted_at)| now_ms - deleted_at < grace_ms) |
| 29 | .collect() |
| 30 | } |
| 31 | |
| 32 | impl<R: Remote> SyncEngine<R> { |
| 33 | pub async fn create_channel( |
| 34 | &self, |
| 35 | team_id: &str, |
| 36 | name: &str, |
| 37 | description: &str, |
| 38 | privacy: ChannelPrivacy, |
| 39 | ) -> Result<()> { |
| 40 | let name = name.trim(); |
| 41 | if name.is_empty() { |
| 42 | return Err(Error::Unsupported("a channel needs a name")); |
| 43 | } |
| 44 | let team_thread_id = self |
| 45 | .store |
| 46 | .team_thread_id(team_id)? |
| 47 | .ok_or_else(|| Error::UnknownConversation(team_id.to_owned()))?; |
| 48 | let object_id = self.my_user_id().await?; |
| 49 | let tenant_id = self.my_tenant_id_from_any_chat().await?; |
| 50 | let source = self.folder_source()?; |
| 51 | let created = source |
| 52 | .create_channel( |
| 53 | NewChannel { |
| 54 | team_thread_id: &team_thread_id, |
| 55 | group_id: team_id, |
| 56 | name, |
| 57 | description: description.trim(), |
| 58 | privacy, |
| 59 | }, |
| 60 | Mailbox { |
| 61 | object_id: &object_id, |
| 62 | tenant_id: &tenant_id, |
| 63 | }, |
| 64 | ) |
| 65 | .await; |
| 66 | self.refresh_teams(true).await; |
| 67 | if let Err(error) = self.refresh_team_layout(source, true).await { |
| 68 | self.report(format!("cannot refresh the team layout: {error}")); |
| 69 | } |
| 70 | let _ = self.events.send(CoreEvent::SidebarChanged); |
| 71 | created |
| 72 | } |
| 73 | |
| 74 | pub async fn channel_description(&self, channel_id: &str) -> Result<String> { |
| 75 | let channel = self |
| 76 | .store |
| 77 | .channel(channel_id)? |
| 78 | .ok_or_else(|| Error::UnknownConversation(channel_id.to_owned()))?; |
| 79 | self.remote |
| 80 | .channel_description(&channel.team_id, channel_id) |
| 81 | .await |
| 82 | } |
| 83 | |
| 84 | pub async fn edit_channel( |
| 85 | &self, |
| 86 | channel_id: &str, |
| 87 | name: &str, |
| 88 | description: Option<&str>, |
| 89 | ) -> Result<()> { |
| 90 | let name = name.trim(); |
| 91 | if name.is_empty() { |
| 92 | return Err(Error::Unsupported("a channel needs a name")); |
| 93 | } |
| 94 | let address = self.existing_channel(channel_id)?; |
| 95 | let (object_id, tenant_id) = self.mailbox_ids().await?; |
| 96 | let source = self.folder_source()?; |
| 97 | source |
| 98 | .update_channel( |
| 99 | address.existing(), |
| 100 | ChannelEdit { |
| 101 | name, |
| 102 | description: description.map(str::trim), |
| 103 | }, |
| 104 | Mailbox { |
| 105 | object_id: &object_id, |
| 106 | tenant_id: &tenant_id, |
| 107 | }, |
| 108 | ) |
| 109 | .await?; |
| 110 | self.store.rename_channel(channel_id, name)?; |
| 111 | let _ = self.events.send(CoreEvent::SidebarChanged); |
| 112 | Ok(()) |
| 113 | } |
| 114 | |
| 115 | pub async fn delete_channel(&self, channel_id: &str) -> Result<()> { |
| 116 | let address = self.existing_channel(channel_id)?; |
| 117 | let (object_id, tenant_id) = self.mailbox_ids().await?; |
| 118 | self.folder_source()? |
| 119 | .delete_channel( |
| 120 | address.existing(), |
| 121 | Mailbox { |
| 122 | object_id: &object_id, |
| 123 | tenant_id: &tenant_id, |
| 124 | }, |
| 125 | ) |
| 126 | .await?; |
| 127 | self.remember_deleted_channel(channel_id)?; |
| 128 | self.store.remove_channel(channel_id)?; |
| 129 | let _ = self.events.send(CoreEvent::SidebarChanged); |
| 130 | Ok(()) |
| 131 | } |
| 132 | |
| 133 | pub(crate) fn deleted_channel_ids(&self) -> HashMap<String, i64> { |
| 134 | let stored = self |
| 135 | .store |
| 136 | .meta(META_DELETED_CHANNELS) |
| 137 | .ok() |
| 138 | .flatten() |
| 139 | .map(|text| parse_deleted_channels(&text)) |
| 140 | .unwrap_or_default(); |
| 141 | recent_deleted_channels(stored, Utc::now().timestamp_millis()) |
| 142 | } |
| 143 | |
| 144 | fn remember_deleted_channel(&self, channel_id: &str) -> Result<()> { |
| 145 | let mut deleted = self.deleted_channel_ids(); |
| 146 | deleted.insert(channel_id.to_owned(), Utc::now().timestamp_millis()); |
| 147 | let text = serde_json::to_string(&deleted).unwrap_or_default(); |
| 148 | Ok(self.store.set_meta(META_DELETED_CHANNELS, &text)?) |
| 149 | } |
| 150 | |
| 151 | fn existing_channel(&self, channel_id: &str) -> Result<ChannelAddress> { |
| 152 | let channel = self |
| 153 | .store |
| 154 | .channel(channel_id)? |
| 155 | .ok_or_else(|| Error::UnknownConversation(channel_id.to_owned()))?; |
| 156 | let team_thread_id = self |
| 157 | .store |
| 158 | .team_thread_id(&channel.team_id)? |
| 159 | .ok_or_else(|| Error::UnknownConversation(channel.team_id.clone()))?; |
| 160 | Ok(ChannelAddress { |
| 161 | team_thread_id, |
| 162 | group_id: channel.team_id, |
| 163 | channel_id: channel.id, |
| 164 | substrate_group_id: self.store.channel_substrate_group_id(channel_id)?, |
| 165 | }) |
| 166 | } |
| 167 | |
| 168 | async fn mailbox_ids(&self) -> Result<(String, String)> { |
| 169 | Ok(( |
| 170 | self.my_user_id().await?, |
| 171 | self.my_tenant_id_from_any_chat().await?, |
| 172 | )) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | struct ChannelAddress { |
| 177 | team_thread_id: String, |
| 178 | group_id: String, |
| 179 | channel_id: String, |
| 180 | substrate_group_id: Option<String>, |
| 181 | } |
| 182 | |
| 183 | impl ChannelAddress { |
| 184 | fn existing(&self) -> ExistingChannel<'_> { |
| 185 | ExistingChannel { |
| 186 | team_thread_id: &self.team_thread_id, |
| 187 | group_id: &self.group_id, |
| 188 | channel_id: &self.channel_id, |
| 189 | substrate_group_id: self.substrate_group_id.as_deref(), |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | #[cfg(test)] |
| 195 | mod tests { |
| 196 | use super::*; |
| 197 | |
| 198 | #[test] |
| 199 | fn deleted_channels_are_kept_for_an_hour() { |
| 200 | let now_ms = 10 * 60 * 60 * 1000; |
| 201 | let deleted = parse_deleted_channels(&format!( |
| 202 | r#"{{"19:fresh":{},"19:old":{}}}"#, |
| 203 | now_ms - 1000, |
| 204 | now_ms - 61 * 60 * 1000 |
| 205 | )); |
| 206 | let recent = recent_deleted_channels(deleted, now_ms); |
| 207 | assert!(recent.contains_key("19:fresh")); |
| 208 | assert!(!recent.contains_key("19:old")); |
| 209 | assert!(parse_deleted_channels("garbage").is_empty()); |
| 210 | } |
| 211 | } |