Support other_user_ids in registerDevice.

GitOrigin-RevId: 7d14fc21be9ebb56f3d759746f6a11cd5bd78c87
This commit is contained in:
levlam 2018-01-10 03:18:03 +03:00
parent c5fb5f1336
commit 3eebb16020
9 changed files with 201 additions and 113 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
project(TDLib VERSION 1.0.0 LANGUAGES CXX C) project(TDLib VERSION 1.0.1 LANGUAGES CXX C)
if (NOT DEFINED CMAKE_MODULE_PATH) if (NOT DEFINED CMAKE_MODULE_PATH)
set(CMAKE_MODULE_PATH "") set(CMAKE_MODULE_PATH "")

View File

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(TdExample VERSION 1.0 LANGUAGES CXX) project(TdExample VERSION 1.0 LANGUAGES CXX)
find_package(Td 1.0.0) find_package(Td 1.0.1)
add_executable(tdjson_example tdjson_example.cpp) add_executable(tdjson_example tdjson_example.cpp)
target_link_libraries(tdjson_example PRIVATE Td::TdJson) target_link_libraries(tdjson_example PRIVATE Td::TdJson)

View File

@ -2613,8 +2613,8 @@ getSupportUser = User;
getWallpapers = Wallpapers; getWallpapers = Wallpapers;
//@description Registers the currently used device for receiving push notifications @device_token Device token //@description Registers the currently used device for receiving push notifications @device_token Device token @other_user_ids List of at most 100 user identifiers of other users currently using the client
registerDevice device_token:DeviceToken = Ok; registerDevice device_token:DeviceToken other_user_ids:vector<int32> = Ok;
//@description Returns t.me URLs recently visited by a newly registered user @referrer Google Play referrer to identify the user //@description Returns t.me URLs recently visited by a newly registered user @referrer Google Play referrer to identify the user

Binary file not shown.

View File

