| 1 | use serde_json::{Value, json}; |
| 2 | use session::{Method, Request, Session}; |
| 3 | |
| 4 | use crate::channel_notifications::{ChannelNotifications, PROPERTY_NAME}; |
| 5 | use crate::chat_notifications::{ |
| 6 | self, ChatNotificationMode, ChatNotificationWrite, DesktopNotifications, |
| 7 | }; |
| 8 | use crate::error::{Error, Result}; |
| 9 | use crate::messages::{MessageTransport, SessionMessageTransport, encode, ensure_success}; |
| 10 | use crate::pins::DEFAULT_REGION; |
| 11 | use crate::read_horizon::{self, ReadHorizon}; |
| 12 | |
| 13 | const PAGE_SIZE: usize = 200; |
| 14 | const MAX_PAGES: usize = 100; |
| 15 | const ALERTS_OFF: &str = "false"; |
| 16 | const CLEAR_HISTORY_PROPERTY: &str = "clearHistoryTime"; |
| 17 | const HIDDEN_TIME_PROPERTY: &str = "unpinnedTime"; |
| 18 | const HISTORY_HIDDEN_TIME_PROPERTY: &str = "historyHiddenTime"; |
| 19 | |
| 20 | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 21 | pub struct ConversationState { |
| 22 | pub id: String, |
| 23 | pub muted: bool, |
| 24 | pub hidden: Option<bool>, |
| 25 | pub read: ReadHorizon, |
| 26 | pub history_cleared_ms: Option<i64>, |
| 27 | pub hidden_at_ms: Option<i64>, |
| 28 | pub notification_settings: Option<String>, |
| 29 | } |
| 30 | |
| 31 | impl ConversationState { |
| 32 | pub fn desktop_notifications(&self) -> DesktopNotifications { |
| 33 | chat_notifications::parse_desktop(self.notification_settings.as_deref()) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub struct Conversations<T: MessageTransport = SessionMessageTransport> { |
| 38 | transport: T, |
| 39 | base_url: String, |
| 40 | } |
| 41 | |
| 42 | impl Conversations<SessionMessageTransport> { |
| 43 | pub fn new(session: &Session) -> Self { |
| 44 | Self::with_region(session, DEFAULT_REGION) |
| 45 | } |
| 46 | |
| 47 | pub fn with_region(session: &Session, region: &str) -> Self { |
| 48 | Conversations::with_transport(SessionMessageTransport::new(session), region) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl<T: MessageTransport> Conversations<T> { |
| 53 | pub fn with_transport(transport: T, region: &str) -> Self { |
| 54 | Conversations { |
| 55 | transport, |
| 56 | base_url: format!( |
| 57 | "https://teams.cloud.microsoft/api/chatsvc/{region}/v1/users/ME/conversations" |
| 58 | ), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn alerts_url(&self, conversation_id: &str) -> String { |
| 63 | format!( |
| 64 | "{}/{}/properties?name=alerts", |
| 65 | self.base_url, |
| 66 | encode(conversation_id) |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | pub async fn conversation_states(&self) -> Result<Vec<ConversationState>> { |
| 71 | let url = format!( |
| 72 | "{}?view=msnp24Equivalent&pageSize={PAGE_SIZE}", |
| 73 | self.base_url |
| 74 | ); |
| 75 | let mut states = Vec::new(); |
| 76 | let mut next_url = Some(url); |
| 77 | for _ in 0..MAX_PAGES { |
| 78 | let Some(url) = next_url.take() else { |
| 79 | break; |
| 80 | }; |
| 81 | let answer = self.transport.send(Request::get(&url)).await?; |
| 82 | ensure_success(&answer)?; |
| 83 | let page = states_in_page(&answer.body)?; |
| 84 | if page.is_empty() { |
| 85 | break; |
| 86 | } |
| 87 | states.extend(page); |
| 88 | next_url = backward_link(&answer.body).filter(|link| *link != url); |
| 89 | } |
| 90 | Ok(states) |
| 91 | } |
| 92 | |
| 93 | pub async fn set_alerts(&self, conversation_id: &str, enabled: bool) -> Result<()> { |
| 94 | let body = json!({"alerts": if enabled { "true" } else { ALERTS_OFF }}); |
| 95 | let answer = self |
| 96 | .transport |
| 97 | .send(Request::with_body( |
| 98 | Method::Put, |
| 99 | self.alerts_url(conversation_id), |
| 100 | body, |
| 101 | )) |
| 102 | .await?; |
| 103 | ensure_success(&answer) |
| 104 | } |
| 105 | |
| 106 | /// Teams writes the merged notification settings first, then `alerts`. |
| 107 | pub async fn set_chat_notifications( |
| 108 | &self, |
| 109 | conversation_id: &str, |
| 110 | current_settings: Option<&str>, |
| 111 | mode: ChatNotificationMode, |
| 112 | desktop: DesktopNotifications, |
| 113 | ) -> Result<ChatNotificationWrite> { |
| 114 | let write = chat_notifications::chat_notification_write(current_settings, mode, desktop); |
| 115 | let url = format!( |
| 116 | "{}/{}/properties?name={}", |
| 117 | self.base_url, |
| 118 | encode(conversation_id), |
| 119 | chat_notifications::PROPERTY_NAME |
| 120 | ); |
| 121 | let body = json!({ chat_notifications::PROPERTY_NAME: write.settings }); |
| 122 | let answer = self |
| 123 | .transport |
| 124 | .send(Request::with_body(Method::Put, url, body)) |
| 125 | .await?; |
| 126 | ensure_success(&answer)?; |
| 127 | self.set_alerts(conversation_id, write.alerts).await?; |
| 128 | Ok(write) |
| 129 | } |
| 130 | |
| 131 | /// Teams' "Remove chat history": everything up to this time disappears for the current user only. |
| 132 | pub async fn clear_history(&self, conversation_id: &str, cleared_until_ms: i64) -> Result<()> { |
| 133 | let answer = self |
| 134 | .transport |
| 135 | .send(clear_history_request( |
| 136 | &self.base_url, |
| 137 | conversation_id, |
| 138 | cleared_until_ms, |
| 139 | )) |
| 140 | .await?; |
| 141 | ensure_success(&answer) |
| 142 | } |
| 143 | |
| 144 | pub async fn set_read_horizon(&self, conversation_id: &str, horizon: &str) -> Result<()> { |
| 145 | self.put_property(conversation_id, read_horizon::PROPERTY_NAME, json!(horizon)) |
| 146 | .await |
| 147 | } |
| 148 | |
| 149 | /// Teams' "Mark as unread": a bookmark marks the conversation unread even when the last message is my own. |
| 150 | pub async fn set_read_bookmark(&self, conversation_id: &str, bookmark: &str) -> Result<()> { |
| 151 | self.put_property( |
| 152 | conversation_id, |
| 153 | read_horizon::BOOKMARK_PROPERTY_NAME, |
| 154 | json!(bookmark), |
| 155 | ) |
| 156 | .await |
| 157 | } |
| 158 | |
| 159 | /// Teams' chat "Hide" (`setChatVisibility`): hidden until a newer message; `None` shows it again. |
| 160 | pub async fn set_hidden_at( |
| 161 | &self, |
| 162 | conversation_id: &str, |
| 163 | hidden_at_ms: Option<i64>, |
| 164 | ) -> Result<()> { |
| 165 | self.put_property(conversation_id, HIDDEN_TIME_PROPERTY, json!(hidden_at_ms)) |
| 166 | .await?; |
| 167 | let history_hidden = hidden_at_ms.map(|millis| millis.to_string()); |
| 168 | self.put_property( |
| 169 | conversation_id, |
| 170 | HISTORY_HIDDEN_TIME_PROPERTY, |
| 171 | json!(history_hidden), |
| 172 | ) |
| 173 | .await |
| 174 | } |
| 175 | |
| 176 | async fn put_property(&self, conversation_id: &str, name: &str, value: Value) -> Result<()> { |
| 177 | let url = format!( |
| 178 | "{}/{}/properties?name={name}", |
| 179 | self.base_url, |
| 180 | encode(conversation_id) |
| 181 | ); |
| 182 | let answer = self |
| 183 | .transport |
| 184 | .send(Request::with_body(Method::Put, url, json!({ name: value }))) |
| 185 | .await?; |
| 186 | ensure_success(&answer) |
| 187 | } |
| 188 | |
| 189 | pub async fn set_channel_notifications( |
| 190 | &self, |
| 191 | channel_id: &str, |
| 192 | notifications: ChannelNotifications, |
| 193 | ) -> Result<()> { |
| 194 | let url = format!( |
| 195 | "{}/{}/properties?name={PROPERTY_NAME}", |
| 196 | self.base_url, |
| 197 | encode(channel_id) |
| 198 | ); |
| 199 | let body = json!({ PROPERTY_NAME: notifications.property_value() }); |
| 200 | let answer = self |
| 201 | .transport |
| 202 | .send(Request::with_body(Method::Put, url, body)) |
| 203 | .await?; |
| 204 | ensure_success(&answer) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | pub fn clear_history_request( |
| 209 | base_url: &str, |
| 210 | conversation_id: &str, |
| 211 | cleared_until_ms: i64, |
| 212 | ) -> Request { |
| 213 | Request::with_body( |
| 214 | Method::Put, |
| 215 | format!( |
| 216 | "{base_url}/{}/properties?name={CLEAR_HISTORY_PROPERTY}", |
| 217 | encode(conversation_id) |
| 218 | ), |
| 219 | json!({ CLEAR_HISTORY_PROPERTY: cleared_until_ms }), |
| 220 | ) |
| 221 | } |
| 222 | |
| 223 | fn history_cleared_ms(conversation: &Value) -> Option<i64> { |
| 224 | millis_property(conversation, CLEAR_HISTORY_PROPERTY) |
| 225 | } |
| 226 | |
| 227 | fn hidden_at_ms(conversation: &Value) -> Option<i64> { |
| 228 | [HIDDEN_TIME_PROPERTY, HISTORY_HIDDEN_TIME_PROPERTY] |
| 229 | .into_iter() |
| 230 | .filter_map(|name| millis_property(conversation, name)) |
| 231 | .max() |
| 232 | } |
| 233 | |
| 234 | fn millis_property(conversation: &Value, name: &str) -> Option<i64> { |
| 235 | let value = &conversation["properties"][name]; |
| 236 | value |
| 237 | .as_i64() |
| 238 | .or_else(|| value.as_str()?.parse().ok()) |
| 239 | .filter(|millis| *millis > 0) |
| 240 | } |
| 241 | |
| 242 | /// One conversation with its thread properties, as Teams reads a meeting or 1:1 thread. |
| 243 | pub fn thread_url(region: &str, thread_id: &str) -> String { |
| 244 | format!( |
| 245 | "https://teams.cloud.microsoft/api/chatsvc/{region}/v1/users/ME/conversations/{}?view=msnp24Equivalent", |
| 246 | thread_id.replace(':', "%3A").replace('@', "%40") |
| 247 | ) |
| 248 | } |
| 249 | |
| 250 | fn backward_link(body: &Value) -> Option<String> { |
| 251 | body.pointer("/_metadata/backwardLink") |
| 252 | .and_then(Value::as_str) |
| 253 | .filter(|link| !link.is_empty()) |
| 254 | .map(str::to_owned) |
| 255 | } |
| 256 | |
| 257 | fn hidden_flag(conversation: &Value) -> Option<bool> { |
| 258 | let flags: Vec<bool> = [ |
| 259 | &conversation["properties"]["hidden"], |
| 260 | &conversation["threadProperties"]["hidden"], |
| 261 | ] |
| 262 | .into_iter() |
| 263 | .filter_map(|value| match value { |
| 264 | Value::Bool(flag) => Some(*flag), |
| 265 | Value::String(text) => text.parse().ok(), |
| 266 | _ => None, |
| 267 | }) |
| 268 | .collect(); |
| 269 | if flags.contains(&true) { |
| 270 | Some(true) |
| 271 | } else { |
| 272 | flags.first().copied() |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | fn states_in_page(body: &Value) -> Result<Vec<ConversationState>> { |
| 277 | let conversations = body |
| 278 | .get("conversations") |
| 279 | .and_then(Value::as_array) |
| 280 | .ok_or_else(|| Error::UnexpectedAnswer("no conversations list".into()))?; |
| 281 | Ok(conversations |
| 282 | .iter() |
| 283 | .filter_map(|conversation| { |
| 284 | let id = conversation["id"].as_str()?; |
| 285 | let muted = conversation["properties"]["alerts"].as_str() == Some(ALERTS_OFF); |
| 286 | Some(ConversationState { |
| 287 | id: id.to_owned(), |
| 288 | muted, |
| 289 | hidden: hidden_flag(conversation), |
| 290 | read: ReadHorizon::from_conversation(conversation), |
| 291 | notification_settings: conversation["properties"] |
| 292 | [chat_notifications::PROPERTY_NAME] |
| 293 | .as_str() |
| 294 | .map(str::to_owned), |
| 295 | history_cleared_ms: history_cleared_ms(conversation), |
| 296 | hidden_at_ms: hidden_at_ms(conversation), |
| 297 | }) |
| 298 | }) |
| 299 | .collect()) |
| 300 | } |
| 301 | |
| 302 | #[cfg(test)] |
| 303 | mod tests { |
| 304 | use std::collections::VecDeque; |
| 305 | use std::sync::Mutex; |
| 306 | |
| 307 | use session::ApiResponse; |
| 308 | |
| 309 | use super::*; |
| 310 | |
| 311 | struct Canned { |
| 312 | answers: Mutex<VecDeque<(u16, Value)>>, |
| 313 | requests: Mutex<Vec<Request>>, |
| 314 | } |
| 315 | |
| 316 | impl MessageTransport for &Canned { |
| 317 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 318 | self.requests.lock().unwrap().push(request); |
| 319 | let (status, body) = self.answers.lock().unwrap().pop_front().expect("exhausted"); |
| 320 | Ok(ApiResponse { |
| 321 | status, |
| 322 | body, |
| 323 | retry_after: None, |
| 324 | }) |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | fn canned(answers: Vec<(u16, Value)>) -> Canned { |
| 329 | Canned { |
| 330 | answers: Mutex::new(answers.into()), |
| 331 | requests: Mutex::new(Vec::new()), |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | fn state(id: &str, muted: bool) -> ConversationState { |
| 336 | ConversationState { |
| 337 | id: id.to_owned(), |
| 338 | muted, |
| 339 | ..Default::default() |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | const BASE: &str = "https://teams.cloud.microsoft/api/chatsvc/emea/v1/users/ME/conversations"; |
| 344 | |
| 345 | #[test] |
| 346 | fn thread_url_escapes_the_thread_id() { |
| 347 | assert_eq!( |
| 348 | thread_url("emea", "19:meeting_a@thread.v2"), |
| 349 | "https://teams.cloud.microsoft/api/chatsvc/emea/v1/users/ME/conversations/19%3Ameeting_a%40thread.v2?view=msnp24Equivalent" |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | #[tokio::test] |
| 354 | async fn mute_states_pair_every_conversation_with_its_flag() { |
| 355 | let transport = canned(vec![( |
| 356 | 200, |
| 357 | json!({ |
| 358 | "conversations": [ |
| 359 | {"id": "19:a", "properties": {"alerts": "false"}}, |
| 360 | {"id": "19:b", "properties": {"alerts": "true"}}, |
| 361 | {"id": "19:c", "properties": {}}, |
| 362 | {"id": "19:d"}, |
| 363 | ], |
| 364 | }), |
| 365 | )]); |
| 366 | let states = Conversations::with_transport(&transport, "emea") |
| 367 | .conversation_states() |
| 368 | .await |
| 369 | .unwrap(); |
| 370 | assert_eq!( |
| 371 | states, |
| 372 | vec![ |
| 373 | state("19:a", true), |
| 374 | state("19:b", false), |
| 375 | state("19:c", false), |
| 376 | state("19:d", false), |
| 377 | ] |
| 378 | ); |
| 379 | let requests = transport.requests.lock().unwrap(); |
| 380 | assert_eq!(requests.len(), 1); |
| 381 | assert_eq!( |
| 382 | requests[0].url, |
| 383 | format!("{BASE}?view=msnp24Equivalent&pageSize=200") |
| 384 | ); |
| 385 | } |
| 386 | |
| 387 | #[tokio::test] |
| 388 | async fn mute_states_follow_the_backward_link_through_every_page() { |
| 389 | let transport = canned(vec![ |
| 390 | ( |
| 391 | 200, |
| 392 | json!({ |
| 393 | "conversations": [{"id": "19:a", "properties": {"alerts": "true"}}], |
| 394 | "_metadata": {"backwardLink": "https://example.test/page2"}, |
| 395 | }), |
| 396 | ), |
| 397 | ( |
| 398 | 200, |
| 399 | json!({ |
| 400 | "conversations": [{"id": "19:old", "properties": {"alerts": "false"}}], |
| 401 | "_metadata": {"backwardLink": "https://example.test/page3"}, |
| 402 | }), |
| 403 | ), |
| 404 | (200, json!({"conversations": [{"id": "19:oldest"}]})), |
| 405 | ]); |
| 406 | let states = Conversations::with_transport(&transport, "emea") |
| 407 | .conversation_states() |
| 408 | .await |
| 409 | .unwrap(); |
| 410 | assert_eq!( |
| 411 | states, |
| 412 | vec![ |
| 413 | state("19:a", false), |
| 414 | state("19:old", true), |
| 415 | state("19:oldest", false), |
| 416 | ] |
| 417 | ); |
| 418 | let requests = transport.requests.lock().unwrap(); |
| 419 | let urls: Vec<&str> = requests |
| 420 | .iter() |
| 421 | .map(|request| request.url.as_str()) |
| 422 | .collect(); |
| 423 | assert_eq!(urls[1], "https://example.test/page2"); |
| 424 | assert_eq!(urls[2], "https://example.test/page3"); |
| 425 | } |
| 426 | |
| 427 | #[tokio::test] |
| 428 | async fn hidden_flag_is_read_from_the_thread_properties_as_text_or_bool() { |
| 429 | let transport = canned(vec![( |
| 430 | 200, |
| 431 | json!({ |
| 432 | "conversations": [ |
| 433 | {"id": "19:a", "threadProperties": {"hidden": "true"}}, |
| 434 | {"id": "19:b", "threadProperties": {"hidden": "false"}}, |
| 435 | {"id": "19:c", "properties": {"hidden": true}}, |
| 436 | {"id": "19:d", "properties": {}}, |
| 437 | {"id": "19:e", "properties": {"hidden": "false"}, "threadProperties": {"hidden": "true"}}, |
| 438 | {"id": "19:f", "properties": {"hidden": "true"}, "threadProperties": {"hidden": "false"}}, |
| 439 | ], |
| 440 | }), |
| 441 | )]); |
| 442 | let states = Conversations::with_transport(&transport, "emea") |
| 443 | .conversation_states() |
| 444 | .await |
| 445 | .unwrap(); |
| 446 | let hidden: Vec<Option<bool>> = states.iter().map(|state| state.hidden).collect(); |
| 447 | assert_eq!( |
| 448 | hidden, |
| 449 | [ |
| 450 | Some(true), |
| 451 | Some(false), |
| 452 | Some(true), |
| 453 | None, |
| 454 | Some(true), |
| 455 | Some(true) |
| 456 | ] |
| 457 | ); |
| 458 | } |
| 459 | |
| 460 | #[tokio::test] |
| 461 | async fn mute_states_fail_on_an_error_status() { |
| 462 | let transport = canned(vec![(500, Value::Null)]); |
| 463 | let outcome = Conversations::with_transport(&transport, "emea") |
| 464 | .conversation_states() |
| 465 | .await; |
| 466 | assert!(outcome.is_err()); |
| 467 | } |
| 468 | |
| 469 | #[tokio::test] |
| 470 | async fn set_alerts_puts_the_flag_as_text() { |
| 471 | let transport = canned(vec![(200, Value::Null), (200, Value::Null)]); |
| 472 | let conversations = Conversations::with_transport(&transport, "emea"); |
| 473 | conversations |
| 474 | .set_alerts("19:a@thread.v2", false) |
| 475 | .await |
| 476 | .unwrap(); |
| 477 | conversations |
| 478 | .set_alerts("19:a@thread.v2", true) |
| 479 | .await |
| 480 | .unwrap(); |
| 481 | let requests = transport.requests.lock().unwrap(); |
| 482 | assert_eq!(requests[0].method, Method::Put); |
| 483 | assert_eq!( |
| 484 | requests[0].url, |
| 485 | format!("{BASE}/19%3Aa%40thread.v2/properties?name=alerts") |
| 486 | ); |
| 487 | assert_eq!(requests[0].body, Some(json!({"alerts": "false"}))); |
| 488 | assert_eq!(requests[1].body, Some(json!({"alerts": "true"}))); |
| 489 | } |
| 490 | |
| 491 | #[tokio::test] |
| 492 | async fn states_carry_the_read_horizon() { |
| 493 | let transport = canned(vec![( |
| 494 | 200, |
| 495 | json!({ |
| 496 | "conversations": [{ |
| 497 | "id": "19:a", |
| 498 | "properties": {"consumptionhorizon": "5;6;7"}, |
| 499 | "lastMessage": {"originalarrivaltime": "1970-01-01T00:00:00.009Z", "from": "x/contacts/8:orgid:b"}, |
| 500 | }], |
| 501 | }), |
| 502 | )]); |
| 503 | let states = Conversations::with_transport(&transport, "emea") |
| 504 | .conversation_states() |
| 505 | .await |
| 506 | .unwrap(); |
| 507 | assert_eq!(states[0].read.consumed_until_ms, 5); |
| 508 | assert_eq!(states[0].read.last_message_ms, 9); |
| 509 | assert!(states[0].read.is_unread("8:orgid:me")); |
| 510 | } |
| 511 | |
| 512 | #[tokio::test] |
| 513 | async fn clear_history_puts_the_time_as_a_number() { |
| 514 | let transport = canned(vec![(200, Value::Null)]); |
| 515 | Conversations::with_transport(&transport, "emea") |
| 516 | .clear_history("19:a_b@unq.gbl.spaces", 1_792_200_000_001) |
| 517 | .await |
| 518 | .unwrap(); |
| 519 | let requests = transport.requests.lock().unwrap(); |
| 520 | assert_eq!(requests[0].method, Method::Put); |
| 521 | assert_eq!( |
| 522 | requests[0].url, |
| 523 | format!("{BASE}/19%3Aa_b%40unq.gbl.spaces/properties?name=clearHistoryTime") |
| 524 | ); |
| 525 | assert_eq!( |
| 526 | requests[0].body, |
| 527 | Some(json!({"clearHistoryTime": 1_792_200_000_001_i64})) |
| 528 | ); |
| 529 | } |
| 530 | |
| 531 | #[tokio::test] |
| 532 | async fn states_carry_the_clear_history_time_as_text_or_number() { |
| 533 | let transport = canned(vec![( |
| 534 | 200, |
| 535 | json!({ |
| 536 | "conversations": [ |
| 537 | {"id": "19:a", "properties": {"clearHistoryTime": "1792200000001"}}, |
| 538 | {"id": "19:b", "properties": {"clearHistoryTime": 1_792_200_000_002_i64}}, |
| 539 | {"id": "19:c", "properties": {"clearHistoryTime": "0"}}, |
| 540 | {"id": "19:d"}, |
| 541 | ], |
| 542 | }), |
| 543 | )]); |
| 544 | let states = Conversations::with_transport(&transport, "emea") |
| 545 | .conversation_states() |
| 546 | .await |
| 547 | .unwrap(); |
| 548 | let cleared: Vec<Option<i64>> = states |
| 549 | .iter() |
| 550 | .map(|state| state.history_cleared_ms) |
| 551 | .collect(); |
| 552 | assert_eq!( |
| 553 | cleared, |
| 554 | [Some(1_792_200_000_001), Some(1_792_200_000_002), None, None] |
| 555 | ); |
| 556 | } |
| 557 | |
| 558 | #[tokio::test] |
| 559 | async fn set_read_horizon_puts_the_consumption_horizon_property() { |
| 560 | let transport = canned(vec![(200, Value::Null)]); |
| 561 | Conversations::with_transport(&transport, "emea") |
| 562 | .set_read_horizon("19:a@thread.skype", "9;10;0") |
| 563 | .await |
| 564 | .unwrap(); |
| 565 | let requests = transport.requests.lock().unwrap(); |
| 566 | assert_eq!(requests[0].method, Method::Put); |
| 567 | assert_eq!( |
| 568 | requests[0].url, |
| 569 | format!("{BASE}/19%3Aa%40thread.skype/properties?name=consumptionhorizon") |
| 570 | ); |
| 571 | assert_eq!( |
| 572 | requests[0].body, |
| 573 | Some(json!({"consumptionhorizon": "9;10;0"})) |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | #[tokio::test] |
| 578 | async fn set_read_bookmark_puts_the_bookmark_property() { |
| 579 | let transport = canned(vec![(200, Value::Null)]); |
| 580 | Conversations::with_transport(&transport, "emea") |
| 581 | .set_read_bookmark("48:notes", "8;10;9") |
| 582 | .await |
| 583 | .unwrap(); |
| 584 | let requests = transport.requests.lock().unwrap(); |
| 585 | assert_eq!(requests[0].method, Method::Put); |
| 586 | assert_eq!( |
| 587 | requests[0].url, |
| 588 | format!("{BASE}/48%3Anotes/properties?name=consumptionHorizonBookmark") |
| 589 | ); |
| 590 | assert_eq!( |
| 591 | requests[0].body, |
| 592 | Some(json!({"consumptionHorizonBookmark": "8;10;9"})) |
| 593 | ); |
| 594 | } |
| 595 | |
| 596 | #[tokio::test] |
| 597 | async fn hide_puts_the_hidden_time_as_number_and_the_history_time_as_text() { |
| 598 | let transport = canned(vec![(200, Value::Null); 4]); |
| 599 | let conversations = Conversations::with_transport(&transport, "emea"); |
| 600 | conversations |
| 601 | .set_hidden_at("48:notes", Some(42)) |
| 602 | .await |
| 603 | .unwrap(); |
| 604 | conversations.set_hidden_at("48:notes", None).await.unwrap(); |
| 605 | let requests = transport.requests.lock().unwrap(); |
| 606 | let writes: Vec<(String, Option<Value>)> = requests |
| 607 | .iter() |
| 608 | .map(|request| (request.url.replace(BASE, ""), request.body.clone())) |
| 609 | .collect(); |
| 610 | assert_eq!( |
| 611 | writes, |
| 612 | [ |
| 613 | ( |
| 614 | "/48%3Anotes/properties?name=unpinnedTime".to_owned(), |
| 615 | Some(json!({"unpinnedTime": 42})) |
| 616 | ), |
| 617 | ( |
| 618 | "/48%3Anotes/properties?name=historyHiddenTime".to_owned(), |
| 619 | Some(json!({"historyHiddenTime": "42"})) |
| 620 | ), |
| 621 | ( |
| 622 | "/48%3Anotes/properties?name=unpinnedTime".to_owned(), |
| 623 | Some(json!({"unpinnedTime": null})) |
| 624 | ), |
| 625 | ( |
| 626 | "/48%3Anotes/properties?name=historyHiddenTime".to_owned(), |
| 627 | Some(json!({"historyHiddenTime": null})) |
| 628 | ), |
| 629 | ] |
| 630 | ); |
| 631 | } |
| 632 | |
| 633 | #[tokio::test] |
| 634 | async fn states_carry_the_newest_hidden_time() { |
| 635 | let transport = canned(vec![( |
| 636 | 200, |
| 637 | json!({"conversations": [ |
| 638 | {"id": "48:notes", "properties": {"unpinnedTime": 30, "historyHiddenTime": "40"}}, |
| 639 | {"id": "19:b", "properties": {"unpinnedTime": "50"}}, |
| 640 | {"id": "19:c", "properties": {}}, |
| 641 | ]}), |
| 642 | )]); |
| 643 | let states = Conversations::with_transport(&transport, "emea") |
| 644 | .conversation_states() |
| 645 | .await |
| 646 | .unwrap(); |
| 647 | let hidden: Vec<Option<i64>> = states.iter().map(|state| state.hidden_at_ms).collect(); |
| 648 | assert_eq!(hidden, [Some(40), Some(50), None]); |
| 649 | } |
| 650 | |
| 651 | #[tokio::test] |
| 652 | async fn chat_notifications_put_the_settings_then_alerts() { |
| 653 | let transport = canned(vec![(200, Value::Null), (200, Value::Null)]); |
| 654 | let write = Conversations::with_transport(&transport, "emea") |
| 655 | .set_chat_notifications( |
| 656 | "19:a@thread.v2", |
| 657 | Some(r#"{"everyone":"On"}"#), |
| 658 | ChatNotificationMode::Mute, |
| 659 | DesktopNotifications::MatchAllChats, |
| 660 | ) |
| 661 | .await |
| 662 | .unwrap(); |
| 663 | assert!(!write.alerts); |
| 664 | let requests = transport.requests.lock().unwrap(); |
| 665 | assert_eq!( |
| 666 | requests[0].url, |
| 667 | format!("{BASE}/19%3Aa%40thread.v2/properties?name=conversationNotificationSettings") |
| 668 | ); |
| 669 | let sent = requests[0].body.as_ref().unwrap()["conversationNotificationSettings"] |
| 670 | .as_str() |
| 671 | .unwrap(); |
| 672 | assert_eq!( |
| 673 | serde_json::from_str::<Value>(sent).unwrap(), |
| 674 | json!({"everyone": "Off", "dskNotif": null, "allNewPosts": "Off"}) |
| 675 | ); |
| 676 | assert_eq!( |
| 677 | requests[1].url, |
| 678 | format!("{BASE}/19%3Aa%40thread.v2/properties?name=alerts") |
| 679 | ); |
| 680 | assert_eq!(requests[1].body, Some(json!({"alerts": "false"}))); |
| 681 | } |
| 682 | |
| 683 | #[tokio::test] |
| 684 | async fn states_carry_the_chat_notification_settings() { |
| 685 | let transport = canned(vec![( |
| 686 | 200, |
| 687 | json!({"conversations": [{"id": "19:a", "properties": {"conversationNotificationSettings": "{\"dskNotif\":\"Off\"}"}}]}), |
| 688 | )]); |
| 689 | let states = Conversations::with_transport(&transport, "emea") |
| 690 | .conversation_states() |
| 691 | .await |
| 692 | .unwrap(); |
| 693 | assert_eq!(states[0].desktop_notifications(), DesktopNotifications::Off); |
| 694 | } |
| 695 | } |