Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Rusakov <[email protected]>
  • Loading branch information
rpallai and KitsuneRal committed Apr 17, 2023
1 parent 818ef95 commit 3c5c3d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
20 changes: 9 additions & 11 deletions client/chatroomwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ void ChatRoomWidget::sendMessage()
}

void ChatRoomWidget::sendMessageFromFragment(const QTextDocumentFragment& text,
enum TextFormat textFormat)
TextFormat textFormat)
{
const auto& plainText = text.toPlainText();
const auto& htmlText =
Expand All @@ -450,14 +450,12 @@ void ChatRoomWidget::sendMessageFromFragment(const QTextDocumentFragment& text,
auto eventRelation = EventRelation::replace(
referencedEventIndex().data(MessageEventModel::EventIdRole).toString()
);
EventContent::TextContent* textContent;
if (htmlText.contains(MarkupRE)) {
textContent = new EventContent::TextContent(htmlText,
QStringLiteral("text/html"), eventRelation);
} else {
textContent = new EventContent::TextContent("",
QStringLiteral("text/plain"), eventRelation);
}
auto* textContent =
htmlText.contains(MarkupRE)
? new EventContent::TextContent(htmlText,
QStringLiteral("text/html"), eventRelation)
: new EventContent::TextContent(QString(),
QStringLiteral("text/plain"), eventRelation);
auto roomMessageEvent = new RoomMessageEvent(plainText,
MessageEventType::Text, textContent);
currentRoom()->postEvent(roomMessageEvent);
Expand Down Expand Up @@ -778,7 +776,7 @@ bool ChatRoomWidget::setReferringMode(const int newMode, const QString& eventId,
const char* icon_name)
{
Q_ASSERT( newMode == Replying || newMode == Editing );
// Actually, we could let the user to refer to pending events too but in
// Actually, we could let the user refer to pending events too but in
// this case we would need a universal pointer instead of event id. Now the
// user cannot start to edit a pending message which might be annoying if
// transactions are acknowledged slowly.
Expand Down Expand Up @@ -876,7 +874,7 @@ void ChatRoomWidget::edit(const QString& eventId)
}

auto htmlText = referencedEventIndex()
.data(MessageEventModel::NudeRichBodyRole)
.data(MessageEventModel::BareRichBodyRole)
.toString();
m_chatEdit->clear();
// We can never be sure which input format was used to build this message.
Expand Down
16 changes: 8 additions & 8 deletions client/models/messageeventmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ QHash<int, QByteArray> MessageEventModel::roleNames() const
roles.insert(EventResolvedTypeRole, "eventResolvedType");
roles.insert(RefRole, "refId");
roles.insert(ReactionsRole, "reactions");
roles.insert(NudeRichBodyRole, "nudeRichBody");
roles.insert(BareRichBodyRole, "bareRichBody");
roles.insert(QuotationRole, "quotation");
roles.insert(HtmlQuotationRole, "htmlQuotation");
return roles;
Expand Down Expand Up @@ -908,7 +908,7 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const
evt, [](const RoomCreateEvent& e) { return e.predecessor().roomId; },
[](const RoomTombstoneEvent& e) { return e.successorRoomId(); });

if (role == NudeRichBodyRole)
if (role == BareRichBodyRole)
{
auto e = eventCast<const Quotient::RoomMessageEvent>(&evt);
if (!e || !e->hasTextContent())
Expand All @@ -919,21 +919,21 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const
QRegularExpression::DotMatchesEverythingOption
};
static const QRegularExpression quoteLines("> .*(?:\n|$)");
QString nudeBody;
QString bareBody;
if (e->mimeType().name() != "text/plain") {
// Naïvely assume that it's HTML
auto htmlBody =
static_cast<const MessageEventContent::TextContent*>(e->content())->body;
auto [cleanHtml, errorPos, errorString] =
HtmlFilter::fromMatrixHtml(htmlBody.remove(quoteBlock), m_currentRoom);
if (errorPos == -1) {
nudeBody = cleanHtml;
bareBody = cleanHtml;
}
}
if (nudeBody.isEmpty()) {
nudeBody = m_currentRoom->prettyPrint(e->plainBody().remove(quoteLines));
if (bareBody.isEmpty()) {
bareBody = m_currentRoom->prettyPrint(e->plainBody().remove(quoteLines));
}
return nudeBody;
return bareBody;
}

if (role == QuotationRole)
Expand All @@ -959,7 +959,7 @@ QVariant MessageEventModel::data(const QModelIndex& idx, int role) const
{
if (isPending)
return QString(); // Cannot construct event link with unknown eventId
QString quotation = data(idx, NudeRichBodyRole).toString();
QString quotation = data(idx, BareRichBodyRole).toString();
if (quotation.isEmpty())
return QString();
const auto authorUser = m_currentRoom->user(evt.senderId());
Expand Down
2 changes: 1 addition & 1 deletion client/models/messageeventmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MessageEventModel: public QAbstractListModel
RefRole,
ReactionsRole,
EventResolvedTypeRole,
NudeRichBodyRole,
BareRichBodyRole,
QuotationRole,
HtmlQuotationRole,
};
Expand Down

0 comments on commit 3c5c3d1

Please sign in to comment.