@ -9,6 +9,7 @@
#include "td/telegram/Global.h" #include "td/telegram/Global.h"
#include "td/telegram/misc.h" #include "td/telegram/misc.h"
#include "td/telegram/net/NetQueryDispatcher.h" #include "td/telegram/net/NetQueryDispatcher.h"
#include "td/telegram/UserId.h"
#include "td/telegram/td_api.hpp" #include "td/telegram/td_api.hpp"
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
@ -16,120 +17,197 @@
#include "td/utils/format.h" #include "td/utils/format.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/Status.h" #include "td/utils/Status.h"
#include "td/utils/tl_helpers.h"
#include <type_traits> #include <type_traits>
namespace td { namespace td {
void DeviceTokenManager::register_device(tl_object_ptr<td_api::DeviceToken> device_token,
Promise<tl_object_ptr<td_api::ok>> promise) {
Token token(*device_token);
if (!clean_input_string(token.token)) {
return promise.set_error(Status::Error(400, "Device token must be encoded in UTF-8"));
}
auto &info = tokens_[token.type]; template <class StorerT>
info.net_query_id = 0; void DeviceTokenManager::TokenInfo::store(StorerT &storer) const {
if (token.token.empty()) { using td::store;
info.state = TokenInfo::State::Unregister; bool has_other_user_ids = !other_user_ids.empty();
if (info.token.empty()) { bool is_sync = state == State::Sync;
info.state = TokenInfo::State::Sync; bool is_unregister = state == State::Unregister;
} bool is_register = state == State::Register;
BEGIN_STORE_FLAGS();
STORE_FLAG(has_other_user_ids);
STORE_FLAG(is_sync);
STORE_FLAG(is_unregister);
STORE_FLAG(is_register);
END_STORE_FLAGS();
store(token, storer);
if (has_other_user_ids) {
store(other_user_ids, storer);
}
}
template <class ParserT>
void DeviceTokenManager::TokenInfo::parse(ParserT &parser) {
using td::parse;
bool has_other_user_ids;
bool is_sync;
bool is_unregister;
bool is_register;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(has_other_user_ids);
PARSE_FLAG(is_sync);
PARSE_FLAG(is_unregister);
PARSE_FLAG(is_register);
END_PARSE_FLAGS();
CHECK(is_sync + is_unregister + is_register == 1);
if (is_sync) {
state = State::Sync;
} else if (is_unregister) {
state = State::Unregister;
} else { } else {
info.token = token.token; state = State::Register;
info.state = TokenInfo::State::Register;
} }
if (info.promise) { parse(token, parser);
info.promise.set_error(Status::Error(5, "Cancelled due to new registerDevice request")); if (has_other_user_ids) {
parse(other_user_ids, parser);
} }
info.promise = std::move(promise);
save_info(token.type);
loop();
} }
DeviceTokenManager::Token::Token(td_api::DeviceToken &device_token) { StringBuilder &operator<<(StringBuilder &string_builder, const DeviceTokenManager::TokenInfo &token_info) {
bool ok = downcast_call(device_token, [&](auto &obj) { token = obj.token_; }); switch (token_info.state) {
CHECK(ok); case DeviceTokenManager::TokenInfo::State::Sync:
type = [&] { string_builder << "Synchronized";
switch (device_token.get_id()) { break;
case td_api::deviceTokenApplePush::ID: case DeviceTokenManager::TokenInfo::State::Unregister:
return TokenType::APNS; string_builder << "Unregister";
case td_api::deviceTokenGoogleCloudMessaging::ID: break;
return TokenType::GCM; case DeviceTokenManager::TokenInfo::State::Register:
case td_api::deviceTokenMicrosoftPush::ID: string_builder << "Register";
return TokenType::MPNS; break;
case td_api::deviceTokenUbuntuPush::ID:
return TokenType::UbuntuPhone;
case td_api::deviceTokenBlackberryPush::ID:
return TokenType::Blackberry;
default:
UNREACHABLE();
}
}();
}
tl_object_ptr<td_api::DeviceToken> DeviceTokenManager::Token::as_td_api() {
switch (type) {
case TokenType::APNS:
return make_tl_object<td_api::deviceTokenApplePush>(token);
case TokenType::GCM:
return make_tl_object<td_api::deviceTokenGoogleCloudMessaging>(token);
case TokenType::MPNS:
return make_tl_object<td_api::deviceTokenMicrosoftPush>(token);
case TokenType::SimplePush:
return make_tl_object<td_api::deviceTokenSimplePush>(token);
case TokenType::UbuntuPhone:
return make_tl_object<td_api::deviceTokenUbuntuPush>(token);
case TokenType::Blackberry:
return make_tl_object<td_api::deviceTokenBlackberryPush>(token);
default: default:
UNREACHABLE(); UNREACHABLE();
} }
} string_builder << " token \"" << format::escaped(token_info.token) << "\"";
DeviceTokenManager::TokenInfo::TokenInfo(string from) { if (!token_info.other_user_ids.empty()) {
if (from.empty()) { string_builder << ", with other users " << token_info.other_user_ids;
return;
} }
char c = from[0]; return string_builder;
if (c == '+') {
state = State::Register;
} else if (c == '-') {
state = State::Unregister;
} else if (c == '=') {
state = State::Sync;
} else {
LOG(ERROR) << "Invalid serialized TokenInfo: " << tag("token_info", from);
return;
}
token = from.substr(1);
} }
string DeviceTokenManager::TokenInfo::serialize() { void DeviceTokenManager::register_device(tl_object_ptr<td_api::DeviceToken> device_token_ptr,
char c = [&] { vector<int32> other_user_ids, Promise<tl_object_ptr<td_api::ok>> promise) {
switch (state) { CHECK(device_token_ptr != nullptr);
case State::Sync: TokenType token_type;
return '='; string token;
case State::Unregister: switch (device_token_ptr->get_id()) {
return '-'; case td_api::deviceTokenApplePush::ID: {
case State::Register: auto device_token = static_cast<td_api::deviceTokenApplePush *>(device_token_ptr.get());
return '+'; token = std::move(device_token->token_);
default: token_type = TokenType::APNS;
UNREACHABLE(); break;
} }
}(); case td_api::deviceTokenGoogleCloudMessaging::ID: {
return c + token; auto device_token = static_cast<td_api::deviceTokenGoogleCloudMessaging *>(device_token_ptr.get());
token = std::move(device_token->token_);
token_type = TokenType::GCM;
break;
}
case td_api::deviceTokenMicrosoftPush::ID: {
auto device_token = static_cast<td_api::deviceTokenMicrosoftPush *>(device_token_ptr.get());
token = std::move(device_token->token_);
token_type = TokenType::MPNS;
break;
}
case td_api::deviceTokenSimplePush::ID: {
auto device_token = static_cast<td_api::deviceTokenSimplePush *>(device_token_ptr.get());
token = std::move(device_token->token_);
token_type = TokenType::SimplePush;
break;
}
case td_api::deviceTokenUbuntuPush::ID: {
auto device_token = static_cast<td_api::deviceTokenUbuntuPush *>(device_token_ptr.get());
token = std::move(device_token->token_);
token_type = TokenType::UbuntuPhone;
break;
}
case td_api::deviceTokenBlackberryPush::ID: {
auto device_token = static_cast<td_api::deviceTokenBlackberryPush *>(device_token_ptr.get());
token = std::move(device_token->token_);
token_type = TokenType::Blackberry;
break;
}
default:
UNREACHABLE();
}
if (!clean_input_string(token)) {
return promise.set_error(Status::Error(400, "Device token must be encoded in UTF-8"));
}
for (auto &other_user_id : other_user_ids) {
UserId user_id(other_user_id);
if (!user_id.is_valid()) {
return promise.set_error(Status::Error(400, "Invalid user_id among other user_ids"));
}
}
if (other_user_ids.size() > MAX_OTHER_USER_IDS) {
return promise.set_error(Status::Error(400, "Too much other user_ids"));
}
auto &info = tokens_[token_type];
info.net_query_id = 0;
if (token.empty()) {
if (info.token.empty()) {
// already unregistered
return promise.set_value(make_tl_object<td_api::ok>());
}
info.state = TokenInfo::State::Unregister;
} else {
info.state = TokenInfo::State::Register;
info.token = std::move(token);
}
info.other_user_ids = std::move(other_user_ids);
info.promise.set_value(make_tl_object<td_api::ok>());
info.promise = std::move(promise);
save_info(token_type);
}
string DeviceTokenManager::get_database_key(int32 token_type) {
return PSTRING() << "device_token" << token_type;
} }
void DeviceTokenManager::start_up() { void DeviceTokenManager::start_up() {
for (int32 token_type = 1; token_type < TokenType::Size; token_type++) { for (int32 token_type = 1; token_type < TokenType::Size; token_type++) {
tokens_[token_type] = TokenInfo(G()->td_db()->get_binlog_pmc()->get(PSTRING() << "device_token" << token_type)); auto serialized = G()->td_db()->get_binlog_pmc()->get(get_database_key(token_type));
LOG(INFO) << token_type << "--->" << tokens_[token_type].serialize(); if (serialized.empty()) {
continue;
}
auto &token = tokens_[token_type];
char c = serialized[0];
if (c == '*') {
unserialize(token, serialized.substr(1)).ensure();
} else {
// legacy
if (c == '+') {
token.state = TokenInfo::State::Register;
} else if (c == '-') {
token.state = TokenInfo::State::Unregister;
} else if (c == '=') {
token.state = TokenInfo::State::Sync;
} else {
LOG(ERROR) << "Invalid serialized TokenInfo: " << format::escaped(serialized);
continue;
}
token.token = serialized.substr(1);
}
LOG(INFO) << "GET device token " << token_type << "--->" << tokens_[token_type];
} }
loop(); loop();
} }
void DeviceTokenManager::save_info(int32 token_type) { void DeviceTokenManager::save_info(int32 token_type) {
auto key = PSTRING() << "device_token" << token_type; LOG(INFO) << "SET device token " << token_type << "--->" << tokens_[token_type];
string value = tokens_[token_type].serialize(); if (tokens_[token_type].token.empty()) {
LOG(INFO) << "SET " << key << "--->" << value; G()->td_db()->get_binlog_pmc()->erase(get_database_key(token_type));
G()->td_db()->get_binlog_pmc()->set(key, value); } else {
G()->td_db()->get_binlog_pmc()->set(get_database_key(token_type), "*" + serialize(tokens_[token_type]));
}
sync_cnt_++; sync_cnt_++;
G()->td_db()->get_binlog_pmc()->force_sync( G()->td_db()->get_binlog_pmc()->force_sync(
PromiseCreator::event(self_closure(this, &DeviceTokenManager::dec_sync_cnt))); PromiseCreator::event(self_closure(this, &DeviceTokenManager::dec_sync_cnt)));
@ -154,12 +232,13 @@ void DeviceTokenManager::loop() {
} }
// have to send query // have to send query
NetQueryPtr net_query; NetQueryPtr net_query;
auto other_user_ids = info.other_user_ids;
if (info.state == TokenInfo::State::Unregister) { if (info.state == TokenInfo::State::Unregister) {
net_query = G()->net_query_creator().create( net_query = G()->net_query_creator().create(
create_storer(telegram_api::account_unregisterDevice(token_type, info.token, {}))); create_storer(telegram_api::account_unregisterDevice(token_type, info.token, std::move(other_user_ids))));
} else { } else {
net_query = G()->net_query_creator().create( net_query = G()->net_query_creator().create(create_storer(
create_storer(telegram_api::account_registerDevice(token_type, info.token, false, {}))); telegram_api::account_registerDevice(token_type, info.token, false, std::move(other_user_ids))));
} }
info.net_query_id = net_query->id(); info.net_query_id = net_query->id();
G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this, token_type)); G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this, token_type));
@ -209,6 +288,5 @@ void DeviceTokenManager::on_result(NetQueryPtr net_query) {
LOG(ERROR) << r_flag.error(); LOG(ERROR) << r_flag.error();
} }
} }
loop();
} }
} // namespace td } // namespace td

