| 1 | use serde_json::{Value, json}; |
| 2 | |
| 3 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 4 | pub enum MentionTarget { |
| 5 | User { user_id: String }, |
| 6 | Channel { channel_id: String }, |
| 7 | Team { team_id: String }, |
| 8 | } |
| 9 | |
| 10 | /// `id` matches the `<at id="n">` tag in the html body. |
| 11 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | pub struct OutgoingMention { |
| 13 | pub id: u32, |
| 14 | pub text: String, |
| 15 | pub target: MentionTarget, |
| 16 | } |
| 17 | |
| 18 | impl OutgoingMention { |
| 19 | pub fn to_json(&self) -> Value { |
| 20 | let mentioned = match &self.target { |
| 21 | MentionTarget::User { user_id } => json!({ |
| 22 | "user": {"id": user_id, "displayName": self.text, "userIdentityType": "aadUser"} |
| 23 | }), |
| 24 | MentionTarget::Channel { channel_id } => json!({ |
| 25 | "conversation": {"id": channel_id, "displayName": self.text, "conversationIdentityType": "channel"} |
| 26 | }), |
| 27 | MentionTarget::Team { team_id } => json!({ |
| 28 | "conversation": {"id": team_id, "displayName": self.text, "conversationIdentityType": "team"} |
| 29 | }), |
| 30 | }; |
| 31 | json!({"id": self.id, "mentionText": self.text, "mentioned": mentioned}) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub(crate) fn message_body(html: &str, mentions: &[OutgoingMention]) -> Value { |
| 36 | let mut body = json!({"body": {"contentType": "html", "content": html}}); |
| 37 | if !mentions.is_empty() { |
| 38 | body["mentions"] = Value::Array(mentions.iter().map(OutgoingMention::to_json).collect()); |
| 39 | } |
| 40 | body |
| 41 | } |
| 42 | |
| 43 | #[cfg(test)] |
| 44 | mod tests { |
| 45 | use super::*; |
| 46 | |
| 47 | fn mention(id: u32, text: &str, target: MentionTarget) -> OutgoingMention { |
| 48 | OutgoingMention { |
| 49 | id, |
| 50 | text: text.to_owned(), |
| 51 | target, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn user_mention_matches_the_documented_payload() { |
| 57 | let value = mention( |
| 58 | 0, |
| 59 | "Ada", |
| 60 | MentionTarget::User { |
| 61 | user_id: "u1".into(), |
| 62 | }, |
| 63 | ) |
| 64 | .to_json(); |
| 65 | assert_eq!( |
| 66 | value, |
| 67 | json!({"id": 0, "mentionText": "Ada", "mentioned": {"user": {"id": "u1", "displayName": "Ada", "userIdentityType": "aadUser"}}}) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn channel_and_team_mentions_use_conversation_identity() { |
| 73 | let channel = mention( |
| 74 | 1, |
| 75 | "General", |
| 76 | MentionTarget::Channel { |
| 77 | channel_id: "19:c".into(), |
| 78 | }, |
| 79 | ) |
| 80 | .to_json(); |
| 81 | let team = mention( |
| 82 | 2, |
| 83 | "Squad", |
| 84 | MentionTarget::Team { |
| 85 | team_id: "t".into(), |
| 86 | }, |
| 87 | ) |
| 88 | .to_json(); |
| 89 | assert_eq!( |
| 90 | channel["mentioned"]["conversation"]["conversationIdentityType"], |
| 91 | "channel" |
| 92 | ); |
| 93 | assert_eq!( |
| 94 | team["mentioned"]["conversation"]["conversationIdentityType"], |
| 95 | "team" |
| 96 | ); |
| 97 | assert_eq!(team["mentioned"]["conversation"]["id"], "t"); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn body_without_mentions_has_no_mentions_key() { |
| 102 | assert!(message_body("<p>x</p>", &[]).get("mentions").is_none()); |
| 103 | let with = message_body( |
| 104 | "<at id=\"0\">A</at>", |
| 105 | &[mention( |
| 106 | 0, |
| 107 | "A", |
| 108 | MentionTarget::User { |
| 109 | user_id: "u".into(), |
| 110 | }, |
| 111 | )], |
| 112 | ); |
| 113 | assert_eq!(with["mentions"].as_array().unwrap().len(), 1); |
| 114 | } |
| 115 | } |