2021-01-14 09:05:14 +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 "api/api_invite_links.h"
|
|
|
|
|
|
|
|
#include "data/data_peer.h"
|
2021-02-05 13:22:02 +00:00
|
|
|
#include "data/data_user.h"
|
2021-01-14 09:05:14 +00:00
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_changes.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kFirstPage = 10;
|
|
|
|
constexpr auto kPerPage = 50;
|
2021-01-15 15:27:34 +00:00
|
|
|
constexpr auto kJoinedFirstPage = 10;
|
2021-01-14 09:05:14 +00:00
|
|
|
|
2021-01-14 13:13:43 +00:00
|
|
|
void BringPermanentToFront(PeerInviteLinks &links) {
|
|
|
|
auto &list = links.links;
|
|
|
|
const auto i = ranges::find_if(list, [](const InviteLink &link) {
|
|
|
|
return link.permanent && !link.revoked;
|
|
|
|
});
|
|
|
|
if (i != end(list) && i != begin(list)) {
|
|
|
|
ranges::rotate(begin(list), i, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemovePermanent(PeerInviteLinks &links) {
|
|
|
|
auto &list = links.links;
|
|
|
|
list.erase(ranges::remove_if(list, [](const InviteLink &link) {
|
|
|
|
return link.permanent && !link.revoked;
|
|
|
|
}), end(list));
|
|
|
|
}
|
|
|
|
|
2021-01-14 09:05:14 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-02-02 10:43:38 +00:00
|
|
|
JoinedByLinkSlice ParseJoinedByLinkSlice(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPmessages_ChatInviteImporters &slice) {
|
|
|
|
auto result = JoinedByLinkSlice();
|
|
|
|
slice.match([&](const MTPDmessages_chatInviteImporters &data) {
|
|
|
|
auto &owner = peer->session().data();
|
|
|
|
owner.processUsers(data.vusers());
|
|
|
|
result.count = data.vcount().v;
|
|
|
|
result.users.reserve(data.vimporters().v.size());
|
2021-03-13 08:26:58 +00:00
|
|
|
for (const auto &importer : data.vimporters().v) {
|
2021-02-02 10:43:38 +00:00
|
|
|
importer.match([&](const MTPDchatInviteImporter &data) {
|
|
|
|
result.users.push_back({
|
2021-04-01 21:04:10 +00:00
|
|
|
.user = owner.user(data.vuser_id()),
|
2021-02-02 10:43:38 +00:00
|
|
|
.date = data.vdate().v,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-19 10:56:01 +00:00
|
|
|
|
2021-01-14 09:05:14 +00:00
|
|
|
InviteLinks::InviteLinks(not_null<ApiWrap*> api) : _api(api) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::create(
|
|
|
|
not_null<PeerData*> peer,
|
2021-01-14 13:13:43 +00:00
|
|
|
Fn<void(Link)> done,
|
|
|
|
TimeId expireDate,
|
|
|
|
int usageLimit) {
|
|
|
|
performCreate(peer, std::move(done), false, expireDate, usageLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::performCreate(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
Fn<void(Link)> done,
|
|
|
|
bool revokeLegacyPermanent,
|
2021-01-14 09:05:14 +00:00
|
|
|
TimeId expireDate,
|
|
|
|
int usageLimit) {
|
2021-01-14 13:13:43 +00:00
|
|
|
if (const auto i = _createCallbacks.find(peer)
|
|
|
|
; i != end(_createCallbacks)) {
|
|
|
|
if (done) {
|
|
|
|
i->second.push_back(std::move(done));
|
|
|
|
}
|
2021-01-14 09:05:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 13:13:43 +00:00
|
|
|
auto &callbacks = _createCallbacks[peer];
|
|
|
|
if (done) {
|
|
|
|
callbacks.push_back(std::move(done));
|
|
|
|
}
|
2021-01-14 09:05:14 +00:00
|
|
|
|
2021-02-02 10:10:38 +00:00
|
|
|
using Flag = MTPmessages_ExportChatInvite::Flag;
|
2021-01-14 13:13:43 +00:00
|
|
|
_api->request(MTPmessages_ExportChatInvite(
|
2021-02-02 10:10:38 +00:00
|
|
|
MTP_flags((revokeLegacyPermanent
|
|
|
|
? Flag::f_legacy_revoke_permanent
|
|
|
|
: Flag(0))
|
|
|
|
| (expireDate ? Flag::f_expire_date : Flag(0))
|
|
|
|
| (usageLimit ? Flag::f_usage_limit : Flag(0))),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(expireDate),
|
|
|
|
MTP_int(usageLimit)
|
2021-01-14 09:05:14 +00:00
|
|
|
)).done([=](const MTPExportedChatInvite &result) {
|
2021-01-14 13:13:43 +00:00
|
|
|
const auto callbacks = _createCallbacks.take(peer);
|
2021-02-05 13:22:02 +00:00
|
|
|
const auto link = prepend(peer, peer->session().user(), result);
|
2021-01-14 13:13:43 +00:00
|
|
|
if (callbacks) {
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback(link);
|
|
|
|
}
|
2021-01-14 09:05:14 +00:00
|
|
|
}
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-01-14 13:13:43 +00:00
|
|
|
_createCallbacks.erase(peer);
|
2021-01-14 09:05:14 +00:00
|
|
|
}).send();
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 17:35:29 +00:00
|
|
|
auto InviteLinks::lookupMyPermanent(not_null<PeerData*> peer) -> Link* {
|
2021-01-14 13:13:43 +00:00
|
|
|
auto i = _firstSlices.find(peer);
|
2021-02-05 17:35:29 +00:00
|
|
|
return (i != end(_firstSlices)) ? lookupMyPermanent(i->second) : nullptr;
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 17:35:29 +00:00
|
|
|
auto InviteLinks::lookupMyPermanent(Links &links) -> Link* {
|
2021-01-14 13:13:43 +00:00
|
|
|
const auto first = links.links.begin();
|
|
|
|
return (first != end(links.links) && first->permanent && !first->revoked)
|
|
|
|
? &*first
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:35:29 +00:00
|
|
|
auto InviteLinks::lookupMyPermanent(const Links &links) const -> const Link* {
|
2021-01-14 13:13:43 +00:00
|
|
|
const auto first = links.links.begin();
|
|
|
|
return (first != end(links.links) && first->permanent && !first->revoked)
|
|
|
|
? &*first
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto InviteLinks::prepend(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
2021-01-14 13:13:43 +00:00
|
|
|
const MTPExportedChatInvite &invite) -> Link {
|
|
|
|
const auto link = parse(peer, invite);
|
2021-02-05 17:35:29 +00:00
|
|
|
if (admin->isSelf()) {
|
|
|
|
prependMyToFirstSlice(peer, admin, link);
|
|
|
|
}
|
|
|
|
_updates.fire(Update{
|
|
|
|
.peer = peer,
|
|
|
|
.admin = admin,
|
|
|
|
.now = link
|
|
|
|
});
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::prependMyToFirstSlice(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<UserData*> admin,
|
|
|
|
const Link &link) {
|
|
|
|
Expects(admin->isSelf());
|
|
|
|
|
2021-01-14 13:13:43 +00:00
|
|
|
auto i = _firstSlices.find(peer);
|
|
|
|
if (i == end(_firstSlices)) {
|
|
|
|
i = _firstSlices.emplace(peer).first;
|
|
|
|
}
|
|
|
|
auto &links = i->second;
|
2021-02-05 17:35:29 +00:00
|
|
|
const auto permanent = lookupMyPermanent(links);
|
2021-01-19 10:26:00 +00:00
|
|
|
const auto hadPermanent = (permanent != nullptr);
|
2021-02-05 13:22:02 +00:00
|
|
|
auto updateOldPermanent = Update{
|
|
|
|
.peer = peer,
|
|
|
|
.admin = admin,
|
|
|
|
};
|
2021-01-19 10:26:00 +00:00
|
|
|
if (link.permanent && hadPermanent) {
|
|
|
|
updateOldPermanent.was = permanent->link;
|
|
|
|
updateOldPermanent.now = *permanent;
|
|
|
|
updateOldPermanent.now->revoked = true;
|
|
|
|
links.links.erase(begin(links.links));
|
|
|
|
if (links.count > 0) {
|
|
|
|
--links.count;
|
2021-01-19 08:41:22 +00:00
|
|
|
}
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
2021-01-19 10:26:00 +00:00
|
|
|
// Must not dereference 'permanent' pointer after that.
|
|
|
|
|
2021-01-14 13:13:43 +00:00
|
|
|
++links.count;
|
2021-01-19 10:26:00 +00:00
|
|
|
if (hadPermanent && !link.permanent) {
|
2021-01-15 16:13:45 +00:00
|
|
|
links.links.insert(begin(links.links) + 1, link);
|
|
|
|
} else {
|
|
|
|
links.links.insert(begin(links.links), link);
|
|
|
|
}
|
2021-01-19 10:26:00 +00:00
|
|
|
|
|
|
|
if (link.permanent) {
|
|
|
|
editPermanentLink(peer, link.link);
|
|
|
|
}
|
2021-01-14 13:13:43 +00:00
|
|
|
notify(peer);
|
2021-01-19 10:26:00 +00:00
|
|
|
|
|
|
|
if (updateOldPermanent.now) {
|
|
|
|
_updates.fire(std::move(updateOldPermanent));
|
|
|
|
}
|
2021-01-14 09:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::edit(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
2021-01-14 09:05:14 +00:00
|
|
|
const QString &link,
|
|
|
|
TimeId expireDate,
|
2021-01-14 13:13:43 +00:00
|
|
|
int usageLimit,
|
|
|
|
Fn<void(Link)> done) {
|
2021-02-05 13:22:02 +00:00
|
|
|
performEdit(
|
|
|
|
peer,
|
|
|
|
admin,
|
|
|
|
link,
|
|
|
|
std::move(done),
|
|
|
|
false,
|
|
|
|
expireDate,
|
|
|
|
usageLimit);
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::performEdit(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
2021-01-14 13:13:43 +00:00
|
|
|
const QString &link,
|
|
|
|
Fn<void(Link)> done,
|
|
|
|
bool revoke,
|
|
|
|
TimeId expireDate,
|
2021-01-14 09:05:14 +00:00
|
|
|
int usageLimit) {
|
2021-01-15 15:27:34 +00:00
|
|
|
const auto key = LinkKey{ peer, link };
|
2021-01-19 10:26:00 +00:00
|
|
|
if (_deleteCallbacks.contains(key)) {
|
|
|
|
return;
|
|
|
|
} else if (const auto i = _editCallbacks.find(key)
|
|
|
|
; i != end(_editCallbacks)) {
|
2021-01-14 13:13:43 +00:00
|
|
|
if (done) {
|
|
|
|
i->second.push_back(std::move(done));
|
|
|
|
}
|
2021-01-14 09:05:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 13:13:43 +00:00
|
|
|
|
2021-01-15 16:13:45 +00:00
|
|
|
auto &callbacks = _editCallbacks[key];
|
|
|
|
if (done) {
|
|
|
|
callbacks.push_back(std::move(done));
|
|
|
|
}
|
2021-02-02 10:43:38 +00:00
|
|
|
using Flag = MTPmessages_EditExportedChatInvite::Flag;
|
|
|
|
_api->request(MTPmessages_EditExportedChatInvite(
|
|
|
|
MTP_flags((revoke ? Flag::f_revoked : Flag(0))
|
2021-02-09 17:52:20 +00:00
|
|
|
| (!revoke ? Flag::f_expire_date : Flag(0))
|
|
|
|
| (!revoke ? Flag::f_usage_limit : Flag(0))),
|
2021-02-02 10:43:38 +00:00
|
|
|
peer->input,
|
|
|
|
MTP_string(link),
|
|
|
|
MTP_int(expireDate),
|
|
|
|
MTP_int(usageLimit)
|
|
|
|
)).done([=](const MTPmessages_ExportedChatInvite &result) {
|
|
|
|
const auto callbacks = _editCallbacks.take(key);
|
|
|
|
const auto peer = key.peer;
|
|
|
|
result.match([&](const auto &data) {
|
|
|
|
_api->session().data().processUsers(data.vusers());
|
|
|
|
const auto link = parse(peer, data.vinvite());
|
|
|
|
auto i = _firstSlices.find(peer);
|
|
|
|
if (i != end(_firstSlices)) {
|
|
|
|
const auto j = ranges::find(
|
|
|
|
i->second.links,
|
|
|
|
key.link,
|
|
|
|
&Link::link);
|
|
|
|
if (j != end(i->second.links)) {
|
|
|
|
if (link.revoked && !j->revoked) {
|
|
|
|
i->second.links.erase(j);
|
|
|
|
if (i->second.count > 0) {
|
|
|
|
--i->second.count;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*j = link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback(link);
|
|
|
|
}
|
|
|
|
_updates.fire(Update{
|
|
|
|
.peer = peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
.admin = admin,
|
2021-02-02 10:43:38 +00:00
|
|
|
.was = key.link,
|
|
|
|
.now = link,
|
|
|
|
});
|
|
|
|
|
|
|
|
using Replaced = MTPDmessages_exportedChatInviteReplaced;
|
|
|
|
if constexpr (Replaced::Is<decltype(data)>()) {
|
2021-02-05 13:22:02 +00:00
|
|
|
prepend(peer, admin, data.vnew_invite());
|
2021-02-02 10:43:38 +00:00
|
|
|
}
|
|
|
|
});
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-02-02 10:43:38 +00:00
|
|
|
_editCallbacks.erase(key);
|
|
|
|
}).send();
|
2021-01-14 09:05:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 13:13:43 +00:00
|
|
|
void InviteLinks::revoke(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
2021-01-14 13:13:43 +00:00
|
|
|
const QString &link,
|
|
|
|
Fn<void(Link)> done) {
|
2021-02-05 13:22:02 +00:00
|
|
|
performEdit(peer, admin, link, std::move(done), true);
|
2021-01-14 09:05:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 16:13:58 +00:00
|
|
|
void InviteLinks::revokePermanent(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
|
|
|
const QString &link,
|
2021-02-02 10:43:38 +00:00
|
|
|
Fn<void()> done) {
|
|
|
|
const auto callback = [=](auto&&) { done(); };
|
2021-02-05 13:22:02 +00:00
|
|
|
if (!link.isEmpty()) {
|
|
|
|
performEdit(peer, admin, link, callback, true);
|
|
|
|
} else if (!admin->isSelf()) {
|
|
|
|
crl::on_main(&peer->session(), done);
|
2021-02-02 10:43:38 +00:00
|
|
|
} else {
|
|
|
|
performCreate(peer, callback, true);
|
|
|
|
}
|
2021-01-18 16:13:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 10:26:00 +00:00
|
|
|
void InviteLinks::destroy(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
2021-01-19 10:26:00 +00:00
|
|
|
const QString &link,
|
|
|
|
Fn<void()> done) {
|
|
|
|
const auto key = LinkKey{ peer, link };
|
|
|
|
|
|
|
|
if (const auto i = _deleteCallbacks.find(key)
|
|
|
|
; i != end(_deleteCallbacks)) {
|
|
|
|
if (done) {
|
|
|
|
i->second.push_back(std::move(done));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &callbacks = _deleteCallbacks[key];
|
|
|
|
if (done) {
|
|
|
|
callbacks.push_back(std::move(done));
|
|
|
|
}
|
2021-02-02 10:43:38 +00:00
|
|
|
_api->request(MTPmessages_DeleteExportedChatInvite(
|
|
|
|
peer->input,
|
|
|
|
MTP_string(link)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
const auto callbacks = _deleteCallbacks.take(key);
|
|
|
|
if (callbacks) {
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_updates.fire(Update{
|
|
|
|
.peer = peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
.admin = admin,
|
2021-02-02 10:43:38 +00:00
|
|
|
.was = key.link,
|
|
|
|
});
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-02-02 10:43:38 +00:00
|
|
|
_deleteCallbacks.erase(key);
|
|
|
|
}).send();
|
2021-01-19 10:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::destroyAllRevoked(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
2021-01-19 10:26:00 +00:00
|
|
|
Fn<void()> done) {
|
|
|
|
if (const auto i = _deleteRevokedCallbacks.find(peer)
|
|
|
|
; i != end(_deleteRevokedCallbacks)) {
|
|
|
|
if (done) {
|
|
|
|
i->second.push_back(std::move(done));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &callbacks = _deleteRevokedCallbacks[peer];
|
|
|
|
if (done) {
|
|
|
|
callbacks.push_back(std::move(done));
|
|
|
|
}
|
2021-02-02 10:43:38 +00:00
|
|
|
_api->request(MTPmessages_DeleteRevokedExportedChatInvites(
|
|
|
|
peer->input,
|
2021-02-05 13:22:02 +00:00
|
|
|
admin->inputUser
|
2021-02-02 10:43:38 +00:00
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
if (const auto callbacks = _deleteRevokedCallbacks.take(peer)) {
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 14:36:37 +00:00
|
|
|
_allRevokedDestroyed.fire({ peer, admin });
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-02-02 10:43:38 +00:00
|
|
|
}).send();
|
2021-01-19 10:26:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:22:02 +00:00
|
|
|
void InviteLinks::requestMyLinks(not_null<PeerData*> peer) {
|
2021-01-14 09:05:14 +00:00
|
|
|
if (_firstSliceRequests.contains(peer)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-02 10:43:38 +00:00
|
|
|
const auto requestId = _api->request(MTPmessages_GetExportedChatInvites(
|
|
|
|
MTP_flags(0),
|
|
|
|
peer->input,
|
|
|
|
MTP_inputUserSelf(),
|
|
|
|
MTPint(), // offset_date
|
|
|
|
MTPstring(), // offset_link
|
|
|
|
MTP_int(kFirstPage)
|
|
|
|
)).done([=](const MTPmessages_ExportedChatInvites &result) {
|
|
|
|
_firstSliceRequests.remove(peer);
|
|
|
|
auto slice = parseSlice(peer, result);
|
|
|
|
auto i = _firstSlices.find(peer);
|
|
|
|
const auto permanent = (i != end(_firstSlices))
|
2021-02-05 17:35:29 +00:00
|
|
|
? lookupMyPermanent(i->second)
|
2021-02-02 10:43:38 +00:00
|
|
|
: nullptr;
|
|
|
|
if (!permanent) {
|
|
|
|
BringPermanentToFront(slice);
|
|
|
|
const auto j = _firstSlices.emplace_or_assign(
|
|
|
|
peer,
|
|
|
|
std::move(slice)).first;
|
2021-02-05 17:35:29 +00:00
|
|
|
if (const auto permanent = lookupMyPermanent(j->second)) {
|
2021-02-02 10:43:38 +00:00
|
|
|
editPermanentLink(peer, permanent->link);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
RemovePermanent(slice);
|
|
|
|
auto &existing = i->second.links;
|
|
|
|
existing.erase(begin(existing) + 1, end(existing));
|
|
|
|
existing.insert(
|
|
|
|
end(existing),
|
|
|
|
begin(slice.links),
|
|
|
|
end(slice.links));
|
|
|
|
i->second.count = std::max(slice.count, int(existing.size()));
|
|
|
|
}
|
|
|
|
notify(peer);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-02-02 10:43:38 +00:00
|
|
|
_firstSliceRequests.remove(peer);
|
|
|
|
}).send();
|
|
|
|
_firstSliceRequests.emplace(peer, requestId);
|
2021-01-14 09:05:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 10:56:01 +00:00
|
|
|
std::optional<JoinedByLinkSlice> InviteLinks::lookupJoinedFirstSlice(
|
|
|
|
LinkKey key) const {
|
2021-01-15 15:27:34 +00:00
|
|
|
const auto i = _firstJoined.find(key);
|
2021-01-19 10:56:01 +00:00
|
|
|
return (i != end(_firstJoined))
|
|
|
|
? std::make_optional(i->second)
|
|
|
|
: std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<JoinedByLinkSlice> InviteLinks::joinedFirstSliceLoaded(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const QString &link) const {
|
|
|
|
return lookupJoinedFirstSlice({ peer, link });
|
2021-01-15 15:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<JoinedByLinkSlice> InviteLinks::joinedFirstSliceValue(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const QString &link,
|
|
|
|
int fullCount) {
|
|
|
|
const auto key = LinkKey{ peer, link };
|
2021-01-19 10:56:01 +00:00
|
|
|
auto current = lookupJoinedFirstSlice(key).value_or(JoinedByLinkSlice());
|
2021-01-15 15:27:34 +00:00
|
|
|
if (current.count == fullCount
|
|
|
|
&& (!fullCount || !current.users.empty())) {
|
|
|
|
return rpl::single(current);
|
|
|
|
}
|
|
|
|
current.count = fullCount;
|
|
|
|
const auto remove = int(current.users.size()) - current.count;
|
|
|
|
if (remove > 0) {
|
|
|
|
current.users.erase(end(current.users) - remove, end(current.users));
|
|
|
|
}
|
|
|
|
requestJoinedFirstSlice(key);
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
return rpl::single(
|
|
|
|
current
|
|
|
|
) | rpl::then(_joinedFirstSliceLoaded.events(
|
|
|
|
) | rpl::filter(
|
|
|
|
_1 == key
|
|
|
|
) | rpl::map([=] {
|
2021-01-19 10:56:01 +00:00
|
|
|
return lookupJoinedFirstSlice(key).value_or(JoinedByLinkSlice());
|
2021-01-15 15:27:34 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-01-19 08:41:22 +00:00
|
|
|
auto InviteLinks::updates(
|
2021-02-05 14:36:37 +00:00
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<UserData*> admin) const -> rpl::producer<Update> {
|
2021-01-19 08:41:22 +00:00
|
|
|
return _updates.events() | rpl::filter([=](const Update &update) {
|
2021-02-05 14:36:37 +00:00
|
|
|
return update.peer == peer && update.admin == admin;
|
2021-01-19 08:41:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-19 10:26:00 +00:00
|
|
|
rpl::producer<> InviteLinks::allRevokedDestroyed(
|
2021-02-05 14:36:37 +00:00
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<UserData*> admin) const {
|
2021-01-19 10:26:00 +00:00
|
|
|
return _allRevokedDestroyed.events(
|
2021-02-05 14:36:37 +00:00
|
|
|
) | rpl::filter([=](const AllRevokedDestroyed &which) {
|
|
|
|
return which.peer == peer && which.admin == admin;
|
|
|
|
}) | rpl::to_empty;
|
2021-01-19 10:26:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 15:27:34 +00:00
|
|
|
void InviteLinks::requestJoinedFirstSlice(LinkKey key) {
|
2021-02-02 10:43:38 +00:00
|
|
|
if (_firstJoinedRequests.contains(key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto requestId = _api->request(MTPmessages_GetChatInviteImporters(
|
|
|
|
key.peer->input,
|
|
|
|
MTP_string(key.link),
|
|
|
|
MTP_int(0), // offset_date
|
|
|
|
MTP_inputUserEmpty(), // offset_user
|
|
|
|
MTP_int(kJoinedFirstPage)
|
|
|
|
)).done([=](const MTPmessages_ChatInviteImporters &result) {
|
|
|
|
_firstJoinedRequests.remove(key);
|
|
|
|
_firstJoined[key] = ParseJoinedByLinkSlice(key.peer, result);
|
|
|
|
_joinedFirstSliceLoaded.fire_copy(key);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-02-02 10:43:38 +00:00
|
|
|
_firstJoinedRequests.remove(key);
|
|
|
|
}).send();
|
|
|
|
_firstJoinedRequests.emplace(key, requestId);
|
2021-01-15 15:27:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:22:02 +00:00
|
|
|
void InviteLinks::setMyPermanent(
|
2021-01-14 13:13:43 +00:00
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPExportedChatInvite &invite) {
|
|
|
|
auto link = parse(peer, invite);
|
2021-01-15 11:42:26 +00:00
|
|
|
if (!link.permanent) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"InviteLinks::setPermanent called with non-permanent link."));
|
|
|
|
return;
|
|
|
|
}
|
2021-01-14 13:13:43 +00:00
|
|
|
auto i = _firstSlices.find(peer);
|
|
|
|
if (i == end(_firstSlices)) {
|
|
|
|
i = _firstSlices.emplace(peer).first;
|
|
|
|
}
|
|
|
|
auto &links = i->second;
|
2021-02-05 13:22:02 +00:00
|
|
|
auto updateOldPermanent = Update{
|
|
|
|
.peer = peer,
|
|
|
|
.admin = peer->session().user(),
|
|
|
|
};
|
2021-02-05 17:35:29 +00:00
|
|
|
if (const auto permanent = lookupMyPermanent(links)) {
|
2021-01-14 13:13:43 +00:00
|
|
|
if (permanent->link == link.link) {
|
|
|
|
if (permanent->usage != link.usage) {
|
|
|
|
permanent->usage = link.usage;
|
2021-01-19 10:26:00 +00:00
|
|
|
_updates.fire(Update{
|
|
|
|
.peer = peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
.admin = peer->session().user(),
|
2021-01-19 10:26:00 +00:00
|
|
|
.was = link.link,
|
|
|
|
.now = *permanent
|
|
|
|
});
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2021-01-19 10:26:00 +00:00
|
|
|
updateOldPermanent.was = permanent->link;
|
|
|
|
updateOldPermanent.now = *permanent;
|
|
|
|
updateOldPermanent.now->revoked = true;
|
|
|
|
links.links.erase(begin(links.links));
|
|
|
|
if (links.count > 0) {
|
|
|
|
--links.count;
|
|
|
|
}
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
|
|
|
links.links.insert(begin(links.links), link);
|
2021-01-19 10:26:00 +00:00
|
|
|
|
2021-01-14 13:13:43 +00:00
|
|
|
editPermanentLink(peer, link.link);
|
|
|
|
notify(peer);
|
2021-01-19 10:26:00 +00:00
|
|
|
|
|
|
|
if (updateOldPermanent.now) {
|
|
|
|
_updates.fire(std::move(updateOldPermanent));
|
|
|
|
}
|
2021-02-05 13:22:02 +00:00
|
|
|
_updates.fire(Update{
|
|
|
|
.peer = peer,
|
|
|
|
.admin = peer->session().user(),
|
|
|
|
.now = link
|
|
|
|
});
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:22:02 +00:00
|
|
|
void InviteLinks::clearMyPermanent(not_null<PeerData*> peer) {
|
2021-01-19 10:26:00 +00:00
|
|
|
auto i = _firstSlices.find(peer);
|
|
|
|
if (i == end(_firstSlices)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &links = i->second;
|
2021-02-05 17:35:29 +00:00
|
|
|
const auto permanent = lookupMyPermanent(links);
|
2021-01-19 10:26:00 +00:00
|
|
|
if (!permanent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:22:02 +00:00
|
|
|
auto updateOldPermanent = Update{
|
|
|
|
.peer = peer,
|
|
|
|
.admin = peer->session().user()
|
|
|
|
};
|
2021-01-19 10:26:00 +00:00
|
|
|
updateOldPermanent.was = permanent->link;
|
|
|
|
updateOldPermanent.now = *permanent;
|
|
|
|
updateOldPermanent.now->revoked = true;
|
|
|
|
links.links.erase(begin(links.links));
|
|
|
|
if (links.count > 0) {
|
|
|
|
--links.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
editPermanentLink(peer, QString());
|
|
|
|
notify(peer);
|
|
|
|
|
|
|
|
if (updateOldPermanent.now) {
|
|
|
|
_updates.fire(std::move(updateOldPermanent));
|
2021-01-14 13:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::notify(not_null<PeerData*> peer) {
|
|
|
|
peer->session().changes().peerUpdated(
|
|
|
|
peer,
|
|
|
|
Data::PeerUpdate::Flag::InviteLinks);
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:22:02 +00:00
|
|
|
auto InviteLinks::myLinks(not_null<PeerData*> peer) const -> const Links & {
|
2021-01-15 11:42:26 +00:00
|
|
|
static const auto kEmpty = Links();
|
2021-01-14 09:05:14 +00:00
|
|
|
const auto i = _firstSlices.find(peer);
|
2021-01-15 11:42:26 +00:00
|
|
|
return (i != end(_firstSlices)) ? i->second : kEmpty;
|
2021-01-14 09:05:14 +00:00
|
|
|
}
|
2021-02-02 10:43:38 +00:00
|
|
|
|
|
|
|
auto InviteLinks::parseSlice(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPmessages_ExportedChatInvites &slice) const -> Links {
|
|
|
|
auto i = _firstSlices.find(peer);
|
|
|
|
const auto permanent = (i != end(_firstSlices))
|
2021-02-05 17:35:29 +00:00
|
|
|
? lookupMyPermanent(i->second)
|
2021-02-02 10:43:38 +00:00
|
|
|
: nullptr;
|
|
|
|
auto result = Links();
|
|
|
|
slice.match([&](const MTPDmessages_exportedChatInvites &data) {
|
|
|
|
peer->session().data().processUsers(data.vusers());
|
|
|
|
result.count = data.vcount().v;
|
|
|
|
for (const auto &invite : data.vinvites().v) {
|
|
|
|
const auto link = parse(peer, invite);
|
|
|
|
if (!permanent || link.link != permanent->link) {
|
|
|
|
result.links.push_back(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-14 09:05:14 +00:00
|
|
|
|
2021-01-14 13:13:43 +00:00
|
|
|
auto InviteLinks::parse(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
const MTPExportedChatInvite &invite) const -> Link {
|
|
|
|
return invite.match([&](const MTPDchatInviteExported &data) {
|
|
|
|
return Link{
|
|
|
|
.link = qs(data.vlink()),
|
2021-04-01 21:04:10 +00:00
|
|
|
.admin = peer->session().data().user(data.vadmin_id()),
|
2021-01-14 13:13:43 +00:00
|
|
|
.date = data.vdate().v,
|
2021-01-18 16:13:58 +00:00
|
|
|
.startDate = data.vstart_date().value_or_empty(),
|
2021-01-14 13:13:43 +00:00
|
|
|
.expireDate = data.vexpire_date().value_or_empty(),
|
|
|
|
.usageLimit = data.vusage_limit().value_or_empty(),
|
|
|
|
.usage = data.vusage().value_or_empty(),
|
2021-02-02 10:43:38 +00:00
|
|
|
.permanent = data.is_permanent(),
|
2021-01-14 13:13:43 +00:00
|
|
|
.revoked = data.is_revoked(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-14 09:05:14 +00:00
|
|
|
void InviteLinks::requestMoreLinks(
|
|
|
|
not_null<PeerData*> peer,
|
2021-02-05 13:22:02 +00:00
|
|
|
not_null<UserData*> admin,
|
2021-01-19 10:56:01 +00:00
|
|
|
TimeId lastDate,
|
|
|
|
const QString &lastLink,
|
2021-01-19 07:14:50 +00:00
|
|
|
bool revoked,
|
2021-01-14 09:05:14 +00:00
|
|
|
Fn<void(Links)> done) {
|
2021-02-02 10:43:38 +00:00
|
|
|
using Flag = MTPmessages_GetExportedChatInvites::Flag;
|
|
|
|
_api->request(MTPmessages_GetExportedChatInvites(
|
|
|
|
MTP_flags(Flag::f_offset_link
|
|
|
|
| (revoked ? Flag::f_revoked : Flag(0))),
|
|
|
|
peer->input,
|
2021-02-05 13:22:02 +00:00
|
|
|
admin->inputUser,
|
2021-02-02 10:43:38 +00:00
|
|
|
MTP_int(lastDate),
|
|
|
|
MTP_string(lastLink),
|
|
|
|
MTP_int(kPerPage)
|
|
|
|
)).done([=](const MTPmessages_ExportedChatInvites &result) {
|
2021-02-05 13:22:02 +00:00
|
|
|
done(parseSlice(peer, result));
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2021-02-02 10:43:38 +00:00
|
|
|
done(Links());
|
|
|
|
}).send();
|
2021-01-14 09:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinks::editPermanentLink(
|
|
|
|
not_null<PeerData*> peer,
|
2021-01-14 13:13:43 +00:00
|
|
|
const QString &link) {
|
2021-01-14 09:05:14 +00:00
|
|
|
if (const auto chat = peer->asChat()) {
|
2021-01-14 13:13:43 +00:00
|
|
|
chat->setInviteLink(link);
|
2021-01-14 09:05:14 +00:00
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
2021-01-14 13:13:43 +00:00
|
|
|
channel->setInviteLink(link);
|
2021-01-14 09:05:14 +00:00
|
|
|
} else {
|
|
|
|
Unexpected("Peer in InviteLinks::editMainLink.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Api
|