| 1 | mod strip; |
| 2 | |
| 3 | use std::sync::OnceLock; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | use teams_core::EmoticonTag; |
| 7 | |
| 8 | pub use strip::{StripImages, StripLoader, StripSource, split_strip}; |
| 9 | |
| 10 | const CATALOG: &str = include_str!("../../../app/assets/emoticons.tsv"); |
| 11 | const ASSET_BASE: &str = "https://statics.teams.cdn.office.net/evergreen-assets/personal-expressions/v2/assets/emoticons"; |
| 12 | const ASSET_SIZE: u32 = 100; |
| 13 | const FRAMES_PER_SECOND: u64 = 24; |
| 14 | const VARIATION_SELECTOR: char = '\u{fe0f}'; |
| 15 | const SKIN_TONES: std::ops::RangeInclusive<char> = '\u{1f3fb}'..='\u{1f3ff}'; |
| 16 | const TONE_NAMES: [&str; 5] = ["light", "medium light", "medium", "medium dark", "dark"]; |
| 17 | const TONE_ID_MARKER: &str = "-tone"; |
| 18 | |
| 19 | #[derive(Debug, PartialEq, Eq)] |
| 20 | pub struct Emoticon { |
| 21 | pub id: &'static str, |
| 22 | pub unicode: &'static str, |
| 23 | etag: &'static str, |
| 24 | frames: u32, |
| 25 | diverse: bool, |
| 26 | description: &'static str, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 30 | pub struct ResolvedEmoticon { |
| 31 | pub emoticon: &'static Emoticon, |
| 32 | tone: Option<u32>, |
| 33 | } |
| 34 | |
| 35 | impl ResolvedEmoticon { |
| 36 | pub fn strip_source(&self) -> StripSource { |
| 37 | let tone = match self.tone { |
| 38 | Some(tone) if self.emoticon.diverse => format!("_s{}", tone + 1), |
| 39 | _ => String::new(), |
| 40 | }; |
| 41 | StripSource { |
| 42 | url: format!( |
| 43 | "{ASSET_BASE}/{}/default/{ASSET_SIZE}_anim_f{tone}.png?v={}", |
| 44 | self.emoticon.id, self.emoticon.etag |
| 45 | ) |
| 46 | .into(), |
| 47 | frame_duration: Duration::from_millis(1000 / FRAMES_PER_SECOND), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn loop_duration(&self) -> Duration { |
| 52 | Duration::from_millis(u64::from(self.emoticon.frames) * 1000 / FRAMES_PER_SECOND) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn catalog() -> &'static [Emoticon] { |
| 57 | static PARSED: OnceLock<Vec<Emoticon>> = OnceLock::new(); |
| 58 | PARSED.get_or_init(|| { |
| 59 | CATALOG |
| 60 | .lines() |
| 61 | .filter_map(|line| { |
| 62 | let mut fields = line.split('\t'); |
| 63 | Some(Emoticon { |
| 64 | id: fields.next()?, |
| 65 | unicode: fields.next()?, |
| 66 | etag: fields.next()?, |
| 67 | frames: fields.next()?.parse().ok()?, |
| 68 | diverse: fields.next()? == "1", |
| 69 | description: fields.next()?, |
| 70 | }) |
| 71 | }) |
| 72 | .collect() |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | fn skin_tone(glyph: &str) -> Option<u32> { |
| 77 | glyph |
| 78 | .chars() |
| 79 | .find(|character| SKIN_TONES.contains(character)) |
| 80 | .map(|tone| tone as u32 - *SKIN_TONES.start() as u32 + 1) |
| 81 | } |
| 82 | |
| 83 | fn base_glyph(glyph: &str) -> String { |
| 84 | glyph |
| 85 | .chars() |
| 86 | .filter(|character| *character != VARIATION_SELECTOR && !SKIN_TONES.contains(character)) |
| 87 | .collect() |
| 88 | } |
| 89 | |
| 90 | pub fn resolve(id: Option<&str>, glyph: &str) -> Option<ResolvedEmoticon> { |
| 91 | let base = base_glyph(glyph); |
| 92 | let id = id.map(|id| id.split_once(TONE_ID_MARKER).map_or(id, |(base, _)| base)); |
| 93 | let by_id = id.and_then(|id| catalog().iter().find(|emoticon| emoticon.id == id)); |
| 94 | let by_unicode = || { |
| 95 | let mut matches = catalog().iter().filter(|emoticon| emoticon.unicode == base); |
| 96 | let first = matches.next()?; |
| 97 | Some( |
| 98 | std::iter::once(first) |
| 99 | .chain(matches) |
| 100 | .find(|emoticon| emoticon.diverse) |
| 101 | .unwrap_or(first), |
| 102 | ) |
| 103 | }; |
| 104 | Some(ResolvedEmoticon { |
| 105 | emoticon: by_id.or_else(by_unicode)?, |
| 106 | tone: skin_tone(glyph), |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | pub fn send_tag(glyph: &str) -> Option<EmoticonTag> { |
| 111 | let resolved = resolve(None, glyph)?; |
| 112 | let emoticon = resolved.emoticon; |
| 113 | let (id, title, suffix) = match resolved.tone.filter(|_| emoticon.diverse) { |
| 114 | Some(tone) => ( |
| 115 | format!("{}{TONE_ID_MARKER}{tone}", emoticon.id), |
| 116 | format!( |
| 117 | "{} with {} skin tone", |
| 118 | emoticon.description, |
| 119 | TONE_NAMES[tone as usize - 1] |
| 120 | ), |
| 121 | format!("_s{}", tone + 1), |
| 122 | ), |
| 123 | None => ( |
| 124 | emoticon.id.to_owned(), |
| 125 | emoticon.description.to_owned(), |
| 126 | String::new(), |
| 127 | ), |
| 128 | }; |
| 129 | Some(EmoticonTag { |
| 130 | id, |
| 131 | title, |
| 132 | image_url: format!( |
| 133 | "{ASSET_BASE}/{}/default/{{size}}_f{suffix}.png", |
| 134 | emoticon.id |
| 135 | ), |
| 136 | }) |
| 137 | } |
| 138 | |
| 139 | #[cfg(test)] |
| 140 | mod tests { |
| 141 | use super::*; |
| 142 | |
| 143 | #[test] |
| 144 | fn the_message_id_picks_the_emoticon_over_the_first_unicode_match() { |
| 145 | assert_eq!( |
| 146 | resolve(Some("laughdog"), "😄").unwrap().emoticon.id, |
| 147 | "laughdog" |
| 148 | ); |
| 149 | assert_eq!( |
| 150 | resolve(None, "😄").unwrap().emoticon.id, |
| 151 | "grinningfacewithsmilingeyes" |
| 152 | ); |
| 153 | assert_eq!(resolve(Some("unknown"), "❤️").unwrap().emoticon.id, "heart"); |
| 154 | assert_eq!(resolve(None, "👍").unwrap().emoticon.id, "yes"); |
| 155 | assert!(resolve(None, "x").is_none()); |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn send_tags_carry_the_tone_id_and_toned_image() { |
| 160 | let heart = send_tag("❤️").unwrap(); |
| 161 | assert_eq!( |
| 162 | (heart.id.as_str(), heart.title.as_str()), |
| 163 | ("heart", "Heart") |
| 164 | ); |
| 165 | assert!(heart.image_url.ends_with("/heart/default/{size}_f.png")); |
| 166 | let thumb = send_tag("👍🏼").unwrap(); |
| 167 | assert_eq!(thumb.id, "yes-tone2"); |
| 168 | assert_eq!(thumb.title, "Yes with medium light skin tone"); |
| 169 | assert!(thumb.image_url.ends_with("/yes/default/{size}_f_s3.png")); |
| 170 | assert_eq!(send_tag("👍").unwrap().id, "yes"); |
| 171 | assert_eq!(resolve(Some("yes-tone2"), "👍🏼").unwrap().emoticon.id, "yes"); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn urls_use_the_100_strip_with_etag_and_tone_only_for_diverse_emoticons() { |
| 176 | let heart = resolve(None, "❤️").unwrap(); |
| 177 | assert!( |
| 178 | heart |
| 179 | .strip_source() |
| 180 | .url |
| 181 | .ends_with("/heart/default/100_anim_f.png?v=v34") |
| 182 | ); |
| 183 | assert_eq!(heart.loop_duration(), Duration::from_millis(73 * 1000 / 24)); |
| 184 | let thumb = resolve(None, "👍🏽").unwrap(); |
| 185 | assert!( |
| 186 | thumb |
| 187 | .strip_source() |
| 188 | .url |
| 189 | .contains("/yes/default/100_anim_f_s4.png?v=") |
| 190 | ); |
| 191 | } |
| 192 | } |