2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/Location.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-10-17 21:47:47 +02:00
|
|
|
double Location::fix_accuracy(double accuracy) {
|
|
|
|
if (!std::isfinite(accuracy) || accuracy <= 0.0) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
2020-10-30 15:01:06 +01:00
|
|
|
if (accuracy >= 1500.0) {
|
|
|
|
return 1500.0;
|
2020-10-17 21:47:47 +02:00
|
|
|
}
|
|
|
|
return accuracy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Location::init(double latitude, double longitude, double horizontal_accuracy, int64 access_hash) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (std::isfinite(latitude) && std::isfinite(longitude) && std::abs(latitude) <= 90 && std::abs(longitude) <= 180) {
|
|
|
|
is_empty_ = false;
|
|
|
|
latitude_ = latitude;
|
|
|
|
longitude_ = longitude;
|
2020-10-17 21:47:47 +02:00
|
|
|
horizontal_accuracy_ = fix_accuracy(horizontal_accuracy);
|
2018-06-25 23:10:53 +02:00
|
|
|
access_hash_ = access_hash;
|
2018-08-14 01:11:49 +02:00
|
|
|
G()->add_location_access_hash(latitude_, longitude_, access_hash_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:47:47 +02:00
|
|
|
Location::Location(double latitude, double longitude, double horizontal_accuracy, int64 access_hash) {
|
|
|
|
init(latitude, longitude, horizontal_accuracy, access_hash);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Location::Location(const tl_object_ptr<secret_api::decryptedMessageMediaGeoPoint> &geo_point)
|
2020-10-17 21:47:47 +02:00
|
|
|
: Location(geo_point->lat_, geo_point->long_, 0.0, 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Location::Location(const tl_object_ptr<telegram_api::GeoPoint> &geo_point_ptr) {
|
|
|
|
if (geo_point_ptr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (geo_point_ptr->get_id()) {
|
|
|
|
case telegram_api::geoPointEmpty::ID:
|
|
|
|
break;
|
|
|
|
case telegram_api::geoPoint::ID: {
|
|
|
|
auto geo_point = static_cast<const telegram_api::geoPoint *>(geo_point_ptr.get());
|
2020-10-17 21:47:47 +02:00
|
|
|
init(geo_point->lat_, geo_point->long_, geo_point->accuracy_radius_, geo_point->access_hash_);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Location::Location(const tl_object_ptr<td_api::location> &location) {
|
|
|
|
if (location == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:47:47 +02:00
|
|
|
init(location->latitude_, location->longitude_, location->horizontal_accuracy_, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Location::empty() const {
|
|
|
|
return is_empty_;
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:18:27 +02:00
|
|
|
bool Location::is_valid_map_point() const {
|
|
|
|
const double MAX_VALID_MAP_LATITUDE = 85.05112877;
|
|
|
|
return !empty() && std::abs(latitude_) <= MAX_VALID_MAP_LATITUDE;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<td_api::location> Location::get_location_object() const {
|
|
|
|
if (empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-17 21:47:47 +02:00
|
|
|
return make_tl_object<td_api::location>(latitude_, longitude_, horizontal_accuracy_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputGeoPoint> Location::get_input_geo_point() const {
|
|
|
|
if (empty()) {
|
|
|
|
return make_tl_object<telegram_api::inputGeoPointEmpty>();
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:47:47 +02:00
|
|
|
int32 flags = 0;
|
|
|
|
if (horizontal_accuracy_ > 0) {
|
|
|
|
flags |= telegram_api::inputGeoPoint::ACCURACY_RADIUS_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_tl_object<telegram_api::inputGeoPoint>(flags, latitude_, longitude_,
|
|
|
|
static_cast<int32>(std::ceil(horizontal_accuracy_)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::inputMediaGeoPoint> Location::get_input_media_geo_point() const {
|
|
|
|
return make_tl_object<telegram_api::inputMediaGeoPoint>(get_input_geo_point());
|
|
|
|
}
|
|
|
|
|
|
|
|
SecretInputMedia Location::get_secret_input_media_geo_point() const {
|
|
|
|
return SecretInputMedia{nullptr, make_tl_object<secret_api::decryptedMessageMediaGeoPoint>(latitude_, longitude_)};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Location &lhs, const Location &rhs) {
|
|
|
|
if (lhs.is_empty_) {
|
|
|
|
return rhs.is_empty_;
|
|
|
|
}
|
|
|
|
return !rhs.is_empty_ && std::abs(lhs.latitude_ - rhs.latitude_) < 1e-6 &&
|
2020-10-17 21:47:47 +02:00
|
|
|
std::abs(lhs.longitude_ - rhs.longitude_) < 1e-6 &&
|
|
|
|
std::abs(lhs.horizontal_accuracy_ - rhs.horizontal_accuracy_) < 1e-6;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Location &lhs, const Location &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const Location &location) {
|
|
|
|
if (location.empty()) {
|
|
|
|
return string_builder << "Location[empty]";
|
|
|
|
}
|
|
|
|
return string_builder << "Location[latitude = " << location.latitude_ << ", longitude = " << location.longitude_
|
2020-10-17 21:47:47 +02:00
|
|
|
<< ", accuracy = " << location.horizontal_accuracy_ << "]";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 22:08:28 +02:00
|
|
|
Result<InputMessageLocation> process_input_message_location(
|
2018-09-28 22:57:34 +02:00
|
|
|
tl_object_ptr<td_api::InputMessageContent> &&input_message_content) {
|
|
|
|
CHECK(input_message_content != nullptr);
|
|
|
|
CHECK(input_message_content->get_id() == td_api::inputMessageLocation::ID);
|
|
|
|
auto input_location = static_cast<const td_api::inputMessageLocation *>(input_message_content.get());
|
|
|
|
|
|
|
|
Location location(input_location->location_);
|
|
|
|
if (location.empty()) {
|
|
|
|
return Status::Error(400, "Wrong location specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int32 MIN_LIVE_LOCATION_PERIOD = 60; // seconds, server side limit
|
|
|
|
constexpr int32 MAX_LIVE_LOCATION_PERIOD = 86400; // seconds, server side limit
|
|
|
|
|
|
|
|
auto period = input_location->live_period_;
|
|
|
|
if (period != 0 && (period < MIN_LIVE_LOCATION_PERIOD || period > MAX_LIVE_LOCATION_PERIOD)) {
|
|
|
|
return Status::Error(400, "Wrong live location period specified");
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:08:28 +02:00
|
|
|
constexpr int32 MIN_LIVE_LOCATION_HEADING = 1; // degrees, server side limit
|
|
|
|
constexpr int32 MAX_LIVE_LOCATION_HEADING = 360; // degrees, server side limit
|
|
|
|
|
|
|
|
auto heading = input_location->heading_;
|
|
|
|
if (heading != 0 && (heading < MIN_LIVE_LOCATION_HEADING || heading > MAX_LIVE_LOCATION_HEADING)) {
|
|
|
|
return Status::Error(400, "Wrong live location heading specified");
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:51:20 +01:00
|
|
|
constexpr int32 MAX_PROXIMITY_ALERT_RADIUS = 100000; // meters, server side limit
|
|
|
|
auto proximity_alert_radius = input_location->proximity_alert_radius_;
|
|
|
|
if (proximity_alert_radius < 0 || proximity_alert_radius > MAX_PROXIMITY_ALERT_RADIUS) {
|
|
|
|
return Status::Error(400, "Wrong live location proximity alert radius specified");
|
2020-10-24 23:49:41 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 15:39:09 +01:00
|
|
|
return InputMessageLocation(std::move(location), period, heading, proximity_alert_radius);
|
2018-09-28 22:57:34 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|