fix(chat): editing a channel post keeps its subject and channel mention
A chatsvc edit PUT replaces all message properties, so editing a channel post deleted its subject on the server. The edit now sends the stored subject again (Remote::edit_post, Messages::edit_post). Starting an edit in a channel also restores user mentions and a mention of the channel itself, so they stay mentions instead of plain text. The in-place post editor with a subject field and toolbar (as Teams web) is not part of this change. Upstream issue danieldaeschle/rusty-teams#133.
@@ -3413,7 +3413,7 @@ impl ConversationView {
3413
3413
let mentions = if in_chat {
3414
3414
teams_core::user_mention_inputs(&record)
3415
3415
} else {
3416
Vec::new()
3416
teams_core::channel_mention_inputs(&record)
3417
3417
};
3418
3418
let draft = Outgoing {
3419
3419
banner: None,
@@ -3413,7 +3413,7 @@ impl ConversationView {3413
let mentions = if in_chat {3413
let mentions = if in_chat {3414
teams_core::user_mention_inputs(&record)3414
teams_core::user_mention_inputs(&record)3415
} else {3415
} else {3416
Vec::new()3416
teams_core::channel_mention_inputs(&record)3417
};3417
};3418
let draft = Outgoing {3418
let draft = Outgoing {3419
banner: None,3419
banner: None,@@ -152,12 +152,28 @@ impl<T: MessageTransport> Messages<T> {
152
152
message_id: &str,
153
153
html: &str,
154
154
mentions: Option<&str>,
155
) -> Result<()> {
156
self.edit_post(conversation, message_id, html, mentions, None)
157
.await
158
}
159
160
/// The PUT replaces all properties, so a channel post sends its `subject` again to keep it.
161
pub async fn edit_post(
162
&self,
163
conversation: &ConversationRef,
164
message_id: &str,
165
html: &str,
166
mentions: Option<&str>,
167
subject: Option<&str>,
155
168
) -> Result<()> {
156
169
let url = self.message_url(conversation, message_id);
157
170
let mut body = edit_body(message_id, html);
158
171
if let Some(mentions) = mentions {
159
172
body["properties"]["mentions"] = json!(mentions);
160
173
}
174
if let Some(subject) = subject {
175
body["properties"]["subject"] = json!(subject);
176
}
161
177
let mut answer = self.put(&url, &body).await?;
162
178
if answer.status == 404 {
163
179
tokio::time::sleep(self.retry_delay).await;
@@ -152,12 +152,28 @@ impl<T: MessageTransport> Messages<T> {152
message_id: &str,152
message_id: &str,153
html: &str,153
html: &str,154
mentions: Option<&str>,154
mentions: Option<&str>,155
) -> Result<()> {156
self.edit_post(conversation, message_id, html, mentions, None)157
.await158
}159
160
/// The PUT replaces all properties, so a channel post sends its `subject` again to keep it.161
pub async fn edit_post(162
&self,163
conversation: &ConversationRef,164
message_id: &str,165
html: &str,166
mentions: Option<&str>,167
subject: Option<&str>,155
) -> Result<()> {168
) -> Result<()> {156
let url = self.message_url(conversation, message_id);169
let url = self.message_url(conversation, message_id);157
let mut body = edit_body(message_id, html);170
let mut body = edit_body(message_id, html);158
if let Some(mentions) = mentions {171
if let Some(mentions) = mentions {159
body["properties"]["mentions"] = json!(mentions);172
body["properties"]["mentions"] = json!(mentions);160
}173
}174
if let Some(subject) = subject {175
body["properties"]["subject"] = json!(subject);176
}161
let mut answer = self.put(&url, &body).await?;177
let mut answer = self.put(&url, &body).await?;162
if answer.status == 404 {178
if answer.status == 404 {163
tokio::time::sleep(self.retry_delay).await;179
tokio::time::sleep(self.retry_delay).await;@@ -109,6 +109,24 @@ async fn channel_root_uses_the_channel_id() {
109
109
);
110
110
}
111
111
112
#[tokio::test]
113
async fn editing_a_post_sends_its_subject_again() {
114
let mock = Mock::new(&[200]);
115
messages(&mock)
116
.edit_post(
117
&ConversationRef::channel_root("19:c@thread.tacv2"),
118
"m1",
119
"x",
120
None,
121
Some("Plan"),
122
)
123
.await
124
.unwrap();
125
let body = mock.requests()[0].body.clone().unwrap();
126
assert_eq!(body["properties"]["subject"], "Plan");
127
assert!(body["properties"]["edittime"].is_string());
128
}
129
112
130
#[tokio::test]
113
131
async fn delete_is_soft_without_a_body() {
114
132
let mock = Mock::new(&[200]);
@@ -109,6 +109,24 @@ async fn channel_root_uses_the_channel_id() {109
);109
);110
}110
}111
111
112
#[tokio::test]113
async fn editing_a_post_sends_its_subject_again() {114
let mock = Mock::new(&[200]);115
messages(&mock)116
.edit_post(117
&ConversationRef::channel_root("19:c@thread.tacv2"),118
"m1",119
"x",120
None,121
Some("Plan"),122
)123
.await124
.unwrap();125
let body = mock.requests()[0].body.clone().unwrap();126
assert_eq!(body["properties"]["subject"], "Plan");127
assert!(body["properties"]["edittime"].is_string());128
}129
112
#[tokio::test]130
#[tokio::test]113
async fn delete_is_soft_without_a_body() {131
async fn delete_is_soft_without_a_body() {114
let mock = Mock::new(&[200]);132
let mock = Mock::new(&[200]);@@ -793,6 +793,7 @@ impl<R: Remote> SyncEngine<R> {
793
793
let (mut html, mentions) = outgoing(&conversation, html, mentions)?;
794
794
let target = self.message_target(conversation_id, message_id)?;
795
795
let mut extras = MessageExtras::default();
796
let mut subject = None;
796
797
if let Some(record) = self
797
798
.store
798
799
.messages_by_id(conversation_id, &[message_id.to_owned()])?
@@ -802,10 +803,20 @@ impl<R: Remote> SyncEngine<R> {
802
803
html.push_str(&preserved.images_html);
803
804
extras.files = preserved.files;
804
805
extras.kept = preserved.kept;
806
subject = record.subject;
807
}
808
match subject {
809
Some(subject) => {
810
self.remote
811
.edit_post(&target, &html, &mentions, &extras, &subject)
812
.await?
813
}
814
None => {
815
self.remote
816
.edit_message(&target, &html, &mentions, &extras)
817
.await?
818
}
805
819
}
806
self.remote
807
.edit_message(&target, &html, &mentions, &extras)
808
.await?;
809
820
self.refresh_message(conversation_id, message_id).await?;
810
821
Ok(())
811
822
}
@@ -793,6 +793,7 @@ impl<R: Remote> SyncEngine<R> {793
let (mut html, mentions) = outgoing(&conversation, html, mentions)?;793
let (mut html, mentions) = outgoing(&conversation, html, mentions)?;794
let target = self.message_target(conversation_id, message_id)?;794
let target = self.message_target(conversation_id, message_id)?;795
let mut extras = MessageExtras::default();795
let mut extras = MessageExtras::default();796
let mut subject = None;796
if let Some(record) = self797
if let Some(record) = self797
.store798
.store798
.messages_by_id(conversation_id, &[message_id.to_owned()])?799
.messages_by_id(conversation_id, &[message_id.to_owned()])?@@ -802,10 +803,20 @@ impl<R: Remote> SyncEngine<R> {802
html.push_str(&preserved.images_html);803
html.push_str(&preserved.images_html);803
extras.files = preserved.files;804
extras.files = preserved.files;804
extras.kept = preserved.kept;805
extras.kept = preserved.kept;806
subject = record.subject;807
}808
match subject {809
Some(subject) => {810
self.remote811
.edit_post(&target, &html, &mentions, &extras, &subject)812
.await?813
}814
None => {815
self.remote816
.edit_message(&target, &html, &mentions, &extras)817
.await?818
}805
}819
}806
self.remote807
.edit_message(&target, &html, &mentions, &extras)808
.await?;809
self.refresh_message(conversation_id, message_id).await?;820
self.refresh_message(conversation_id, message_id).await?;810
Ok(())821
Ok(())811
}822
}@@ -196,9 +196,10 @@ pub use store::{ConversationHit, HIGHLIGHT_END, HIGHLIGHT_START, SearchHit};
196
196
pub use stored::{
197
197
AttachmentInfo, FileCard, FileKind, ImageKind, ImageRef, MentionInfo, PROFILE_LINK_PREFIX,
198
198
QuoteInfo, ReactionInfo, ReactionSummary, adaptive_cards, attachments, can_delete, can_edit,
199
card_texts, copy_text, files, images, linked_message_spans, linked_message_spans_with,
200
mentions, mentions_whole_chat, message_quote, message_spans, quote_html, quotes,
201
reaction_summaries, reactions, readable_text, translated_spans, user_mention_inputs,
199
card_texts, channel_mention_inputs, copy_text, files, images, linked_message_spans,
200
linked_message_spans_with, mentions, mentions_whole_chat, message_quote, message_spans,
201
quote_html, quotes, reaction_summaries, reactions, readable_text, translated_spans,
202
user_mention_inputs,
202
203
};
203
204
pub use table::{Cell, PICKER_COLUMNS, PICKER_ROWS, TABLE_MARK, Table, TableEdit};
204
205
pub use team_admin::{MAX_CHANNEL_DESCRIPTION_LENGTH, MAX_CHANNEL_NAME_LENGTH};
@@ -196,9 +196,10 @@ pub use store::{ConversationHit, HIGHLIGHT_END, HIGHLIGHT_START, SearchHit};196
pub use stored::{196
pub use stored::{197
AttachmentInfo, FileCard, FileKind, ImageKind, ImageRef, MentionInfo, PROFILE_LINK_PREFIX,197
AttachmentInfo, FileCard, FileKind, ImageKind, ImageRef, MentionInfo, PROFILE_LINK_PREFIX,198
QuoteInfo, ReactionInfo, ReactionSummary, adaptive_cards, attachments, can_delete, can_edit,198
QuoteInfo, ReactionInfo, ReactionSummary, adaptive_cards, attachments, can_delete, can_edit,199
card_texts, copy_text, files, images, linked_message_spans, linked_message_spans_with,199
card_texts, channel_mention_inputs, copy_text, files, images, linked_message_spans,200
mentions, mentions_whole_chat, message_quote, message_spans, quote_html, quotes,200
linked_message_spans_with, mentions, mentions_whole_chat, message_quote, message_spans,201
reaction_summaries, reactions, readable_text, translated_spans, user_mention_inputs,201
quote_html, quotes, reaction_summaries, reactions, readable_text, translated_spans,202
user_mention_inputs,202
};203
};203
pub use table::{Cell, PICKER_COLUMNS, PICKER_ROWS, TABLE_MARK, Table, TableEdit};204
pub use table::{Cell, PICKER_COLUMNS, PICKER_ROWS, TABLE_MARK, Table, TableEdit};204
pub use team_admin::{MAX_CHANNEL_DESCRIPTION_LENGTH, MAX_CHANNEL_NAME_LENGTH};205
pub use team_admin::{MAX_CHANNEL_DESCRIPTION_LENGTH, MAX_CHANNEL_NAME_LENGTH};@@ -179,6 +179,17 @@ pub trait Remote {
179
179
mentions: &[OutgoingMention],
180
180
extras: &MessageExtras,
181
181
) -> Result<()>;
182
/// `edit_message` that keeps a channel post's subject.
183
async fn edit_post(
184
&self,
185
target: &MessageTarget,
186
html: &str,
187
mentions: &[OutgoingMention],
188
extras: &MessageExtras,
189
_subject: &str,
190
) -> Result<()> {
191
self.edit_message(target, html, mentions, extras).await
192
}
182
193
async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
183
194
async fn undo_soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
184
195
async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;
@@ -973,37 +984,18 @@ impl Remote for Graph {
973
984
mentions: &[OutgoingMention],
974
985
extras: &MessageExtras,
975
986
) -> Result<()> {
976
match chatsvc_conversation(target) {
977
Some(conversation) if mentions.is_empty() => {
978
let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
979
Ok(Messages::new(self.session())
980
.edit_message(&conversation, target.message_id(), &html)
981
.await?)
982
}
983
Some(conversation) => {
984
let Some(entries) = crate::teams_quote::mention_entries(mentions) else {
985
if matches!(conversation, ConversationRef::Chat { .. }) {
986
return Err(Error::Unsupported("editing in a team or tag mention"));
987
}
988
return Ok(Graph::edit_message(self, target, html, mentions, extras).await?);
989
};
990
let html = format!(
991
"{}{}{}",
992
extras.kept_html(),
993
crate::teams_quote::chatsvc_mention_html(html, mentions),
994
extras.files_html()
995
);
996
Ok(Messages::new(self.session())
997
.edit_message_with_mentions(
998
&conversation,
999
target.message_id(),
1000
&html,
1001
Some(&serde_json::Value::Array(entries).to_string()),
1002
)
1003
.await?)
1004
}
1005
None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
1006
}
987
edit_chatsvc_or_graph(self, target, html, mentions, extras, None).await
988
}
989
990
async fn edit_post(
991
&self,
992
target: &MessageTarget,
993
html: &str,
994
mentions: &[OutgoingMention],
995
extras: &MessageExtras,
996
subject: &str,
997
) -> Result<()> {
998
edit_chatsvc_or_graph(self, target, html, mentions, extras, Some(subject)).await
1007
999
}
1008
1000
1009
1001
async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
@@ -1713,6 +1705,49 @@ impl Remote for Graph {
1713
1705
}
1714
1706
}
1715
1707
1708
/// chatsvc for chats (when enabled) and channels, Graph otherwise; `subject` keeps a channel post's subject.
1709
async fn edit_chatsvc_or_graph(
1710
graph: &Graph,
1711
target: &MessageTarget,
1712
html: &str,
1713
mentions: &[OutgoingMention],
1714
extras: &MessageExtras,
1715
subject: Option<&str>,
1716
) -> Result<()> {
1717
let Some(conversation) = chatsvc_conversation(target) else {
1718
return Ok(Graph::edit_message(graph, target, html, mentions, extras).await?);
1719
};
1720
let (html, entries) = if mentions.is_empty() {
1721
(
1722
format!("{}{html}{}", extras.kept_html(), extras.files_html()),
1723
None,
1724
)
1725
} else {
1726
let Some(entries) = crate::teams_quote::mention_entries(mentions) else {
1727
if matches!(conversation, ConversationRef::Chat { .. }) {
1728
return Err(Error::Unsupported("editing in a team or tag mention"));
1729
}
1730
return Ok(Graph::edit_message(graph, target, html, mentions, extras).await?);
1731
};
1732
let html = format!(
1733
"{}{}{}",
1734
extras.kept_html(),
1735
crate::teams_quote::chatsvc_mention_html(html, mentions),
1736
extras.files_html()
1737
);
1738
(html, Some(serde_json::Value::Array(entries).to_string()))
1739
};
1740
Ok(Messages::new(graph.session())
1741
.edit_post(
1742
&conversation,
1743
target.message_id(),
1744
&html,
1745
entries.as_deref(),
1746
subject,
1747
)
1748
.await?)
1749
}
1750
1716
1751
fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
1717
1752
match target {
1718
1753
MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
@@ -179,6 +179,17 @@ pub trait Remote {179
mentions: &[OutgoingMention],179
mentions: &[OutgoingMention],180
extras: &MessageExtras,180
extras: &MessageExtras,181
) -> Result<()>;181
) -> Result<()>;182
/// `edit_message` that keeps a channel post's subject.183
async fn edit_post(184
&self,185
target: &MessageTarget,186
html: &str,187
mentions: &[OutgoingMention],188
extras: &MessageExtras,189
_subject: &str,190
) -> Result<()> {191
self.edit_message(target, html, mentions, extras).await192
}182
async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;193
async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;183
async fn undo_soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;194
async fn undo_soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;184
async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;195
async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;@@ -973,37 +984,18 @@ impl Remote for Graph {973
mentions: &[OutgoingMention],984
mentions: &[OutgoingMention],974
extras: &MessageExtras,985
extras: &MessageExtras,975
) -> Result<()> {986
) -> Result<()> {976
match chatsvc_conversation(target) {987
edit_chatsvc_or_graph(self, target, html, mentions, extras, None).await977
Some(conversation) if mentions.is_empty() => {988
}978
let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());989
979
Ok(Messages::new(self.session())990
async fn edit_post(980
.edit_message(&conversation, target.message_id(), &html)991
&self,981
.await?)992
target: &MessageTarget,982
}993
html: &str,983
Some(conversation) => {994
mentions: &[OutgoingMention],984
let Some(entries) = crate::teams_quote::mention_entries(mentions) else {995
extras: &MessageExtras,985
if matches!(conversation, ConversationRef::Chat { .. }) {996
subject: &str,986
return Err(Error::Unsupported("editing in a team or tag mention"));997
) -> Result<()> {987
}998
edit_chatsvc_or_graph(self, target, html, mentions, extras, Some(subject)).await988
return Ok(Graph::edit_message(self, target, html, mentions, extras).await?);989
};990
let html = format!(991
"{}{}{}",992
extras.kept_html(),993
crate::teams_quote::chatsvc_mention_html(html, mentions),994
extras.files_html()995
);996
Ok(Messages::new(self.session())997
.edit_message_with_mentions(998
&conversation,999
target.message_id(),1000
&html,1001
Some(&serde_json::Value::Array(entries).to_string()),1002
)1003
.await?)1004
}1005
None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),1006
}1007
}999
}1008
1000
1009
async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {1001
async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {@@ -1713,6 +1705,49 @@ impl Remote for Graph {1713
}1705
}1714
}1706
}1715
1707
1708
/// chatsvc for chats (when enabled) and channels, Graph otherwise; `subject` keeps a channel post's subject.1709
async fn edit_chatsvc_or_graph(1710
graph: &Graph,1711
target: &MessageTarget,1712
html: &str,1713
mentions: &[OutgoingMention],1714
extras: &MessageExtras,1715
subject: Option<&str>,1716
) -> Result<()> {1717
let Some(conversation) = chatsvc_conversation(target) else {1718
return Ok(Graph::edit_message(graph, target, html, mentions, extras).await?);1719
};1720
let (html, entries) = if mentions.is_empty() {1721
(1722
format!("{}{html}{}", extras.kept_html(), extras.files_html()),1723
None,1724
)1725
} else {1726
let Some(entries) = crate::teams_quote::mention_entries(mentions) else {1727
if matches!(conversation, ConversationRef::Chat { .. }) {1728
return Err(Error::Unsupported("editing in a team or tag mention"));1729
}1730
return Ok(Graph::edit_message(graph, target, html, mentions, extras).await?);1731
};1732
let html = format!(1733
"{}{}{}",1734
extras.kept_html(),1735
crate::teams_quote::chatsvc_mention_html(html, mentions),1736
extras.files_html()1737
);1738
(html, Some(serde_json::Value::Array(entries).to_string()))1739
};1740
Ok(Messages::new(graph.session())1741
.edit_post(1742
&conversation,1743
target.message_id(),1744
&html,1745
entries.as_deref(),1746
subject,1747
)1748
.await?)1749
}1750
1716
fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {1751
fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {1717
match target {1752
match target {1718
MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),1753
MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),@@ -819,11 +819,35 @@ pub(crate) fn quote_time(message_id: &str) -> Option<String> {
819
819
pub fn user_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {
820
820
let infos = mentions(record);
821
821
let mut inputs = Vec::new();
822
collect_user_mentions(&spans_with_mentions(record, &infos), &infos, &mut inputs);
822
collect_user_mentions(
823
&spans_with_mentions(record, &infos),
824
&infos,
825
None,
826
&mut inputs,
827
);
828
inputs
829
}
830
831
/// For editing a channel message: the user mentions plus a mention of the channel itself.
832
pub fn channel_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {
833
let infos = mentions(record);
834
let mut inputs = Vec::new();
835
let channel = Some(record.conversation_id.as_str());
836
collect_user_mentions(
837
&spans_with_mentions(record, &infos),
838
&infos,
839
channel,
840
&mut inputs,
841
);
823
842
inputs
824
843
}
825
844
826
fn collect_user_mentions(spans: &[Span], infos: &[MentionInfo], inputs: &mut Vec<MentionInput>) {
845
fn collect_user_mentions(
846
spans: &[Span],
847
infos: &[MentionInfo],
848
channel: Option<&str>,
849
inputs: &mut Vec<MentionInput>,
850
) {
827
851
for span in spans {
828
852
match span {
829
853
Span::Bold(children)
@@ -838,22 +862,29 @@ fn collect_user_mentions(spans: &[Span], infos: &[MentionInfo], inputs: &mut Vec
838
862
| Span::Heading { children, .. }
839
863
| Span::Quote { children, .. }
840
864
| Span::BlockQuote(children)
841
| Span::Link { children, .. } => collect_user_mentions(children, infos, inputs),
865
| Span::Link { children, .. } => {
866
collect_user_mentions(children, infos, channel, inputs)
867
}
842
868
Span::List { items, .. } => items
843
869
.iter()
844
.for_each(|item| collect_user_mentions(item, infos, inputs)),
870
.for_each(|item| collect_user_mentions(item, infos, channel, inputs)),
845
871
Span::Table { rows, .. } => rows
846
872
.iter()
847
873
.flatten()
848
.for_each(|cell| collect_user_mentions(cell, infos, inputs)),
874
.for_each(|cell| collect_user_mentions(cell, infos, channel, inputs)),
849
875
Span::Mention { name, id } => {
850
876
let parsed: Option<i64> = id.as_deref().and_then(|id| id.parse().ok());
851
let user_id = infos
877
let info = infos
852
878
.iter()
853
.find(|info| parsed.is_some() && info.id == parsed)
854
.and_then(|info| info.user_id.as_deref());
855
if let Some(user_id) = user_id {
879
.find(|info| parsed.is_some() && info.id == parsed);
880
if let Some(user_id) = info.and_then(|info| info.user_id.as_deref()) {
856
881
inputs.push(MentionInput::user(user_id, name));
882
} else if let Some(channel) = channel
883
&& info.is_some_and(|info| {
884
info.group && info.target_id.as_deref() == Some(channel)
885
})
886
{
887
inputs.push(MentionInput::channel(channel, name));
857
888
}
858
889
}
859
890
_ => {}
@@ -1499,4 +1530,28 @@ mod tests {
1499
1530
]
1500
1531
);
1501
1532
}
1533
1534
#[test]
1535
fn editing_a_channel_post_keeps_its_channel_mention() {
1536
let mut post = with_mentions(
1537
r#"<at id="0">General</at> and <at id="1">Bob</at> <at id="2">Squad</at>"#,
1538
r#"[
1539
{"user_id":null,"name":"General","id":0,"target_id":"19:c@thread.tacv2","group":true},
1540
{"user_id":"U2","name":"Bob","id":1,"target_id":"U2"},
1541
{"user_id":null,"name":"Squad","id":2,"target_id":"19:t@thread.tacv2","group":true}
1542
]"#,
1543
);
1544
post.conversation_id = "19:c@thread.tacv2".to_owned();
1545
assert_eq!(
1546
channel_mention_inputs(&post),
1547
vec![
1548
MentionInput::channel("19:c@thread.tacv2", "General"),
1549
MentionInput::user("U2", "Bob"),
1550
]
1551
);
1552
assert_eq!(
1553
user_mention_inputs(&post),
1554
vec![MentionInput::user("U2", "Bob")]
1555
);
1556
}
1502
1557
}
@@ -819,11 +819,35 @@ pub(crate) fn quote_time(message_id: &str) -> Option<String> {819
pub fn user_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {819
pub fn user_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {820
let infos = mentions(record);820
let infos = mentions(record);821
let mut inputs = Vec::new();821
let mut inputs = Vec::new();822
collect_user_mentions(&spans_with_mentions(record, &infos), &infos, &mut inputs);822
collect_user_mentions(823
&spans_with_mentions(record, &infos),824
&infos,825
None,826
&mut inputs,827
);828
inputs829
}830
831
/// For editing a channel message: the user mentions plus a mention of the channel itself.832
pub fn channel_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {833
let infos = mentions(record);834
let mut inputs = Vec::new();835
let channel = Some(record.conversation_id.as_str());836
collect_user_mentions(837
&spans_with_mentions(record, &infos),838
&infos,839
channel,840
&mut inputs,841
);823
inputs842
inputs824
}843
}825
844
826
fn collect_user_mentions(spans: &[Span], infos: &[MentionInfo], inputs: &mut Vec<MentionInput>) {845
fn collect_user_mentions(846
spans: &[Span],847
infos: &[MentionInfo],848
channel: Option<&str>,849
inputs: &mut Vec<MentionInput>,850
) {827
for span in spans {851
for span in spans {828
match span {852
match span {829
Span::Bold(children)853
Span::Bold(children)@@ -838,22 +862,29 @@ fn collect_user_mentions(spans: &[Span], infos: &[MentionInfo], inputs: &mut Vec838
| Span::Heading { children, .. }862
| Span::Heading { children, .. }839
| Span::Quote { children, .. }863
| Span::Quote { children, .. }840
| Span::BlockQuote(children)864
| Span::BlockQuote(children)841
| Span::Link { children, .. } => collect_user_mentions(children, infos, inputs),865
| Span::Link { children, .. } => {866
collect_user_mentions(children, infos, channel, inputs)867
}842
Span::List { items, .. } => items868
Span::List { items, .. } => items843
.iter()869
.iter()844
.for_each(|item| collect_user_mentions(item, infos, inputs)),870
.for_each(|item| collect_user_mentions(item, infos, channel, inputs)),845
Span::Table { rows, .. } => rows871
Span::Table { rows, .. } => rows846
.iter()872
.iter()847
.flatten()873
.flatten()848
.for_each(|cell| collect_user_mentions(cell, infos, inputs)),874
.for_each(|cell| collect_user_mentions(cell, infos, channel, inputs)),849
Span::Mention { name, id } => {875
Span::Mention { name, id } => {850
let parsed: Option<i64> = id.as_deref().and_then(|id| id.parse().ok());876
let parsed: Option<i64> = id.as_deref().and_then(|id| id.parse().ok());851
let user_id = infos877
let info = infos852
.iter()878
.iter()853
.find(|info| parsed.is_some() && info.id == parsed)879
.find(|info| parsed.is_some() && info.id == parsed);854
.and_then(|info| info.user_id.as_deref());880
if let Some(user_id) = info.and_then(|info| info.user_id.as_deref()) {855
if let Some(user_id) = user_id {856
inputs.push(MentionInput::user(user_id, name));881
inputs.push(MentionInput::user(user_id, name));882
} else if let Some(channel) = channel883
&& info.is_some_and(|info| {884
info.group && info.target_id.as_deref() == Some(channel)885
})886
{887
inputs.push(MentionInput::channel(channel, name));857
}888
}858
}889
}859
_ => {}890
_ => {}@@ -1499,4 +1530,28 @@ mod tests {1499
]1530
]1500
);1531
);1501
}1532
}1533
1534
#[test]1535
fn editing_a_channel_post_keeps_its_channel_mention() {1536
let mut post = with_mentions(1537
r#"<at id="0">General</at> and <at id="1">Bob</at> <at id="2">Squad</at>"#,1538
r#"[1539
{"user_id":null,"name":"General","id":0,"target_id":"19:c@thread.tacv2","group":true},1540
{"user_id":"U2","name":"Bob","id":1,"target_id":"U2"},1541
{"user_id":null,"name":"Squad","id":2,"target_id":"19:t@thread.tacv2","group":true}1542
]"#,1543
);1544
post.conversation_id = "19:c@thread.tacv2".to_owned();1545
assert_eq!(1546
channel_mention_inputs(&post),1547
vec![1548
MentionInput::channel("19:c@thread.tacv2", "General"),1549
MentionInput::user("U2", "Bob"),1550
]1551
);1552
assert_eq!(1553
user_mention_inputs(&post),1554
vec![MentionInput::user("U2", "Bob")]1555
);1556
}1502
}1557
}