| 1 | use serde_json::{Value, json}; |
| 2 | |
| 3 | use crate::stored::{AttachmentInfo, ImageRef}; |
| 4 | |
| 5 | pub const ANNOUNCEMENT_BANNER_TYPE: &str = |
| 6 | "application/vnd.microsoft.teams.messaging-announcementBanner"; |
| 7 | pub const ANNOUNCEMENT_APP_ID: &str = "announcement-card"; |
| 8 | const DEFAULT_BANNER_COLOR: &str = "periwinkleBlue"; |
| 9 | const COLOR_THEME: &str = "colorTheme"; |
| 10 | |
| 11 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | pub enum BannerBackground { |
| 13 | Color(String), |
| 14 | Image(String), |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | pub struct AnnouncementBanner { |
| 19 | pub title: String, |
| 20 | pub background: BannerBackground, |
| 21 | } |
| 22 | |
| 23 | impl AnnouncementBanner { |
| 24 | pub fn from_attachments(attachments: &[AttachmentInfo]) -> Option<AnnouncementBanner> { |
| 25 | attachments |
| 26 | .iter() |
| 27 | .filter(|attachment| { |
| 28 | attachment.content_type.as_deref() == Some(ANNOUNCEMENT_BANNER_TYPE) |
| 29 | }) |
| 30 | .find_map(|attachment| AnnouncementBanner::parse(attachment.content.as_deref()?)) |
| 31 | } |
| 32 | |
| 33 | pub fn parse(content: &str) -> Option<AnnouncementBanner> { |
| 34 | let content: Value = serde_json::from_str(content).ok()?; |
| 35 | let title = content.get("title")?.as_str()?.trim().to_owned(); |
| 36 | let details = content.get("cardImageDetails"); |
| 37 | let image = details |
| 38 | .filter(|_| content.get("cardImageType").and_then(Value::as_str) != Some(COLOR_THEME)) |
| 39 | .and_then(|details| { |
| 40 | image_source(details, "croppedImage") |
| 41 | .or_else(|| image_source(details, "originalImage")) |
| 42 | }); |
| 43 | let background = match image { |
| 44 | Some(url) => BannerBackground::Image(url), |
| 45 | None => BannerBackground::Color( |
| 46 | details |
| 47 | .and_then(|details| details.get(COLOR_THEME)) |
| 48 | .and_then(Value::as_str) |
| 49 | .unwrap_or(DEFAULT_BANNER_COLOR) |
| 50 | .to_owned(), |
| 51 | ), |
| 52 | }; |
| 53 | Some(AnnouncementBanner { title, background }) |
| 54 | } |
| 55 | |
| 56 | pub fn image(&self) -> Option<ImageRef> { |
| 57 | let BannerBackground::Image(url) = &self.background else { |
| 58 | return None; |
| 59 | }; |
| 60 | Some(ImageRef { |
| 61 | id: url.clone(), |
| 62 | url: url.clone(), |
| 63 | width: None, |
| 64 | height: None, |
| 65 | alt: Some(self.title.clone()), |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | /// `id` must be unique per message; Teams uses `announcement-card-<guid>`. |
| 70 | pub fn attachment(&self, id: String) -> graph::KeptAttachment { |
| 71 | graph::KeptAttachment { |
| 72 | id, |
| 73 | content_type: Some(ANNOUNCEMENT_BANNER_TYPE.to_owned()), |
| 74 | content: Some(self.content()), |
| 75 | content_url: None, |
| 76 | name: None, |
| 77 | teams_app_id: Some(ANNOUNCEMENT_APP_ID.to_owned()), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Only color banners can be sent; uploaded images need the Teams image service. |
| 82 | pub fn content(&self) -> String { |
| 83 | let color = match &self.background { |
| 84 | BannerBackground::Color(color) => color.as_str(), |
| 85 | BannerBackground::Image(_) => DEFAULT_BANNER_COLOR, |
| 86 | }; |
| 87 | json!({ |
| 88 | "title": self.title, |
| 89 | "cardImageType": COLOR_THEME, |
| 90 | "cardImageDetails": { COLOR_THEME: color }, |
| 91 | }) |
| 92 | .to_string() |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn image_source(details: &Value, key: &str) -> Option<String> { |
| 97 | let found = details |
| 98 | .get(key) |
| 99 | .or_else(|| details.get("uploadedImageDetail")?.get(key))?; |
| 100 | found |
| 101 | .get("source") |
| 102 | .or_else(|| found.get("url")) |
| 103 | .and_then(Value::as_str) |
| 104 | .filter(|url| url.starts_with("https://")) |
| 105 | .map(str::to_owned) |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | mod tests { |
| 110 | use super::*; |
| 111 | |
| 112 | fn banner_attachment(content: &str) -> AttachmentInfo { |
| 113 | serde_json::from_value(json!({ |
| 114 | "content_type": ANNOUNCEMENT_BANNER_TYPE, |
| 115 | "name": null, |
| 116 | "url": null, |
| 117 | "text": null, |
| 118 | "content": content, |
| 119 | })) |
| 120 | .unwrap() |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn color_banner_from_graph_content() { |
| 125 | let content = r#"{"title":"Release day","cardImageType":"colorTheme","cardImageDetails":{"colorTheme":"periwinkleBlue"}}"#; |
| 126 | let banner = AnnouncementBanner::from_attachments(&[banner_attachment(content)]).unwrap(); |
| 127 | assert_eq!(banner.title, "Release day"); |
| 128 | assert_eq!( |
| 129 | banner.background, |
| 130 | BannerBackground::Color("periwinkleBlue".into()) |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn uploaded_image_banner_uses_the_cropped_image() { |
| 136 | let content = r#"{"title":"Hi","cardImageType":"uploadedImage","cardImageDetails":{"uploadedImageDetail":{"originalImage":{"source":"https://example.test/o"},"croppedImage":{"source":"https://example.test/c"}}}}"#; |
| 137 | let banner = AnnouncementBanner::parse(content).unwrap(); |
| 138 | assert_eq!( |
| 139 | banner.background, |
| 140 | BannerBackground::Image("https://example.test/c".into()) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn content_round_trips() { |
| 146 | let banner = AnnouncementBanner { |
| 147 | title: "Ship it".into(), |
| 148 | background: BannerBackground::Color("teal".into()), |
| 149 | }; |
| 150 | assert_eq!(AnnouncementBanner::parse(&banner.content()), Some(banner)); |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn banner_without_content_is_none() { |
| 155 | let mut attachment = banner_attachment(""); |
| 156 | attachment.content = None; |
| 157 | assert_eq!(AnnouncementBanner::from_attachments(&[attachment]), None); |
| 158 | } |
| 159 | } |