mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(history): represent message state as enum class
ensures that only one state is set at a time, avoids managing multiple bools
This commit is contained in:
parent
f6a15366ef
commit
527c7d2d3c
|
@ -394,9 +394,14 @@ void ChatHistory::loadHistoryIntoSessionChatLog(ChatLogIdx start) const
|
|||
std::find_if(dispatchedMessageRowIdMap.begin(), dispatchedMessageRowIdMap.end(),
|
||||
[&](RowId dispatchedId) { return dispatchedId == message.id; });
|
||||
|
||||
bool isComplete = dispatchedMessageIt == dispatchedMessageRowIdMap.end();
|
||||
auto chatLogMessage = ChatLogMessage{isComplete, processedMessage};
|
||||
if (isComplete) {
|
||||
MessageState messageState;
|
||||
if (dispatchedMessageIt == dispatchedMessageRowIdMap.end()) {
|
||||
messageState = MessageState::complete;
|
||||
} else {
|
||||
messageState = MessageState::pending;
|
||||
}
|
||||
auto chatLogMessage = ChatLogMessage{messageState, processedMessage};
|
||||
if (messageState == MessageState::complete) {
|
||||
sessionChatLog.insertCompleteMessageAtIdx(currentIdx, sender, message.dispName,
|
||||
chatLogMessage);
|
||||
} else {
|
||||
|
|
|
@ -23,13 +23,13 @@
|
|||
#include "src/core/toxfile.h"
|
||||
#include "src/core/toxpk.h"
|
||||
#include "src/model/message.h"
|
||||
#include "src/persistence/history.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct ChatLogMessage
|
||||
{
|
||||
bool isComplete;
|
||||
bool isBroken;
|
||||
MessageState state;
|
||||
Message message;
|
||||
};
|
||||
|
||||
|
|
|
@ -303,7 +303,7 @@ void SessionChatLog::insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sen
|
|||
item.setDisplayName(senderName);
|
||||
}
|
||||
|
||||
assert(message.isComplete == true);
|
||||
assert(message.state == MessageState::complete);
|
||||
|
||||
items.emplace(idx, std::move(item));
|
||||
}
|
||||
|
@ -318,7 +318,7 @@ void SessionChatLog::insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& s
|
|||
item.setDisplayName(senderName);
|
||||
}
|
||||
|
||||
assert(message.isComplete == false);
|
||||
assert(message.state == MessageState::pending);
|
||||
|
||||
items.emplace(idx, std::move(item));
|
||||
outgoingMessages.insert(dispatchId, idx);
|
||||
|
@ -344,7 +344,7 @@ void SessionChatLog::onMessageReceived(const ToxPk& sender, const Message& messa
|
|||
auto messageIdx = nextIdx++;
|
||||
|
||||
ChatLogMessage chatLogMessage;
|
||||
chatLogMessage.isComplete = true;
|
||||
chatLogMessage.state = MessageState::complete;
|
||||
chatLogMessage.message = message;
|
||||
items.emplace(messageIdx, ChatLogItem(sender, chatLogMessage));
|
||||
|
||||
|
@ -360,7 +360,7 @@ void SessionChatLog::onMessageSent(DispatchedMessageId id, const Message& messag
|
|||
auto messageIdx = nextIdx++;
|
||||
|
||||
ChatLogMessage chatLogMessage;
|
||||
chatLogMessage.isComplete = false;
|
||||
chatLogMessage.state = MessageState::pending;
|
||||
chatLogMessage.message = message;
|
||||
items.emplace(messageIdx, ChatLogItem(coreIdHandler.getSelfPublicKey(), chatLogMessage));
|
||||
|
||||
|
@ -390,7 +390,7 @@ void SessionChatLog::onMessageComplete(DispatchedMessageId id)
|
|||
return;
|
||||
}
|
||||
|
||||
messageIt->second.getContentAsMessage().isComplete = true;
|
||||
messageIt->second.getContentAsMessage().state = MessageState::complete;
|
||||
|
||||
emit this->itemUpdated(messageIt->first);
|
||||
}
|
||||
|
|
|
@ -222,6 +222,20 @@ void dbSchemaUpgrade(std::shared_ptr<RawDatabase>& db)
|
|||
<< "->" << SCHEMA_VERSION << ")";
|
||||
}
|
||||
}
|
||||
|
||||
MessageState getMessageState(bool isPending, bool isBroken)
|
||||
{
|
||||
assert(!(isPending && isBroken));
|
||||
MessageState messageState;
|
||||
if (isPending) {
|
||||
messageState = MessageState::pending;
|
||||
} else if (isBroken) {
|
||||
messageState = MessageState::broken;
|
||||
} else {
|
||||
messageState = MessageState::complete;
|
||||
}
|
||||
return messageState;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
/**
|
||||
|
@ -664,8 +678,11 @@ QList<History::HistMessage> History::getMessagesForFriend(const ToxPk& friendPk,
|
|||
auto display_name = QString::fromUtf8(row[4].toByteArray().replace('\0', ""));
|
||||
auto sender_key = row[5].toString();
|
||||
auto isBroken = !row[13].isNull();
|
||||
|
||||
MessageState messageState = getMessageState(isPending, isBroken);
|
||||
|
||||
if (row[7].isNull()) {
|
||||
messages += {id, isPending, isBroken, timestamp, friend_key,
|
||||
messages += {id, messageState, timestamp, friend_key,
|
||||
display_name, sender_key, row[6].toString()};
|
||||
} else {
|
||||
ToxFile file;
|
||||
|
@ -677,7 +694,7 @@ QList<History::HistMessage> History::getMessagesForFriend(const ToxPk& friendPk,
|
|||
file.direction = static_cast<ToxFile::FileDirection>(row[11].toLongLong());
|
||||
file.status = static_cast<ToxFile::FileStatus>(row[12].toInt());
|
||||
messages +=
|
||||
{id, isPending, isBroken, timestamp, friend_key, display_name, sender_key, file};
|
||||
{id, messageState, timestamp, friend_key, display_name, sender_key, file};
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -711,7 +728,10 @@ QList<History::HistMessage> History::getUndeliveredMessagesForFriend(const ToxPk
|
|||
auto display_name = QString::fromUtf8(row[4].toByteArray().replace('\0', ""));
|
||||
auto sender_key = row[5].toString();
|
||||
auto isBroken = !row[7].isNull();
|
||||
ret += {id, isPending, isBroken, timestamp, friend_key,
|
||||
|
||||
MessageState messageState = getMessageState(isPending, isBroken);
|
||||
|
||||
ret += {id, messageState, timestamp, friend_key,
|
||||
display_name, sender_key, row[6].toString()};
|
||||
};
|
||||
|
||||
|
|
|
@ -105,33 +105,38 @@ struct FileDbInsertionData
|
|||
};
|
||||
Q_DECLARE_METATYPE(FileDbInsertionData);
|
||||
|
||||
enum class MessageState
|
||||
{
|
||||
complete,
|
||||
pending,
|
||||
broken
|
||||
};
|
||||
|
||||
class History : public QObject, public std::enable_shared_from_this<History>
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
struct HistMessage
|
||||
{
|
||||
HistMessage(RowId id, bool isPending, bool isBroken, QDateTime timestamp, QString chat, QString dispName,
|
||||
HistMessage(RowId id, MessageState state, QDateTime timestamp, QString chat, QString dispName,
|
||||
QString sender, QString message)
|
||||
: chat{chat}
|
||||
, sender{sender}
|
||||
, dispName{dispName}
|
||||
, timestamp{timestamp}
|
||||
, id{id}
|
||||
, isPending{isPending}
|
||||
, isBroken{isBroken}
|
||||
, state{state}
|
||||
, content(std::move(message))
|
||||
{}
|
||||
|
||||
HistMessage(RowId id, bool isPending, bool isBroken, QDateTime timestamp, QString chat, QString dispName,
|
||||
HistMessage(RowId id, MessageState state, QDateTime timestamp, QString chat, QString dispName,
|
||||
QString sender, ToxFile file)
|
||||
: chat{chat}
|
||||
, sender{sender}
|
||||
, dispName{dispName}
|
||||
, timestamp{timestamp}
|
||||
, id{id}
|
||||
, isPending{isPending}
|
||||
, isBroken{isBroken}
|
||||
, state{state}
|
||||
, content(std::move(file))
|
||||
{}
|
||||
|
||||
|
@ -141,8 +146,7 @@ public:
|
|||
QString dispName;
|
||||
QDateTime timestamp;
|
||||
RowId id;
|
||||
bool isPending;
|
||||
bool isBroken;
|
||||
MessageState state;
|
||||
HistMessageContent content;
|
||||
};
|
||||
|
||||
|
|
|
@ -179,7 +179,7 @@ ChatMessage::Ptr createMessage(const QString& displayName, bool isSelf, bool col
|
|||
}
|
||||
|
||||
// Spinner is displayed by passing in an empty date
|
||||
auto timestamp = chatLogMessage.isComplete ? chatLogMessage.message.timestamp : QDateTime();
|
||||
auto timestamp = chatLogMessage.state == MessageState::complete ? chatLogMessage.message.timestamp : QDateTime();
|
||||
|
||||
return ChatMessage::createChatMessage(displayName, chatLogMessage.message.content, messageType,
|
||||
isSelf, timestamp, colorizeNames);
|
||||
|
@ -190,7 +190,7 @@ void renderMessage(const QString& displayName, bool isSelf, bool colorizeNames,
|
|||
{
|
||||
|
||||
if (chatMessage) {
|
||||
if (chatLogMessage.isComplete) {
|
||||
if (chatLogMessage.state == MessageState::complete) {
|
||||
chatMessage->markAsDelivered(chatLogMessage.message.timestamp);
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue
Block a user