1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(core): Remove length parameter from splitMessages

* From the archeology I managed, MAX_GROUP_MESSAGE_LEN was a workaround
for an old toxcore bug. Testing removal of this parameter and replacing it
with tox_max_message_length() seems to work fine.
This commit is contained in:
Mick Sayson 2019-06-20 08:37:26 -07:00
parent 22a4c38bfd
commit 3fd4ce5952
3 changed files with 19 additions and 15 deletions

View File

@ -44,8 +44,6 @@
const QString Core::TOX_EXT = ".tox"; const QString Core::TOX_EXT = ".tox";
#define MAX_GROUP_MESSAGE_LEN 1024
#define ASSERT_CORE_THREAD assert(QThread::currentThread() == coreThread.get()) #define ASSERT_CORE_THREAD assert(QThread::currentThread() == coreThread.get())
namespace { namespace {
@ -715,17 +713,21 @@ void Core::sendGroupMessageWithType(int groupId, const QString& message, Tox_Mes
{ {
QMutexLocker ml{&coreLoopLock}; QMutexLocker ml{&coreLoopLock};
QStringList cMessages = splitMessage(message, MAX_GROUP_MESSAGE_LEN); int size = message.toUtf8().size();
auto maxSize = tox_max_message_length();
if (size > maxSize) {
qCritical() << "Core::sendMessageWithType called with message of size:" << size
<< "when max is:" << maxSize << ". Ignoring.";
return;
}
for (auto& part : cMessages) { ToxString cMsg(message);
ToxString cMsg(part); Tox_Err_Conference_Send_Message error;
Tox_Err_Conference_Send_Message error; bool ok =
bool ok = tox_conference_send_message(tox.get(), groupId, type, cMsg.data(), cMsg.size(), &error);
tox_conference_send_message(tox.get(), groupId, type, cMsg.data(), cMsg.size(), &error); if (!ok || !parseConferenceSendMessageError(error)) {
if (!ok || !parseConferenceSendMessageError(error)) { emit groupSentFailed(groupId);
emit groupSentFailed(groupId); return;
return;
}
} }
} }
@ -1434,11 +1436,13 @@ QString Core::getFriendUsername(uint32_t friendnumber) const
return sname.getQString(); return sname.getQString();
} }
QStringList Core::splitMessage(const QString& message, int maxLen) QStringList Core::splitMessage(const QString& message)
{ {
QStringList splittedMsgs; QStringList splittedMsgs;
QByteArray ba_message{message.toUtf8()}; QByteArray ba_message{message.toUtf8()};
const auto maxLen = tox_max_message_length();
while (ba_message.size() > maxLen) { while (ba_message.size() > maxLen) {
int splitPos = ba_message.lastIndexOf('\n', maxLen - 1); int splitPos = ba_message.lastIndexOf('\n', maxLen - 1);

View File

@ -71,7 +71,7 @@ public:
~Core(); ~Core();
static const QString TOX_EXT; static const QString TOX_EXT;
static QStringList splitMessage(const QString& message, int maxLen); static QStringList splitMessage(const QString& message);
QString getPeerName(const ToxPk& id) const; QString getPeerName(const ToxPk& id) const;
QVector<uint32_t> getFriendList() const; QVector<uint32_t> getFriendList() const;
GroupId getGroupPersistentId(uint32_t groupNumber) const; GroupId getGroupPersistentId(uint32_t groupNumber) const;

View File

@ -24,7 +24,7 @@ std::vector<Message> processOutgoingMessage(bool isAction, const QString& conten
{ {
std::vector<Message> ret; std::vector<Message> ret;
QStringList splitMsgs = Core::splitMessage(content, tox_max_message_length()); QStringList splitMsgs = Core::splitMessage(content);
ret.reserve(splitMsgs.size()); ret.reserve(splitMsgs.size());
QDateTime timestamp = QDateTime::currentDateTime(); QDateTime timestamp = QDateTime::currentDateTime();