| 1 | use chrono::{DateTime, SecondsFormat, Utc}; |
| 2 | use percent_encoding::{AsciiSet, NON_ALPHANUMERIC, utf8_percent_encode}; |
| 3 | use session::GRAPH; |
| 4 | |
| 5 | const ENCODE: &AsciiSet = &NON_ALPHANUMERIC |
| 6 | .remove(b'-') |
| 7 | .remove(b'_') |
| 8 | .remove(b'.') |
| 9 | .remove(b'~'); |
| 10 | |
| 11 | pub fn segment(value: &str) -> String { |
| 12 | utf8_percent_encode(value, ENCODE).to_string() |
| 13 | } |
| 14 | |
| 15 | pub fn me() -> String { |
| 16 | format!("{GRAPH}/v1.0/me?$select=id,displayName,mail,userPrincipalName") |
| 17 | } |
| 18 | |
| 19 | pub fn user_photo(user_id: &str) -> String { |
| 20 | format!( |
| 21 | "{GRAPH}/v1.0/users/{}/photos/48x48/$value", |
| 22 | segment(user_id) |
| 23 | ) |
| 24 | } |
| 25 | |
| 26 | pub fn chats(top: usize) -> String { |
| 27 | format!( |
| 28 | "{GRAPH}/v1.0/me/chats?$expand=members,lastMessagePreview\ |
| 29 | &$orderby=lastMessagePreview/createdDateTime%20desc&$top={top}" |
| 30 | ) |
| 31 | } |
| 32 | |
| 33 | pub fn chat(chat_id: &str) -> String { |
| 34 | format!( |
| 35 | "{GRAPH}/v1.0/chats/{}?$expand=members,lastMessagePreview", |
| 36 | segment(chat_id) |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | pub fn chat_members(chat_id: &str) -> String { |
| 41 | format!("{GRAPH}/v1.0/chats/{}/members", segment(chat_id)) |
| 42 | } |
| 43 | |
| 44 | pub fn chat_message(chat_id: &str, message_id: &str) -> String { |
| 45 | format!( |
| 46 | "{GRAPH}/v1.0/chats/{}/messages/{}", |
| 47 | segment(chat_id), |
| 48 | segment(message_id) |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | pub fn soft_delete(user_id: &str, chat_id: &str, message_id: &str) -> String { |
| 53 | format!( |
| 54 | "{GRAPH}/v1.0/users/{}/chats/{}/messages/{}/softDelete", |
| 55 | segment(user_id), |
| 56 | segment(chat_id), |
| 57 | segment(message_id) |
| 58 | ) |
| 59 | } |
| 60 | |
| 61 | pub fn create_chat() -> String { |
| 62 | format!("{GRAPH}/v1.0/chats") |
| 63 | } |
| 64 | |
| 65 | pub fn chat_messages(chat_id: &str, before: Option<DateTime<Utc>>, top: usize) -> String { |
| 66 | let mut url = format!( |
| 67 | "{GRAPH}/v1.0/chats/{}/messages?$top={top}&$orderby=createdDateTime%20desc", |
| 68 | segment(chat_id) |
| 69 | ); |
| 70 | if let Some(before) = before { |
| 71 | let stamp = before.to_rfc3339_opts(SecondsFormat::Millis, true); |
| 72 | url.push_str(&format!( |
| 73 | "&$filter=createdDateTime%20lt%20{}", |
| 74 | segment(&stamp) |
| 75 | )); |
| 76 | } |
| 77 | url |
| 78 | } |
| 79 | |
| 80 | pub fn chat_message_collection(chat_id: &str) -> String { |
| 81 | format!("{GRAPH}/v1.0/chats/{}/messages", segment(chat_id)) |
| 82 | } |
| 83 | |
| 84 | pub fn reply_with_quote(chat_id: &str) -> String { |
| 85 | format!( |
| 86 | "{GRAPH}/beta/chats/{}/messages/replyWithQuote", |
| 87 | segment(chat_id) |
| 88 | ) |
| 89 | } |
| 90 | |
| 91 | pub fn mark_chat_read(chat_id: &str) -> String { |
| 92 | format!( |
| 93 | "{GRAPH}/v1.0/chats/{}/markChatReadForUser", |
| 94 | segment(chat_id) |
| 95 | ) |
| 96 | } |
| 97 | |
| 98 | pub fn joined_teams() -> String { |
| 99 | format!("{GRAPH}/v1.0/me/joinedTeams?$select=id,displayName") |
| 100 | } |
| 101 | |
| 102 | pub fn channels(team_id: &str) -> String { |
| 103 | format!( |
| 104 | "{GRAPH}/v1.0/teams/{}/channels?$select=id,displayName,membershipType", |
| 105 | segment(team_id) |
| 106 | ) |
| 107 | } |
| 108 | |
| 109 | pub fn channel_messages(team_id: &str, channel_id: &str, top: usize) -> String { |
| 110 | format!( |
| 111 | "{GRAPH}/v1.0/teams/{}/channels/{}/messages?$top={top}&$expand=replies", |
| 112 | segment(team_id), |
| 113 | segment(channel_id) |
| 114 | ) |
| 115 | } |
| 116 | |
| 117 | pub fn channel_post(team_id: &str, channel_id: &str) -> String { |
| 118 | format!( |
| 119 | "{GRAPH}/v1.0/teams/{}/channels/{}/messages", |
| 120 | segment(team_id), |
| 121 | segment(channel_id) |
| 122 | ) |
| 123 | } |
| 124 | |
| 125 | pub fn channel_message(team_id: &str, channel_id: &str, message_id: &str) -> String { |
| 126 | format!( |
| 127 | "{}/{}", |
| 128 | channel_post(team_id, channel_id), |
| 129 | segment(message_id) |
| 130 | ) |
| 131 | } |
| 132 | |
| 133 | pub fn channel_reply_post(team_id: &str, channel_id: &str, message_id: &str) -> String { |
| 134 | format!( |
| 135 | "{}/replies", |
| 136 | channel_message(team_id, channel_id, message_id) |
| 137 | ) |
| 138 | } |
| 139 | |
| 140 | pub fn channel_reply(team_id: &str, channel_id: &str, message_id: &str, reply_id: &str) -> String { |
| 141 | format!( |
| 142 | "{}/{}", |
| 143 | channel_reply_post(team_id, channel_id, message_id), |
| 144 | segment(reply_id) |
| 145 | ) |
| 146 | } |
| 147 | |
| 148 | pub fn channel_replies(team_id: &str, channel_id: &str, message_id: &str, top: usize) -> String { |
| 149 | format!( |
| 150 | "{}?$top={top}", |
| 151 | channel_reply_post(team_id, channel_id, message_id) |
| 152 | ) |
| 153 | } |
| 154 | |
| 155 | pub fn channel_delta( |
| 156 | team_id: &str, |
| 157 | channel_id: &str, |
| 158 | modified_after: Option<DateTime<Utc>>, |
| 159 | ) -> String { |
| 160 | let mut url = format!("{}/delta", channel_post(team_id, channel_id)); |
| 161 | if let Some(after) = modified_after { |
| 162 | let stamp = after.to_rfc3339_opts(SecondsFormat::Millis, true); |
| 163 | url.push_str(&format!( |
| 164 | "?$filter=lastModifiedDateTime%20gt%20{}", |
| 165 | segment(&stamp) |
| 166 | )); |
| 167 | } |
| 168 | url |
| 169 | } |
| 170 | |
| 171 | pub fn people_search(query: &str, top: usize) -> String { |
| 172 | let term = query.replace('"', ""); |
| 173 | let search = segment(&format!("\"displayName:{term}\" OR \"mail:{term}\"")); |
| 174 | format!( |
| 175 | "{GRAPH}/v1.0/users?$search={search}&$select=id,displayName,mail,userPrincipalName,jobTitle,department&$top={top}" |
| 176 | ) |
| 177 | } |
| 178 | |
| 179 | pub fn user_by_address(address: &str) -> String { |
| 180 | let escaped = address.replace('\'', "''"); |
| 181 | let filter = segment(&format!( |
| 182 | "mail eq '{escaped}' or userPrincipalName eq '{escaped}'" |
| 183 | )); |
| 184 | format!( |
| 185 | "{GRAPH}/v1.0/users?$filter={filter}&$select=id,displayName,mail,userPrincipalName,jobTitle,department" |
| 186 | ) |
| 187 | } |
| 188 | |
| 189 | #[cfg(test)] |
| 190 | mod tests { |
| 191 | use chrono::TimeZone; |
| 192 | |
| 193 | use super::*; |
| 194 | |
| 195 | #[test] |
| 196 | fn chat_ids_are_path_encoded() { |
| 197 | assert_eq!(segment("19:abc@thread.v2"), "19%3Aabc%40thread.v2"); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn reply_with_quote_uses_the_beta_endpoint() { |
| 202 | assert_eq!( |
| 203 | reply_with_quote("19:abc@thread.v2"), |
| 204 | "https://graph.microsoft.com/beta/chats/19%3Aabc%40thread.v2/messages/replyWithQuote" |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn chats_url_orders_newest_first() { |
| 210 | let url = chats(50); |
| 211 | assert!(url.starts_with("https://graph.microsoft.com/v1.0/me/chats?")); |
| 212 | assert!(url.contains("$expand=members,lastMessagePreview")); |
| 213 | assert!(url.contains("$orderby=lastMessagePreview/createdDateTime%20desc")); |
| 214 | assert!(url.ends_with("$top=50")); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn messages_url_without_cursor() { |
| 219 | let url = chat_messages("19:abc@thread.v2", None, 30); |
| 220 | assert_eq!( |
| 221 | url, |
| 222 | "https://graph.microsoft.com/v1.0/chats/19%3Aabc%40thread.v2/messages?$top=30&$orderby=createdDateTime%20desc" |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn messages_url_with_cursor_filters_older() { |
| 228 | let before = Utc.with_ymd_and_hms(2026, 10, 6, 9, 0, 0).unwrap(); |
| 229 | let url = chat_messages("c", Some(before), 50); |
| 230 | assert!( |
| 231 | url.ends_with("&$filter=createdDateTime%20lt%202026-10-06T09%3A00%3A00.000Z"), |
| 232 | "{url}" |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn channel_messages_expand_replies() { |
| 238 | let url = channel_messages("team-1", "19:chan@thread.tacv2", 20); |
| 239 | assert!(url.contains("/teams/team-1/channels/19%3Achan%40thread.tacv2/messages?")); |
| 240 | assert!(url.ends_with("$top=20&$expand=replies")); |
| 241 | } |
| 242 | } |