From acf8afd2d68da20f9aef5437069693138f6009dc Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 14 Oct 2019 19:07:31 +0300 Subject: [PATCH] Add td_api::setChatLocation. GitOrigin-RevId: 1bf8060374189bd2c9435b9c57ed499723de7246 --- td/generate/scheme/td_api.tl | 6 +- td/generate/scheme/td_api.tlo | Bin 161608 -> 161732 bytes td/telegram/ContactsManager.cpp | 100 +++++++++++++++++++++++++++++++- td/telegram/ContactsManager.h | 4 ++ td/telegram/DialogLocation.h | 2 +- td/telegram/Td.cpp | 7 +++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 9 +++ 8 files changed, 125 insertions(+), 5 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2ddb3c86c..98eb80d6b 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -288,7 +288,7 @@ botCommand command:string description:string = BotCommand; botInfo description:string commands:vector = BotInfo; -//@description Represents a location of a chat @location The location @address Location address; as defined by the chat creator +//@description Represents a location of a chat @location The location @address Location address; 1-64 characters, as defined by the chat creator chatLocation location:location address:string = ChatLocation; @@ -3442,12 +3442,16 @@ setChatDescription chat_id:int53 description:string = Ok; //-If new chat members don't have access to old messages in the supergroup, then toggleSupergroupIsAllHistoryAvailable needs to be used first to change that setChatDiscussionGroup chat_id:int53 discussion_chat_id:int53 = Ok; +//@description Changes the lcoation of a chat. Available only for location-based supergroups, see supergroupFullInfo.can_set_location @chat_id Chat identifier @location New location for the chat; must be valid and not null +setChatLocation chat_id:int53 location:chatLocation = Ok; + //@description Pins a message in a chat; requires can_pin_messages rights @chat_id Identifier of the chat @message_id Identifier of the new pinned message @disable_notification True, if there should be no notification about the pinned message pinChatMessage chat_id:int53 message_id:int53 disable_notification:Bool = Ok; //@description Removes the pinned message from a chat; requires can_pin_messages rights in the group or channel @chat_id Identifier of the chat unpinChatMessage chat_id:int53 = Ok; + //@description Adds current user as a new member to a chat. Private and secret chats can't be joined using this method @chat_id Chat identifier joinChat chat_id:int53 = Ok; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 99552a216261c616d8eed9adfeb555470ec0538c..bdd6a1d1acc74bdc94665ac46b81d85a6aa1398e 100644 GIT binary patch delta 61 zcmV-D0K)&s?g_;234pW#q{#tex24Ger;QY<9Y@j+b7gcxXkm0rZ)0I}X>V?qP$>d1 Tmnh=_D3^fB0T8!LR|5PWQJfiC delta 25 hcmX@|p7X>z&W0_FOHMK-ZC`qlae43d6X8sMX649)-m diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 77315c083..09748eb73 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1291,6 +1291,44 @@ class SetDiscussionGroupQuery : public Td::ResultHandler { } }; +class EditLocationQuery : public Td::ResultHandler { + Promise promise_; + ChannelId channel_id_; + DialogLocation location_; + + public: + explicit EditLocationQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(ChannelId channel_id, const DialogLocation &location) { + channel_id_ = channel_id; + location_ = location; + + auto input_channel = td->contacts_manager_->get_input_channel(channel_id); + CHECK(input_channel != nullptr); + + send_query(G()->net_query_creator().create(create_storer(telegram_api::channels_editLocation( + std::move(input_channel), location_.get_input_geo_point(), location_.get_address())))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + bool result = result_ptr.move_as_ok(); + LOG_IF(INFO, !result) << "Edit chat location has failed"; + + td->contacts_manager_->on_update_channel_location(channel_id_, location_); + promise_.set_value(Unit()); + } + + void on_error(uint64 id, Status status) override { + promise_.set_error(std::move(status)); + } +}; + class ReportChannelSpamQuery : public Td::ResultHandler { Promise promise_; ChannelId channel_id_; @@ -4751,6 +4789,38 @@ void ContactsManager::set_channel_discussion_group(DialogId dialog_id, DialogId std::move(group_input_channel)); } +void ContactsManager::set_channel_location(DialogId dialog_id, const DialogLocation &location, + Promise &&promise) { + if (location.empty()) { + return promise.set_error(Status::Error(400, "Invalid chat location specified")); + } + + if (!dialog_id.is_valid()) { + return promise.set_error(Status::Error(400, "Invalid chat specified")); + } + if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + return promise.set_error(Status::Error(400, "Chat not found")); + } + + if (dialog_id.get_type() != DialogType::Channel) { + return promise.set_error(Status::Error(400, "Chat is not a supergroup")); + } + + auto channel_id = dialog_id.get_channel_id(); + const Channel *c = get_channel(channel_id); + if (c == nullptr) { + return promise.set_error(Status::Error(400, "Chat info not found")); + } + if (!c->is_megagroup) { + return promise.set_error(Status::Error(400, "Chat is not a supergroup")); + } + if (!c->status.is_creator()) { + return promise.set_error(Status::Error(400, "Have not enough rights in the supergroup")); + } + + td_->create_handler(std::move(promise))->send(channel_id, location); +} + void ContactsManager::report_channel_spam(ChannelId channel_id, UserId user_id, const vector &message_ids, Promise &&promise) { auto c = get_channel(channel_id); @@ -7698,7 +7768,6 @@ void ContactsManager::on_get_chat_full(tl_object_ptr &&c if (channel_full->stickerset_ != nullptr) { sticker_set_id = td_->stickers_manager_->on_get_sticker_set(std::move(channel_full->stickerset_), true); } - auto location = DialogLocation(std::move(channel_full->location_)); ChannelFull *channel = &channels_full_[channel_id]; channel->expires_at = Time::now() + CHANNEL_FULL_EXPIRE_TIME; @@ -7707,7 +7776,7 @@ void ContactsManager::on_get_chat_full(tl_object_ptr &&c channel->banned_count != banned_count || channel->can_get_participants != can_get_participants || channel->can_set_username != can_set_username || channel->can_set_sticker_set != can_set_sticker_set || channel->can_set_location != can_set_location || channel->can_view_statistics != can_view_statistics || - channel->sticker_set_id != sticker_set_id || channel->location != location || channel->is_all_history_available != is_all_history_available) { + channel->sticker_set_id != sticker_set_id || channel->is_all_history_available != is_all_history_available) { channel->description = std::move(channel_full->about_); channel->participant_count = participant_count; channel->administrator_count = administrator_count; @@ -7720,7 +7789,6 @@ void ContactsManager::on_get_chat_full(tl_object_ptr &&c channel->can_view_statistics = can_view_statistics; channel->is_all_history_available = is_all_history_available; channel->sticker_set_id = sticker_set_id; - channel->location = std::move(location); channel->is_changed = true; @@ -7782,6 +7850,8 @@ void ContactsManager::on_get_chat_full(tl_object_ptr &&c } on_update_channel_full_linked_channel_id(channel, channel_id, linked_channel_id); + on_update_channel_full_location(channel, channel_id, DialogLocation(std::move(channel_full->location_))); + ChatId migrated_from_chat_id; MessageId migrated_from_max_message_id; @@ -8929,6 +8999,22 @@ void ContactsManager::on_update_channel_full_linked_channel_id(ChannelFull *chan } } +void ContactsManager::on_update_channel_full_location(ChannelFull *channel_full, ChannelId channel_id, + const DialogLocation &location) { + if (channel_full->location != location) { + channel_full->location = location; + channel_full->is_changed = true; + } + + Channel *c = get_channel(channel_id); + CHECK(c != nullptr); + if (location.empty() == c->has_location) { + c->has_location = !location.empty(); + c->need_send_update = true; + update_channel(c, channel_id); + } +} + void ContactsManager::on_get_dialog_invite_link_info(const string &invite_link, tl_object_ptr &&chat_invite_ptr) { auto &invite_link_info = invite_link_infos_[invite_link]; @@ -9691,6 +9777,14 @@ void ContactsManager::on_update_channel_linked_channel_id(ChannelId channel_id, } } +void ContactsManager::on_update_channel_location(ChannelId channel_id, const DialogLocation &location) { + auto channel_full = get_channel_full_force(channel_id); + if (channel_full != nullptr) { + on_update_channel_full_location(channel_full, channel_id, location); + update_channel_full(channel_full, channel_id); + } +} + void ContactsManager::on_update_channel_is_all_history_available(ChannelId channel_id, bool is_all_history_available) { if (!channel_id.is_valid()) { LOG(ERROR) << "Receive invalid " << channel_id; diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 4f224fb98..33aad20b6 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -175,6 +175,7 @@ class ContactsManager : public Actor { void on_update_channel_description(ChannelId channel_id, string &&description); void on_update_channel_sticker_set(ChannelId channel_id, StickerSetId sticker_set_id); void on_update_channel_linked_channel_id(ChannelId channel_id, ChannelId group_channel_id); + void on_update_channel_location(ChannelId channel_id, const DialogLocation &location); void on_update_channel_is_all_history_available(ChannelId channel_id, bool is_all_history_available); void on_update_channel_default_permissions(ChannelId channel_id, RestrictedRights default_permissions); @@ -308,6 +309,8 @@ class ContactsManager : public Actor { void set_channel_discussion_group(DialogId dialog_id, DialogId discussion_dialog_id, Promise &&promise); + void set_channel_location(DialogId dialog_id, const DialogLocation &location, Promise &&promise); + void report_channel_spam(ChannelId channel_id, UserId user_id, const vector &message_ids, Promise &&promise); @@ -1000,6 +1003,7 @@ class ContactsManager : public Actor { tl_object_ptr &&invite_link_ptr); void on_update_channel_full_linked_channel_id(ChannelFull *channel_full, ChannelId channel_id, ChannelId linked_channel_id); + void on_update_channel_full_location(ChannelFull *channel_full, ChannelId channel_id, const DialogLocation &location); static bool speculative_add_count(int32 &count, int32 new_count); diff --git a/td/telegram/DialogLocation.h b/td/telegram/DialogLocation.h index ea0d917ba..8a0b95b6b 100644 --- a/td/telegram/DialogLocation.h +++ b/td/telegram/DialogLocation.h @@ -28,7 +28,7 @@ class DialogLocation { public: DialogLocation() = default; - DialogLocation(telegram_api::object_ptr &&channel_location_ptr); + explicit DialogLocation(telegram_api::object_ptr &&channel_location_ptr); explicit DialogLocation(td_api::object_ptr &&chat_location); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 6b85db1b4..fc12864cc 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6057,6 +6057,13 @@ void Td::on_request(uint64 id, const td_api::setChatDiscussionGroup &request) { std::move(promise)); } +void Td::on_request(uint64 id, td_api::setChatLocation &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + contacts_manager_->set_channel_location(DialogId(request.chat_id_), DialogLocation(std::move(request.location_)), + std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::pinChatMessage &request) { CREATE_OK_REQUEST_PROMISE(); messages_manager_->pin_dialog_message(DialogId(request.chat_id_), MessageId(request.message_id_), diff --git a/td/telegram/Td.h b/td/telegram/Td.h index eb7d666ed..9f12a8af2 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -670,6 +670,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, const td_api::setChatDiscussionGroup &request); + void on_request(uint64 id, td_api::setChatLocation &request); + void on_request(uint64 id, const td_api::pinChatMessage &request); void on_request(uint64 id, const td_api::unpinChatMessage &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index e11414ed9..5d4063307 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3496,6 +3496,15 @@ class CliClient final : public Actor { std::tie(chat_id, group_chat_id) = split(args); send_request(td_api::make_object(as_chat_id(chat_id), as_chat_id(group_chat_id))); + } else if (op == "scl") { + string chat_id; + string latitude; + string longitude; + + std::tie(chat_id, args) = split(args); + std::tie(latitude, longitude) = split(args); + send_request(td_api::make_object( + as_chat_id(chat_id), td_api::make_object(as_location(latitude, longitude), "address"))); } else if (op == "pcm" || op == "pcms") { string chat_id; string message_id;