| 1 | use chrono::{DateTime, Utc}; |
| 2 | use serde_json::Value; |
| 3 | |
| 4 | pub const PROPERTY_NAME: &str = "consumptionhorizon"; |
| 5 | pub const BOOKMARK_PROPERTY_NAME: &str = "consumptionHorizonBookmark"; |
| 6 | |
| 7 | /// Read state of one conversation as chatsvc lists it, judged like the Teams worker (`hasUnreadMessages`). |
| 8 | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
| 9 | pub struct ReadHorizon { |
| 10 | pub consumed_until_ms: i64, |
| 11 | pub bookmark_ms: i64, |
| 12 | pub bookmark_timestamp_ms: i64, |
| 13 | pub bookmark_message_set: bool, |
| 14 | pub last_message_ms: i64, |
| 15 | pub last_message_from: Option<String>, |
| 16 | } |
| 17 | |
| 18 | impl ReadHorizon { |
| 19 | pub fn from_conversation(conversation: &Value) -> Self { |
| 20 | let properties = &conversation["properties"]; |
| 21 | let horizon = parse_horizon(properties["consumptionhorizon"].as_str()); |
| 22 | let bookmark = parse_horizon(properties["consumptionHorizonBookmark"].as_str()); |
| 23 | let last_message = &conversation["lastMessage"]; |
| 24 | ReadHorizon { |
| 25 | consumed_until_ms: horizon.arrival_ms, |
| 26 | bookmark_ms: bookmark.arrival_ms, |
| 27 | bookmark_timestamp_ms: bookmark.timestamp_ms, |
| 28 | bookmark_message_set: bookmark.message_set, |
| 29 | last_message_ms: last_message_ms(conversation), |
| 30 | last_message_from: last_message["from"] |
| 31 | .as_str() |
| 32 | .and_then(|from| from.rsplit('/').next()) |
| 33 | .filter(|from| !from.is_empty()) |
| 34 | .map(str::to_owned), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn read_until_ms(&self, app_horizon_ms: i64) -> i64 { |
| 39 | self.consumed_until_ms.max(app_horizon_ms) |
| 40 | } |
| 41 | |
| 42 | pub fn is_unread(&self, my_user_id: &str) -> bool { |
| 43 | self.is_unread_after(my_user_id, 0) |
| 44 | } |
| 45 | |
| 46 | /// `app_horizon_ms` is the `appConsumptionHorizon` entry of the conversation's app ("Mark all as read"). |
| 47 | pub fn is_unread_after(&self, my_user_id: &str, app_horizon_ms: i64) -> bool { |
| 48 | let app_horizon_set = app_horizon_ms > 0; |
| 49 | let bookmark_cleared = app_horizon_set && app_horizon_ms >= self.bookmark_timestamp_ms; |
| 50 | let bookmark_ms = if bookmark_cleared { |
| 51 | 0 |
| 52 | } else { |
| 53 | self.bookmark_ms |
| 54 | }; |
| 55 | if bookmark_ms != 0 && self.bookmark_message_set { |
| 56 | return true; |
| 57 | } |
| 58 | let consumed_until_ms = self.read_until_ms(app_horizon_ms); |
| 59 | let unread = bookmark_ms != 0 || self.last_message_ms > consumed_until_ms; |
| 60 | let from_me = self |
| 61 | .last_message_from |
| 62 | .as_deref() |
| 63 | .is_some_and(|from| !my_user_id.is_empty() && from.contains(my_user_id)); |
| 64 | unread && !from_me |
| 65 | } |
| 66 | |
| 67 | pub fn last_message_at(&self) -> Option<DateTime<Utc>> { |
| 68 | (self.last_message_ms > 0) |
| 69 | .then(|| DateTime::from_timestamp_millis(self.last_message_ms)) |
| 70 | .flatten() |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// The value Teams writes when a conversation is read: last arrival time, now, client message id. |
| 75 | pub fn read_up_to(last_message_at: DateTime<Utc>, now: DateTime<Utc>) -> String { |
| 76 | format!( |
| 77 | "{};{};0", |
| 78 | last_message_at.timestamp_millis(), |
| 79 | now.timestamp_millis() |
| 80 | ) |
| 81 | } |
| 82 | |
| 83 | /// Teams' unread bookmark on a message: one millisecond before its arrival, now, its id. |
| 84 | pub fn bookmark_at( |
| 85 | message_arrived_at: DateTime<Utc>, |
| 86 | message_id: &str, |
| 87 | now: DateTime<Utc>, |
| 88 | ) -> String { |
| 89 | format!( |
| 90 | "{};{};{message_id}", |
| 91 | message_arrived_at.timestamp_millis() - 1, |
| 92 | now.timestamp_millis() |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | /// The value Teams writes to clear the bookmark when a conversation is read. |
| 97 | pub fn cleared_bookmark(now: DateTime<Utc>) -> String { |
| 98 | format!("0;{};0", now.timestamp_millis()) |
| 99 | } |
| 100 | |
| 101 | struct ParsedHorizon { |
| 102 | arrival_ms: i64, |
| 103 | timestamp_ms: i64, |
| 104 | message_set: bool, |
| 105 | } |
| 106 | |
| 107 | fn parse_horizon(text: Option<&str>) -> ParsedHorizon { |
| 108 | let parts: Vec<&str> = text.unwrap_or_default().split(';').collect(); |
| 109 | let number = |part: &str| part.trim().parse::<i64>().ok(); |
| 110 | let unset = ParsedHorizon { |
| 111 | arrival_ms: 0, |
| 112 | timestamp_ms: 0, |
| 113 | message_set: false, |
| 114 | }; |
| 115 | let [arrival, timestamp, message_id] = parts.as_slice() else { |
| 116 | return unset; |
| 117 | }; |
| 118 | let message_id = message_id.trim(); |
| 119 | match (number(arrival), number(timestamp)) { |
| 120 | (Some(arrival_ms), Some(timestamp_ms)) if !message_id.is_empty() => ParsedHorizon { |
| 121 | arrival_ms, |
| 122 | timestamp_ms, |
| 123 | message_set: message_id != "0", |
| 124 | }, |
| 125 | _ => unset, |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | fn last_message_ms(conversation: &Value) -> i64 { |
| 130 | if let Some(time) = conversation["lastMessageTimeUtc"].as_i64() { |
| 131 | return time; |
| 132 | } |
| 133 | conversation["lastMessage"]["originalarrivaltime"] |
| 134 | .as_str() |
| 135 | .and_then(|text| DateTime::parse_from_rfc3339(text).ok()) |
| 136 | .map_or(0, |time| time.timestamp_millis()) |
| 137 | } |
| 138 | |
| 139 | #[cfg(test)] |
| 140 | mod tests { |
| 141 | use serde_json::json; |
| 142 | |
| 143 | use super::*; |
| 144 | |
| 145 | const ME: &str = "8:orgid:me"; |
| 146 | |
| 147 | fn conversation( |
| 148 | horizon: Option<&str>, |
| 149 | bookmark: Option<&str>, |
| 150 | last: Option<(&str, &str)>, |
| 151 | ) -> Value { |
| 152 | json!({ |
| 153 | "id": "19:channel@thread.skype", |
| 154 | "properties": { |
| 155 | "consumptionhorizon": horizon, |
| 156 | "consumptionHorizonBookmark": bookmark, |
| 157 | }, |
| 158 | "lastMessage": last.map(|(arrival, from)| json!({ |
| 159 | "originalarrivaltime": arrival, |
| 160 | "from": format!("https://teams.cloud.microsoft/api/chatsvc/emea/v1/users/ME/contacts/{from}"), |
| 161 | })).unwrap_or_else(|| json!({})), |
| 162 | }) |
| 163 | } |
| 164 | |
| 165 | fn unread(value: &Value) -> bool { |
| 166 | ReadHorizon::from_conversation(value).is_unread(ME) |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn message_after_the_horizon_is_unread() { |
| 171 | let value = conversation( |
| 172 | Some("1791884440747;1791885752824;1131155174560809354"), |
| 173 | None, |
| 174 | Some(("2026-10-16T09:42:25.6780000Z", "8:orgid:other")), |
| 175 | ); |
| 176 | assert!(unread(&value)); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn message_at_or_before_the_horizon_is_read() { |
| 181 | let value = conversation( |
| 182 | Some("1792171970947;1792171971513;1792171970879"), |
| 183 | Some(""), |
| 184 | Some(("2026-10-16T17:32:50.9470000Z", "8:orgid:other")), |
| 185 | ); |
| 186 | assert!(!unread(&value)); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn missing_horizon_counts_every_message_as_unread() { |
| 191 | let value = conversation(None, None, Some(("2026-10-16T20:30:58.901Z", "28:bot"))); |
| 192 | assert!(unread(&value)); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn channel_without_messages_is_read() { |
| 197 | assert!(!unread(&conversation(None, None, None))); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn own_last_message_is_read() { |
| 202 | let value = conversation(Some("1;2;3"), None, Some(("2026-10-16T09:42:25.678Z", ME))); |
| 203 | assert!(!unread(&value)); |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | fn bookmark_with_a_message_marks_unread_even_for_own_message() { |
| 208 | let value = conversation( |
| 209 | Some("1792171970947;1792171971513;1"), |
| 210 | Some("1792171970000;1792171971513;42"), |
| 211 | Some(("2026-10-16T17:32:50.9470000Z", ME)), |
| 212 | ); |
| 213 | assert!(unread(&value)); |
| 214 | } |
| 215 | |
| 216 | #[test] |
| 217 | fn empty_bookmark_from_teams_is_ignored() { |
| 218 | let value = conversation( |
| 219 | Some("1792171970947;1792171971513;1"), |
| 220 | Some("0;1697436994849;0"), |
| 221 | Some(("2026-10-16T17:32:50.9470000Z", "8:orgid:other")), |
| 222 | ); |
| 223 | assert!(!unread(&value)); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn malformed_horizon_counts_as_none() { |
| 228 | let value = conversation( |
| 229 | Some("garbage"), |
| 230 | None, |
| 231 | Some(("2026-10-16T09:42:25.678Z", "8:orgid:other")), |
| 232 | ); |
| 233 | assert!(unread(&value)); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn app_horizon_reads_everything_that_arrived_before_it() { |
| 238 | let value = conversation( |
| 239 | Some("1791884440747;1791885752824;1"), |
| 240 | None, |
| 241 | Some(("2026-10-16T09:42:25.6780000Z", "8:orgid:other")), |
| 242 | ); |
| 243 | let horizon = ReadHorizon::from_conversation(&value); |
| 244 | assert!(horizon.is_unread_after(ME, 0)); |
| 245 | assert!(horizon.is_unread_after(ME, horizon.last_message_ms - 1)); |
| 246 | assert!(!horizon.is_unread_after(ME, horizon.last_message_ms)); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn app_horizon_clears_an_older_bookmark_only() { |
| 251 | let value = conversation( |
| 252 | Some("1792171970947;1792171971513;1"), |
| 253 | Some("1792171970000;1792171971513;42"), |
| 254 | Some(("2026-10-16T17:32:50.9470000Z", "8:orgid:other")), |
| 255 | ); |
| 256 | let horizon = ReadHorizon::from_conversation(&value); |
| 257 | assert!(horizon.is_unread_after(ME, 1792171971512)); |
| 258 | assert!(!horizon.is_unread_after(ME, 1792171971513)); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn read_value_carries_the_last_arrival_and_now() { |
| 263 | let last = DateTime::from_timestamp_millis(1792182658901).unwrap(); |
| 264 | let now = DateTime::from_timestamp_millis(1792190000000).unwrap(); |
| 265 | assert_eq!(read_up_to(last, now), "1792182658901;1792190000000;0"); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn own_note_marked_unread_by_bookmark_is_unread_until_the_bookmark_is_cleared() { |
| 270 | let arrived = DateTime::from_timestamp_millis(1792182658901).unwrap(); |
| 271 | let now = DateTime::from_timestamp_millis(1792190000000).unwrap(); |
| 272 | let bookmark = bookmark_at(arrived, "1792182658901", now); |
| 273 | assert_eq!(bookmark, "1792182658900;1792190000000;1792182658901"); |
| 274 | let last = Some(("2026-10-16T21:10:58.901Z", ME)); |
| 275 | let marked = conversation(Some("1792182658901;1;0"), Some(&bookmark), last); |
| 276 | assert!(unread(&marked)); |
| 277 | let cleared = cleared_bookmark(now); |
| 278 | assert_eq!(cleared, "0;1792190000000;0"); |
| 279 | assert!(!unread(&conversation( |
| 280 | Some("1792182658901;1;0"), |
| 281 | Some(&cleared), |
| 282 | last |
| 283 | ))); |
| 284 | } |
| 285 | } |