mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-22 11:00:48 +03:00
Remove redundant user search suggestion code
This commit is contained in:
parent
37df79f796
commit
bc93f24a22
7 changed files with 42 additions and 122 deletions
|
@ -85,8 +85,6 @@ constexpr auto OUTBOUND_MEGOLM_SESSIONS_DB("outbound_megolm_sessions");
|
||||||
using CachedReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
|
using CachedReceipts = std::multimap<uint64_t, std::string, std::greater<uint64_t>>;
|
||||||
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
|
using Receipts = std::map<std::string, std::map<std::string, uint64_t>>;
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(SearchResult)
|
|
||||||
Q_DECLARE_METATYPE(std::vector<SearchResult>)
|
|
||||||
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(RoomSearchResult)
|
||||||
|
@ -2334,39 +2332,6 @@ Cache::searchRooms(const std::string &query, std::uint8_t max_items)
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<SearchResult>
|
|
||||||
Cache::searchUsers(const std::string &room_id, const std::string &query, std::uint8_t max_items)
|
|
||||||
{
|
|
||||||
std::multimap<int, std::pair<std::string, std::string>> items;
|
|
||||||
|
|
||||||
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
|
|
||||||
auto cursor = lmdb::cursor::open(txn, getMembersDb(txn, room_id));
|
|
||||||
|
|
||||||
std::string user_id, user_data;
|
|
||||||
while (cursor.get(user_id, user_data, MDB_NEXT)) {
|
|
||||||
const auto display_name = displayName(room_id, user_id);
|
|
||||||
const int score = utils::levenshtein_distance(query, display_name);
|
|
||||||
|
|
||||||
items.emplace(score, std::make_pair(user_id, display_name));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto end = items.begin();
|
|
||||||
|
|
||||||
if (items.size() >= max_items)
|
|
||||||
std::advance(end, max_items);
|
|
||||||
else if (items.size() > 0)
|
|
||||||
std::advance(end, items.size());
|
|
||||||
|
|
||||||
std::vector<SearchResult> results;
|
|
||||||
for (auto it = items.begin(); it != end; it++) {
|
|
||||||
const auto user = it->second;
|
|
||||||
results.push_back(SearchResult{QString::fromStdString(user.first),
|
|
||||||
QString::fromStdString(user.second)});
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<RoomMember>
|
std::vector<RoomMember>
|
||||||
Cache::getMembers(const std::string &room_id, std::size_t startIndex, std::size_t len)
|
Cache::getMembers(const std::string &room_id, std::size_t startIndex, std::size_t len)
|
||||||
{
|
{
|
||||||
|
@ -3762,8 +3727,6 @@ namespace cache {
|
||||||
void
|
void
|
||||||
init(const QString &user_id)
|
init(const QString &user_id)
|
||||||
{
|
{
|
||||||
qRegisterMetaType<SearchResult>();
|
|
||||||
qRegisterMetaType<std::vector<SearchResult>>();
|
|
||||||
qRegisterMetaType<RoomMember>();
|
qRegisterMetaType<RoomMember>();
|
||||||
qRegisterMetaType<RoomSearchResult>();
|
qRegisterMetaType<RoomSearchResult>();
|
||||||
qRegisterMetaType<RoomInfo>();
|
qRegisterMetaType<RoomInfo>();
|
||||||
|
@ -4075,11 +4038,6 @@ calculateRoomReadStatus()
|
||||||
instance_->calculateRoomReadStatus();
|
instance_->calculateRoomReadStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<SearchResult>
|
|
||||||
searchUsers(const std::string &room_id, const std::string &query, std::uint8_t max_items)
|
|
||||||
{
|
|
||||||
return instance_->searchUsers(room_id, query, max_items);
|
|
||||||
}
|
|
||||||
std::vector<RoomSearchResult>
|
std::vector<RoomSearchResult>
|
||||||
searchRooms(const std::string &query, std::uint8_t max_items)
|
searchRooms(const std::string &query, std::uint8_t max_items)
|
||||||
{
|
{
|
||||||
|
|
|
@ -194,8 +194,6 @@ calculateRoomReadStatus(const std::string &room_id);
|
||||||
void
|
void
|
||||||
calculateRoomReadStatus();
|
calculateRoomReadStatus();
|
||||||
|
|
||||||
std::vector<SearchResult>
|
|
||||||
searchUsers(const std::string &room_id, const std::string &query, std::uint8_t max_items = 5);
|
|
||||||
std::vector<RoomSearchResult>
|
std::vector<RoomSearchResult>
|
||||||
searchRooms(const std::string &query, std::uint8_t max_items = 5);
|
searchRooms(const std::string &query, std::uint8_t max_items = 5);
|
||||||
|
|
||||||
|
|
|
@ -24,12 +24,6 @@ struct RoomMember
|
||||||
QImage avatar;
|
QImage avatar;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SearchResult
|
|
||||||
{
|
|
||||||
QString user_id;
|
|
||||||
QString display_name;
|
|
||||||
};
|
|
||||||
|
|
||||||
//! Used to uniquely identify a list of read receipts.
|
//! Used to uniquely identify a list of read receipts.
|
||||||
struct ReadReceiptKey
|
struct ReadReceiptKey
|
||||||
{
|
{
|
||||||
|
|
|
@ -164,9 +164,6 @@ public:
|
||||||
bool calculateRoomReadStatus(const std::string &room_id);
|
bool calculateRoomReadStatus(const std::string &room_id);
|
||||||
void calculateRoomReadStatus();
|
void calculateRoomReadStatus();
|
||||||
|
|
||||||
std::vector<SearchResult> searchUsers(const std::string &room_id,
|
|
||||||
const std::string &query,
|
|
||||||
std::uint8_t max_items = 5);
|
|
||||||
std::vector<RoomSearchResult> searchRooms(const std::string &query,
|
std::vector<RoomSearchResult> searchRooms(const std::string &query,
|
||||||
std::uint8_t max_items = 5);
|
std::uint8_t max_items = 5);
|
||||||
|
|
||||||
|
|
|
@ -118,7 +118,7 @@ QuickSwitcher::QuickSwitcher(QWidget *parent)
|
||||||
connect(roomSearch_, &RoomSearchInput::hiding, this, [this]() { popup_.hide(); });
|
connect(roomSearch_, &RoomSearchInput::hiding, this, [this]() { popup_.hide(); });
|
||||||
connect(roomSearch_, &QLineEdit::returnPressed, this, [this]() {
|
connect(roomSearch_, &QLineEdit::returnPressed, this, [this]() {
|
||||||
reset();
|
reset();
|
||||||
popup_.selectHoveredSuggestion<RoomItem>();
|
popup_.selectHoveredSuggestion();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
#include "../Utils.h"
|
#include "../Utils.h"
|
||||||
#include "../ui/Avatar.h"
|
#include "../ui/Avatar.h"
|
||||||
#include "../ui/DropShadow.h"
|
#include "../ui/DropShadow.h"
|
||||||
|
#include "ChatPage.h"
|
||||||
|
#include "PopupItem.h"
|
||||||
#include "SuggestionsPopup.h"
|
#include "SuggestionsPopup.h"
|
||||||
|
|
||||||
SuggestionsPopup::SuggestionsPopup(QWidget *parent)
|
SuggestionsPopup::SuggestionsPopup(QWidget *parent)
|
||||||
|
@ -65,44 +67,6 @@ SuggestionsPopup::addRooms(const std::vector<RoomSearchResult> &rooms)
|
||||||
selectNextSuggestion();
|
selectNextSuggestion();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
SuggestionsPopup::addUsers(const std::vector<SearchResult> &users)
|
|
||||||
{
|
|
||||||
if (users.empty()) {
|
|
||||||
hide();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t layoutCount = layout_->count();
|
|
||||||
const size_t userCount = users.size();
|
|
||||||
|
|
||||||
// Remove the extra widgets from the layout.
|
|
||||||
if (userCount < layoutCount)
|
|
||||||
removeLayoutItemsAfter(userCount - 1);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < userCount; ++i) {
|
|
||||||
auto item = layout_->itemAt(i);
|
|
||||||
|
|
||||||
// Create a new widget if there isn't already one in that
|
|
||||||
// layout position.
|
|
||||||
if (!item) {
|
|
||||||
auto user = new UserItem(this, users.at(i).user_id);
|
|
||||||
connect(user, &UserItem::clicked, this, &SuggestionsPopup::itemSelected);
|
|
||||||
layout_->addWidget(user);
|
|
||||||
} else {
|
|
||||||
// Update the current widget with the new data.
|
|
||||||
auto userWidget = qobject_cast<UserItem *>(item->widget());
|
|
||||||
if (userWidget)
|
|
||||||
userWidget->updateItem(users.at(i).user_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resetSelection();
|
|
||||||
adjustSize();
|
|
||||||
|
|
||||||
selectNextSuggestion();
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SuggestionsPopup::hoverSelection()
|
SuggestionsPopup::hoverSelection()
|
||||||
{
|
{
|
||||||
|
@ -111,6 +75,19 @@ SuggestionsPopup::hoverSelection()
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
SuggestionsPopup::selectHoveredSuggestion()
|
||||||
|
{
|
||||||
|
const auto item = layout_->itemAt(selectedItem_);
|
||||||
|
if (!item)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto &widget = qobject_cast<RoomItem *>(item->widget());
|
||||||
|
emit itemSelected(displayName(ChatPage::instance()->currentRoom(), widget->selectedText()));
|
||||||
|
|
||||||
|
resetSelection();
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
SuggestionsPopup::selectNextSuggestion()
|
SuggestionsPopup::selectNextSuggestion()
|
||||||
{
|
{
|
||||||
|
@ -160,3 +137,23 @@ SuggestionsPopup::paintEvent(QPaintEvent *)
|
||||||
QPainter p(this);
|
QPainter p(this);
|
||||||
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
SuggestionsPopup::selectLastItem()
|
||||||
|
{
|
||||||
|
selectedItem_ = layout_->count() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
SuggestionsPopup::removeLayoutItemsAfter(size_t startingPos)
|
||||||
|
{
|
||||||
|
size_t posToRemove = layout_->count() - 1;
|
||||||
|
|
||||||
|
QLayoutItem *item;
|
||||||
|
while (startingPos <= posToRemove && (item = layout_->takeAt(posToRemove)) != nullptr) {
|
||||||
|
delete item->widget();
|
||||||
|
delete item;
|
||||||
|
|
||||||
|
posToRemove = layout_->count() - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,8 +3,9 @@
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
#include "CacheStructs.h"
|
#include "CacheStructs.h"
|
||||||
#include "ChatPage.h"
|
|
||||||
#include "PopupItem.h"
|
class QVBoxLayout;
|
||||||
|
class QLayoutItem;
|
||||||
|
|
||||||
class SuggestionsPopup : public QWidget
|
class SuggestionsPopup : public QWidget
|
||||||
{
|
{
|
||||||
|
@ -13,22 +14,9 @@ class SuggestionsPopup : public QWidget
|
||||||
public:
|
public:
|
||||||
explicit SuggestionsPopup(QWidget *parent = nullptr);
|
explicit SuggestionsPopup(QWidget *parent = nullptr);
|
||||||
|
|
||||||
template<class Item>
|
void selectHoveredSuggestion();
|
||||||
void selectHoveredSuggestion()
|
|
||||||
{
|
|
||||||
const auto item = layout_->itemAt(selectedItem_);
|
|
||||||
if (!item)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const auto &widget = qobject_cast<Item *>(item->widget());
|
|
||||||
emit itemSelected(
|
|
||||||
displayName(ChatPage::instance()->currentRoom(), widget->selectedText()));
|
|
||||||
|
|
||||||
resetSelection();
|
|
||||||
}
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void addUsers(const std::vector<SearchResult> &users);
|
|
||||||
void addRooms(const std::vector<RoomSearchResult> &rooms);
|
void addRooms(const std::vector<RoomSearchResult> &rooms);
|
||||||
|
|
||||||
//! Move to the next available suggestion item.
|
//! Move to the next available suggestion item.
|
||||||
|
@ -51,20 +39,8 @@ private:
|
||||||
void hoverSelection();
|
void hoverSelection();
|
||||||
void resetSelection() { selectedItem_ = -1; }
|
void resetSelection() { selectedItem_ = -1; }
|
||||||
void selectFirstItem() { selectedItem_ = 0; }
|
void selectFirstItem() { selectedItem_ = 0; }
|
||||||
void selectLastItem() { selectedItem_ = layout_->count() - 1; }
|
void selectLastItem();
|
||||||
void removeLayoutItemsAfter(size_t startingPos)
|
void removeLayoutItemsAfter(size_t startingPos);
|
||||||
{
|
|
||||||
size_t posToRemove = layout_->count() - 1;
|
|
||||||
|
|
||||||
QLayoutItem *item;
|
|
||||||
while (startingPos <= posToRemove &&
|
|
||||||
(item = layout_->takeAt(posToRemove)) != nullptr) {
|
|
||||||
delete item->widget();
|
|
||||||
delete item;
|
|
||||||
|
|
||||||
posToRemove = layout_->count() - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QVBoxLayout *layout_;
|
QVBoxLayout *layout_;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue