| 1 | use std::future::Future; |
| 2 | use std::pin::Pin; |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use chatsvc::{ChatSection, ChatSectionSettings, Pins, UserSettings, pins::SessionTransport}; |
| 6 | use chrono::Utc; |
| 7 | use session::Session; |
| 8 | use store::{ChannelLayoutRecord, FolderRecord, Store, TeamLayoutRecord}; |
| 9 | |
| 10 | use crate::engine::SyncEngine; |
| 11 | use crate::error::{Error, Result}; |
| 12 | use crate::events::CoreEvent; |
| 13 | use crate::remote::Remote; |
| 14 | |
| 15 | pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; |
| 16 | |
| 17 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 18 | pub enum FolderKind { |
| 19 | Favorites, |
| 20 | UserCreated, |
| 21 | Recent, |
| 22 | Meeting, |
| 23 | Muted, |
| 24 | } |
| 25 | |
| 26 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 27 | pub struct ChatFolder { |
| 28 | pub id: String, |
| 29 | pub name: String, |
| 30 | pub kind: FolderKind, |
| 31 | pub expanded: bool, |
| 32 | pub conversation_ids: Vec<String>, |
| 33 | } |
| 34 | |
| 35 | pub trait FolderSource: Send + Sync { |
| 36 | fn folders(&self) -> BoxFuture<'_, Result<Vec<ChatFolder>>>; |
| 37 | fn pinned_channels(&self) -> BoxFuture<'_, Result<Vec<String>>>; |
| 38 | fn team_layout(&self) -> BoxFuture<'_, Result<Vec<TeamLayoutRecord>>>; |
| 39 | fn section_settings(&self) -> BoxFuture<'_, Result<ChatSectionSettings>>; |
| 40 | fn set_section_enabled(&self, section: ChatSection, enabled: bool) |
| 41 | -> BoxFuture<'_, Result<()>>; |
| 42 | fn set_folder_expanded<'a>( |
| 43 | &'a self, |
| 44 | folder_id: &'a str, |
| 45 | expanded: bool, |
| 46 | ) -> BoxFuture<'a, Result<()>>; |
| 47 | fn move_to_folder<'a>( |
| 48 | &'a self, |
| 49 | conversation_id: &'a str, |
| 50 | target_folder_id: &'a str, |
| 51 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>>; |
| 52 | fn remove_from_folder<'a>( |
| 53 | &'a self, |
| 54 | conversation_id: &'a str, |
| 55 | folder_id: &'a str, |
| 56 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>>; |
| 57 | } |
| 58 | |
| 59 | pub struct ChatsvcFolderSource { |
| 60 | pins: Pins<SessionTransport>, |
| 61 | settings: UserSettings, |
| 62 | } |
| 63 | |
| 64 | impl ChatsvcFolderSource { |
| 65 | pub fn new(session: &Session) -> Self { |
| 66 | ChatsvcFolderSource { |
| 67 | pins: Pins::new(session), |
| 68 | settings: UserSettings::new(session), |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl FolderSource for ChatsvcFolderSource { |
| 74 | fn folders(&self) -> BoxFuture<'_, Result<Vec<ChatFolder>>> { |
| 75 | Box::pin(async { Ok(convert(self.pins.folders().await?)) }) |
| 76 | } |
| 77 | |
| 78 | fn pinned_channels(&self) -> BoxFuture<'_, Result<Vec<String>>> { |
| 79 | Box::pin(async { Ok(self.pins.pinned_channels().await?.channel_ids) }) |
| 80 | } |
| 81 | |
| 82 | fn team_layout(&self) -> BoxFuture<'_, Result<Vec<TeamLayoutRecord>>> { |
| 83 | Box::pin(async { |
| 84 | Ok(self |
| 85 | .pins |
| 86 | .team_layout() |
| 87 | .await? |
| 88 | .into_iter() |
| 89 | .map(layout_record) |
| 90 | .collect()) |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | fn section_settings(&self) -> BoxFuture<'_, Result<ChatSectionSettings>> { |
| 95 | Box::pin(async { Ok(self.settings.chat_sections().await?) }) |
| 96 | } |
| 97 | |
| 98 | fn set_section_enabled( |
| 99 | &self, |
| 100 | section: ChatSection, |
| 101 | enabled: bool, |
| 102 | ) -> BoxFuture<'_, Result<()>> { |
| 103 | Box::pin(async move { Ok(self.settings.set_chat_section(section, enabled).await?) }) |
| 104 | } |
| 105 | |
| 106 | fn set_folder_expanded<'a>( |
| 107 | &'a self, |
| 108 | folder_id: &'a str, |
| 109 | expanded: bool, |
| 110 | ) -> BoxFuture<'a, Result<()>> { |
| 111 | Box::pin(async move { Ok(self.pins.set_folder_expanded(folder_id, expanded).await?) }) |
| 112 | } |
| 113 | |
| 114 | fn move_to_folder<'a>( |
| 115 | &'a self, |
| 116 | conversation_id: &'a str, |
| 117 | target_folder_id: &'a str, |
| 118 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>> { |
| 119 | Box::pin(async move { |
| 120 | Ok(convert( |
| 121 | self.pins |
| 122 | .move_to_folder(conversation_id, target_folder_id) |
| 123 | .await?, |
| 124 | )) |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | fn remove_from_folder<'a>( |
| 129 | &'a self, |
| 130 | conversation_id: &'a str, |
| 131 | folder_id: &'a str, |
| 132 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>> { |
| 133 | Box::pin(async move { |
| 134 | Ok(convert( |
| 135 | self.pins |
| 136 | .remove_from_folder(conversation_id, folder_id) |
| 137 | .await?, |
| 138 | )) |
| 139 | }) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fn convert(folders: chatsvc::Folders) -> Vec<ChatFolder> { |
| 144 | folders |
| 145 | .folders |
| 146 | .into_iter() |
| 147 | .map(|folder| ChatFolder { |
| 148 | id: folder.id, |
| 149 | name: folder.name, |
| 150 | kind: match folder.kind { |
| 151 | chatsvc::FolderKind::Favorites => FolderKind::Favorites, |
| 152 | chatsvc::FolderKind::UserCreated => FolderKind::UserCreated, |
| 153 | chatsvc::FolderKind::Recent => FolderKind::Recent, |
| 154 | chatsvc::FolderKind::Meeting => FolderKind::Meeting, |
| 155 | chatsvc::FolderKind::Muted => FolderKind::Muted, |
| 156 | }, |
| 157 | expanded: folder.expanded, |
| 158 | conversation_ids: folder.conversation_ids, |
| 159 | }) |
| 160 | .collect() |
| 161 | } |
| 162 | |
| 163 | fn layout_record(team: chatsvc::TeamLayout) -> TeamLayoutRecord { |
| 164 | TeamLayoutRecord { |
| 165 | team_id: team.team_id, |
| 166 | hidden: team.hidden, |
| 167 | channels: team |
| 168 | .channels |
| 169 | .into_iter() |
| 170 | .map(|channel| ChannelLayoutRecord { |
| 171 | channel_id: channel.channel_id, |
| 172 | general: channel.general, |
| 173 | hidden: channel.hidden, |
| 174 | }) |
| 175 | .collect(), |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | const META_TEAM_LAYOUT_AT: &str = "team_layout_refreshed_at"; |
| 180 | const KIND_FAVORITES: &str = "Favorites"; |
| 181 | const KIND_USER_CREATED: &str = "UserCreated"; |
| 182 | const KIND_RECENT: &str = "RecentChats"; |
| 183 | const KIND_MEETING: &str = "MeetingChats"; |
| 184 | const KIND_MUTED: &str = "MutedChats"; |
| 185 | const META_MUTED_SECTION: &str = "chat_section_muted"; |
| 186 | const META_MEETING_SECTION: &str = "chat_section_meeting"; |
| 187 | |
| 188 | fn to_record(folder: &ChatFolder) -> FolderRecord { |
| 189 | FolderRecord { |
| 190 | id: folder.id.clone(), |
| 191 | name: folder.name.clone(), |
| 192 | kind: match folder.kind { |
| 193 | FolderKind::Favorites => KIND_FAVORITES, |
| 194 | FolderKind::UserCreated => KIND_USER_CREATED, |
| 195 | FolderKind::Recent => KIND_RECENT, |
| 196 | FolderKind::Meeting => KIND_MEETING, |
| 197 | FolderKind::Muted => KIND_MUTED, |
| 198 | } |
| 199 | .to_owned(), |
| 200 | expanded: folder.expanded, |
| 201 | conversation_ids: folder.conversation_ids.clone(), |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | fn from_record(record: FolderRecord) -> ChatFolder { |
| 206 | ChatFolder { |
| 207 | id: record.id, |
| 208 | name: record.name, |
| 209 | kind: match record.kind.as_str() { |
| 210 | KIND_FAVORITES => FolderKind::Favorites, |
| 211 | KIND_RECENT => FolderKind::Recent, |
| 212 | KIND_MEETING => FolderKind::Meeting, |
| 213 | KIND_MUTED => FolderKind::Muted, |
| 214 | _ => FolderKind::UserCreated, |
| 215 | }, |
| 216 | expanded: record.expanded, |
| 217 | conversation_ids: record.conversation_ids, |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | pub fn stored_section_settings(store: &Store) -> ChatSectionSettings { |
| 222 | let read = |key: &str| { |
| 223 | store |
| 224 | .meta(key) |
| 225 | .ok() |
| 226 | .flatten() |
| 227 | .and_then(|value| value.parse().ok()) |
| 228 | }; |
| 229 | ChatSectionSettings { |
| 230 | muted: read(META_MUTED_SECTION), |
| 231 | meeting: read(META_MEETING_SECTION), |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | pub fn store_section_settings(store: &Store, settings: &ChatSectionSettings) -> Result<()> { |
| 236 | for (key, value) in [ |
| 237 | (META_MUTED_SECTION, settings.muted), |
| 238 | (META_MEETING_SECTION, settings.meeting), |
| 239 | ] { |
| 240 | let text = value.map(|enabled| enabled.to_string()).unwrap_or_default(); |
| 241 | store.set_meta(key, &text)?; |
| 242 | } |
| 243 | Ok(()) |
| 244 | } |
| 245 | |
| 246 | impl<R: Remote> SyncEngine<R> { |
| 247 | pub fn with_folder_source(mut self, source: Arc<dyn FolderSource>) -> Self { |
| 248 | self.folder_source = Some(source); |
| 249 | self |
| 250 | } |
| 251 | |
| 252 | pub fn chat_folders(&self) -> Result<Vec<ChatFolder>> { |
| 253 | Ok(self.store.folders()?.into_iter().map(from_record).collect()) |
| 254 | } |
| 255 | |
| 256 | pub fn pinned_channels(&self) -> Result<Vec<String>> { |
| 257 | Ok(self.store.pinned_channel_ids()?) |
| 258 | } |
| 259 | |
| 260 | pub async fn refresh_folders(&self) -> Result<()> { |
| 261 | let source = self.folder_source()?; |
| 262 | let (folders, channels) = tokio::try_join!(source.folders(), source.pinned_channels())?; |
| 263 | self.store_folders(&folders, &channels)?; |
| 264 | self.refresh_section_settings(source).await; |
| 265 | self.refresh_team_layout(source).await |
| 266 | } |
| 267 | |
| 268 | async fn refresh_section_settings(&self, source: &Arc<dyn FolderSource>) { |
| 269 | let outcome = match source.section_settings().await { |
| 270 | Ok(settings) => self.apply_section_settings(&settings), |
| 271 | Err(error) => Err(error), |
| 272 | }; |
| 273 | if let Err(error) = outcome { |
| 274 | self.report(format!("cannot refresh chat list settings: {error}")); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | fn apply_section_settings(&self, settings: &ChatSectionSettings) -> Result<()> { |
| 279 | if stored_section_settings(&self.store) != *settings { |
| 280 | store_section_settings(&self.store, settings)?; |
| 281 | let _ = self.events.send(CoreEvent::FoldersChanged); |
| 282 | } |
| 283 | Ok(()) |
| 284 | } |
| 285 | |
| 286 | pub async fn set_section_enabled(&self, section: ChatSection, enabled: bool) -> Result<()> { |
| 287 | self.folder_source()? |
| 288 | .set_section_enabled(section, enabled) |
| 289 | .await |
| 290 | } |
| 291 | |
| 292 | pub async fn set_folder_expanded(&self, folder_id: &str, expanded: bool) -> Result<()> { |
| 293 | self.folder_source()? |
| 294 | .set_folder_expanded(folder_id, expanded) |
| 295 | .await |
| 296 | } |
| 297 | |
| 298 | async fn refresh_team_layout(&self, source: &Arc<dyn FolderSource>) -> Result<()> { |
| 299 | let last = self.store.meta_time(META_TEAM_LAYOUT_AT)?; |
| 300 | if last.is_some_and(|last| Utc::now() - last < self.config.team_layout_refresh_interval) { |
| 301 | return Ok(()); |
| 302 | } |
| 303 | let layout = source.team_layout().await?; |
| 304 | let before = self.store.sidebar()?.teams; |
| 305 | self.store.replace_team_layout(&layout)?; |
| 306 | self.store.set_meta_time(META_TEAM_LAYOUT_AT, Utc::now())?; |
| 307 | if self.store.sidebar()?.teams != before { |
| 308 | let _ = self.events.send(CoreEvent::SidebarChanged); |
| 309 | } |
| 310 | Ok(()) |
| 311 | } |
| 312 | |
| 313 | pub async fn move_to_folder( |
| 314 | &self, |
| 315 | conversation_id: &str, |
| 316 | target_folder_id: &str, |
| 317 | ) -> Result<()> { |
| 318 | let folders = self |
| 319 | .folder_source()? |
| 320 | .move_to_folder(conversation_id, target_folder_id) |
| 321 | .await?; |
| 322 | self.store_folders(&folders, &self.store.pinned_channel_ids()?) |
| 323 | } |
| 324 | |
| 325 | pub async fn remove_from_folder(&self, conversation_id: &str, folder_id: &str) -> Result<()> { |
| 326 | let folders = self |
| 327 | .folder_source()? |
| 328 | .remove_from_folder(conversation_id, folder_id) |
| 329 | .await?; |
| 330 | self.store_folders(&folders, &self.store.pinned_channel_ids()?) |
| 331 | } |
| 332 | |
| 333 | fn folder_source(&self) -> Result<&Arc<dyn FolderSource>> { |
| 334 | self.folder_source |
| 335 | .as_ref() |
| 336 | .ok_or(Error::Unsupported("chat folders without a folder source")) |
| 337 | } |
| 338 | |
| 339 | fn store_folders(&self, folders: &[ChatFolder], channel_ids: &[String]) -> Result<()> { |
| 340 | let previous = (self.chat_folders()?, self.store.pinned_channel_ids()?); |
| 341 | let records: Vec<FolderRecord> = folders.iter().map(to_record).collect(); |
| 342 | self.store.replace_folders(&records, channel_ids)?; |
| 343 | if previous != (folders.to_vec(), channel_ids.to_vec()) { |
| 344 | let _ = self.events.send(CoreEvent::FoldersChanged); |
| 345 | } |
| 346 | Ok(()) |
| 347 | } |
| 348 | } |