2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Append server-side topic search results.

This commit is contained in:
John Preston
2022-10-26 18:18:00 +04:00
parent d6ee5b3456
commit 60aef7871a
9 changed files with 224 additions and 73 deletions

View File

@@ -2061,6 +2061,18 @@ void InnerWidget::onHashtagFilterUpdate(QStringView newFilter) {
clearMouseSelection(true);
}
void InnerWidget::appendToFiltered(Key key) {
for (const auto &row : _filterResults) {
if (row->key() == key) {
return;
}
}
auto row = std::make_unique<Row>(key, _filterResults.size());
const auto [i, ok] = _filterResultsGlobal.emplace(key, std::move(row));
_filterResults.emplace_back(i->second.get());
trackSearchResultsHistory(key.owningHistory());
}
InnerWidget::~InnerWidget() {
clearSearchResults();
}
@@ -2069,10 +2081,14 @@ void InnerWidget::clearSearchResults(bool clearPeerSearchResults) {
if (clearPeerSearchResults) _peerSearchResults.clear();
_searchResults.clear();
_searchResultsLifetime.destroy();
_searchResultsHistories.clear();
_searchedCount = _searchedMigratedCount = 0;
}
void InnerWidget::trackSearchResultsHistory(not_null<History*> history) {
if (!_searchResultsHistories.emplace(history).second) {
return;
}
const auto channel = history->peer->asChannel();
if (!channel || channel->isBroadcast()) {
return;
@@ -2088,19 +2104,51 @@ void InnerWidget::trackSearchResultsHistory(not_null<History*> history) {
row->invalidateTopic();
}
}
auto removed = false;
for (auto i = begin(_filterResultsGlobal)
; i != end(_filterResultsGlobal);) {
if (const auto topic = i->first.topic()) {
if (topic->channel() == channel) {
removed = true;
_filterResults.erase(
ranges::remove(_filterResults, i->first, &Row::key),
end(_filterResults));
i = _filterResultsGlobal.erase(i);
continue;
}
}
++i;
}
if (removed) {
refresh();
clearMouseSelection(true);
}
update();
}, _searchResultsLifetime);
if (const auto forum = channel->forum()) {
forum->topicDestroyed(
) | rpl::start_with_next([=](not_null<Data::ForumTopic*> topic) {
const auto from = ranges::remove(
auto removed = false;
const auto sfrom = ranges::remove(
_searchResults,
topic.get(),
&FakeRow::topic);
if (from != end(_searchResults)) {
_searchResults.erase(from, end(_searchResults));
refresh(true);
if (sfrom != end(_searchResults)) {
_searchResults.erase(sfrom, end(_searchResults));
removed = true;
}
const auto ffrom = ranges::remove(
_filterResults,
topic.get(),
&Row::topic);
if (ffrom != end(_filterResults)) {
_filterResults.erase(ffrom, end(_filterResults));
removed = true;
}
_filterResultsGlobal.erase(Key(topic));
if (removed) {
refresh();
clearMouseSelection(true);
}
}, _searchResultsLifetime);
@@ -2133,6 +2181,10 @@ void InnerWidget::setLoadMoreCallback(Fn<void()> callback) {
_loadMoreCallback = std::move(callback);
}
void InnerWidget::setLoadMoreFilteredCallback(Fn<void()> callback) {
_loadMoreFilteredCallback = std::move(callback);
}
auto InnerWidget::chosenRow() const -> rpl::producer<ChosenRow> {
return _chosenRow.events();
}
@@ -2183,8 +2235,14 @@ void InnerWidget::visibleTopBottomUpdated(
_visibleTop = visibleTop;
_visibleBottom = visibleBottom;
loadPeerPhotos();
if (_visibleTop + PreloadHeightsCount * (_visibleBottom - _visibleTop)
>= height()) {
const auto loadTill = _visibleTop
+ PreloadHeightsCount * (_visibleBottom - _visibleTop);
if (_state == WidgetState::Filtered && loadTill >= peerSearchOffset()) {
if (_loadMoreFilteredCallback) {
_loadMoreFilteredCallback();
}
}
if (loadTill >= height()) {
if (_loadMoreCallback) {
_loadMoreCallback();
}
@@ -2310,31 +2368,12 @@ void InnerWidget::peerSearchReceived(
return;
}
const auto alreadyAdded = [&](not_null<PeerData*> peer) {
for (const auto &row : _filterResults) {
if (const auto history = row->history()) {
if (history->peer == peer) {
return true;
}
}
}
return false;
};
_peerSearchQuery = query.toLower().trimmed();
_peerSearchResults.clear();
_peerSearchResults.reserve(result.size());
for (const auto &mtpPeer : my) {
if (const auto peer = session().data().peerLoaded(peerFromMTP(mtpPeer))) {
if (alreadyAdded(peer)) {
continue;
}
auto row = std::make_unique<Row>(
peer->owner().history(peer),
_filterResults.size());
const auto [i, ok] = _filterResultsGlobal.emplace(
peer,
std::move(row));
_filterResults.emplace_back(i->second.get());
appendToFiltered(peer->owner().history(peer));
} else {
LOG(("API Error: "
"user %1 was not loaded in InnerWidget::peopleReceived()"

View File

@@ -131,10 +131,12 @@ public:
void applyFilterUpdate(QString newFilter, bool force = false);
void onHashtagFilterUpdate(QStringView newFilter);
void appendToFiltered(Key key);
PeerData *updateFromParentDrag(QPoint globalPosition);
void setLoadMoreCallback(Fn<void()> callback);
void setLoadMoreFilteredCallback(Fn<void()> callback);
[[nodiscard]] rpl::producer<> listBottomReached() const;
[[nodiscard]] rpl::producer<> cancelSearchFromUserRequests() const;
[[nodiscard]] rpl::producer<ChosenRow> chosenRow() const;
@@ -389,9 +391,7 @@ private:
bool _hashtagDeletePressed = false;
std::vector<not_null<Row*>> _filterResults;
base::flat_map<
not_null<PeerData*>,
std::unique_ptr<Row>> _filterResultsGlobal;
base::flat_map<Key, std::unique_ptr<Row>> _filterResultsGlobal;
int _filteredSelected = -1;
int _filteredPressed = -1;
@@ -404,6 +404,7 @@ private:
int _peerSearchPressed = -1;
std::vector<std::unique_ptr<FakeRow>> _searchResults;
base::flat_set<not_null<History*>> _searchResultsHistories;
rpl::lifetime _searchResultsLifetime;
int _searchedCount = 0;
int _searchedMigratedCount = 0;
@@ -432,6 +433,7 @@ private:
std::unique_ptr<Ui::VideoUserpic>> _videoUserpics;
Fn<void()> _loadMoreCallback;
Fn<void()> _loadMoreFilteredCallback;
rpl::event_stream<> _listBottomReached;
rpl::event_stream<ChosenRow> _chosenRow;
rpl::event_stream<> _updated;

View File

@@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "dialogs/dialogs_key.h"
#include "dialogs/dialogs_entry.h"
#include "history/history.h"
#include "history/history_item.h"
#include "history/view/history_view_top_bar_widget.h"
#include "history/view/history_view_contact_status.h"
#include "history/view/history_view_requests_bar.h"
@@ -350,6 +351,14 @@ Widget::Widget(
setAcceptDrops(true);
_inner->setLoadMoreFilteredCallback([=] {
const auto state = _inner->state();
if (state == WidgetState::Filtered
&& !_topicSearchFull
&& searchForTopicsRequired(_topicSearchQuery)) {
searchTopics();
}
});
_inner->setLoadMoreCallback([=] {
const auto state = _inner->state();
if (state == WidgetState::Filtered
@@ -358,7 +367,7 @@ Widget::Widget(
&& _searchFull
&& !_searchFullMigrated))) {
searchMore();
} else if (_openedForum) {
} else if (_openedForum && state == WidgetState::Default) {
_openedForum->forum()->requestTopics();
} else {
const auto folder = _inner->shownFolder();
@@ -728,6 +737,7 @@ void Widget::changeOpenedSubsection(
refreshTopBars();
updateControlsVisibility(true);
_peerSearchRequest = 0;
_api.request(base::take(_topicSearchRequest)).cancel();
if (animated == anim::type::normal) {
_connecting->setForceHidden(true);
_cacheOver = grabForFolderSlideAnimation();
@@ -749,6 +759,7 @@ void Widget::changeOpenedForum(ChannelData *forum, anim::type animated) {
cancelSearch();
}
_openedForum = forum;
_api.request(base::take(_topicSearchRequest)).cancel();
_inner->changeOpenedForum(forum);
}, (forum != nullptr), animated);
}
@@ -1157,6 +1168,7 @@ bool Widget::searchMessages(bool searchCache) {
if (q.isEmpty() && !_searchFromAuthor) {
cancelSearchRequest();
_api.request(base::take(_peerSearchRequest)).cancel();
_api.request(base::take(_topicSearchRequest)).cancel();
return true;
}
if (searchCache) {
@@ -1284,14 +1296,36 @@ bool Widget::searchMessages(bool searchCache) {
MTP_vector<MTPUser>(0)),
0);
}
if (searchForTopicsRequired(query)) {
if (searchCache) {
if (_topicSearchQuery != query) {
result = false;
}
} else if (_topicSearchQuery != query) {
_topicSearchQuery = query;
_topicSearchFull = false;
searchTopics();
}
} else {
_topicSearchQuery = query;
_topicSearchFull = true;
}
return result;
}
bool Widget::searchForPeersRequired(const QString &query) const {
if (_searchInChat || _openedForum || query.isEmpty()) {
return false;
}
return (query[0] != '#');
return !_searchInChat
&& !_openedForum
&& !query.isEmpty()
&& (query[0] != '#');
}
bool Widget::searchForTopicsRequired(const QString &query) const {
return !_searchInChat
&& _openedForum
&& !query.isEmpty()
&& (query[0] != '#')
&& !_openedForum->forum()->topicsList()->loaded();
}
void Widget::needSearchMessages() {
@@ -1337,6 +1371,45 @@ void Widget::searchMessages(
}
}
void Widget::searchTopics() {
if (_topicSearchRequest || _topicSearchFull) {
return;
}
_api.request(base::take(_topicSearchRequest)).cancel();
_topicSearchRequest = _api.request(MTPchannels_GetForumTopics(
MTP_flags(MTPchannels_GetForumTopics::Flag::f_q),
_openedForum->inputChannel,
MTP_string(_topicSearchQuery),
MTP_int(_topicSearchOffsetDate),
MTP_int(_topicSearchOffsetId),
MTP_int(_topicSearchOffsetTopicId),
MTP_int(kSearchPerPage)
)).done([=](const MTPmessages_ForumTopics &result) {
_topicSearchRequest = 0;
const auto savedTopicId = _topicSearchOffsetId;
const auto byCreation = result.data().is_order_by_create_date();
_openedForum->forum()->applyReceivedTopics(result, [&](
not_null<Data::ForumTopic*> topic) {
_topicSearchOffsetTopicId = topic->rootId();
if (byCreation) {
_topicSearchOffsetId = _topicSearchOffsetTopicId;
_topicSearchOffsetDate = topic->creationDate();
} else if (const auto last = topic->lastServerMessage()) {
_topicSearchOffsetId = last->id;
_topicSearchOffsetDate = last->date();
}
_inner->appendToFiltered(topic);
});
if (_topicSearchOffsetTopicId != savedTopicId) {
_inner->refresh();
} else {
_topicSearchFull = true;
}
}).fail([=] {
_topicSearchFull = true;
}).send();
}
void Widget::searchMore() {
if (_searchRequest || _searchInHistoryRequest) {
return;
@@ -1833,6 +1906,11 @@ void Widget::clearSearchCache() {
}
_searchQuery = QString();
_searchQueryFrom = nullptr;
_topicSearchQuery = QString();
_topicSearchOffsetDate = 0;
_topicSearchOffsetId = _topicSearchOffsetTopicId = 0;
_api.request(base::take(_peerSearchRequest)).cancel();
_api.request(base::take(_topicSearchRequest)).cancel();
cancelSearchRequest();
}

View File

@@ -88,6 +88,7 @@ public:
void scrollToEntry(const RowDescriptor &entry);
void searchMessages(const QString &query, Key inChat = {});
void searchTopics();
void searchMore();
void updateForwardBar();
@@ -150,7 +151,8 @@ private:
void setupMainMenuToggle();
void setupDownloadBar();
void setupShortcuts();
bool searchForPeersRequired(const QString &query) const;
[[nodiscard]] bool searchForPeersRequired(const QString &query) const;
[[nodiscard]] bool searchForTopicsRequired(const QString &query) const;
void setSearchInChat(Key chat, PeerData *from = nullptr);
void showCalendar();
void showSearchFrom();
@@ -244,6 +246,13 @@ private:
bool _peerSearchFull = false;
mtpRequestId _peerSearchRequest = 0;
QString _topicSearchQuery;
TimeId _topicSearchOffsetDate = 0;
MsgId _topicSearchOffsetId = 0;
MsgId _topicSearchOffsetTopicId = 0;
bool _topicSearchFull = false;
mtpRequestId _topicSearchRequest = 0;
QString _searchQuery;
PeerData *_searchQueryFrom = nullptr;
int32 _searchNextRate = 0;

View File

@@ -624,9 +624,13 @@ void PaintRow(
} else {
p.setPen(context.active
? st::dialogsNameFgActive
: context.selected
? st::dialogsArchiveFgOver
: st::dialogsArchiveFg);
: entry->folder()
? (context.selected
? st::dialogsArchiveFgOver
: st::dialogsArchiveFg)
: (context.selected
? st::dialogsNameFgOver
: st::dialogsNameFg));
auto text = entry->chatListName(); // TODO feed name with emoji
auto textWidth = st::semiboldFont->width(text);
if (textWidth > rectForName.width()) {