From b40226826fe914d0d5d265230ebc5bac8058b6f7 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Thu, 3 Oct 2024 09:18:02 +0300 Subject: [PATCH] Restore fallback support for user mentions Fallback support was intentionally removed in 990851296828168c2106e, because it was deemed OK to do so. It turns out that Element iOS still doesn't properly do user mentions (and likely never will, until Element X replaces it), so we can't just drop the fallback user mentions logic without affecting all these clients. It's possible that the Element Android is no better (unverified claim). --- src/conversation/matrix/utils/mod.rs | 32 +++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/conversation/matrix/utils/mod.rs b/src/conversation/matrix/utils/mod.rs index cb37ccb..ec63da2 100644 --- a/src/conversation/matrix/utils/mod.rs +++ b/src/conversation/matrix/utils/mod.rs @@ -423,18 +423,26 @@ fn is_event_mentioning_bot( event_content: &RoomMessageEventContent, bot_user_id: &OwnedUserId, ) -> bool { - // As a fallback, we used to do string matching (`event_content.body().contains(bot_user_id.as_str())`) here as well. - // However, this is unreliable. In 2024+, clients that do not have proper mentions support should get fixed, - // instead of us having to deal with the possibility of false positives. - // - let Some(mentions) = &event_content.mentions else { - return false; - }; - - mentions - .user_ids - .iter() - .any(|user_id| user_id == bot_user_id) + if let Some(mentions) = &event_content.mentions { + mentions + .user_ids + .iter() + .any(|user_id| user_id == bot_user_id) + } else { + // For compatibility with clients that do not support the new Mentions specification + // (see https://spec.matrix.org/latest/client-server-api/#user-and-room-mentions), + // we also do string matching here. + // + // As of 2024-10-03, at least Element iOS does not support the new Mentions specification + // and is still quite widespread. + // + // It may be even better to match not only against the MXID, but also against the bot's + // room-specific display name. + // + // We may consider dropping this string-matching behavior altogether in the future, + // so improving this compatibility block is not a high priority. + event_content.body().contains(bot_user_id.as_str()) + } } /// Strips the rich reply fallback text from the given text.