diff --git a/src/libclient/api/interaction.h b/src/libclient/api/interaction.h index f4ff6074c642e986c153b052cd0c1335c988e71c..5c184305ee52941af5bd4f7c4c3011a48381bfc7 100644 --- a/src/libclient/api/interaction.h +++ b/src/libclient/api/interaction.h @@ -20,9 +20,11 @@ #include <QString> #include <QObject> +#include <QFileInfo> #include <ctime> #include "typedefs.h" +#include "../dbus/configurationmanager.h" namespace lrc { @@ -454,7 +456,10 @@ struct Info return status == Status::SUCCESS || status == Status::DISPLAYED; } - void init(const MapStringString& message, const QString& accountURI) + void init(const MapStringString& message, + const QString& accountURI, + const QString& accountId, + const QString& conversationId) { type = to_type(message["type"]); if (message.contains("react-to") && type == Type::TEXT) { @@ -482,16 +487,42 @@ struct Info duration = message["duration"].toInt() / 1000; if (message.contains("confId")) confId = message["confId"]; + } else if (type == Type::DATA_TRANSFER) { + QString path; + qlonglong bytesProgress, totalSize; + ConfigurationManager::instance().fileTransferInfo(accountId, + conversationId, + message["fileId"], + path, + totalSize, + bytesProgress); + QFileInfo fi(path); + body = fi.isSymLink() ? fi.symLinkTarget() : path; + transferStatus = bytesProgress == 0 ? TransferStatus::TRANSFER_AWAITING_HOST + : bytesProgress == totalSize ? TransferStatus::TRANSFER_FINISHED + : TransferStatus::TRANSFER_ONGOING; + } commit = message; } - Info(const MapStringString& message, const QString& accountURI) + // NOTE: The `accountId` and `conversationId` arguments are only used for messages of + // type DATA_TRANSFER. They can therefore be omitted if the caller knows that `message` + // is of a different type. They must be provided otherwise, as failure to do so would + // result in the `body` and `transferStatus` fields of the returned Info struct to + // contain incorrect information whenever `message` is of type DATA_TRANSFER. + Info(const MapStringString& message, + const QString& accountURI, + const QString& accountId = "", + const QString& conversationId = "") { - init(message, accountURI); + init(message, accountURI, accountId, conversationId); } - Info(const SwarmMessage& msg, const QString& accountUri) + Info(const SwarmMessage& msg, + const QString& accountUri, + const QString& accountId, + const QString& conversationId) { MapStringString msgBody; for (auto it = msg.body.cbegin(); it != msg.body.cend(); ++it) { @@ -499,7 +530,7 @@ struct Info const auto& value = it.value(); msgBody.insert(key, value); } - init(msgBody, accountUri); + init(msgBody, accountUri, accountId, conversationId); parentId = msg.linearizedParent; type = to_type(msg.type); for (const auto& edition : msg.editions) diff --git a/src/libclient/conversationmodel.cpp b/src/libclient/conversationmodel.cpp index bfcf74251e2a7cf8bc3ebf6240d29e0ee5cd44e3..6c182309e4bd419ad403e0757083de526ab5a419 100644 --- a/src/libclient/conversationmodel.cpp +++ b/src/libclient/conversationmodel.cpp @@ -2285,31 +2285,14 @@ ConversationModelPimpl::slotSwarmLoaded(uint32_t requestId, auto& conversation = getConversationForUid(conversationId).get(); for (const auto& message : messages) { QString msgId = message.id; - auto msg = interaction::Info(message, linked.owner.profileInfo.uri); + auto msg = interaction::Info(message, linked.owner.profileInfo.uri, accountId, conversationId); auto downloadFile = false; if (msg.type == interaction::Type::INITIAL) { allLoaded = true; } else if (msg.type == interaction::Type::DATA_TRANSFER) { QString fileId = message.body.value("fileId"); - QString path; - qlonglong bytesProgress, totalSize; - linked.owner.dataTransferModel->fileTransferInfo(accountId, - conversationId, - fileId, - path, - totalSize, - bytesProgress); - QFileInfo fi(path); - if (fi.isSymLink()) { - msg.body = fi.symLinkTarget(); - } else { - msg.body = path; - } - msg.transferStatus = bytesProgress == 0 ? interaction::TransferStatus::TRANSFER_AWAITING_HOST - : bytesProgress == totalSize ? interaction::TransferStatus::TRANSFER_FINISHED - : interaction::TransferStatus::TRANSFER_ONGOING; linked.owner.dataTransferModel->registerTransferId(fileId, msgId); - downloadFile = (bytesProgress == 0); + downloadFile = (msg.transferStatus == interaction::TransferStatus::TRANSFER_AWAITING_HOST); } // If message is loaded, insert message at beginning @@ -2351,25 +2334,12 @@ ConversationModelPimpl::slotMessagesFound(uint32_t requestId, QMap<QString, interaction::Info> messageDetailedInformation; if (requestId == mediaResearchRequestId) { Q_FOREACH (const MapStringString& msg, messageIds) { - auto intInfo = interaction::Info(msg, ""); - if (intInfo.type == interaction::Type::DATA_TRANSFER) { - auto fileId = msg["fileId"]; - - QString path; - qlonglong bytesProgress, totalSize; - linked.owner.dataTransferModel->fileTransferInfo(accountId, - conversationId, - fileId, - path, - totalSize, - bytesProgress); - intInfo.body = path; - } + auto intInfo = interaction::Info(msg, "", accountId, conversationId); messageDetailedInformation[msg["id"]] = std::move(intInfo); } } else if (requestId == msgResearchRequestId) { Q_FOREACH (const MapStringString& msg, messageIds) { - auto intInfo = interaction::Info(msg, ""); + auto intInfo = interaction::Info(msg, "", accountId, conversationId); if (intInfo.type == interaction::Type::TEXT) { messageDetailedInformation[msg["id"]] = std::move(intInfo); } @@ -2395,33 +2365,14 @@ ConversationModelPimpl::slotMessageReceived(const QString& accountId, } } QString msgId = message.id; - auto msg = interaction::Info(message, linked.owner.profileInfo.uri); + auto msg = interaction::Info(message, linked.owner.profileInfo.uri, accountId, conversationId); if (msg.type == interaction::Type::CALL) { msg.body = interaction::getCallInteractionString(msg.authorUri == linked.owner.profileInfo.uri, msg); } else if (msg.type == interaction::Type::DATA_TRANSFER) { - // save data transfer interaction to db and assosiate daemon id with interaction id, - // conversation id and db id QString fileId = message.body.value("fileId"); - QString path; - qlonglong bytesProgress, totalSize; - linked.owner.dataTransferModel->fileTransferInfo(accountId, - conversationId, - fileId, - path, - totalSize, - bytesProgress); - QFileInfo fi(path); - if (fi.isSymLink()) { - msg.body = fi.symLinkTarget(); - } else { - msg.body = path; - } - msg.transferStatus = bytesProgress == 0 ? interaction::TransferStatus::TRANSFER_AWAITING_HOST - : bytesProgress == totalSize ? interaction::TransferStatus::TRANSFER_FINISHED - : interaction::TransferStatus::TRANSFER_ONGOING; linked.owner.dataTransferModel->registerTransferId(fileId, msgId); } @@ -2473,7 +2424,7 @@ ConversationModelPimpl::slotMessageUpdated(const QString& accountId, try { auto& conversation = getConversationForUid(conversationId).get(); QString msgId = message.id; - auto msg = interaction::Info(message, linked.owner.profileInfo.uri); + auto msg = interaction::Info(message, linked.owner.profileInfo.uri, accountId, conversationId); if (!conversation.interactions->update(msgId, msg)) { qDebug() << "Message not found or cannot be reparented."; diff --git a/src/libclient/messagelistmodel.cpp b/src/libclient/messagelistmodel.cpp index 75c4299f3a22a11d30d7d118d0cc3616a2c394dd..be4ae6c49147d0a4a5bb20a860f0bcdb2c41b5ba 100644 --- a/src/libclient/messagelistmodel.cpp +++ b/src/libclient/messagelistmodel.cpp @@ -201,12 +201,7 @@ MessageListModel::update(const QString& id, const interaction::Info& interaction return true; } } - // TODO: look into why this update with an empty body is broadcasted just - // after loading the messages. This is a workaround to avoid the empty - // file transfer path. Until then, don't update the body if it's empty. - if (!interaction.body.isEmpty()) { - current.body = interaction.body; - } + current.body = interaction.body; current.commit = interaction.commit; current.previousBodies = interaction.previousBodies; current.parsedBody = interaction.parsedBody;