From 8d5c17036a3023b77469b9d43264bacf94fedeaf Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 14 Aug 2018 02:11:49 +0300 Subject: [PATCH] Use Location access hashes. GitOrigin-RevId: 1f018d5e42e1c657492f2e1da74700632825c8f9 --- td/generate/scheme/td_api.tl | 2 +- td/telegram/Global.cpp | 35 +++++++++++++++++++++++ td/telegram/Global.h | 9 ++++++ td/telegram/Location.cpp | 2 ++ td/telegram/Location.h | 12 ++++---- td/telegram/files/FileGenerateManager.cpp | 4 +-- 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 77af12dd9..e703b67c1 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3277,7 +3277,7 @@ setStickerPositionInSet sticker:InputFile position:int32 = Ok; removeStickerFromSet sticker:InputFile = Ok; -//@description Returns information about a file with a map thumbnail @location Location of the map center @zoom Map zoom level; 13-20 @width Map width in pixels before applying scale; 16-1024 @height Map height in pixels before applying scale; 16-1024 @scale Map scale; 1-3 @chat_id Identifier of a chat, in which the thumbnail will be shown. Use 0 if unknown +//@description Returns information about a file with a map thumbnail in PNG format @location Location of the map center @zoom Map zoom level; 13-20 @width Map width in pixels before applying scale; 16-1024 @height Map height in pixels before applying scale; 16-1024 @scale Map scale; 1-3 @chat_id Identifier of a chat, in which the thumbnail will be shown. Use 0 if unknown getMapThumbnailFile location:location zoom:int32 width:int32 height:int32 scale:int32 chat_id:int53 = File; diff --git a/td/telegram/Global.cpp b/td/telegram/Global.cpp index 992b5ae98..e5b975342 100644 --- a/td/telegram/Global.cpp +++ b/td/telegram/Global.cpp @@ -21,6 +21,8 @@ #include "td/utils/port/Clocks.h" #include "td/utils/tl_helpers.h" +#include + namespace td { Global::Global() = default; @@ -120,4 +122,37 @@ void Global::set_shared_config(std::unique_ptr shared_config) { shared_config_ = std::move(shared_config); } +int64 Global::get_location_key(double latitude, double longitude) { + const double PI = 3.14159265358979323846; + latitude *= PI / 180; + longitude *= PI / 180; + + int64 key = 0; + if (latitude < 0) { + latitude = -latitude; + key = 65536; + } + + double f = std::tan(PI / 4 - latitude / 2); + key += static_cast(f * std::cos(longitude) * 128) * 256; + key += static_cast(f * std::sin(longitude) * 128); + return key; +} + +int64 Global::get_location_access_hash(double latitude, double longitude) { + auto it = location_access_hashes_.find(get_location_key(latitude, longitude)); + if (it == location_access_hashes_.end()) { + return 0; + } + return it->second; +} + +void Global::add_location_access_hash(double latitude, double longitude, int64 access_hash) { + if (access_hash == 0) { + return; + } + + location_access_hashes_[get_location_key(latitude, longitude)] = access_hash; +} + } // namespace td diff --git a/td/telegram/Global.h b/td/telegram/Global.h index 55b6ad602..444ec68da 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -28,6 +28,7 @@ #include #include #include +#include namespace td { class AnimationsManager; @@ -301,6 +302,10 @@ class Global : public ActorContext { net_stats_file_callbacks_ = std::move(callbacks); } + int64 get_location_access_hash(double latitude, double longitude); + + void add_location_access_hash(double latitude, double longitude, int64 access_hash); + private: std::shared_ptr dh_config_; @@ -346,6 +351,10 @@ class Global : public ActorContext { int32 my_id_ = 0; // hack + static int64 get_location_key(double latitude, double longitude); + + std::unordered_map location_access_hashes_; + void do_close(Promise<> on_finish, bool destroy_flag); }; diff --git a/td/telegram/Location.cpp b/td/telegram/Location.cpp index b499ddb5a..8702dcb5d 100644 --- a/td/telegram/Location.cpp +++ b/td/telegram/Location.cpp @@ -6,6 +6,7 @@ // #include "td/telegram/Location.h" +#include "td/telegram/Global.h" #include "td/telegram/secret_api.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" @@ -23,6 +24,7 @@ void Location::init(double latitude, double longitude, int64 access_hash) { latitude_ = latitude; longitude_ = longitude; access_hash_ = access_hash; + G()->add_location_access_hash(latitude_, longitude_, access_hash_); } } diff --git a/td/telegram/Location.h b/td/telegram/Location.h index 8479e5f3c..33c593808 100644 --- a/td/telegram/Location.h +++ b/td/telegram/Location.h @@ -6,16 +6,17 @@ // #pragma once -#include "td/utils/common.h" -#include "td/utils/StringBuilder.h" -#include "td/utils/tl_helpers.h" +#include "td/telegram/Global.h" +#include "td/telegram/SecretInputMedia.h" +#include "td/telegram/Version.h" #include "td/telegram/secret_api.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" -#include "td/telegram/SecretInputMedia.h" -#include "td/telegram/Version.h" +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" +#include "td/utils/tl_helpers.h" namespace td { @@ -98,6 +99,7 @@ class Location { parse(longitude_, parser); if (has_access_hash) { parse(access_hash_, parser); + G()->add_location_access_hash(latitude_, longitude_, access_hash_); } } }; diff --git a/td/telegram/files/FileGenerateManager.cpp b/td/telegram/files/FileGenerateManager.cpp index 4e4b5dd21..fd0c85789 100644 --- a/td/telegram/files/FileGenerateManager.cpp +++ b/td/telegram/files/FileGenerateManager.cpp @@ -152,7 +152,6 @@ class MapDownloadGenerateActor : public FileGenerateActor { TRY_RESULT(width, to_integer_safe(parts[5])); TRY_RESULT(height, to_integer_safe(parts[6])); TRY_RESULT(scale, to_integer_safe(parts[7])); - int64 access_hash = 0; if (zoom < 13 || zoom > 20) { return Status::Error("Wrong zoom"); @@ -171,12 +170,13 @@ class MapDownloadGenerateActor : public FileGenerateActor { return Status::Error("Wrong scale"); } - file_name_ = PSTRING() << "map_" << zoom << "_" << x << "_" << y << ".jpg"; + file_name_ = PSTRING() << "map_" << zoom << "_" << x << "_" << y << ".png"; const double PI = 3.14159265358979323846; double longitude = (x + 0.1) * 360.0 / size - 180; double latitude = 90 - 360 * std::atan(std::exp(((y + 0.1) / size - 0.5) * 2 * PI)) / PI; + int64 access_hash = G()->get_location_access_hash(latitude, longitude); return make_tl_object( make_tl_object(latitude, longitude), access_hash, width, height, zoom, scale); }