| 1 | use std::future::Future; |
| 2 | use std::pin::Pin; |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use chatsvc::{Pins, pins::SessionTransport}; |
| 6 | use session::Session; |
| 7 | use chrono::Utc; |
| 8 | use store::{ChannelLayoutRecord, FolderRecord, 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 | } |
| 22 | |
| 23 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 24 | pub struct ChatFolder { |
| 25 | pub id: String, |
| 26 | pub name: String, |
| 27 | pub kind: FolderKind, |
| 28 | pub conversation_ids: Vec<String>, |
| 29 | } |
| 30 | |
| 31 | pub trait FolderSource: Send + Sync { |
| 32 | fn folders(&self) -> BoxFuture<'_, Result<Vec<ChatFolder>>>; |
| 33 | fn pinned_channels(&self) -> BoxFuture<'_, Result<Vec<String>>>; |
| 34 | fn team_layout(&self) -> BoxFuture<'_, Result<Vec<TeamLayoutRecord>>>; |
| 35 | fn move_to_folder<'a>( |
| 36 | &'a self, |
| 37 | conversation_id: &'a str, |
| 38 | target_folder_id: &'a str, |
| 39 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>>; |
| 40 | fn remove_from_folder<'a>( |
| 41 | &'a self, |
| 42 | conversation_id: &'a str, |
| 43 | folder_id: &'a str, |
| 44 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>>; |
| 45 | } |
| 46 | |
| 47 | pub struct ChatsvcFolderSource { |
| 48 | pins: Pins<SessionTransport>, |
| 49 | } |
| 50 | |
| 51 | impl ChatsvcFolderSource { |
| 52 | pub fn new(session: &Session) -> Self { |
| 53 | ChatsvcFolderSource { |
| 54 | pins: Pins::new(session), |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl FolderSource for ChatsvcFolderSource { |
| 60 | fn folders(&self) -> BoxFuture<'_, Result<Vec<ChatFolder>>> { |
| 61 | Box::pin(async { Ok(convert(self.pins.folders().await?)) }) |
| 62 | } |
| 63 | |
| 64 | fn pinned_channels(&self) -> BoxFuture<'_, Result<Vec<String>>> { |
| 65 | Box::pin(async { Ok(self.pins.pinned_channels().await?.channel_ids) }) |
| 66 | } |
| 67 | |
| 68 | fn team_layout(&self) -> BoxFuture<'_, Result<Vec<TeamLayoutRecord>>> { |
| 69 | Box::pin(async { |
| 70 | Ok(self |
| 71 | .pins |
| 72 | .team_layout() |
| 73 | .await? |
| 74 | .into_iter() |
| 75 | .map(layout_record) |
| 76 | .collect()) |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | fn move_to_folder<'a>( |
| 81 | &'a self, |
| 82 | conversation_id: &'a str, |
| 83 | target_folder_id: &'a str, |
| 84 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>> { |
| 85 | Box::pin(async move { |
| 86 | Ok(convert( |
| 87 | self.pins |
| 88 | .move_to_folder(conversation_id, target_folder_id) |
| 89 | .await?, |
| 90 | )) |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | fn remove_from_folder<'a>( |
| 95 | &'a self, |
| 96 | conversation_id: &'a str, |
| 97 | folder_id: &'a str, |
| 98 | ) -> BoxFuture<'a, Result<Vec<ChatFolder>>> { |
| 99 | Box::pin(async move { |
| 100 | Ok(convert( |
| 101 | self.pins |
| 102 | .remove_from_folder(conversation_id, folder_id) |
| 103 | .await?, |
| 104 | )) |
| 105 | }) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn convert(folders: chatsvc::Folders) -> Vec<ChatFolder> { |
| 110 | folders |
| 111 | .folders |
| 112 | .into_iter() |
| 113 | .map(|folder| ChatFolder { |
| 114 | id: folder.id, |
| 115 | name: folder.name, |
| 116 | kind: match folder.kind { |
| 117 | chatsvc::FolderKind::Favorites => FolderKind::Favorites, |
| 118 | chatsvc::FolderKind::UserCreated => FolderKind::UserCreated, |
| 119 | }, |
| 120 | conversation_ids: folder.conversation_ids, |
| 121 | }) |
| 122 | .collect() |
| 123 | } |
| 124 | |
| 125 | fn layout_record(team: chatsvc::TeamLayout) -> TeamLayoutRecord { |
| 126 | TeamLayoutRecord { |
| 127 | team_id: team.team_id, |
| 128 | hidden: team.hidden, |
| 129 | channels: team |
| 130 | .channels |
| 131 | .into_iter() |
| 132 | .map(|channel| ChannelLayoutRecord { |
| 133 | channel_id: channel.channel_id, |
| 134 | general: channel.general, |
| 135 | hidden: channel.hidden, |
| 136 | }) |
| 137 | .collect(), |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | const META_TEAM_LAYOUT_AT: &str = "team_layout_refreshed_at"; |
| 142 | const KIND_FAVORITES: &str = "Favorites"; |
| 143 | const KIND_USER_CREATED: &str = "UserCreated"; |
| 144 | |
| 145 | fn to_record(folder: &ChatFolder) -> FolderRecord { |
| 146 | FolderRecord { |
| 147 | id: folder.id.clone(), |
| 148 | name: folder.name.clone(), |
| 149 | kind: match folder.kind { |
| 150 | FolderKind::Favorites => KIND_FAVORITES, |
| 151 | FolderKind::UserCreated => KIND_USER_CREATED, |
| 152 | } |
| 153 | .to_owned(), |
| 154 | conversation_ids: folder.conversation_ids.clone(), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | fn from_record(record: FolderRecord) -> ChatFolder { |
| 159 | ChatFolder { |
| 160 | id: record.id, |
| 161 | name: record.name, |
| 162 | kind: if record.kind == KIND_FAVORITES { |
| 163 | FolderKind::Favorites |
| 164 | } else { |
| 165 | FolderKind::UserCreated |
| 166 | }, |
| 167 | conversation_ids: record.conversation_ids, |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl<R: Remote> SyncEngine<R> { |
| 172 | pub fn with_folder_source(mut self, source: Arc<dyn FolderSource>) -> Self { |
| 173 | self.folder_source = Some(source); |
| 174 | self |
| 175 | } |
| 176 | |
| 177 | pub fn chat_folders(&self) -> Result<Vec<ChatFolder>> { |
| 178 | Ok(self.store.folders()?.into_iter().map(from_record).collect()) |
| 179 | } |
| 180 | |
| 181 | pub fn pinned_channels(&self) -> Result<Vec<String>> { |
| 182 | Ok(self.store.pinned_channel_ids()?) |
| 183 | } |
| 184 | |
| 185 | pub async fn refresh_folders(&self) -> Result<()> { |
| 186 | let source = self.folder_source()?; |
| 187 | let (folders, channels) = tokio::try_join!(source.folders(), source.pinned_channels())?; |
| 188 | self.store_folders(&folders, &channels)?; |
| 189 | self.refresh_team_layout(source).await |
| 190 | } |
| 191 | |
| 192 | async fn refresh_team_layout(&self, source: &Arc<dyn FolderSource>) -> Result<()> { |
| 193 | let last = self.store.meta_time(META_TEAM_LAYOUT_AT)?; |
| 194 | if last.is_some_and(|last| Utc::now() - last < self.config.team_layout_refresh_interval) { |
| 195 | return Ok(()); |
| 196 | } |
| 197 | let layout = source.team_layout().await?; |
| 198 | let before = self.store.sidebar()?.teams; |
| 199 | self.store.replace_team_layout(&layout)?; |
| 200 | self.store.set_meta_time(META_TEAM_LAYOUT_AT, Utc::now())?; |
| 201 | if self.store.sidebar()?.teams != before { |
| 202 | let _ = self.events.send(CoreEvent::SidebarChanged); |
| 203 | } |
| 204 | Ok(()) |
| 205 | } |
| 206 | |
| 207 | pub async fn move_to_folder( |
| 208 | &self, |
| 209 | conversation_id: &str, |
| 210 | target_folder_id: &str, |
| 211 | ) -> Result<()> { |
| 212 | let folders = self |
| 213 | .folder_source()? |
| 214 | .move_to_folder(conversation_id, target_folder_id) |
| 215 | .await?; |
| 216 | self.store_folders(&folders, &self.store.pinned_channel_ids()?) |
| 217 | } |
| 218 | |
| 219 | pub async fn remove_from_folder(&self, conversation_id: &str, folder_id: &str) -> Result<()> { |
| 220 | let folders = self |
| 221 | .folder_source()? |
| 222 | .remove_from_folder(conversation_id, folder_id) |
| 223 | .await?; |
| 224 | self.store_folders(&folders, &self.store.pinned_channel_ids()?) |
| 225 | } |
| 226 | |
| 227 | fn folder_source(&self) -> Result<&Arc<dyn FolderSource>> { |
| 228 | self.folder_source |
| 229 | .as_ref() |
| 230 | .ok_or(Error::Unsupported("chat folders without a folder source")) |
| 231 | } |
| 232 | |
| 233 | fn store_folders(&self, folders: &[ChatFolder], channel_ids: &[String]) -> Result<()> { |
| 234 | let previous = (self.chat_folders()?, self.store.pinned_channel_ids()?); |
| 235 | let records: Vec<FolderRecord> = folders.iter().map(to_record).collect(); |
| 236 | self.store.replace_folders(&records, channel_ids)?; |
| 237 | if previous != (folders.to_vec(), channel_ids.to_vec()) { |
| 238 | let _ = self.events.send(CoreEvent::FoldersChanged); |
| 239 | } |
| 240 | Ok(()) |
| 241 | } |
| 242 | } |