Commit 388b44e6 authored by LNJ's avatar LNJ 💬

Fix roster sorting

By a mistake the items were not updated correctly. The old
'lastExchanged' datetime was set again instead of the new.

This also removes the ORDER BY clause from the SELECT query in the
RosterDb. Sorting is done completely in the RosterModel now.

There was also a remaining issue with the id being not optional in the
messages table: When updating a message an empty id could still be
inserted. This caused a crash.

Fixes #298.
parent 98d49341
Pipeline #5688 passed with stages
in 45 minutes and 6 seconds
......@@ -108,8 +108,13 @@ QSqlRecord MessageDb::createUpdateRecord(const Message &oldMsg, const Message &n
"timestamp",
newMsg.stamp().toString(Qt::ISODate)
));
if (oldMsg.id() != newMsg.id())
rec.append(Utils::createSqlField("id", newMsg.id()));
if (oldMsg.id() != newMsg.id()) {
// TODO: remove as soon as 'NOT NULL' was removed from id column
if (newMsg.id().isEmpty())
rec.append(Utils::createSqlField("id", QStringLiteral(" ")));
else
rec.append(Utils::createSqlField("id", newMsg.id()));
}
if (oldMsg.body() != newMsg.body())
rec.append(Utils::createSqlField("message", newMsg.body()));
if (oldMsg.isSent() != newMsg.isSent())
......
......@@ -82,13 +82,13 @@ QSqlRecord RosterDb::createUpdateRecord(const RosterItem &oldItem, const RosterI
rec.append(Utils::createSqlField("lastMessage", newItem.lastMessage()));
if (oldItem.lastExchanged() != newItem.lastExchanged())
rec.append(Utils::createSqlField(
"lastExchanged",
oldItem.lastExchanged().toString(Qt::ISODateWithMs)
"lastExchanged",
newItem.lastExchanged().toString(Qt::ISODateWithMs)
));
if (oldItem.unreadMessages() != newItem.unreadMessages())
rec.append(Utils::createSqlField(
"unreadMessages",
newItem.unreadMessages()
"unreadMessages",
newItem.unreadMessages()
));
return rec;
}
......@@ -247,7 +247,7 @@ void RosterDb::fetchItems()
{
QSqlQuery query(QSqlDatabase::database(DB_CONNECTION));
query.setForwardOnly(true);
Utils::execQuery(query, "SELECT * FROM Roster ORDER BY lastExchanged");
Utils::execQuery(query, "SELECT * FROM Roster");
QVector<RosterItem> items;
parseItemsFromQuery(query, items);
......
......@@ -127,6 +127,13 @@ void RosterModel::handleItemsFetched(const QVector<RosterItem> &items)
{
beginResetModel();
m_items = items;
std::sort(
m_items.begin(),
m_items.end(),
[] (const RosterItem &a, const RosterItem &b) {
return a.lastExchanged() > b.lastExchanged();
}
);
endResetModel();
}
......
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