| @@ -98,11 +98,19 @@ impl Verification { |
| 98 | 98 | |
| 99 | 99 | #[derive(Clone)] |
| 100 | 100 | struct InnerVerificationBuffer { |
| 101 | | verification: Rc<RefCell<Verification>>, |
| 101 | verification: Rc<RefCell<Option<Verification>>>, |
| 102 | 102 | connection: Rc<RefCell<Option<Connection>>>, |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | impl InnerVerificationBuffer { |
| 106 | fn verification(&self) -> Option<Verification> { |
| 107 | self.verification.borrow().clone() |
| 108 | } |
| 109 | |
| 110 | fn release_sdk_state(&self) { |
| 111 | self.verification.borrow_mut().take(); |
| 112 | } |
| 113 | |
| 106 | 114 | fn print_message(buffer: BufferHandle, message: &str) { |
| 107 | 115 | if let Ok(buffer) = buffer.upgrade() { |
| 108 | 116 | buffer.print(message); |
| @@ -115,7 +123,9 @@ impl InnerVerificationBuffer { |
| 115 | 123 | |
| 116 | 124 | pub async fn accept(&self, buffer: BufferHandle) -> Result<(), Error> { |
| 117 | 125 | if let Some(c) = self.connection.borrow().clone() { |
| 118 | | let verification = self.verification.borrow().clone(); |
| 126 | let Some(verification) = self.verification() else { |
| 127 | return Ok(()); |
| 128 | }; |
| 119 | 129 | let verification_clone = verification.clone(); |
| 120 | 130 | |
| 121 | 131 | c.spawn(async move { verification_clone.accept().await }) |
| @@ -142,7 +152,7 @@ impl InnerVerificationBuffer { |
| 142 | 152 | } else if let Some(sas) = |
| 143 | 153 | c.spawn(async move { request.start_sas().await }).await? |
| 144 | 154 | { |
| 145 | | *self.verification.borrow_mut() = sas.into(); |
| 155 | *self.verification.borrow_mut() = Some(sas.into()); |
| 146 | 156 | } |
| 147 | 157 | } |
| 148 | 158 | } |
| @@ -152,16 +162,14 @@ impl InnerVerificationBuffer { |
| 152 | 162 | |
| 153 | 163 | pub async fn confirm(&self, buffer: BufferHandle) -> Result<(), Error> { |
| 154 | 164 | if let Some(c) = self.connection.borrow().clone() { |
| 155 | | if let Verification::Sas(s) = self.verification.borrow().clone() { |
| 165 | if let Some(Verification::Sas(s)) = self.verification() { |
| 156 | 166 | let sas = s.clone(); |
| 157 | 167 | c.spawn(async move { s.confirm().await }).await?; |
| 158 | 168 | |
| 159 | 169 | if sas.is_done() { |
| 160 | 170 | self.print_done(buffer); |
| 161 | 171 | } |
| 162 | | } else if let Verification::Qr(qr) = |
| 163 | | self.verification.borrow().clone() |
| 164 | | { |
| 172 | } else if let Some(Verification::Qr(qr)) = self.verification() { |
| 165 | 173 | c.spawn(async move { qr.confirm().await }).await?; |
| 166 | 174 | |
| 167 | 175 | // if qr.is_done() { |
| @@ -177,7 +185,10 @@ impl InnerVerificationBuffer { |
| 177 | 185 | |
| 178 | 186 | pub async fn cancel(&self) -> Result<(), Error> { |
| 179 | 187 | if let Some(c) = self.connection.borrow().clone() { |
| 180 | | let verification = self.verification.borrow().clone(); |
| 188 | let Some(verification) = self.verification() else { |
| 189 | return Ok(()); |
| 190 | }; |
| 191 | |
| 181 | 192 | c.spawn(async move { verification.cancel().await }).await?; |
| 182 | 193 | } |
| 183 | 194 | |
| @@ -232,7 +243,7 @@ impl VerificationBuffer { |
| 232 | 243 | connection: Rc<RefCell<Option<Connection>>>, |
| 233 | 244 | ) -> Self { |
| 234 | 245 | let inner = InnerVerificationBuffer { |
| 235 | | verification: Rc::new(RefCell::new(verification.into())), |
| 246 | verification: Rc::new(RefCell::new(Some(verification.into()))), |
| 236 | 247 | connection, |
| 237 | 248 | }; |
| 238 | 249 | |
| @@ -268,6 +279,10 @@ impl VerificationBuffer { |
| 268 | 279 | self.buffer.clone() |
| 269 | 280 | } |
| 270 | 281 | |
| 282 | pub fn release_sdk_state(&self) { |
| 283 | self.inner.release_sdk_state(); |
| 284 | } |
| 285 | |
| 271 | 286 | pub fn accept(&self) { |
| 272 | 287 | let buffer = self.buffer(); |
| 273 | 288 | let inner = self.inner.clone(); |
| @@ -286,12 +301,14 @@ impl VerificationBuffer { |
| 286 | 301 | } |
| 287 | 302 | |
| 288 | 303 | pub async fn update_qr(&mut self, qr: QrVerification) { |
| 289 | | *self.inner.verification.borrow_mut() = qr.into(); |
| 304 | *self.inner.verification.borrow_mut() = Some(qr.into()); |
| 290 | 305 | } |
| 291 | 306 | |
| 292 | 307 | pub async fn update(&mut self, sas: SasVerification) -> Result<(), Error> { |
| 293 | | *self.inner.verification.borrow_mut() = sas.into(); |
| 294 | | let verification = self.inner.verification.borrow().clone(); |
| 308 | *self.inner.verification.borrow_mut() = Some(sas.into()); |
| 309 | let Some(verification) = self.inner.verification() else { |
| 310 | return Ok(()); |
| 311 | }; |
| 295 | 312 | |
| 296 | 313 | if let Some(c) = self.inner.connection.borrow().clone() { |
| 297 | 314 | c.spawn(async move { verification.accept().await }).await?; |
| @@ -305,8 +322,8 @@ impl VerificationBuffer { |
| 305 | 322 | pub async fn handle_event(&self, event: &AnyToDeviceEvent) { |
| 306 | 323 | match event { |
| 307 | 324 | AnyToDeviceEvent::KeyVerificationRequest(e) => { |
| 308 | | if let Verification::Request(_) = |
| 309 | | self.inner.verification.borrow().clone() |
| 325 | if let Some(Verification::Request(_)) = |
| 326 | self.inner.verification() |
| 310 | 327 | { |
| 311 | 328 | let content = |
| 312 | 329 | e.content.render(&VerificationContext::ToDevice); |
| @@ -315,7 +332,9 @@ impl VerificationBuffer { |
| 315 | 332 | } |
| 316 | 333 | } |
| 317 | 334 | AnyToDeviceEvent::KeyVerificationStart(e) => { |
| 318 | | let verification = self.inner.verification.borrow().clone(); |
| 335 | let Some(verification) = self.inner.verification() else { |
| 336 | return; |
| 337 | }; |
| 319 | 338 | |
| 320 | 339 | if let Ok(verification) = verification.try_into() { |
| 321 | 340 | let content = |
| @@ -337,8 +356,7 @@ impl VerificationBuffer { |
| 337 | 356 | self.print_sas(&e.content); |
| 338 | 357 | } |
| 339 | 358 | AnyToDeviceEvent::KeyVerificationMac(_) => { |
| 340 | | if let Verification::Sas(sas) = |
| 341 | | self.inner.verification.borrow().clone() |
| 359 | if let Some(Verification::Sas(sas)) = self.inner.verification() |
| 342 | 360 | { |
| 343 | 361 | if sas.is_done() { |
| 344 | 362 | self.inner.print_done(self.buffer.clone()); |
| @@ -363,8 +381,7 @@ impl VerificationBuffer { |
| 363 | 381 | } |
| 364 | 382 | |
| 365 | 383 | fn print_sas(&self, content: &ToDeviceKeyVerificationKeyEventContent) { |
| 366 | | if let Verification::Sas(sas) = self.inner.verification.borrow().clone() |
| 367 | | { |
| 384 | if let Some(Verification::Sas(sas)) = self.inner.verification() { |
| 368 | 385 | let message = content.render(&sas); |
| 369 | 386 | self.print(&message); |
| 370 | 387 | } |