From 27e80b8e42a6f2ab93a5e14d9aea92139d396ba4 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 28 Nov 2021 16:26:34 +0400 Subject: [PATCH] Fix crash in inconsistent local send as peer data. --- .../main/session/send_as_peers.cpp | 19 +++++++++++++++++++ .../SourceFiles/main/session/send_as_peers.h | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/Telegram/SourceFiles/main/session/send_as_peers.cpp b/Telegram/SourceFiles/main/session/send_as_peers.cpp index 35f75c8e6..a3ef575a4 100644 --- a/Telegram/SourceFiles/main/session/send_as_peers.cpp +++ b/Telegram/SourceFiles/main/session/send_as_peers.cpp @@ -8,7 +8,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/session/send_as_peers.h" #include "data/data_user.h" +#include "data/data_channel.h" #include "data/data_session.h" +#include "data/data_changes.h" #include "main/main_session.h" #include "apiwrap.h" @@ -22,6 +24,21 @@ constexpr auto kRequestEach = 30 * crl::time(1000); SendAsPeers::SendAsPeers(not_null session) : _session(session) , _onlyMe({ session->user() }) { + _session->changes().peerUpdates( + Data::PeerUpdate::Flag::Rights + ) | rpl::map([=](const Data::PeerUpdate &update) { + const auto peer = update.peer; + const auto channel = peer->asChannel(); + return std::tuple( + peer, + peer->amAnonymous(), + channel ? channel->isPublic() : false); + }) | rpl::distinct_until_changed( + ) | rpl::filter([=](not_null peer, bool, bool) { + return _lists.contains(peer); + }) | rpl::start_with_next([=](not_null peer, bool, bool) { + refresh(peer); + }, _lifetime); } bool SendAsPeers::shouldChoose(not_null peer) { @@ -96,6 +113,8 @@ not_null SendAsPeers::ResolveChosen( const auto i = ranges::find(list, chosen, &PeerData::id); return (i != end(list)) ? (*i) + : !list.empty() + ? list.front() : (peer->isMegagroup() && peer->amAnonymous()) ? peer : peer->session().user(); diff --git a/Telegram/SourceFiles/main/session/send_as_peers.h b/Telegram/SourceFiles/main/session/send_as_peers.h index 35a1db64e..49663db44 100644 --- a/Telegram/SourceFiles/main/session/send_as_peers.h +++ b/Telegram/SourceFiles/main/session/send_as_peers.h @@ -26,6 +26,8 @@ public: void saveChosen(not_null peer, not_null chosen); void setChosen(not_null peer, PeerId chosenId); [[nodiscard]] PeerId chosen(not_null peer) const; + + // If !list(peer).empty() then the result will be from that list. [[nodiscard]] not_null resolveChosen( not_null peer) const; @@ -48,6 +50,8 @@ private: rpl::event_stream> _updates; + rpl::lifetime _lifetime; + }; } // namespace Main