| 1 | use std::sync::{Arc, OnceLock}; |
| 2 | |
| 3 | use app_call_model::report::{WebShadow, report_directory, save_report}; |
| 4 | use gpui_kit::*; |
| 5 | use serde_json::json; |
| 6 | use teams_core::DialogIdentity; |
| 7 | use webview::{DialogEvent, DialogHandle, DialogMode, DialogSpec, NEW_WINDOW_KIND, WebViewTransport}; |
| 8 | |
| 9 | use super::sdk::{self, Effect, HostContext, HostPage}; |
| 10 | use super::{UrlDialog, UrlDialogNext}; |
| 11 | use crate::app_state::{AppHandle, AppState}; |
| 12 | use crate::backend::Engine; |
| 13 | use crate::{notify, runtime, theme}; |
| 14 | |
| 15 | static HOST: OnceLock<Arc<WebViewTransport>> = OnceLock::new(); |
| 16 | const CALL_WINDOW_WIDTH: u32 = 1100; |
| 17 | const CALL_WINDOW_HEIGHT: u32 = 760; |
| 18 | |
| 19 | pub fn install_host(transport: Arc<WebViewTransport>) { |
| 20 | let _ = HOST.set(transport); |
| 21 | } |
| 22 | |
| 23 | pub fn open(dialog: UrlDialog, engine: Option<Arc<Engine>>, cx: &mut App) { |
| 24 | let Some(host) = HOST.get().cloned() else { |
| 25 | cx.open_url(&dialog.fallback_url); |
| 26 | return; |
| 27 | }; |
| 28 | let state = cx.global::<AppHandle>().0.clone(); |
| 29 | let owner = cx |
| 30 | .active_window() |
| 31 | .and_then(|window| { |
| 32 | window |
| 33 | .update(cx, |_, window, _| notify::native_handle(window)) |
| 34 | .ok() |
| 35 | }) |
| 36 | .flatten(); |
| 37 | cx.spawn(async move |cx| run(host, dialog, state, engine, owner, cx).await) |
| 38 | .detach(); |
| 39 | } |
| 40 | |
| 41 | /// Teams on the web in its own window, signed in through the shared WebView2 profile. |
| 42 | pub fn open_call_window(url: String, title: String, cx: &mut App) -> bool { |
| 43 | let Some(host) = HOST.get().cloned() else { |
| 44 | return false; |
| 45 | }; |
| 46 | let (_handle, mut events) = host.open_dialog(DialogSpec { |
| 47 | mode: DialogMode::Call, |
| 48 | title, |
| 49 | width: CALL_WINDOW_WIDTH, |
| 50 | height: CALL_WINDOW_HEIGHT, |
| 51 | background: theme::background_hex(), |
| 52 | owner: None, |
| 53 | page_url: url, |
| 54 | page_html: String::new(), |
| 55 | }); |
| 56 | cx.spawn(async move |cx| { |
| 57 | let mut shadow = WebShadow::new(chrono::Utc::now()); |
| 58 | while let Some(DialogEvent::Message(raw)) = events.recv().await { |
| 59 | if shadow.observe(&raw) { |
| 60 | continue; |
| 61 | } |
| 62 | let link = serde_json::from_str::<serde_json::Value>(&raw) |
| 63 | .ok() |
| 64 | .filter(|message| message.get("kind").and_then(serde_json::Value::as_str) == Some(NEW_WINDOW_KIND)) |
| 65 | .and_then(|message| message.get("url")?.as_str().map(str::to_owned)); |
| 66 | if let Some(link) = link { |
| 67 | cx.update(|cx| cx.open_url(&link)); |
| 68 | } |
| 69 | } |
| 70 | if shadow.is_empty() { |
| 71 | return; |
| 72 | } |
| 73 | let document = shadow.document(); |
| 74 | let saved = cx |
| 75 | .background_executor() |
| 76 | .spawn(async move { save_report(&report_directory(), &document) }) |
| 77 | .await; |
| 78 | if let Err(error) = saved { |
| 79 | log::warn!("web call report not saved: {error}"); |
| 80 | } |
| 81 | }) |
| 82 | .detach(); |
| 83 | true |
| 84 | } |
| 85 | |
| 86 | async fn run( |
| 87 | host: Arc<WebViewTransport>, |
| 88 | dialog: UrlDialog, |
| 89 | state: Entity<AppState>, |
| 90 | engine: Option<Arc<Engine>>, |
| 91 | owner: Option<isize>, |
| 92 | cx: &mut AsyncApp, |
| 93 | ) { |
| 94 | let identity = fetch_identity(engine, &dialog.scope.conversation_id).await; |
| 95 | let context = HostContext { |
| 96 | user_id: identity.user_id, |
| 97 | user_display_name: identity.display_name, |
| 98 | tenant_id: identity.tenant_id, |
| 99 | conversation_id: dialog.scope.conversation_id.clone(), |
| 100 | app_id: dialog.app_id.clone(), |
| 101 | session_id: uuid::Uuid::new_v4().to_string(), |
| 102 | dark: true, |
| 103 | web_application_resource: dialog.web_application_resource.clone(), |
| 104 | }; |
| 105 | let page_html = HostPage { |
| 106 | task_url: &dialog.url, |
| 107 | background: theme::background_hex(), |
| 108 | token_acquirer_script: &session::token_acquirer_script(), |
| 109 | } |
| 110 | .html(); |
| 111 | let (handle, mut events) = host.open_dialog(DialogSpec { |
| 112 | mode: DialogMode::Hosted, |
| 113 | title: dialog.title.clone(), |
| 114 | width: dialog.width, |
| 115 | height: dialog.height, |
| 116 | background: theme::background_hex(), |
| 117 | owner, |
| 118 | page_url: sdk::host_page_url(), |
| 119 | page_html, |
| 120 | }); |
| 121 | while let Some(DialogEvent::Message(raw)) = events.recv().await { |
| 122 | let handled = sdk::handle(&raw, &context); |
| 123 | for directive in &handled.directives { |
| 124 | handle.post(directive.to_string()); |
| 125 | } |
| 126 | if let Some(effect) = handled.effect { |
| 127 | apply(effect, &dialog, &state, &handle, cx).await; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | async fn fetch_identity(engine: Option<Arc<Engine>>, conversation_id: &str) -> DialogIdentity { |
| 133 | let empty = DialogIdentity { |
| 134 | user_id: String::new(), |
| 135 | display_name: String::new(), |
| 136 | tenant_id: String::new(), |
| 137 | }; |
| 138 | let Some(engine) = engine else { |
| 139 | return empty; |
| 140 | }; |
| 141 | let conversation_id = conversation_id.to_owned(); |
| 142 | let lookup = runtime::spawn(async move { engine.dialog_identity(&conversation_id).await.ok() }); |
| 143 | lookup.await.ok().flatten().unwrap_or(empty) |
| 144 | } |
| 145 | |
| 146 | async fn apply( |
| 147 | effect: Effect, |
| 148 | dialog: &UrlDialog, |
| 149 | state: &Entity<AppState>, |
| 150 | handle: &DialogHandle, |
| 151 | cx: &mut AsyncApp, |
| 152 | ) { |
| 153 | match effect { |
| 154 | Effect::Resize { width, height } => handle.resize(width, height), |
| 155 | Effect::OpenLink(link) => { |
| 156 | cx.update(|cx| cx.open_url(&link)); |
| 157 | } |
| 158 | Effect::Submit(result) => { |
| 159 | let scope = dialog.scope.clone(); |
| 160 | let next = state |
| 161 | .update(cx, |state, cx| state.submit_url_dialog(scope, result, cx)) |
| 162 | .await; |
| 163 | match next { |
| 164 | UrlDialogNext::Close => handle.close(), |
| 165 | UrlDialogNext::Navigate { url, width, height } => { |
| 166 | handle.resize(Some(width), Some(height)); |
| 167 | handle.post(json!({"kind": "load", "url": url}).to_string()); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |