2021-11-09 12:10:51 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "main/session/send_as_peers.h"
|
|
|
|
|
|
|
|
#include "data/data_user.h"
|
2021-11-28 12:26:34 +00:00
|
|
|
#include "data/data_channel.h"
|
2021-11-09 12:10:51 +00:00
|
|
|
#include "data/data_session.h"
|
2021-11-28 12:26:34 +00:00
|
|
|
#include "data/data_changes.h"
|
2021-11-09 12:10:51 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Main {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kRequestEach = 30 * crl::time(1000);
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SendAsPeers::SendAsPeers(not_null<Session*> session)
|
|
|
|
: _session(session)
|
2022-09-02 16:07:53 +00:00
|
|
|
, _onlyMe({ { .peer = session->user(), .premiumRequired = false } }) {
|
2021-11-28 12:26:34 +00:00
|
|
|
_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<PeerData*> peer, bool, bool) {
|
|
|
|
return _lists.contains(peer);
|
|
|
|
}) | rpl::start_with_next([=](not_null<PeerData*> peer, bool, bool) {
|
2021-11-29 05:24:38 +00:00
|
|
|
refresh(peer, true);
|
2021-11-28 12:26:34 +00:00
|
|
|
}, _lifetime);
|
2021-11-09 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 17:04:42 +00:00
|
|
|
bool SendAsPeers::shouldChoose(not_null<PeerData*> peer) {
|
|
|
|
refresh(peer);
|
2023-01-10 18:56:20 +00:00
|
|
|
return Data::CanSendAnything(peer, false) && (list(peer).size() > 1);
|
2021-11-09 17:04:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 05:24:38 +00:00
|
|
|
void SendAsPeers::refresh(not_null<PeerData*> peer, bool force) {
|
2021-11-09 12:10:51 +00:00
|
|
|
if (!peer->isMegagroup()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto now = crl::now();
|
|
|
|
const auto i = _lastRequestTime.find(peer);
|
|
|
|
const auto when = (i == end(_lastRequestTime)) ? -1 : i->second;
|
2021-11-29 05:24:38 +00:00
|
|
|
if (!force && (when >= 0 && now < when + kRequestEach)) {
|
2021-11-09 12:10:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_lastRequestTime[peer] = now;
|
|
|
|
request(peer);
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:07:53 +00:00
|
|
|
const std::vector<SendAsPeer> &SendAsPeers::list(
|
2021-11-09 13:50:33 +00:00
|
|
|
not_null<PeerData*> peer) const {
|
2021-11-09 12:10:51 +00:00
|
|
|
const auto i = _lists.find(peer);
|
|
|
|
return (i != end(_lists)) ? i->second : _onlyMe;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<not_null<PeerData*>> SendAsPeers::updated() const {
|
|
|
|
return _updates.events();
|
|
|
|
}
|
|
|
|
|
2021-11-09 13:50:33 +00:00
|
|
|
void SendAsPeers::saveChosen(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<PeerData*> chosen) {
|
|
|
|
peer->session().api().request(MTPmessages_SaveDefaultSendAs(
|
|
|
|
peer->input,
|
|
|
|
chosen->input
|
|
|
|
)).send();
|
|
|
|
|
|
|
|
setChosen(peer, chosen->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendAsPeers::setChosen(not_null<PeerData*> peer, PeerId chosenId) {
|
|
|
|
if (chosen(peer) == chosenId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto fallback = peer->amAnonymous()
|
|
|
|
? peer
|
|
|
|
: peer->session().user();
|
|
|
|
if (fallback->id == chosenId) {
|
|
|
|
_chosen.remove(peer);
|
|
|
|
} else {
|
|
|
|
_chosen[peer] = chosenId;
|
|
|
|
}
|
|
|
|
_updates.fire_copy(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerId SendAsPeers::chosen(not_null<PeerData*> peer) const {
|
|
|
|
const auto i = _chosen.find(peer);
|
|
|
|
return (i != end(_chosen)) ? i->second : PeerId();
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<PeerData*> SendAsPeers::resolveChosen(
|
|
|
|
not_null<PeerData*> peer) const {
|
|
|
|
return ResolveChosen(peer, list(peer), chosen(peer));
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<PeerData*> SendAsPeers::ResolveChosen(
|
|
|
|
not_null<PeerData*> peer,
|
2022-09-02 16:07:53 +00:00
|
|
|
const std::vector<SendAsPeer> &list,
|
2021-11-09 13:50:33 +00:00
|
|
|
PeerId chosen) {
|
2022-09-02 16:07:53 +00:00
|
|
|
const auto i = ranges::find(list, chosen, [](const SendAsPeer &as) {
|
|
|
|
return as.peer->id;
|
|
|
|
});
|
2021-11-09 13:50:33 +00:00
|
|
|
return (i != end(list))
|
2022-09-02 16:07:53 +00:00
|
|
|
? i->peer
|
2021-11-28 12:26:34 +00:00
|
|
|
: !list.empty()
|
2022-09-02 16:07:53 +00:00
|
|
|
? list.front().peer
|
2021-11-09 13:50:33 +00:00
|
|
|
: (peer->isMegagroup() && peer->amAnonymous())
|
|
|
|
? peer
|
|
|
|
: peer->session().user();
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:10:51 +00:00
|
|
|
void SendAsPeers::request(not_null<PeerData*> peer) {
|
2021-11-09 13:50:33 +00:00
|
|
|
peer->session().api().request(MTPchannels_GetSendAs(
|
2021-11-09 12:10:51 +00:00
|
|
|
peer->input
|
|
|
|
)).done([=](const MTPchannels_SendAsPeers &result) {
|
2022-09-02 16:07:53 +00:00
|
|
|
auto parsed = std::vector<SendAsPeer>();
|
2021-11-09 13:50:33 +00:00
|
|
|
auto &owner = peer->owner();
|
2021-11-09 12:10:51 +00:00
|
|
|
result.match([&](const MTPDchannels_sendAsPeers &data) {
|
|
|
|
owner.processUsers(data.vusers());
|
|
|
|
owner.processChats(data.vchats());
|
2022-09-02 16:07:53 +00:00
|
|
|
const auto &list = data.vpeers().v;
|
|
|
|
parsed.reserve(list.size());
|
|
|
|
for (const auto &as : list) {
|
|
|
|
const auto &data = as.data();
|
|
|
|
const auto peerId = peerFromMTP(data.vpeer());
|
|
|
|
if (const auto peer = owner.peerLoaded(peerId)) {
|
|
|
|
parsed.push_back({
|
|
|
|
.peer = peer,
|
|
|
|
.premiumRequired = data.is_premium_required(),
|
|
|
|
});
|
2021-11-09 12:10:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-09-02 16:07:53 +00:00
|
|
|
if (parsed.size() > 1) {
|
2021-11-09 12:10:51 +00:00
|
|
|
auto &now = _lists[peer];
|
2022-09-02 16:07:53 +00:00
|
|
|
if (now != parsed) {
|
|
|
|
now = std::move(parsed);
|
2021-11-09 12:10:51 +00:00
|
|
|
_updates.fire_copy(peer);
|
|
|
|
}
|
|
|
|
} else if (const auto i = _lists.find(peer); i != end(_lists)) {
|
|
|
|
_lists.erase(i);
|
|
|
|
_updates.fire_copy(peer);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Main
|