From 4984181613a56b99dc26597d23dbc98489f13b84 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Tue, 19 Mar 2024 20:51:50 +0100 Subject: [PATCH] Refactor and improve emoji detection in reactions UCHAR_EMOJI_PRESENTATION is replaced with UCHAR_EMOJI, which seems to be more useful for decting emojis. The lambda is also turned into a proper function as there's no apparent reason for it to be a lambda. --- src/models/reactionmodel.cpp | 46 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/models/reactionmodel.cpp b/src/models/reactionmodel.cpp index 9586f44e5..e3f4887b7 100644 --- a/src/models/reactionmodel.cpp +++ b/src/models/reactionmodel.cpp @@ -162,6 +162,30 @@ QHash ReactionModel::roleNames() const }; } +bool isEmoji(const QString &text) +{ +#ifdef HAVE_ICU + QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, text); + int from = 0; + while (finder.toNextBoundary() != -1) { + auto to = finder.position(); + if (text[from].isSpace()) { + from = to; + continue; + } + + auto first = text.mid(from, to - from).toUcs4()[0]; + if (!u_hasBinaryProperty(first, UCHAR_EMOJI)) { + return false; + } + from = to; + } + return true; +#else + return false; +#endif +} + QString ReactionModel::reactionText(QString text) const { text = text.toHtmlEscaped(); @@ -174,28 +198,6 @@ QString ReactionModel::reactionText(QString text) const return QStringLiteral("") .arg(m_room->connection()->makeMediaUrl(QUrl(text)).toString(), QString::number(size)); } - const auto isEmoji = [](const QString &text) { -#ifdef HAVE_ICU - QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, text); - int from = 0; - while (finder.toNextBoundary() != -1) { - auto to = finder.position(); - if (text[from].isSpace()) { - from = to; - continue; - } - - auto first = text.mid(from, to - from).toUcs4()[0]; - if (!u_hasBinaryProperty(first, UCHAR_EMOJI_PRESENTATION)) { - return false; - } - from = to; - } - return true; -#else - return false; -#endif - }; return isEmoji(text) ? QStringLiteral("") + text + QStringLiteral("") : text; }