| 1 | use std::path::PathBuf; |
| 2 | use std::sync::Arc; |
| 3 | use std::time::Instant; |
| 4 | |
| 5 | use graph::Graph; |
| 6 | use store::{Sidebar, Store}; |
| 7 | use teams_core::{SyncConfig, SyncEngine}; |
| 8 | |
| 9 | use crate::BoxedResult; |
| 10 | |
| 11 | fn open_store(database: Option<PathBuf>) -> BoxedResult<Arc<Store>> { |
| 12 | let store = match database { |
| 13 | Some(path) => Store::open(&path)?, |
| 14 | None => Store::open_default()?, |
| 15 | }; |
| 16 | Ok(Arc::new(store)) |
| 17 | } |
| 18 | |
| 19 | fn counts(sidebar: &Sidebar) -> (usize, usize, usize) { |
| 20 | let channels = sidebar.teams.iter().map(|entry| entry.channels.len()).sum(); |
| 21 | (sidebar.chats.len(), sidebar.teams.len(), channels) |
| 22 | } |
| 23 | |
| 24 | pub async fn sync(graph: Graph, database: Option<PathBuf>, chat_limit: usize) -> BoxedResult<()> { |
| 25 | let config = SyncConfig { |
| 26 | chat_limit, |
| 27 | ..SyncConfig::default() |
| 28 | }; |
| 29 | let engine = SyncEngine::with_config(graph, open_store(database)?, config); |
| 30 | |
| 31 | let started = Instant::now(); |
| 32 | let summary = engine.refresh_sidebar().await?; |
| 33 | println!( |
| 34 | "network refresh: {} chats in {} pages (full: {}), teams refreshed: {}, {} teams, {} channels ({} teams failed) in {} ms", |
| 35 | summary.chats, |
| 36 | summary.chat_pages, |
| 37 | summary.full_chat_refresh, |
| 38 | summary.teams_refreshed, |
| 39 | summary.teams, |
| 40 | summary.channels, |
| 41 | summary.failed_teams, |
| 42 | started.elapsed().as_millis() |
| 43 | ); |
| 44 | |
| 45 | let started = Instant::now(); |
| 46 | let sidebar = engine.sidebar()?; |
| 47 | let elapsed = started.elapsed(); |
| 48 | let (chats, teams, channels) = counts(&sidebar); |
| 49 | println!( |
| 50 | "cache read: {chats} chats, {teams} teams, {channels} channels in {:.2} ms", |
| 51 | elapsed.as_secs_f64() * 1000.0 |
| 52 | ); |
| 53 | let unread = sidebar.chats.iter().filter(|chat| chat.unread).count(); |
| 54 | println!("unread chats: {unread}"); |
| 55 | Ok(()) |
| 56 | } |
| 57 | |
| 58 | struct Target { |
| 59 | id: String, |
| 60 | title: String, |
| 61 | } |
| 62 | |
| 63 | fn find_target(sidebar: &Sidebar, needle: &str) -> Vec<Target> { |
| 64 | let needle = needle.to_lowercase(); |
| 65 | let chats = sidebar.chats.iter().map(|chat| Target { |
| 66 | id: chat.id.clone(), |
| 67 | title: chat.title.clone(), |
| 68 | }); |
| 69 | let channels = sidebar.teams.iter().flat_map(|entry| { |
| 70 | entry.channels.iter().map(|channel| Target { |
| 71 | id: channel.id.clone(), |
| 72 | title: format!("{} / {}", entry.team.name, channel.name), |
| 73 | }) |
| 74 | }); |
| 75 | chats |
| 76 | .chain(channels) |
| 77 | .filter(|target| target.title.to_lowercase().contains(&needle)) |
| 78 | .collect() |
| 79 | } |
| 80 | |
| 81 | pub async fn open(graph: Graph, database: Option<PathBuf>, title: &str) -> BoxedResult<()> { |
| 82 | let engine = SyncEngine::new(graph, open_store(database)?); |
| 83 | if engine.sidebar()?.chats.is_empty() { |
| 84 | engine.refresh_sidebar().await?; |
| 85 | } |
| 86 | let sidebar = engine.sidebar()?; |
| 87 | let matches = find_target(&sidebar, title); |
| 88 | let Some(target) = matches.first() else { |
| 89 | return Err(format!("no chat or channel title contains {title:?}").into()); |
| 90 | }; |
| 91 | if matches.len() > 1 { |
| 92 | eprintln!( |
| 93 | "{} matches, using the first: {}", |
| 94 | matches.len(), |
| 95 | target.title |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | let started = Instant::now(); |
| 100 | let cached = engine.open_conversation(&target.id)?; |
| 101 | println!( |
| 102 | "{}: {} cached messages in {:.2} ms", |
| 103 | target.title, |
| 104 | cached.len(), |
| 105 | started.elapsed().as_secs_f64() * 1000.0 |
| 106 | ); |
| 107 | |
| 108 | let started = Instant::now(); |
| 109 | let delta = engine.fetch_newer(&target.id).await?; |
| 110 | println!( |
| 111 | "network delta: {} new, {} changed in {} ms", |
| 112 | delta.added.len(), |
| 113 | delta.updated.len(), |
| 114 | started.elapsed().as_millis() |
| 115 | ); |
| 116 | println!("cached now: {}", engine.store().message_count(&target.id)?); |
| 117 | Ok(()) |
| 118 | } |