View File

@ -18,44 +18,48 @@
#include <array> #include <array>
namespace td { namespace td {
class DeviceTokenManager : public NetQueryCallback { class DeviceTokenManager : public NetQueryCallback {
public: public:
explicit DeviceTokenManager(ActorShared<> parent) : parent_(std::move(parent)) { explicit DeviceTokenManager(ActorShared<> parent) : parent_(std::move(parent)) {
} }
void register_device(tl_object_ptr<td_api::DeviceToken> device_token, Promise<tl_object_ptr<td_api::ok>> promise); void register_device(tl_object_ptr<td_api::DeviceToken> device_token_ptr, vector<int32> other_user_ids,
Promise<tl_object_ptr<td_api::ok>> promise);
private: private:
static constexpr size_t MAX_OTHER_USER_IDS = 100;
ActorShared<> parent_; ActorShared<> parent_;
enum TokenType : int32 { APNS = 1, GCM = 2, MPNS = 3, SimplePush = 4, UbuntuPhone = 5, Blackberry = 6, Size }; enum TokenType : int32 { APNS = 1, GCM = 2, MPNS = 3, SimplePush = 4, UbuntuPhone = 5, Blackberry = 6, Size };
struct Token {
TokenType type;
string token;
Token(TokenType type, string token) : type(type), token(std::move(token)) {
}
explicit Token(td_api::DeviceToken &device_token);
tl_object_ptr<td_api::DeviceToken> as_td_api();
};
struct TokenInfo { struct TokenInfo {
enum class State { Sync, Unregister, Register }; enum class State { Sync, Unregister, Register };
State state = State::Sync; State state = State::Sync;
string token; string token;
uint64 net_query_id = 0; uint64 net_query_id = 0;
vector<int32> other_user_ids;
Promise<tl_object_ptr<td_api::ok>> promise; Promise<tl_object_ptr<td_api::ok>> promise;
TokenInfo() = default; template <class StorerT>
explicit TokenInfo(string from); void store(StorerT &storer) const;
string serialize(); template <class ParserT>
void parse(ParserT &parser);
}; };
friend StringBuilder &operator<<(StringBuilder &string_builder, const TokenInfo &token_info);
std::array<TokenInfo, TokenType::Size> tokens_; std::array<TokenInfo, TokenType::Size> tokens_;
int32 sync_cnt_{0}; int32 sync_cnt_{0};
void start_up() override; void start_up() override;
static string get_database_key(int32 token_type);
void save_info(int32 token_type); void save_info(int32 token_type);
void dec_sync_cnt(); void dec_sync_cnt();
void loop() override; void loop() override;
void on_result(NetQueryPtr net_query) override; void on_result(NetQueryPtr net_query) override;
}; };
} // namespace td } // namespace td

View File

@ -4598,7 +4598,7 @@ void Td::on_request(uint64 id, td_api::registerDevice &request) {
CREATE_REQUEST_PROMISE(promise); CREATE_REQUEST_PROMISE(promise);
send_closure(device_token_manager_, &DeviceTokenManager::register_device, std::move(request.device_token_), send_closure(device_token_manager_, &DeviceTokenManager::register_device, std::move(request.device_token_),
std::move(promise)); std::move(request.other_user_ids_), std::move(promise));
} }
void Td::on_request(uint64 id, td_api::getUserPrivacySettingRules &request) { void Td::on_request(uint64 id, td_api::getUserPrivacySettingRules &request) {

View File

@ -187,7 +187,7 @@ class Td final : public NetQueryCallback {
static td_api::object_ptr<td_api::Object> static_request(td_api::object_ptr<td_api::Function> function); static td_api::object_ptr<td_api::Object> static_request(td_api::object_ptr<td_api::Function> function);
private: private:
static constexpr const char *tdlib_version = "1.0.0"; static constexpr const char *tdlib_version = "1.0.1";
static constexpr int32 ONLINE_TIMEOUT = 240; static constexpr int32 ONLINE_TIMEOUT = 240;
void send_result(uint64 id, tl_object_ptr<td_api::Object> object); void send_result(uint64 id, tl_object_ptr<td_api::Object> object);

View File

@ -1010,9 +1010,15 @@ class CliClient final : public Actor {
std::tie(dc_id, ip_port) = split(args); std::tie(dc_id, ip_port) = split(args);
send_request(make_tl_object<td_api::processDcUpdate>(dc_id, ip_port)); send_request(make_tl_object<td_api::processDcUpdate>(dc_id, ip_port));
} else if (op == "rdb" || op == "RegisterDeviceB") { } else if (op == "rdb" || op == "RegisterDeviceB") {
send_request(make_tl_object<td_api::registerDevice>(make_tl_object<td_api::deviceTokenBlackberryPush>(args))); send_request(make_tl_object<td_api::registerDevice>(make_tl_object<td_api::deviceTokenBlackberryPush>(args),
as_user_ids("")));
} else if (op == "rdu" || op == "RegisterDeviceU") { } else if (op == "rdu" || op == "RegisterDeviceU") {
send_request(make_tl_object<td_api::registerDevice>(make_tl_object<td_api::deviceTokenUbuntuPush>(args))); string token;
string other_user_ids_str;
std::tie(token, other_user_ids_str) = split(args);
send_request(make_tl_object<td_api::registerDevice>(make_tl_object<td_api::deviceTokenUbuntuPush>(token),
as_user_ids(other_user_ids_str)));
} else if (op == "gpf") { } else if (op == "gpf") {
string chat_id; string chat_id;
string message_id; string message_id;