| @@ -14,7 +14,7 @@ use matrix_sdk::EncryptionState; |
| 14 | 14 | use matrix_sdk::authentication::matrix::MatrixSession; |
| 15 | 15 | use matrix_sdk::media::MediaRetentionPolicy; |
| 16 | 16 | use matrix_sdk::reqwest::header::{HeaderMap, HeaderValue}; |
| 17 | | use matrix_sdk::ruma::{OwnedDeviceId, owned_server_name}; |
| 17 | use matrix_sdk::ruma::{DeviceId, OwnedDeviceId, owned_server_name}; |
| 18 | 18 | use modalkit::crossterm; |
| 19 | 19 | use modalkit::env::vim::VimMode; |
| 20 | 20 | use modalkit::keybindings::InputKey; |
| @@ -278,6 +278,18 @@ pub struct Session { |
| 278 | 278 | device_id: OwnedDeviceId, |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | /// A device ID saved at logout so that the next login can reuse it. |
| 282 | /// |
| 283 | /// The SDK store is keyed by user and device ID, so logging back in with |
| 284 | /// a freshly issued device ID would leave the existing store unusable |
| 285 | /// (#775). Saving the ID lets the next login ask the homeserver for the |
| 286 | /// same device and keep using that store. |
| 287 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 288 | pub struct SavedDevice { |
| 289 | user_id: OwnedUserId, |
| 290 | device_id: OwnedDeviceId, |
| 291 | } |
| 292 | |
| 281 | 293 | impl From<Session> for MatrixSession { |
| 282 | 294 | fn from(session: Session) -> Self { |
| 283 | 295 | MatrixSession { |
| @@ -1203,6 +1215,7 @@ impl IambConfig { |
| 1203 | 1215 | pub struct ApplicationSettings { |
| 1204 | 1216 | pub layout_json: PathBuf, |
| 1205 | 1217 | pub session_json: PathBuf, |
| 1218 | pub device_json: PathBuf, |
| 1206 | 1219 | pub session_json_old: PathBuf, |
| 1207 | 1220 | pub sled_dir: PathBuf, |
| 1208 | 1221 | pub sqlite_dir: PathBuf, |
| @@ -1343,6 +1356,9 @@ impl ApplicationSettings { |
| 1343 | 1356 | let mut session_json = profile_data_dir.clone(); |
| 1344 | 1357 | session_json.push("session.json"); |
| 1345 | 1358 | |
| 1359 | let mut device_json = profile_data_dir; |
| 1360 | device_json.push("device.json"); |
| 1361 | |
| 1346 | 1362 | let mut session_json_old = profile_dir; |
| 1347 | 1363 | session_json_old.push("session.json"); |
| 1348 | 1364 | |
| @@ -1361,6 +1377,7 @@ impl ApplicationSettings { |
| 1361 | 1377 | sled_dir, |
| 1362 | 1378 | layout_json, |
| 1363 | 1379 | session_json, |
| 1380 | device_json, |
| 1364 | 1381 | session_json_old, |
| 1365 | 1382 | sqlite_dir, |
| 1366 | 1383 | sqlite_cache_dir, |
| @@ -1412,6 +1429,32 @@ impl ApplicationSettings { |
| 1412 | 1429 | Ok(()) |
| 1413 | 1430 | } |
| 1414 | 1431 | |
| 1432 | /// The device ID saved at logout, if it was saved for this profile's |
| 1433 | /// user. A missing or stale file is not an error: the next login then |
| 1434 | /// simply gets a fresh device ID from the homeserver. |
| 1435 | pub fn read_saved_device(&self) -> Option<OwnedDeviceId> { |
| 1436 | let file = File::open(self.device_json.as_path()).ok()?; |
| 1437 | let reader = BufReader::new(file); |
| 1438 | let saved: SavedDevice = serde_json::from_reader(reader).ok()?; |
| 1439 | |
| 1440 | if saved.user_id == self.profile.user_id { |
| 1441 | Some(saved.device_id) |
| 1442 | } else { |
| 1443 | None |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | pub fn write_saved_device(&self, device_id: &DeviceId) -> Result<(), IambError> { |
| 1448 | let file = File::create(self.device_json.as_path())?; |
| 1449 | let writer = BufWriter::new(file); |
| 1450 | let saved = SavedDevice { |
| 1451 | user_id: self.profile.user_id.clone(), |
| 1452 | device_id: device_id.to_owned(), |
| 1453 | }; |
| 1454 | serde_json::to_writer(writer, &saved).map_err(IambError::from)?; |
| 1455 | Ok(()) |
| 1456 | } |
| 1457 | |
| 1415 | 1458 | pub fn get_user_char_span(&self, user_id: &UserId) -> Span<'_> { |
| 1416 | 1459 | let (color, c) = self |
| 1417 | 1460 | .tunables |