Commit 9f10743365
Unsigned
Layout: unified · split
src/connection.rs +42 −60
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | use std::{ |
| 2 | cell::RefCell, collections::BTreeMap, path::PathBuf, rc::Weak, sync::Arc, | |
| 3 | time::Duration, | |
| 2 | cell::RefCell, collections::BTreeMap, future::Future, path::PathBuf, | |
| 3 | rc::Rc, rc::Weak, sync::Arc, time::Duration, | |
| 4 | 4 | }; |
| 5 | 5 | |
| 6 | 6 | use async_std::sync::{channel as async_channel, Receiver, Sender}; |
| @@ -91,6 +91,17 @@ pub struct Connection { | ||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | impl Connection { |
| 94 | pub async fn spawn<F>(&self, future: F) -> F::Output | |
| 95 | where | |
| 96 | F: Future + Send + 'static, | |
| 97 | F::Output: Send + 'static, | |
| 98 | { | |
| 99 | self.runtime | |
| 100 | .spawn(future) | |
| 101 | .await | |
| 102 | .expect("Tokio error while sending a message") | |
| 103 | } | |
| 104 | ||
| 94 | 105 | pub fn new(server: &MatrixServer, client: &Client) -> Self { |
| 95 | 106 | let (tx, rx) = async_channel(1000); |
| 96 | 107 | |
| @@ -143,23 +154,16 @@ impl Connection { | ||
| 143 | 154 | let room_id = room_id.to_owned(); |
| 144 | 155 | let client = self.client.clone(); |
| 145 | 156 | |
| 146 | let handle = self | |
| 147 | .runtime | |
| 148 | .spawn(async move { | |
| 149 | client | |
| 150 | .room_send( | |
| 151 | &room_id, | |
| 152 | content, | |
| 153 | Some(transaction_id.unwrap_or_else(Uuid::new_v4)), | |
| 154 | ) | |
| 155 | .await | |
| 156 | }) | |
| 157 | .await; | |
| 158 | ||
| 159 | match handle { | |
| 160 | Ok(response) => response, | |
| 161 | Err(e) => panic!("Tokio error while sending a message {:?}", e), | |
| 162 | } | |
| 157 | self.spawn(async move { | |
| 158 | client | |
| 159 | .room_send( | |
| 160 | &room_id, | |
| 161 | content, | |
| 162 | Some(transaction_id.unwrap_or_else(Uuid::new_v4)), | |
| 163 | ) | |
| 164 | .await | |
| 165 | }) | |
| 166 | .await | |
| 163 | 167 | } |
| 164 | 168 | |
| 165 | 169 | pub async fn delete_devices( |
| @@ -168,36 +172,21 @@ impl Connection { | ||
| 168 | 172 | auth_info: Option<InteractiveAuthInfo>, |
| 169 | 173 | ) -> MatrixResult<DeleteDevicesResponse> { |
| 170 | 174 | let client = self.client.clone(); |
| 171 | let handle = self | |
| 172 | .runtime | |
| 173 | .spawn(async move { | |
| 174 | if let Some(info) = auth_info { | |
| 175 | let auth = Some(info.as_auth_data()); | |
| 176 | client.delete_devices(&devices, auth).await | |
| 177 | } else { | |
| 178 | client.delete_devices(&devices, None).await | |
| 179 | } | |
| 180 | }) | |
| 181 | .await; | |
| 182 | ||
| 183 | match handle { | |
| 184 | Ok(response) => response, | |
| 185 | Err(e) => panic!("Tokio error while sending a message {:?}", e), | |
| 186 | } | |
| 175 | self.spawn(async move { | |
| 176 | if let Some(info) = auth_info { | |
| 177 | let auth = Some(info.as_auth_data()); | |
| 178 | client.delete_devices(&devices, auth).await | |
| 179 | } else { | |
| 180 | client.delete_devices(&devices, None).await | |
| 181 | } | |
| 182 | }) | |
| 183 | .await | |
| 187 | 184 | } |
| 188 | 185 | |
| 189 | 186 | /// Get the list of our own devices. |
| 190 | 187 | pub async fn devices(&self) -> MatrixResult<DevicesResponse> { |
| 191 | 188 | let client = self.client.clone(); |
| 192 | let handle = self | |
| 193 | .runtime | |
| 194 | .spawn(async move { client.devices().await }) | |
| 195 | .await; | |
| 196 | ||
| 197 | match handle { | |
| 198 | Ok(response) => response, | |
| 199 | Err(e) => panic!("Tokio error while sending a message {:?}", e), | |
| 200 | } | |
| 189 | self.spawn(async move { client.devices().await }).await | |
| 201 | 190 | } |
| 202 | 191 | |
| 203 | 192 | /// Set or reset a typing notice. |
| @@ -216,23 +205,16 @@ impl Connection { | ||
| 216 | 205 | let room_id = room_id.to_owned(); |
| 217 | 206 | let client = self.client.clone(); |
| 218 | 207 | |
| 219 | let handle = self | |
| 220 | .runtime | |
| 221 | .spawn(async move { | |
| 222 | let typing = if typing { | |
| 223 | Typing::Yes(TYPING_NOTICE_TIMEOUT) | |
| 224 | } else { | |
| 225 | Typing::No | |
| 226 | }; | |
| 227 | ||
| 228 | client.typing_notice(&room_id, typing).await | |
| 229 | }) | |
| 230 | .await; | |
| 208 | self.spawn(async move { | |
| 209 | let typing = if typing { | |
| 210 | Typing::Yes(TYPING_NOTICE_TIMEOUT) | |
| 211 | } else { | |
| 212 | Typing::No | |
| 213 | }; | |
| 231 | 214 | |
| 232 | match handle { | |
| 233 | Ok(response) => response, | |
| 234 | Err(e) => panic!("Tokio error while sending a message {:?}", e), | |
| 235 | } | |
| 215 | client.typing_notice(&room_id, typing).await | |
| 216 | }) | |
| 217 | .await | |
| 236 | 218 | } |
| 237 | 219 | |
| 238 | 220 | fn save_device_id( |