Skip to content

Commit

Permalink
wip saved sublists
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jul 2, 2024
1 parent cca862e commit 6a2ad9c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Telegram/SourceFiles/api/api_editing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ mtpRequestId EditCaption(
return session->sender().request(TLeditMessageCaption(
peerToTdbChat(item->history()->peer->id),
tl_int53(item->id.bare),
Api::FormattedTextToTdb(caption)
Api::FormattedTextToTdb(caption),
tl_bool(options.invertCaption)
)).done([=](const TLmessage &result) {
session->data().processMessage(result, NewMessageType::Existing);
done();
Expand Down
13 changes: 9 additions & 4 deletions Telegram/SourceFiles/api/api_messages_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ For license and copyright information please follow this link:

#include "tdb/tdb_sender.h"
#include "tdb/tdb_tl_scheme.h"
#include "data/data_saved_messages.h"

namespace Api {
namespace {
Expand Down Expand Up @@ -121,17 +122,21 @@ void MessagesSearch::searchRequest() {
if (_requestId) {
_history->session().sender().request(_requestId).cancel();
}
const auto from = _request.from;
const auto fromPeer = _history->peer->isUser() ? nullptr : from;
const auto savedPeer = _history->peer->isSelf() ? from : nullptr;
_requestId = _history->session().sender().request(TLsearchChatMessages(
peerToTdbChat(_history->peer->id),
tl_string(_query),
(_from
? peerToSender(_from->id)
tl_string(_request.query),
(fromPeer
? peerToSender(fromPeer->id)
: std::optional<TLmessageSender>()),
tl_int53(_offsetId.bare), // from_message_id
tl_int32(0), // offset
tl_int32(kSearchPerPage),
std::nullopt, // filter
tl_int53(0) // message_thread_id
tl_int53(0), // message_thread_id
tl_int53(_history->owner().savedMessages().sublistId(savedPeer))
)).done([=](const TLfoundChatMessages &result, RequestId id) {
searchReceived(result, id, nextToken);
}).fail([=](const Error &error) {
Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/data/data_msg_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Q_DECLARE_METATYPE(MsgId);

using StoryId = int32;
using BusinessShortcutId = int32;
using SavedSublistId = int64;

struct FullStoryId {
PeerId peer = 0;
Expand Down
84 changes: 83 additions & 1 deletion Telegram/SourceFiles/data/data_saved_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ For license and copyright information please follow this link:
#include "history/history_item.h"
#include "main/main_session.h"

#include "tdb/tdb_tl_scheme.h"
#include "data/data_user.h"

namespace Data {
namespace {

using namespace Tdb;

constexpr auto kPerPage = 50;
constexpr auto kFirstPerPage = 10;
constexpr auto kListPerPage = 100;
Expand Down Expand Up @@ -72,9 +77,19 @@ void SavedMessages::loadMore(not_null<SavedSublist*> sublist) {
_loadMore.call();
}

SavedSublistId SavedMessages::sublistId(PeerData *savedSublistPeer) const {
if (!savedSublistPeer) {
return 0;
}
const auto i = _sublistIds.find(savedSublistPeer->id);
return (i != end(_sublistIds)) ? i->second : 0;
}

void SavedMessages::sendLoadMore() {
if (_loadMoreRequestId || _chatsList.loaded()) {
return;
}
#if 0 // mtp
} else if (!_pinnedLoaded) {
loadPinned();
}
Expand All @@ -92,11 +107,19 @@ void SavedMessages::sendLoadMore() {
if (error.type() == u"SAVED_DIALOGS_UNSUPPORTED"_q) {
_unsupported = true;
}
#endif
_loadMoreRequestId = _owner->session().sender().request(
TLloadSavedMessagesTopics(
tl_int32(_sublists.empty() ? kListPerPage : kListFirstPerPage))
).done([=] {
_loadMoreRequestId = 0;
}).fail([=] {
_chatsList.setLoaded();
_loadMoreRequestId = 0;
}).send();
}

#if 0 // mtp
void SavedMessages::loadPinned() {
if (_pinnedRequestId) {
return;
Expand All @@ -114,6 +137,7 @@ void SavedMessages::loadPinned() {
_pinnedRequestId = 0;
}).send();
}
#endif

void SavedMessages::sendLoadMore(not_null<SavedSublist*> sublist) {
if (_loadMoreRequests.contains(sublist) || sublist->isFullLoaded()) {
Expand All @@ -123,6 +147,7 @@ void SavedMessages::sendLoadMore(not_null<SavedSublist*> sublist) {
const auto offsetId = list.empty() ? MsgId(0) : list.back()->id;
const auto offsetDate = list.empty() ? MsgId(0) : list.back()->date();
const auto limit = offsetId ? kPerPage : kFirstPerPage;
#if 0 // mtp
const auto requestId = _owner->session().api().request(
MTPmessages_GetSavedHistory(
sublist->peer()->input,
Expand Down Expand Up @@ -150,37 +175,62 @@ void SavedMessages::sendLoadMore(not_null<SavedSublist*> sublist) {
count = data.vcount().v;
}
});

#endif
const auto requestId = _owner->session().sender().request(
TLgetSavedMessagesTopicHistory(
tl_int53(sublistId(sublist->peer())),
tl_int53(offsetId.bare),
tl_int32(0), // offset
tl_int32(limit))
).done([=](const TLmessages &result) {
_loadMoreRequests.remove(sublist);
const auto &data = result.data();
const auto count = data.vtotal_count().v;
const auto list = &data.vmessages().v;
if (!list) {
sublist->setFullLoaded();
return;
}
auto items = std::vector<not_null<HistoryItem*>>();
items.reserve(list->size());
for (const auto &message : *list) {
#if 0 // mtp
const auto item = owner().addNewMessage(
message,
{},
NewMessageType::Existing);
#endif
if (!message) {
continue;
}
const auto item = owner().processMessage(
*message,
NewMessageType::Existing);
if (item) {
items.push_back(item);
}
}
sublist->append(std::move(items), count);
if (list->empty()) {
sublist->setFullLoaded();
}
#if 0 // mtp
if (result.type() == mtpc_messages_messages) {
sublist->setFullLoaded();
}
}).fail([=](const MTP::Error &error) {
if (error.type() == u"SAVED_DIALOGS_UNSUPPORTED"_q) {
_unsupported = true;
}
#endif
}).fail([=] {
sublist->setFullLoaded();
_loadMoreRequests.remove(sublist);
}).send();
_loadMoreRequests[sublist] = requestId;
}

#if 0 // mtp
void SavedMessages::apply(
const MTPmessages_SavedDialogs &result,
bool pinned) {
Expand Down Expand Up @@ -247,6 +297,7 @@ void SavedMessages::apply(
_offsetPeer = offsetPeer;
}
}
#endif

void SavedMessages::sendLoadMoreRequests() {
if (_loadMoreScheduled) {
Expand All @@ -257,6 +308,7 @@ void SavedMessages::sendLoadMoreRequests() {
}
}

#if 0 // mtp
void SavedMessages::apply(const MTPDupdatePinnedSavedDialogs &update) {
const auto list = update.vorder();
if (!list) {
Expand Down Expand Up @@ -296,5 +348,35 @@ void SavedMessages::apply(const MTPDupdateSavedDialogPinned &update) {
DEBUG_LOG(("API Error: Folder in updateSavedDialogPinned."));
});
}
#endif

void SavedMessages::apply(const TLDupdateSavedMessagesTopic &update) {
const auto &data = update.vtopic().data();
const auto id = data.vid().v;
auto peer = (PeerData*)nullptr;
data.vtype().match([&](const TLDsavedMessagesTopicTypeMyNotes &) {
peer = session().user();
}, [&](const TLDsavedMessagesTopicTypeAuthorHidden &) {
peer = session().data().peer(PeerData::kSavedHiddenAuthorId);
}, [&](const TLDsavedMessagesTopicTypeSavedFromChat &data) {
peer = session().data().peer(peerFromTdbChat(data.vchat_id()));
});
_sublistIds[peer->id] = id;

const auto selfId = _owner->session().userPeerId();
auto item = (HistoryItem*)nullptr;
if (const auto last = data.vlast_message()) {
item = _owner->processMessage(*last, NewMessageType::Existing);
}
if (item) {
const auto entry = sublist(peer);
const auto entryPinned = data.vis_pinned().v;
entry->applyMaybeLast(item);
}
}

void SavedMessages::apply(const TLDupdateSavedMessagesTopicCount &update) {
_chatsList.setCloudListSize(update.vtopic_count().v);
}

} // namespace Data
14 changes: 14 additions & 0 deletions Telegram/SourceFiles/data/data_saved_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ For license and copyright information please follow this link:

#include "dialogs/dialogs_main_list.h"

namespace Tdb {
class TLDupdateSavedMessagesTopic;
class TLDupdateSavedMessagesTopicCount;
} // namespace Tdb

namespace Main {
class Session;
} // namespace Main
Expand All @@ -34,8 +39,14 @@ class SavedMessages final {
void loadMore();
void loadMore(not_null<SavedSublist*> sublist);

#if 0 // mtp
void apply(const MTPDupdatePinnedSavedDialogs &update);
void apply(const MTPDupdateSavedDialogPinned &update);
#endif
void apply(const Tdb::TLDupdateSavedMessagesTopic &update);
void apply(const Tdb::TLDupdateSavedMessagesTopicCount &update);

[[nodiscard]] SavedSublistId sublistId(PeerData *savedSublistPeer) const;

private:
void loadPinned();
Expand All @@ -51,14 +62,17 @@ class SavedMessages final {
base::flat_map<
not_null<PeerData*>,
std::unique_ptr<SavedSublist>> _sublists;
base::flat_map<PeerId, SavedSublistId> _sublistIds;

base::flat_map<not_null<SavedSublist*>, mtpRequestId> _loadMoreRequests;
mtpRequestId _loadMoreRequestId = 0;
mtpRequestId _pinnedRequestId = 0;

#if 0 // mtp
TimeId _offsetDate = 0;
MsgId _offsetId = 0;
PeerData *_offsetPeer = nullptr;
#endif

SingleQueuedInvokation _loadMore;
base::flat_set<not_null<SavedSublist*>> _loadMoreSublistsScheduled;
Expand Down

0 comments on commit 6a2ad9c

Please sign in to comment.