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

@ -92,7 +92,8 @@ HEADERS += \
src/imageitem.h \ src/imageitem.h \
src/accountlistmodel.h \ src/accountlistmodel.h \
src/spectraluser.h \ src/spectraluser.h \
src/notifications/manager.h src/notifications/manager.h \
src/utils.h
SOURCES += src/main.cpp \ SOURCES += src/main.cpp \
src/controller.cpp \ src/controller.cpp \
@ -104,7 +105,8 @@ SOURCES += src/main.cpp \
src/userlistmodel.cpp \ src/userlistmodel.cpp \
src/imageitem.cpp \ src/imageitem.cpp \
src/accountlistmodel.cpp \ src/accountlistmodel.cpp \
src/spectraluser.cpp src/spectraluser.cpp \
src/utils.cpp
unix:!mac { unix:!mac {
SOURCES += src/notifications/managerlinux.cpp SOURCES += src/notifications/managerlinux.cpp

View File

@ -13,6 +13,8 @@
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtQml> // for qmlRegisterType() #include <QtQml> // for qmlRegisterType()
#include "utils.h"
static QString parseAvatarUrl(QUrl url) { static QString parseAvatarUrl(QUrl url) {
return url.host() + "/" + url.path(); return url.host() + "/" + url.path();
} }
@ -232,120 +234,13 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const {
std::min(row, timelineBaseIndex()); std::min(row, timelineBaseIndex());
const auto& evt = isPending ? **pendingIt : **timelineIt; const auto& evt = isPending ? **pendingIt : **timelineIt;
using namespace QMatrixClient;
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
if (evt.isRedacted()) { return utils::eventToString(evt, m_currentRoom, Qt::RichText);
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"));
} }
if (role == MessageRole) { if (role == MessageRole) {
static const QRegExp rmReplyRegExp("^> <@.*:.*> .*\n\n(.*)"); static const QRegExp rmReplyRegExp("^> <@.*:.*> .*\n\n(.*)");
return evt.contentJson().value("body").toString().replace(rmReplyRegExp, return utils::eventToString(evt, m_currentRoom).replace(rmReplyRegExp, "\\1");
"\\1");
} }
if (role == Qt::ToolTipRole) { if (role == Qt::ToolTipRole) {

View File

@ -1,6 +1,7 @@
#include "roomlistmodel.h" #include "roomlistmodel.h"
#include "user.h" #include "user.h"
#include "utils.h"
#include "events/roomevent.h" #include "events/roomevent.h"
@ -79,13 +80,12 @@ void RoomListModel::connectRoomSignals(SpectralRoom* room) {
room, &Room::aboutToAddNewMessages, this, room, &Room::aboutToAddNewMessages, this,
[=](QMatrixClient::RoomEventsRange eventsRange) { [=](QMatrixClient::RoomEventsRange eventsRange) {
RoomEvent* event = (eventsRange.end() - 1)->get(); RoomEvent* event = (eventsRange.end() - 1)->get();
if (event->isStateEvent()) return;
User* sender = room->user(event->senderId()); User* sender = room->user(event->senderId());
if (sender == room->localUser()) return; if (sender == room->localUser()) return;
QUrl _url = room->avatarUrl(); QUrl _url = room->avatarUrl();
emit newMessage( emit newMessage(
room->id(), event->id(), room->displayName(), sender->displayname(), room->id(), event->id(), room->displayName(), sender->displayname(),
event->contentJson().value("body").toString(), room->avatar(128), utils::eventToString(*event), room->avatar(128),
QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) +
"/avatar/" + _url.authority() + '_' + _url.fileName() + ".png")); "/avatar/" + _url.authority() + '_' + _url.fileName() + ".png"));
}); });

View File

