Add getChatInviteLinks method.

This commit is contained in:
levlam 2021-01-14 19:39:51 +03:00
parent 9f37354951
commit efc12342c2
7 changed files with 109 additions and 5 deletions

View File

@ -552,6 +552,9 @@ supergroupMembersFilterBots = SupergroupMembersFilter;
//@is_expired True, if the link is already expired @is_revoked True, if the link was revoked //@is_expired True, if the link is already expired @is_revoked True, if the link was revoked
chatInviteLink invite_link:string administrator_user_id:int32 date:int32 expire_date:int32 usage_limit:int32 usage_count:int32 is_permanent:Bool is_expired:Bool is_revoked:Bool = ChatInviteLink; chatInviteLink invite_link:string administrator_user_id:int32 date:int32 expire_date:int32 usage_limit:int32 usage_count:int32 is_permanent:Bool is_expired:Bool is_revoked:Bool = ChatInviteLink;
//@description Contains a list of chat invite links @total_count Approximate total count of chat invite links found @invite_links List of invite links
chatInviteLinks total_count:int32 invite_links:vector<chatInviteLink> = ChatInviteLinks;
//@description Contains information about a chat invite link //@description Contains information about a chat invite link
//@chat_id Chat identifier of the invite link; 0 if the user has no access to the chat before joining //@chat_id Chat identifier of the invite link; 0 if the user has no access to the chat before joining
//@accessible_for If non-zero, the amount of time for which read access to the chat will remain available, in seconds //@accessible_for If non-zero, the amount of time for which read access to the chat will remain available, in seconds
@ -4365,6 +4368,10 @@ createChatInviteLink chat_id:int53 expire_date:int32 usage_limit:int32 is_perman
//@is_revoked True, if the link is revoked //@is_revoked True, if the link is revoked
editChatInviteLink chat_id:int53 invite_link:string expire_date:int32 usage_limit:int32 is_revoked:Bool = ChatInviteLink; editChatInviteLink chat_id:int53 invite_link:string expire_date:int32 usage_limit:int32 is_revoked:Bool = ChatInviteLink;
//@description Returns exported invite links for a chat @chat_id Chat identifier @administrator_user_id If not 0, only invite links created by the specified administrator will be returned
//@offset_invite_link Invite link starting after which to return invite links; use empty string to get results from the beginning @limit Maximum number of invite links to return
getChatInviteLinks chat_id:int53 administrator_user_id:int32 offset_invite_link:string limit:int32 = ChatInviteLinks;
//@description Checks the validity of an invite link for a chat and returns information about the corresponding chat @invite_link Invite link to be checked; must begin with "https://t.me/joinchat/", "https://telegram.me/joinchat/", or "https://telegram.dog/joinchat/" //@description Checks the validity of an invite link for a chat and returns information about the corresponding chat @invite_link Invite link to be checked; must begin with "https://t.me/joinchat/", "https://telegram.me/joinchat/", or "https://telegram.dog/joinchat/"
checkChatInviteLink invite_link:string = ChatInviteLinkInfo; checkChatInviteLink invite_link:string = ChatInviteLinkInfo;

Binary file not shown.

View File

