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