// SPDX-FileCopyrightText: 2021 Nheko Contributors // SPDX-FileCopyrightText: 2022 Nheko Contributors // // SPDX-License-Identifier: GPL-3.0-or-later #include "Utils.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Cache.h" #include "Cache_p.h" #include "Config.h" #include "EventAccessors.h" #include "Logging.h" #include "MatrixClient.h" #include "UserSettingsPage.h" using TimelineEvent = mtx::events::collections::TimelineEvents; template static DescInfo createDescriptionInfo(const Event &event, const QString &localUser, const QString &displayName) { const auto msg = std::get(event); const auto sender = QString::fromStdString(msg.sender); const auto username = displayName; const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts); auto body = utils::event_body(event).trimmed(); if (mtx::accessors::relations(event).reply_to()) body = QString::fromStdString(utils::stripReplyFromBody(body.toStdString())); return DescInfo{QString::fromStdString(msg.event_id), sender, utils::messageDescription(username, body, sender == localUser), utils::descriptiveTime(ts), msg.origin_server_ts, ts}; } std::string utils::stripReplyFromBody(const std::string &bodyi) { QString body = QString::fromStdString(bodyi); if (body.startsWith(QLatin1String("> <"))) { auto segments = body.split('\n'); while (!segments.isEmpty() && segments.begin()->startsWith('>')) segments.erase(segments.begin()); if (!segments.empty() && segments.first().isEmpty()) segments.erase(segments.begin()); body = segments.join('\n'); } body.replace(QLatin1String("@room"), QString::fromUtf8("@\u2060room")); return body.toStdString(); } std::string utils::stripReplyFromFormattedBody(const std::string &formatted_bodyi) { QString formatted_body = QString::fromStdString(formatted_bodyi); formatted_body.remove(QRegularExpression(QStringLiteral(".*"), QRegularExpression::DotMatchesEverythingOption)); formatted_body.replace(QLatin1String("@room"), QString::fromUtf8("@\u2060room")); return formatted_body.toStdString(); } RelatedInfo utils::stripReplyFallbacks(const TimelineEvent &event, std::string id, QString room_id_) { RelatedInfo related = {}; related.quoted_user = QString::fromStdString(mtx::accessors::sender(event)); related.related_event = std::move(id); related.type = mtx::accessors::msg_type(event); // get body, strip reply fallback, then transform the event to text, if it is a media event // etc related.quoted_body = QString::fromStdString(mtx::accessors::body(event)); related.quoted_body = QString::fromStdString(stripReplyFromBody(related.quoted_body.toStdString())); related.quoted_body = utils::getQuoteBody(related); // get quoted body and strip reply fallback related.quoted_formatted_body = mtx::accessors::formattedBodyWithFallback(event); related.quoted_formatted_body = QString::fromStdString( stripReplyFromFormattedBody(related.quoted_formatted_body.toStdString())); related.room = room_id_; return related; } QString utils::localUser() { return QString::fromStdString(http::client()->user_id().to_string()); } bool utils::codepointIsEmoji(uint code) { // TODO: Be more precise here. return (code >= 0x2600 && code <= 0x27bf) || (code >= 0x2b00 && code <= 0x2bff) || (code >= 0x1f000 && code <= 0x1faff) || code == 0x200d || code == 0xfe0f; } QString utils::replaceEmoji(const QString &body) { QString fmtBody; fmtBody.reserve(body.size()); QVector utf32_string = body.toUcs4(); bool insideFontBlock = false; bool insideTag = false; for (auto &code : utf32_string) { if (code == U'<') insideTag = true; else if (code == U'>') insideTag = false; if (!insideTag && utils::codepointIsEmoji(code)) { if (!insideFontBlock) { fmtBody += QStringLiteral("emojiFont() % QStringLiteral("\">"); insideFontBlock = true; } else if (code == 0xfe0f) { // BUG(Nico): // Workaround https://bugreports.qt.io/browse/QTBUG-97401 // See also https://github.com/matrix-org/matrix-react-sdk/pull/1458/files // Nheko bug: https://github.com/Nheko-Reborn/nheko/issues/439 continue; } } else { if (insideFontBlock) { fmtBody += QStringLiteral(""); insideFontBlock = false; } } if (QChar::requiresSurrogates(code)) { QChar emoji[] = {static_cast(QChar::highSurrogate(code)), static_cast(QChar::lowSurrogate(code))}; fmtBody.append(emoji, 2); } else { fmtBody.append(QChar(static_cast(code))); } } if (insideFontBlock) { fmtBody += QStringLiteral(""); } return fmtBody; } void utils::setScaleFactor(float factor) { if (factor < 1 || factor > 3) return; QSettings settings; settings.setValue(QStringLiteral("settings/scale_factor"), factor); } float utils::scaleFactor() { QSettings settings; return settings.value(QStringLiteral("settings/scale_factor"), -1).toFloat(); } QString utils::descriptiveTime(const QDateTime &then) { const auto now = QDateTime::currentDateTime(); const auto days = then.daysTo(now); if (days == 0) return QLocale::system().toString(then.time(), QLocale::ShortFormat); else if (days < 2) return QString(QCoreApplication::translate("descriptiveTime", "Yesterday")); else if (days < 7) return then.toString(QStringLiteral("dddd")); return QLocale::system().toString(then.date(), QLocale::ShortFormat); } DescInfo utils::getMessageDescription(const TimelineEvent &event, const QString &localUser, const QString &displayName) { using Audio = mtx::events::RoomEvent; using Emote = mtx::events::RoomEvent; using File = mtx::events::RoomEvent; using Image = mtx::events::RoomEvent; using Notice = mtx::events::RoomEvent; using Text = mtx::events::RoomEvent; using Video = mtx::events::RoomEvent; using CallInvite = mtx::events::RoomEvent; using CallAnswer = mtx::events::RoomEvent; using CallHangUp = mtx::events::RoomEvent; using CallReject = mtx::events::RoomEvent; using Encrypted = mtx::events::EncryptedEvent; if (std::holds_alternative