@ -1644,6 +1644,70 @@ class EditChatInviteLinkQuery : public Td::ResultHandler {
} }
}; };
class GetExportedChatInvitesQuery : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::chatInviteLinks>> promise_;
DialogId dialog_id_;
public:
explicit GetExportedChatInvitesQuery(Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&promise)
: promise_(std::move(promise)) {
}
void send(DialogId dialog_id, UserId administrator_user_id, const string &offset_invite_link, int32 limit) {
dialog_id_ = dialog_id;
auto input_peer = td->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
if (input_peer == nullptr) {
return on_error(0, Status::Error(400, "Can't access the chat"));
}
auto input_user = td->contacts_manager_->get_input_user(administrator_user_id);
int32 flags = 0;
if (input_user != nullptr) {
flags |= telegram_api::messages_getExportedChatInvites::ADMIN_ID_MASK;
}
if (!offset_invite_link.empty()) {
flags |= telegram_api::messages_getExportedChatInvites::OFFSET_LINK_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_getExportedChatInvites(
flags, std::move(input_peer), std::move(input_user), offset_invite_link, limit)));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::messages_getExportedChatInvites>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetExportedChatInvitesQuery: " << to_string(result);
td->contacts_manager_->on_get_users(std::move(result->users_), "GetExportedChatInvitesQuery");
int32 total_count = result->count_;
if (total_count < static_cast<int32>(result->invites_.size())) {
LOG(ERROR) << "Receive wrong total count of invite links " << total_count << " in " << dialog_id_;
total_count = static_cast<int32>(result->invites_.size());
}
vector<td_api::object_ptr<td_api::chatInviteLink>> invite_links;
for (auto &invite : result->invites_) {
DialogInviteLink invite_link(std::move(invite));
if (!invite_link.is_valid()) {
LOG(ERROR) << "Receive invalid invite link in " << dialog_id_;
total_count--;
continue;
}
invite_links.push_back(invite_link.get_chat_invite_link_object(td->contacts_manager_.get()));
}
promise_.set_value(td_api::make_object<td_api::chatInviteLinks>(total_count, std::move(invite_links)));
}
void on_error(uint64 id, Status status) override {
td->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetExportedChatInvitesQuery");
promise_.set_error(std::move(status));
}
};
class CheckDialogInviteLinkQuery : public Td::ResultHandler { class CheckDialogInviteLinkQuery : public Td::ResultHandler {
Promise<Unit> promise_; Promise<Unit> promise_;
string invite_link_; string invite_link_;
@ -6586,8 +6650,6 @@ Status ContactsManager::can_manage_dialog_invite_links(DialogId dialog_id) {
void ContactsManager::export_dialog_invite_link(DialogId dialog_id, int32 expire_date, int32 usage_limit, void ContactsManager::export_dialog_invite_link(DialogId dialog_id, int32 expire_date, int32 usage_limit,
bool is_permanent, bool is_permanent,
Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise) { Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise) {
LOG(INFO) << "Receive CreateDialogInviteLink request for " << dialog_id;
get_me(PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, expire_date, usage_limit, is_permanent, get_me(PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, expire_date, usage_limit, is_permanent,
promise = std::move(promise)](Result<Unit> &&result) mutable { promise = std::move(promise)](Result<Unit> &&result) mutable {
if (result.is_error()) { if (result.is_error()) {
@ -6615,8 +6677,6 @@ void ContactsManager::export_dialog_invite_link_impl(DialogId dialog_id, int32 e
void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &invite_link, int32 expire_date, void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &invite_link, int32 expire_date,
int32 usage_limit, bool is_revoked, int32 usage_limit, bool is_revoked,
Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise) { Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise) {
LOG(INFO) << "Receive EditDialogInviteLink request for link " << invite_link << " in " << dialog_id;
TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id)); TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id));
// if (!DialogInviteLink::is_valid_invite_link(invite_link)) { // if (!DialogInviteLink::is_valid_invite_link(invite_link)) {
@ -6627,6 +6687,23 @@ void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &
->send(dialog_id, invite_link, expire_date, usage_limit, is_revoked); ->send(dialog_id, invite_link, expire_date, usage_limit, is_revoked);
} }
void ContactsManager::get_dialog_invite_links(DialogId dialog_id, UserId administrator_user_id,
const string &offset_invite_link, int32 limit,
Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&promise) {
TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id));
if (administrator_user_id != UserId() && !have_input_user(administrator_user_id)) {
return promise.set_error(Status::Error(400, "Administrator user not found"));
}
if (limit <= 0) {
return promise.set_error(Status::Error(400, "Parameter limit must be positive"));
}
td_->create_handler<GetExportedChatInvitesQuery>(std::move(promise))
->send(dialog_id, administrator_user_id, offset_invite_link, limit);
}
void ContactsManager::check_dialog_invite_link(const string &invite_link, Promise<Unit> &&promise) const { void ContactsManager::check_dialog_invite_link(const string &invite_link, Promise<Unit> &&promise) const {
if (invite_link_infos_.count(invite_link) > 0) { if (invite_link_infos_.count(invite_link) > 0) {
return promise.set_value(Unit()); return promise.set_value(Unit());

View File

@ -393,6 +393,9 @@ class ContactsManager : public Actor {
void edit_dialog_invite_link(DialogId dialog_id, const string &link, int32 expire_date, int32 usage_limit, void edit_dialog_invite_link(DialogId dialog_id, const string &link, int32 expire_date, int32 usage_limit,
bool is_revoked, Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise); bool is_revoked, Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise);
void get_dialog_invite_links(DialogId dialog_id, UserId administrator_user_id, const string &offset_invite_link,
int32 limit, Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&promise);
void check_dialog_invite_link(const string &invite_link, Promise<Unit> &&promise) const; void check_dialog_invite_link(const string &invite_link, Promise<Unit> &&promise) const;
void import_dialog_invite_link(const string &invite_link, Promise<DialogId> &&promise); void import_dialog_invite_link(const string &invite_link, Promise<DialogId> &&promise);

View File

@ -6288,6 +6288,13 @@ void Td::on_request(uint64 id, td_api::editChatInviteLink &request) {
request.usage_limit_, request.is_revoked_, std::move(promise)); request.usage_limit_, request.is_revoked_, std::move(promise));
} }
void Td::on_request(uint64 id, td_api::getChatInviteLinks &request) {
CREATE_REQUEST_PROMISE();
CLEAN_INPUT_STRING(request.offset_invite_link_);
contacts_manager_->get_dialog_invite_links(DialogId(request.chat_id_), UserId(request.administrator_user_id_),
request.offset_invite_link_, request.limit_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::checkChatInviteLink &request) { void Td::on_request(uint64 id, td_api::checkChatInviteLink &request) {
CHECK_IS_USER(); CHECK_IS_USER();
CLEAN_INPUT_STRING(request.invite_link_); CLEAN_INPUT_STRING(request.invite_link_);

View File

@ -790,6 +790,8 @@ class Td final : public NetQueryCallback {
void on_request(uint64 id, td_api::editChatInviteLink &request); void on_request(uint64 id, td_api::editChatInviteLink &request);
void on_request(uint64 id, td_api::getChatInviteLinks &request);
void on_request(uint64 id, td_api::checkChatInviteLink &request); void on_request(uint64 id, td_api::checkChatInviteLink &request);
void on_request(uint64 id, td_api::joinChatByInviteLink &request); void on_request(uint64 id, td_api::joinChatByInviteLink &request);

View File

@ -2698,6 +2698,14 @@ class CliClient final : public Actor {
get_args(args, chat_id, invite_link, expire_date, usage_limit, is_revoked); get_args(args, chat_id, invite_link, expire_date, usage_limit, is_revoked);
send_request(td_api::make_object<td_api::editChatInviteLink>(as_chat_id(chat_id), invite_link, expire_date, send_request(td_api::make_object<td_api::editChatInviteLink>(as_chat_id(chat_id), invite_link, expire_date,
usage_limit, is_revoked)); usage_limit, is_revoked));
} else if (op == "gcil") {
string chat_id;
string administrator_user_id;
string offset_invite_link;
string limit;
get_args(args, chat_id, administrator_user_id, offset_invite_link, limit);
send_request(td_api::make_object<td_api::getChatInviteLinks>(
as_chat_id(chat_id), as_user_id(administrator_user_id), offset_invite_link, as_limit(limit)));
} else if (op == "ccil") { } else if (op == "ccil") {
send_request(td_api::make_object<td_api::checkChatInviteLink>(args)); send_request(td_api::make_object<td_api::checkChatInviteLink>(args));
} else if (op == "jcbil") { } else if (op == "jcbil") {
@ -3691,7 +3699,7 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::unpinAllChatMessages>(as_chat_id(chat_id))); send_request(td_api::make_object<td_api::unpinAllChatMessages>(as_chat_id(chat_id)));
} else if (op == "grib") { } else if (op == "grib") {
send_request(td_api::make_object<td_api::getRecentInlineBots>()); send_request(td_api::make_object<td_api::getRecentInlineBots>());
} else if (op == "spc" || op == "su" || op == "sch") { } else if (op == "spc" || op == "su") {
send_request(td_api::make_object<td_api::searchPublicChat>(args)); send_request(td_api::make_object<td_api::searchPublicChat>(args));
} else if (op == "spcs") { } else if (op == "spcs") {
send_request(td_api::make_object<td_api::searchPublicChats>(args)); send_request(td_api::make_object<td_api::searchPublicChats>(args));