@ -12,6 +12,8 @@
#include <QMetaObject> #include <QMetaObject>
#include <QMimeDatabase> #include <QMimeDatabase>
#include "utils.h"
SpectralRoom::SpectralRoom(Connection* connection, QString roomId, SpectralRoom::SpectralRoom(Connection* connection, QString roomId,
JoinState joinState) JoinState joinState)
: Room(connection, std::move(roomId), joinState) { : Room(connection, std::move(roomId), joinState) {
@ -105,9 +107,8 @@ void SpectralRoom::sendTypingNotification(bool isTyping) {
QString SpectralRoom::lastEvent() { QString SpectralRoom::lastEvent() {
if (timelineSize() == 0) return ""; if (timelineSize() == 0) return "";
const RoomEvent* lastEvent = messageEvents().rbegin()->get(); const RoomEvent* lastEvent = messageEvents().rbegin()->get();
if (lastEvent->contentJson().value("body").toString() == "") return "";
return user(lastEvent->senderId())->displayname() + ": " + return user(lastEvent->senderId())->displayname() + ": " +
lastEvent->contentJson().value("body").toString(); utils::removeReply(utils::eventToString(*lastEvent, this));
} }
bool SpectralRoom::isEventHighlighted(const RoomEvent* e) const { bool SpectralRoom::isEventHighlighted(const RoomEvent* e) const {

6
src/utils.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "utils.h"
QString utils::removeReply(const QString& text) {
QString result(text);
return result.remove(utils::removeReplyRegex);
}

140
src/utils.h Normal file
View File

@ -0,0 +1,140 @@
#ifndef Utils_H
#define Utils_H
#include "room.h"
#include <QObject>
#include <QRegExp>
#include <QString>
#include <events/redactionevent.h>
#include <events/roomavatarevent.h>
#include <events/roommemberevent.h>
#include <events/simplestateevents.h>
namespace utils {
const QRegExp removeReplyRegex{"> <.*>.*\\n\\n"};
QString removeReply(const QString& text);
template <typename BaseEventT>
QString eventToString(const BaseEventT& evt,
QMatrixClient::Room* room = nullptr,
Qt::TextFormat format = Qt::PlainText) {
bool prettyPrint = (format == Qt::RichText);
using namespace QMatrixClient;
return visit(
evt,
[room, prettyPrint](const RoomMessageEvent& e) {
using namespace MessageEventContent;
if (prettyPrint && 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 = prettyPrint && room ? room->prettyPrint(e.plainBody())
: e.plainBody();
if (fileCaption.isEmpty()) return QObject::tr("a file");
}
return prettyPrint && room ? room->prettyPrint(e.plainBody())
: e.plainBody();
},
[room](const RoomMemberEvent& e) {
// FIXME: Rewind to the name that was at the time of this event
QString subjectName =
room ? room->roomMembername(e.userId()) : e.userId();
// The below code assumes senderName output in AuthorRole
switch (e.membership()) {
case MembershipType::Invite:
if (e.repeatsState())
return QObject::tr("reinvited %1 to the room").arg(subjectName);
FALLTHROUGH;
case MembershipType::Join: {
if (e.repeatsState())
return QObject::tr("joined the room (repeated)");
if (!e.prevContent() ||
e.membership() != e.prevContent()->membership) {
return e.membership() == MembershipType::Invite
? QObject::tr("invited %1 to the room")
.arg(subjectName)
: QObject::tr("joined the room");
}
QString text{};
if (e.isRename()) {
if (e.displayName().isEmpty())
text = QObject::tr("cleared their display name");
else
text = QObject::tr("changed their display name to %1")
.arg(e.displayName());
}
if (e.isAvatarUpdate()) {
if (!text.isEmpty()) text += " and ";
if (e.avatarUrl().isEmpty())
text += QObject::tr("cleared the avatar");
else
text += QObject::tr("updated the avatar");
}
return text;
}
case MembershipType::Leave:
if (e.prevContent() &&
e.prevContent()->membership == MembershipType::Ban) {
return (e.senderId() != e.userId())
? QObject::tr("unbanned %1").arg(subjectName)
: QObject::tr("self-unbanned");
}
return (e.senderId() != e.userId())
? QObject::tr("has kicked %1 from the room")
.arg(subjectName)
: QObject::tr("left the room");
case MembershipType::Ban:
return (e.senderId() != e.userId())
? QObject::tr("banned %1 from the room ")
.arg(subjectName)
: QObject::tr(" self-banned from the room ");
case MembershipType::Knock:
return QObject::tr("knocked");
default:;
}
return QObject::tr("made something unknown");
},
[](const RoomAliasesEvent& e) {
return QObject::tr("set aliases to: %1").arg(e.aliases().join(","));
},
[](const RoomCanonicalAliasEvent& e) {
return (e.alias().isEmpty())
? QObject::tr("cleared the room main alias")
: QObject::tr("set the room main alias to: %1")
.arg(e.alias());
},
[](const RoomNameEvent& e) {
return (e.name().isEmpty())
? QObject::tr("cleared the room name")
: QObject::tr("set the room name to: %1").arg(e.name());
},
[](const RoomTopicEvent& e) {
return (e.topic().isEmpty())
? QObject::tr("cleared the topic")
: QObject::tr("set the topic to: %1").arg(e.topic());
},
[](const RoomAvatarEvent&) {
return QObject::tr("changed the room avatar");
},
[](const EncryptionEvent&) {
return QObject::tr("activated End-to-End Encryption");
},
QObject::tr("Unknown Event"));
};
} // namespace utils
#endif