Speedup quick switcher

This commit is contained in:
Nicolas Werner 2023-01-07 17:39:32 +01:00
parent 82e5fa1b45
commit fd949f0b83
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
5 changed files with 57 additions and 38 deletions

View file

@ -99,7 +99,6 @@ using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
Q_DECLARE_METATYPE(RoomMember) Q_DECLARE_METATYPE(RoomMember)
Q_DECLARE_METATYPE(mtx::responses::Timeline) Q_DECLARE_METATYPE(mtx::responses::Timeline)
Q_DECLARE_METATYPE(RoomSearchResult)
Q_DECLARE_METATYPE(RoomInfo) Q_DECLARE_METATYPE(RoomInfo)
Q_DECLARE_METATYPE(mtx::responses::QueryKeys) Q_DECLARE_METATYPE(mtx::responses::QueryKeys)
@ -2451,6 +2450,39 @@ Cache::roomInfo(bool withInvites)
return result; return result;
} }
std::vector<RoomNameAlias>
Cache::roomNamesAndAliases()
{
auto txn = ro_txn(env_);
std::vector<RoomNameAlias> result;
result.reserve(roomsDb_.size(txn));
std::string_view room_id;
std::string_view room_data;
auto roomsCursor = lmdb::cursor::open(txn, roomsDb_);
while (roomsCursor.get(room_id, room_data, MDB_NEXT)) {
try {
std::string room_id_str = std::string(room_id);
RoomInfo info = nlohmann::json::parse(std::move(room_data)).get<RoomInfo>();
auto aliases = getStateEvent<mtx::events::state::CanonicalAlias>(txn, room_id_str);
std::string alias;
if (aliases) {
alias = aliases->content.alias;
}
result.push_back(RoomNameAlias{.id = std::move(room_id_str),
.name = std::move(info.name),
.alias = std::move(alias)});
} catch (std::exception &e) {
nhlog::db()->warn("Failed to add room {} to result: {}", room_id, e.what());
}
}
return result;
}
std::string std::string
Cache::getLastEventId(lmdb::txn &txn, const std::string &room_id) Cache::getLastEventId(lmdb::txn &txn, const std::string &room_id)
{ {
@ -5136,7 +5168,6 @@ void
init(const QString &user_id) init(const QString &user_id)
{ {
qRegisterMetaType<RoomMember>(); qRegisterMetaType<RoomMember>();
qRegisterMetaType<RoomSearchResult>();
qRegisterMetaType<RoomInfo>(); qRegisterMetaType<RoomInfo>();
qRegisterMetaType<QMap<QString, RoomInfo>>(); qRegisterMetaType<QMap<QString, RoomInfo>>();
qRegisterMetaType<std::map<QString, RoomInfo>>(); qRegisterMetaType<std::map<QString, RoomInfo>>();

View file

@ -103,6 +103,12 @@ to_json(nlohmann::json &j, const RoomInfo &info);
void void
from_json(const nlohmann::json &j, RoomInfo &info); from_json(const nlohmann::json &j, RoomInfo &info);
//! A plain struct with roomid, name and alias used for filling the room completer.
struct RoomNameAlias
{
std::string id, name, alias;
};
//! Basic information per member. //! Basic information per member.
struct MemberInfo struct MemberInfo
{ {

View file

@ -167,6 +167,9 @@ public:
RoomInfo singleRoomInfo(const std::string &room_id); RoomInfo singleRoomInfo(const std::string &room_id);
std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res); std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res);
std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms); std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms);
std::vector<RoomNameAlias> roomNamesAndAliases();
void updateLastMessageTimestamp(const std::string &room_id, uint64_t ts); void updateLastMessageTimestamp(const std::string &room_id, uint64_t ts);
//! Calculates which the read status of a room. //! Calculates which the read status of a room.

View file

@ -12,34 +12,16 @@
#include "Cache_p.h" #include "Cache_p.h"
#include "CompletionModelRoles.h" #include "CompletionModelRoles.h"
#include "UserSettingsPage.h" #include "UserSettingsPage.h"
#include "Utils.h"
RoomsModel::RoomsModel(bool showOnlyRoomWithAliases, QObject *parent) RoomsModel::RoomsModel(bool showOnlyRoomWithAliases, QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
, showOnlyRoomWithAliases_(showOnlyRoomWithAliases) , showOnlyRoomWithAliases_(showOnlyRoomWithAliases)
{ {
std::vector<std::string> rooms_ = cache::joinedRooms(); rooms = cache::client()->roomNamesAndAliases();
roomInfos = cache::getRoomInfo(rooms_);
if (!showOnlyRoomWithAliases_) {
roomids.reserve(rooms_.size());
roomAliases.reserve(rooms_.size());
}
for (const auto &r : rooms_) { if (showOnlyRoomWithAliases_)
auto roomAliasesList = utils::erase_if(rooms, [](auto &r) { return r.alias.empty(); });
cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>(r);
if (showOnlyRoomWithAliases_) {
if (roomAliasesList && !roomAliasesList->content.alias.empty()) {
roomids.push_back(QString::fromStdString(r));
roomAliases.push_back(QString::fromStdString(roomAliasesList->content.alias));
}
} else {
roomids.push_back(QString::fromStdString(r));
roomAliases.push_back(roomAliasesList
? QString::fromStdString(roomAliasesList->content.alias)
: QLatin1String(""));
}
}
} }
QHash<int, QByteArray> QHash<int, QByteArray>
@ -60,29 +42,28 @@ RoomsModel::data(const QModelIndex &index, int role) const
if (hasIndex(index.row(), index.column(), index.parent())) { if (hasIndex(index.row(), index.column(), index.parent())) {
switch (role) { switch (role) {
case CompletionModel::CompletionRole: { case CompletionModel::CompletionRole: {
auto alias = QString::fromStdString(rooms[index.row()].alias);
if (UserSettings::instance()->markdown()) { if (UserSettings::instance()->markdown()) {
QString percentEncoding = QUrl::toPercentEncoding(roomAliases[index.row()]); QString percentEncoding = QUrl::toPercentEncoding(alias);
return QStringLiteral("[%1](https://matrix.to/#/%2)") return QStringLiteral("[%1](https://matrix.to/#/%2)")
.arg(QString(roomAliases[index.row()]) .arg(alias.replace("[", "\\[").replace("]", "\\]").toHtmlEscaped(),
.replace("[", "\\[")
.replace("]", "\\]")
.toHtmlEscaped(),
percentEncoding); percentEncoding);
} else { } else {
return roomAliases[index.row()]; return alias;
} }
} }
case CompletionModel::SearchRole: case CompletionModel::SearchRole:
case Qt::DisplayRole: case Qt::DisplayRole:
case Roles::RoomAlias: case Roles::RoomAlias:
return roomAliases[index.row()].toHtmlEscaped(); return QString::fromStdString(rooms[index.row()].alias).toHtmlEscaped();
case CompletionModel::SearchRole2: case CompletionModel::SearchRole2:
case Roles::RoomName: case Roles::RoomName:
return QString::fromStdString(roomInfos.at(roomids[index.row()]).name).toHtmlEscaped(); return QString::fromStdString(rooms[index.row()].name);
case Roles::AvatarUrl: case Roles::AvatarUrl:
return QString::fromStdString(roomInfos.at(roomids[index.row()]).avatar_url); return QString::fromStdString(
cache::client()->singleRoomInfo(rooms[index.row()].id).avatar_url);
case Roles::RoomID: case Roles::RoomID:
return roomids[index.row()].toHtmlEscaped(); return QString::fromStdString(rooms[index.row()].id).toHtmlEscaped();
} }
} }
return {}; return {};

View file

@ -27,13 +27,11 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override int rowCount(const QModelIndex &parent = QModelIndex()) const override
{ {
(void)parent; (void)parent;
return (int)roomids.size(); return (int)rooms.size();
} }
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
private: private:
std::vector<QString> roomids; std::vector<RoomNameAlias> rooms;
std::vector<QString> roomAliases;
std::map<QString, RoomInfo> roomInfos;
bool showOnlyRoomWithAliases_; bool showOnlyRoomWithAliases_;
}; };