Clean up event to string related codes.

Working on #55.
This commit is contained in:
Black Hat
2018-10-24 21:15:26 +08:00
parent 47782f3198
commit e19e3b8ff9
6 changed files with 159 additions and 115 deletions

View File

@@ -13,6 +13,8 @@
#include <QtCore/QDebug>
#include <QtQml> // for qmlRegisterType()
#include "utils.h"
static QString parseAvatarUrl(QUrl url) {
return url.host() + "/" + url.path();
}
@@ -232,120 +234,13 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const {
std::min(row, timelineBaseIndex());
const auto& evt = isPending ? **pendingIt : **timelineIt;
using namespace QMatrixClient;
if (role == Qt::DisplayRole) {
if (evt.isRedacted()) {
auto reason = evt.redactedBecause()->reason();
if (reason.isEmpty()) return tr("Redacted");
return tr("Redacted: %1").arg(evt.redactedBecause()->reason());
}
return visit(
evt,
[this](const RoomMessageEvent& e) {
using namespace MessageEventContent;
if (e.hasTextContent() && e.mimeType().name() != "text/plain") {
static const QRegExp userPillRegExp(
"<a href=\"https://matrix.to/#/@.*:.*\">(.*)</a>");
QString formattedStr(
static_cast<const TextContent*>(e.content())->body);
formattedStr.replace(userPillRegExp,
"<b class=\"user-pill\">\\1</b>");
return formattedStr;
}
if (e.hasFileContent()) {
auto fileCaption = e.content()->fileInfo()->originalName;
if (fileCaption.isEmpty())
fileCaption = m_currentRoom->prettyPrint(e.plainBody());
if (fileCaption.isEmpty()) return tr("a file");
}
return m_currentRoom->prettyPrint(e.plainBody());
},
[this](const RoomMemberEvent& e) {
// FIXME: Rewind to the name that was at the time of this event
QString subjectName = m_currentRoom->roomMembername(e.userId());
// The below code assumes senderName output in AuthorRole
switch (e.membership()) {
case MembershipType::Invite:
if (e.repeatsState())
return tr("reinvited %1 to the room").arg(subjectName);
FALLTHROUGH;
case MembershipType::Join: {
if (e.repeatsState()) return tr("joined the room (repeated)");
if (!e.prevContent() ||
e.membership() != e.prevContent()->membership) {
return e.membership() == MembershipType::Invite
? tr("invited %1 to the room").arg(subjectName)
: tr("joined the room");
}
QString text{};
if (e.isRename()) {
if (e.displayName().isEmpty())
text = tr("cleared their display name");
else
text = tr("changed their display name to %1")
.arg(e.displayName());
}
if (e.isAvatarUpdate()) {
if (!text.isEmpty()) text += " and ";
if (e.avatarUrl().isEmpty())
text += tr("cleared the avatar");
else
text += tr("updated the avatar");
}
return text;
}
case MembershipType::Leave:
if (e.prevContent() &&
e.prevContent()->membership == MembershipType::Ban) {
return (e.senderId() != e.userId())
? tr("unbanned %1").arg(subjectName)
: tr("self-unbanned");
}
return (e.senderId() != e.userId())
? tr("has kicked %1 from the room").arg(subjectName)
: tr("left the room");
case MembershipType::Ban:
return (e.senderId() != e.userId())
? tr("banned %1 from the room").arg(subjectName)
: tr("self-banned from the room");
case MembershipType::Knock:
return tr("knocked");
default:;
}
return tr("made something unknown");
},
[](const RoomAliasesEvent& e) {
return tr("set aliases to: %1").arg(e.aliases().join(", "));
},
[](const RoomCanonicalAliasEvent& e) {
return (e.alias().isEmpty())
? tr("cleared the room main alias")
: tr("set the room main alias to: %1").arg(e.alias());
},
[](const RoomNameEvent& e) {
return (e.name().isEmpty())
? tr("cleared the room name")
: tr("set the room name to: %1").arg(e.name());
},
[](const RoomTopicEvent& e) {
return (e.topic().isEmpty())
? tr("cleared the topic")
: tr("set the topic to: %1").arg(e.topic());
},
[](const RoomAvatarEvent&) { return tr("changed the room avatar"); },
[](const EncryptionEvent&) {
return tr("activated End-to-End Encryption");
},
tr("Unknown Event"));
return utils::eventToString(evt, m_currentRoom, Qt::RichText);
}
if (role == MessageRole) {
static const QRegExp rmReplyRegExp("^> <@.*:.*> .*\n\n(.*)");
return evt.contentJson().value("body").toString().replace(rmReplyRegExp,
"\\1");
return utils::eventToString(evt, m_currentRoom).replace(rmReplyRegExp, "\\1");
}
if (role == Qt::ToolTipRole) {