BlueGit

ict rusty-teams

fix(chat): send emoticons as Teams composer spans, with skin tones

- Graph re-resolves an <emoji> tag from the unicode alone: 👍 became the "like" face and skin tones were lost (live test in 48:notes)
- send the composer span instead: itemid with tone id (yes-tone2), toned image, 50 px when emoji-only, 20 px inline
- tone ids in received messages resolve to the base emoticon

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-15 · 6 weeks ago

Hash
59e679565132d53526fd93b888319890d91590ba
Parent
cd07b8d0
2 files changed +90 −24
crates/app-base/src/emoticons/mod.rs +35 −8
@@ -13,6 +13,8 @@ const ASSET_SIZE: u32 = 100;
13 13 const FRAMES_PER_SECOND: u64 = 24;
14 14 const VARIATION_SELECTOR: char = '\u{fe0f}';
15 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";
16 18
17 19 #[derive(Debug, PartialEq, Eq)]
18 20 pub struct Emoticon {
@@ -87,6 +89,7 @@ fn base_glyph(glyph: &str) -> String {
87 89
88 90 pub fn resolve(id: Option<&str>, glyph: &str) -> Option<ResolvedEmoticon> {
89 91 let base = base_glyph(glyph);
92 let id = id.map(|id| id.split_once(TONE_ID_MARKER).map_or(id, |(base, _)| base));
90 93 let by_id = id.and_then(|id| catalog().iter().find(|emoticon| emoticon.id == id));
91 94 let by_unicode = || {
92 95 let mut matches = catalog().iter().filter(|emoticon| emoticon.unicode == base);
@@ -105,13 +108,31 @@ pub fn resolve(id: Option<&str>, glyph: &str) -> Option<ResolvedEmoticon> {
105 108 }
106 109
107 110 pub fn send_tag(glyph: &str) -> Option<EmoticonTag> {
108 if skin_tone(glyph).is_some() {
109 return None;
110 }
111 let emoticon = resolve(None, glyph)?.emoticon;
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 };
112 129 Some(EmoticonTag {
113 id: emoticon.id.to_owned(),
114 title: emoticon.description.to_owned(),
130 id,
131 title,
132 image_url: format!(
133 "{ASSET_BASE}/{}/default/{{size}}_f{suffix}.png",
134 emoticon.id
135 ),
115 136 })
116 137 }
117 138
@@ -135,13 +156,19 @@ mod tests {
135 156 }
136 157
137 158 #[test]
138 fn send_tags_name_the_emoticon_and_skip_skin_tones() {
159 fn send_tags_carry_the_tone_id_and_toned_image() {
139 160 let heart = send_tag("❤️").unwrap();
140 161 assert_eq!(
141 162 (heart.id.as_str(), heart.title.as_str()),
142 163 ("heart", "Heart")
143 164 );
144 assert!(send_tag("👍🏽").is_none());
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");
145 172 }
146 173
147 174 #[test]
@@ -13,6 +13,8 @@ const ASSET_SIZE: u32 = 100;
13const FRAMES_PER_SECOND: u64 = 24;
13const FRAMES_PER_SECOND: u64 = 24;
14const VARIATION_SELECTOR: char = '\u{fe0f}';
14const VARIATION_SELECTOR: char = '\u{fe0f}';
15const SKIN_TONES: std::ops::RangeInclusive<char> = '\u{1f3fb}'..='\u{1f3ff}';
15const SKIN_TONES: std::ops::RangeInclusive<char> = '\u{1f3fb}'..='\u{1f3ff}';
16const TONE_NAMES: [&str; 5] = ["light", "medium light", "medium", "medium dark", "dark"];
17const TONE_ID_MARKER: &str = "-tone";
16
18
17#[derive(Debug, PartialEq, Eq)]
19#[derive(Debug, PartialEq, Eq)]
18pub struct Emoticon {
20pub struct Emoticon {
@@ -87,6 +89,7 @@ fn base_glyph(glyph: &str) -> String {
87
89
88pub fn resolve(id: Option<&str>, glyph: &str) -> Option<ResolvedEmoticon> {
90pub fn resolve(id: Option<&str>, glyph: &str) -> Option<ResolvedEmoticon> {
89 let base = base_glyph(glyph);
91 let base = base_glyph(glyph);
92 let id = id.map(|id| id.split_once(TONE_ID_MARKER).map_or(id, |(base, _)| base));
90 let by_id = id.and_then(|id| catalog().iter().find(|emoticon| emoticon.id == id));
93 let by_id = id.and_then(|id| catalog().iter().find(|emoticon| emoticon.id == id));
91 let by_unicode = || {
94 let by_unicode = || {
92 let mut matches = catalog().iter().filter(|emoticon| emoticon.unicode == base);
95 let mut matches = catalog().iter().filter(|emoticon| emoticon.unicode == base);
@@ -105,13 +108,31 @@ pub fn resolve(id: Option<&str>, glyph: &str) -> Option<ResolvedEmoticon> {
105}
108}
106
109
107pub fn send_tag(glyph: &str) -> Option<EmoticonTag> {
110pub fn send_tag(glyph: &str) -> Option<EmoticonTag> {
108 if skin_tone(glyph).is_some() {
111 let resolved = resolve(None, glyph)?;
109 return None;
112 let emoticon = resolved.emoticon;
110 }
113 let (id, title, suffix) = match resolved.tone.filter(|_| emoticon.diverse) {
111 let emoticon = resolve(None, glyph)?.emoticon;
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 };
112 Some(EmoticonTag {
129 Some(EmoticonTag {
113 id: emoticon.id.to_owned(),
130 id,
114 title: emoticon.description.to_owned(),
131 title,
132 image_url: format!(
133 "{ASSET_BASE}/{}/default/{{size}}_f{suffix}.png",
134 emoticon.id
135 ),
115 })
136 })
116}
137}
117
138
@@ -135,13 +156,19 @@ mod tests {
135 }
156 }
136
157
137 #[test]
158 #[test]
138 fn send_tags_name_the_emoticon_and_skip_skin_tones() {
159 fn send_tags_carry_the_tone_id_and_toned_image() {
139 let heart = send_tag("❤️").unwrap();
160 let heart = send_tag("❤️").unwrap();
140 assert_eq!(
161 assert_eq!(
141 (heart.id.as_str(), heart.title.as_str()),
162 (heart.id.as_str(), heart.title.as_str()),
142 ("heart", "Heart")
163 ("heart", "Heart")
143 );
164 );
144 assert!(send_tag("👍🏽").is_none());
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");
145 }
172 }
146
173
147 #[test]
174 #[test]
crates/core/src/emoticon_tags.rs +55 −16
@@ -1,18 +1,31 @@
1 1 use unicode_segmentation::UnicodeSegmentation;
2 2
3 use crate::jumbo::is_emoji_grapheme;
3 use crate::jumbo::{is_emoji_grapheme, jumbo_emoji};
4 4 use crate::markdown::escape_html;
5 5
6 6 const VERBATIM_ELEMENTS: [&str; 2] = ["code", "pre"];
7 const INLINE_SIZE: u32 = 20;
8 const JUMBO_SIZE: u32 = 50;
9 const SIZE_PLACEHOLDER: &str = "{size}";
7 10
8 11 pub struct EmoticonTag {
9 12 pub id: String,
10 13 pub title: String,
14 pub image_url: String,
11 15 }
12 16
13 /// Graph turns `<emoji>` into the Teams emoticon markup, so the official client animates it
14 /// and shows emoji-only messages big. Text inside `code` and `pre` stays as written.
17 /// The Teams composer markup. Graph keeps it; a Graph `<emoji>` tag would be re-resolved from
18 /// the unicode alone, which loses skin tones and picks the wrong emoticon for some characters.
15 19 pub fn emoticon_tags(html: &str, lookup: impl Fn(&str) -> Option<EmoticonTag>) -> String {
20 let inline = tagged(html, &lookup, INLINE_SIZE);
21 if inline != html && jumbo_emoji(&inline).is_some() {
22 tagged(html, &lookup, JUMBO_SIZE)
23 } else {
24 inline
25 }
26 }
27
28 fn tagged(html: &str, lookup: &impl Fn(&str) -> Option<EmoticonTag>, size: u32) -> String {
16 29 let mut output = String::with_capacity(html.len());
17 30 let mut verbatim_depth = 0usize;
18 31 let mut rest = html;
@@ -22,7 +35,7 @@ pub fn emoticon_tags(html: &str, lookup: impl Fn(&str) -> Option<EmoticonTag>) -
22 35 if verbatim_depth > 0 {
23 36 output.push_str(text);
24 37 } else {
25 push_tagged(&mut output, text, &lookup);
38 push_tagged(&mut output, text, lookup, size);
26 39 }
27 40 let tag_end = after_text.find('>').map_or(after_text.len(), |end| end + 1);
28 41 let (tag, after_tag) = after_text.split_at(tag_end);
@@ -52,23 +65,32 @@ fn tag_name(tag: &str) -> Option<(String, bool)> {
52 65 Some((name.to_ascii_lowercase(), closing))
53 66 }
54 67
55 fn push_tagged(output: &mut String, text: &str, lookup: &impl Fn(&str) -> Option<EmoticonTag>) {
68 fn push_tagged(
69 output: &mut String,
70 text: &str,
71 lookup: &impl Fn(&str) -> Option<EmoticonTag>,
72 size: u32,
73 ) {
56 74 for grapheme in text.graphemes(true) {
57 75 match is_emoji_grapheme(grapheme)
58 76 .then(|| lookup(grapheme))
59 77 .flatten()
60 78 {
61 Some(tag) => output.push_str(&format!(
62 "<emoji id=\"{}\" alt=\"{}\" title=\"{}\"></emoji>",
63 escape_html(&tag.id),
64 grapheme,
65 escape_html(&tag.title)
66 )),
79 Some(tag) => output.push_str(&span(&tag, grapheme, size)),
67 80 None => output.push_str(grapheme),
68 81 }
69 82 }
70 83 }
71 84
85 fn span(tag: &EmoticonTag, glyph: &str, size: u32) -> String {
86 let id = escape_html(&tag.id);
87 let title = escape_html(&tag.title);
88 let source = escape_html(&tag.image_url.replace(SIZE_PLACEHOLDER, &size.to_string()));
89 format!(
90 "<span contenteditable=\"false\" title=\"{title}\" type=\"({id})\" class=\"animated-emoticon-{size}-{id}\" itemscope=\"\"><img itemscope=\"\" itemtype=\"http://schema.skype.com/Emoji\" itemid=\"{id}\" src=\"{source}\" title=\"{title}\" alt=\"{glyph}\" style=\"width:{size}px;height:{size}px;\"></span>"
91 )
92 }
93
72 94 #[cfg(test)]
73 95 mod tests {
74 96 use super::*;
@@ -77,24 +99,41 @@ mod tests {
77 99 glyph.starts_with('\u{2764}').then(|| EmoticonTag {
78 100 id: "heart".into(),
79 101 title: "Heart".into(),
102 image_url: "https://e/heart/default/{size}_f.png".into(),
80 103 })
81 104 }
82 105
106 fn heart_span(size: u32) -> String {
107 format!(
108 "<span contenteditable=\"false\" title=\"Heart\" type=\"(heart)\" class=\"animated-emoticon-{size}-heart\" itemscope=\"\"><img itemscope=\"\" itemtype=\"http://schema.skype.com/Emoji\" itemid=\"heart\" src=\"https://e/heart/default/{size}_f.png\" title=\"Heart\" alt=\"❤️\" style=\"width:{size}px;height:{size}px;\"></span>"
109 )
110 }
111
83 112 #[test]
84 fn known_emoji_in_text_become_emoji_tags() {
113 fn emoji_in_text_become_inline_composer_spans() {
85 114 assert_eq!(
86 115 emoticon_tags("<p>love ❤️ it 😀</p>", heart_only),
87 "<p>love <emoji id=\"heart\" alt=\"❤️\" title=\"Heart\"></emoji> it 😀</p>"
116 format!("<p>love {} it 😀</p>", heart_span(20))
117 );
118 }
119
120 #[test]
121 fn emoji_only_messages_get_the_50_size() {
122 assert_eq!(
123 emoticon_tags("<p>❤️ ❤️</p>", heart_only),
124 format!("<p>{} {}</p>", heart_span(50), heart_span(50))
88 125 );
89 126 }
90 127
91 128 #[test]
92 129 fn code_attributes_and_markup_stay_untouched() {
93 let html =
94 "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\">❤️</a> <code>❤️</code></p>";
130 let html = "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\">❤️</a></p>";
95 131 assert_eq!(
96 132 emoticon_tags(html, heart_only),
97 "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\"><emoji id=\"heart\" alt=\"❤️\" title=\"Heart\"></emoji></a> <code>❤️</code></p>"
133 format!(
134 "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\">{}</a></p>",
135 heart_span(20)
136 )
98 137 );
99 138 }
100 139 }
@@ -1,18 +1,31 @@
1use unicode_segmentation::UnicodeSegmentation;
1use unicode_segmentation::UnicodeSegmentation;
2
2
3use crate::jumbo::is_emoji_grapheme;
3use crate::jumbo::{is_emoji_grapheme, jumbo_emoji};
4use crate::markdown::escape_html;
4use crate::markdown::escape_html;
5
5
6const VERBATIM_ELEMENTS: [&str; 2] = ["code", "pre"];
6const VERBATIM_ELEMENTS: [&str; 2] = ["code", "pre"];
7const INLINE_SIZE: u32 = 20;
8const JUMBO_SIZE: u32 = 50;
9const SIZE_PLACEHOLDER: &str = "{size}";
7
10
8pub struct EmoticonTag {
11pub struct EmoticonTag {
9 pub id: String,
12 pub id: String,
10 pub title: String,
13 pub title: String,
14 pub image_url: String,
11}
15}
12
16
13/// Graph turns `<emoji>` into the Teams emoticon markup, so the official client animates it
17/// The Teams composer markup. Graph keeps it; a Graph `<emoji>` tag would be re-resolved from
14/// and shows emoji-only messages big. Text inside `code` and `pre` stays as written.
18/// the unicode alone, which loses skin tones and picks the wrong emoticon for some characters.
15pub fn emoticon_tags(html: &str, lookup: impl Fn(&str) -> Option<EmoticonTag>) -> String {
19pub fn emoticon_tags(html: &str, lookup: impl Fn(&str) -> Option<EmoticonTag>) -> String {
20 let inline = tagged(html, &lookup, INLINE_SIZE);
21 if inline != html && jumbo_emoji(&inline).is_some() {
22 tagged(html, &lookup, JUMBO_SIZE)
23 } else {
24 inline
25 }
26}
27
28fn tagged(html: &str, lookup: &impl Fn(&str) -> Option<EmoticonTag>, size: u32) -> String {
16 let mut output = String::with_capacity(html.len());
29 let mut output = String::with_capacity(html.len());
17 let mut verbatim_depth = 0usize;
30 let mut verbatim_depth = 0usize;
18 let mut rest = html;
31 let mut rest = html;
@@ -22,7 +35,7 @@ pub fn emoticon_tags(html: &str, lookup: impl Fn(&str) -> Option<EmoticonTag>) -
22 if verbatim_depth > 0 {
35 if verbatim_depth > 0 {
23 output.push_str(text);
36 output.push_str(text);
24 } else {
37 } else {
25 push_tagged(&mut output, text, &lookup);
38 push_tagged(&mut output, text, lookup, size);
26 }
39 }
27 let tag_end = after_text.find('>').map_or(after_text.len(), |end| end + 1);
40 let tag_end = after_text.find('>').map_or(after_text.len(), |end| end + 1);
28 let (tag, after_tag) = after_text.split_at(tag_end);
41 let (tag, after_tag) = after_text.split_at(tag_end);
@@ -52,23 +65,32 @@ fn tag_name(tag: &str) -> Option<(String, bool)> {
52 Some((name.to_ascii_lowercase(), closing))
65 Some((name.to_ascii_lowercase(), closing))
53}
66}
54
67
55fn push_tagged(output: &mut String, text: &str, lookup: &impl Fn(&str) -> Option<EmoticonTag>) {
68fn push_tagged(
69 output: &mut String,
70 text: &str,
71 lookup: &impl Fn(&str) -> Option<EmoticonTag>,
72 size: u32,
73) {
56 for grapheme in text.graphemes(true) {
74 for grapheme in text.graphemes(true) {
57 match is_emoji_grapheme(grapheme)
75 match is_emoji_grapheme(grapheme)
58 .then(|| lookup(grapheme))
76 .then(|| lookup(grapheme))
59 .flatten()
77 .flatten()
60 {
78 {
61 Some(tag) => output.push_str(&format!(
79 Some(tag) => output.push_str(&span(&tag, grapheme, size)),
62 "<emoji id=\"{}\" alt=\"{}\" title=\"{}\"></emoji>",
63 escape_html(&tag.id),
64 grapheme,
65 escape_html(&tag.title)
66 )),
67 None => output.push_str(grapheme),
80 None => output.push_str(grapheme),
68 }
81 }
69 }
82 }
70}
83}
71
84
85fn span(tag: &EmoticonTag, glyph: &str, size: u32) -> String {
86 let id = escape_html(&tag.id);
87 let title = escape_html(&tag.title);
88 let source = escape_html(&tag.image_url.replace(SIZE_PLACEHOLDER, &size.to_string()));
89 format!(
90 "<span contenteditable=\"false\" title=\"{title}\" type=\"({id})\" class=\"animated-emoticon-{size}-{id}\" itemscope=\"\"><img itemscope=\"\" itemtype=\"http://schema.skype.com/Emoji\" itemid=\"{id}\" src=\"{source}\" title=\"{title}\" alt=\"{glyph}\" style=\"width:{size}px;height:{size}px;\"></span>"
91 )
92}
93
72#[cfg(test)]
94#[cfg(test)]
73mod tests {
95mod tests {
74 use super::*;
96 use super::*;
@@ -77,24 +99,41 @@ mod tests {
77 glyph.starts_with('\u{2764}').then(|| EmoticonTag {
99 glyph.starts_with('\u{2764}').then(|| EmoticonTag {
78 id: "heart".into(),
100 id: "heart".into(),
79 title: "Heart".into(),
101 title: "Heart".into(),
102 image_url: "https://e/heart/default/{size}_f.png".into(),
80 })
103 })
81 }
104 }
82
105
106 fn heart_span(size: u32) -> String {
107 format!(
108 "<span contenteditable=\"false\" title=\"Heart\" type=\"(heart)\" class=\"animated-emoticon-{size}-heart\" itemscope=\"\"><img itemscope=\"\" itemtype=\"http://schema.skype.com/Emoji\" itemid=\"heart\" src=\"https://e/heart/default/{size}_f.png\" title=\"Heart\" alt=\"❤️\" style=\"width:{size}px;height:{size}px;\"></span>"
109 )
110 }
111
83 #[test]
112 #[test]
84 fn known_emoji_in_text_become_emoji_tags() {
113 fn emoji_in_text_become_inline_composer_spans() {
85 assert_eq!(
114 assert_eq!(
86 emoticon_tags("<p>love ❤️ it 😀</p>", heart_only),
115 emoticon_tags("<p>love ❤️ it 😀</p>", heart_only),
87 "<p>love <emoji id=\"heart\" alt=\"❤️\" title=\"Heart\"></emoji> it 😀</p>"
116 format!("<p>love {} it 😀</p>", heart_span(20))
117 );
118 }
119
120 #[test]
121 fn emoji_only_messages_get_the_50_size() {
122 assert_eq!(
123 emoticon_tags("<p>❤️ ❤️</p>", heart_only),
124 format!("<p>{} {}</p>", heart_span(50), heart_span(50))
88 );
125 );
89 }
126 }
90
127
91 #[test]
128 #[test]
92 fn code_attributes_and_markup_stay_untouched() {
129 fn code_attributes_and_markup_stay_untouched() {
93 let html =
130 let html = "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\">❤️</a></p>";
94 "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\">❤️</a> <code>❤️</code></p>";
95 assert_eq!(
131 assert_eq!(
96 emoticon_tags(html, heart_only),
132 emoticon_tags(html, heart_only),
97 "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\"><emoji id=\"heart\" alt=\"❤️\" title=\"Heart\"></emoji></a> <code>❤️</code></p>"
133 format!(
134 "<pre><code>❤️</code></pre><p><a href=\"x\" title=\"❤️\">{}</a></p>",
135 heart_span(20)
136 )
98 );
137 );
99 }
138 }
100}
139}