Verified Commit 2e36f9fa authored by Jonah Brüchert's avatar Jonah Brüchert 🌳 Committed by Linus Jahn

MessageModel: Truncate messages to 10k characters

Fixes #350.
Signed-off-by: Linus Jahn's avatarLinus Jahn <lnj@kaidan.im>
parent b4f7e1fc
Pipeline #14318 passed with stages
in 24 minutes and 35 seconds
......@@ -42,6 +42,8 @@
#define INVITATION_URL "https://i.kaidan.im/#"
#define APPLICATION_SOURCE_CODE_URL "https://invent.kde.org/kde/kaidan"
#define MESSAGE_MAX_CHARS 1e4
// XML namespaces
#define NS_SPOILERS "urn:xmpp:spoiler:0"
#define NS_CARBONS "urn:xmpp:carbons:2"
......
......@@ -198,6 +198,7 @@ void MessageModel::handleMessagesFetched(const QVector<Message> &msgs)
beginInsertRows(QModelIndex(), rowCount(), rowCount() + msgs.length() - 1);
for (auto msg : msgs) {
msg.setSentByMe(kaidan->getJid() == msg.from());
processMessage(msg);
m_messages << msg;
}
endInsertRows();
......@@ -222,10 +223,13 @@ void MessageModel::insertMessage(int idx, const Message &msg)
endInsertRows();
}
void MessageModel::addMessage(const Message &msg)
void MessageModel::addMessage(Message msg)
{
if (QXmppUtils::jidToBareJid(msg.from()) == m_chatPartner
|| QXmppUtils::jidToBareJid(msg.to()) == m_chatPartner) {
processMessage(msg);
// index where to add the new message
int i = 0;
for (const auto &message : qAsConst(m_messages)) {
......@@ -318,3 +322,12 @@ int MessageModel::searchForMessageFromOldToNew(const QString &searchString, cons
return indexOfFoundMessage;
}
void MessageModel::processMessage(Message &msg)
{
if (msg.body().size() > MESSAGE_MAX_CHARS) {
auto body = msg.body();
body.truncate(MESSAGE_MAX_CHARS);
msg.setBody(body);
}
}
......@@ -118,7 +118,7 @@ signals:
private slots:
void handleMessagesFetched(const QVector<Message> &m_messages);
void addMessage(const Message &msg);
void addMessage(Message msg);
void updateMessage(const QString &id,
const std::function<void (Message &)> &updateMsg);
void setMessageAsSent(const QString &msgId);
......@@ -128,6 +128,12 @@ private:
void clearAll();
void insertMessage(int i, const Message &msg);
/**
* Shortens messages to 10000 if longer to prevent DoS
* @param message to process
*/
void processMessage(Message &msg);
Kaidan *kaidan;
MessageDb *msgDb;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment