Skip to content

Commit

Permalink
put ngc history sync into a background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Apr 10, 2023
1 parent 646a2ab commit 2baad9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
17 changes: 13 additions & 4 deletions src/model/chathistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "src/widget/widget.h"
#include "src/widget/form/chatform.h"

#include <thread>

namespace {
/**
* @brief Determines if the given idx needs to be loaded from history
Expand Down Expand Up @@ -297,10 +299,17 @@ void ChatHistory::onGroupSyncHistReqRecv(const ToxPk& sender, int groupnumber, i
std::ignore = peernumber;

if (canUseHistory()) {
auto& chatId = chat.getPersistentId();
qDebug() << "ChatHistory::onGroupSyncHistReqRecv:";
const QDateTime _130_min_back_date = QDateTime::currentDateTime().addSecs(-(130 * 60)); // HINT: max. 130 minutes history
history->getGroupMessagesXMinutesBack(chatId, _130_min_back_date, sender, groupnumber, peernumber);
const ChatId& chatId = chat.getPersistentId();
const QByteArray& chatIdByteArray = chatId.getByteArray();
// const bool isGuiThread = QThread::currentThread() == QCoreApplication::instance()->thread();
// qDebug() << QString("onGroupSyncHistReqRecv:THREAD:008:") << QThread::currentThreadId() << "isGuiThread" << isGuiThread;
auto t_sync_history = [](History* history_, const QByteArray& chatIdByteArray_, const ToxPk& sender_, int groupnumber_, int peernumber_)
{
const QDateTime _130_min_back_date = QDateTime::currentDateTime().addSecs(-(130 * 60)); // HINT: max. 130 minutes history
history_->getGroupMessagesXMinutesBack(chatIdByteArray_, _130_min_back_date, sender_, groupnumber_, peernumber_);
};
std::thread t_it_thread(t_sync_history, history, chatIdByteArray, sender, groupnumber, peernumber);
t_it_thread.detach();
}
}

Expand Down
29 changes: 12 additions & 17 deletions src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ static size_t xnet_pack_u32_hist(uint8_t *bytes, uint32_t v)
return p - bytes;
}

QList<History::HistMessage> History::getGroupMessagesXMinutesBack(const ChatId& chatId, const QDateTime& date,
QList<History::HistMessage> History::getGroupMessagesXMinutesBack(const QByteArray& chatIdByteArray, const QDateTime& date,
const ToxPk& sender, int groupnumber, int peernumber)
{
if (historyAccessBlocked()) {
Expand All @@ -633,8 +633,8 @@ QList<History::HistMessage> History::getGroupMessagesXMinutesBack(const ChatId&
"LEFT JOIN authors ON aliases.owner = authors.id "
"WHERE history.chat_id = ");
QVector<QByteArray> boundParams;
addChatIdSubQuery(queryText, boundParams, chatId);

boundParams.append(chatIdByteArray);
queryText += "(SELECT id FROM chats WHERE uuid = ?)";
queryText += QString(" AND timestamp >= %1").arg(date.toMSecsSinceEpoch());
queryText += QString(" AND text_messages.private = '0'");
queryText += QString(" order by timestamp ASC;");
Expand All @@ -645,7 +645,7 @@ QList<History::HistMessage> History::getGroupMessagesXMinutesBack(const ChatId&

Tox* toxcore = settings.getToxcore();
QList<HistMessage> messages;
auto rowCallback = [&chatId, &messages, &toxcore, groupnumber, peernumber](const QVector<QVariant>& row) {
auto rowCallback = [&toxcore, groupnumber, peernumber](const QVector<QVariant>& row) {
auto it = row.begin();

constexpr auto messageOffset = 3;
Expand Down Expand Up @@ -807,30 +807,25 @@ QList<History::HistMessage> History::getGroupMessagesXMinutesBack(const ChatId&
messageContent.toUtf8().size());
// -----------------------------------------------------------
// now send the whole thing
Tox_Err_Group_Send_Custom_Private_Packet error;
std::ignore = error;

QByteArray data_buf_bytearray = QByteArray(reinterpret_cast<const char*>(data_buf), data_length);
//
// QByteArray data_buf_bytearray = QByteArray(reinterpret_cast<const char*>(data_buf), data_length);
// qDebug() << QString("getGroupMessagesXMinutesBack:send_bytes:") << QString::fromUtf8(data_buf_bytearray.toHex()).toUpper();

if (toxcore != nullptr)
{
const bool isGuiThread3 = QThread::currentThread() == QCoreApplication::instance()->thread();
qDebug() << QString("getGroupMessagesXMinutesBack:THREAD:002:") << QThread::currentThreadId() << "isGuiThread" << isGuiThread3;
// HINT: wait "n" milliseconds before sending the sync message
QThread::msleep(n);
Tox_Err_Group_Send_Custom_Private_Packet error;
std::ignore = error;
int result = tox_group_send_custom_private_packet(toxcore, (groupnumber - Settings::NGC_GROUPNUM_OFFSET),
peernumber, 1, data_buf,
data_length, &error);
std::ignore = result;
// const bool isGuiThread3 = QThread::currentThread() == QCoreApplication::instance()->thread();
// qDebug() << QString("getGroupMessagesXMinutesBack:THREAD:002:") << QThread::currentThreadId() << "isGuiThread" << isGuiThread3;
// qDebug() << QString("getGroupMessagesXMinutesBack:sending_request:groupnumber:") << groupnumber << "groupnumber_corr:" << (groupnumber - Settings::NGC_GROUPNUM_OFFSET);
// qDebug() << QString("getGroupMessagesXMinutesBack:sending_request:result:") << result << "error:" << error;
}
free(data_buf);

if (toxcore != nullptr)
{
// HINT: wait "n" milliseconds before sending the next sync message
QThread::msleep(n);
}
}
else
{
Expand Down
5 changes: 1 addition & 4 deletions src/persistence/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ class History : public QObject, public std::enable_shared_from_this<History>
, content(std::move(message))
, ngcMsgid{ngc_msgid_}
{
// this->ngcMsgid = ngc_msgid_;
// qDebug() << "ngcMsgid:" << ngcMsgid.left(5) << "ngc_msgid_:" << ngc_msgid_.left(5) << "dispName:" << dispName;
}

HistMessage(RowId id_, MessageState state_, QDateTime timestamp_, std::unique_ptr<ChatId> chat_, QString dispName_,
Expand Down Expand Up @@ -248,8 +246,7 @@ class History : public QObject, public std::enable_shared_from_this<History>
size_t getNumMessagesForChat(const ChatId& chatId);
size_t getNumMessagesForChatBeforeDate(const ChatId& chatId, const QDateTime& date);
QList<HistMessage> getMessagesForChat(const ChatId& chatId, size_t firstIdx, size_t lastIdx);
QList<HistMessage> getGroupMessagesXMinutesBack(const ChatId& chatId, const QDateTime& date, const ToxPk& sender, int groupnumber, int peernumber);

QList<HistMessage> getGroupMessagesXMinutesBack(const QByteArray& chatIdByteArray, const QDateTime& date, const ToxPk& sender, int groupnumber, int peernumber);
QList<HistMessage> getUndeliveredMessagesForChat(const ChatId& chatId);
QDateTime getDateWhereFindPhrase(const ChatId& chatId, const QDateTime& from, QString phrase,
const ParameterSearch& parameter);
Expand Down

0 comments on commit 2baad9a

Please sign in to comment.