Support initial folder for secret chats.

GitOrigin-RevId: 57894f3ff767a0b24bac08c8251f51f3e4722b6a
This commit is contained in:
levlam 2020-06-30 12:11:22 +03:00
parent de07f2151e
commit cf93b3c69c
9 changed files with 74 additions and 24 deletions

View File

@ -3896,9 +3896,11 @@ template <class StorerT>
void ContactsManager::SecretChat::store(StorerT &storer) const {
using td::store;
bool has_layer = layer > SecretChatActor::DEFAULT_LAYER;
bool has_initial_folder_id = initial_folder_id != FolderId();
BEGIN_STORE_FLAGS();
STORE_FLAG(is_outbound);
STORE_FLAG(has_layer);
STORE_FLAG(has_initial_folder_id);
END_STORE_FLAGS();
store(access_hash, storer);
@ -3910,15 +3912,20 @@ void ContactsManager::SecretChat::store(StorerT &storer) const {
if (has_layer) {
store(layer, storer);
}
if (has_initial_folder_id) {
store(initial_folder_id, storer);
}
}
template <class ParserT>
void ContactsManager::SecretChat::parse(ParserT &parser) {
using td::parse;
bool has_layer;
bool has_initial_folder_id;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(is_outbound);
PARSE_FLAG(has_layer);
PARSE_FLAG(has_initial_folder_id);
END_PARSE_FLAGS();
if (parser.version() >= static_cast<int32>(Version::AddAccessHashToSecretChat)) {
@ -3936,6 +3943,9 @@ void ContactsManager::SecretChat::parse(ParserT &parser) {
} else {
layer = SecretChatActor::DEFAULT_LAYER;
}
if (has_initial_folder_id) {
parse(initial_folder_id, parser);
}
}
tl_object_ptr<telegram_api::InputUser> ContactsManager::get_input_user(UserId user_id) const {
@ -4307,6 +4317,14 @@ int32 ContactsManager::get_secret_chat_layer(SecretChatId secret_chat_id) const
return c->layer;
}
FolderId ContactsManager::get_secret_chat_initial_folder_id(SecretChatId secret_chat_id) const {
auto c = get_secret_chat(secret_chat_id);
if (c == nullptr) {
return FolderId::main();
}
return c->initial_folder_id;
}
UserId ContactsManager::get_my_id() const {
LOG_IF(ERROR, !my_id_.is_valid()) << "Wrong or unknown my id returned";
return my_id_;
@ -12871,7 +12889,7 @@ bool ContactsManager::get_secret_chat(SecretChatId secret_chat_id, bool force, P
void ContactsManager::on_update_secret_chat(SecretChatId secret_chat_id, int64 access_hash, UserId user_id,
SecretChatState state, bool is_outbound, int32 ttl, int32 date,
string key_hash, int32 layer) {
string key_hash, int32 layer, FolderId initial_folder_id) {
LOG(INFO) << "Update " << secret_chat_id << " with " << user_id << " and access_hash " << access_hash;
auto *secret_chat = add_secret_chat(secret_chat_id);
if (access_hash != secret_chat->access_hash) {
@ -12914,6 +12932,10 @@ void ContactsManager::on_update_secret_chat(SecretChatId secret_chat_id, int64 a
secret_chat->layer = layer;
secret_chat->is_changed = true;
}
if (initial_folder_id != FolderId() && initial_folder_id != secret_chat->initial_folder_id) {
secret_chat->initial_folder_id = initial_folder_id;
secret_chat->is_changed = true;
}
update_secret_chat(secret_chat, secret_chat_id);
}

View File

@ -19,6 +19,7 @@
#include "td/telegram/DialogParticipant.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileSourceId.h"
#include "td/telegram/FolderId.h"
#include "td/telegram/Location.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/net/DcId.h"
@ -130,6 +131,7 @@ class ContactsManager : public Actor {
bool get_secret_chat_is_outbound(SecretChatId secret_chat_id) const;
SecretChatState get_secret_chat_state(SecretChatId secret_chat_id) const;
int32 get_secret_chat_layer(SecretChatId secret_chat_id) const;
FolderId get_secret_chat_initial_folder_id(SecretChatId secret_chat_id) const;
void on_imported_contacts(int64 random_id, vector<UserId> imported_contact_user_ids,
vector<int32> unimported_contact_invites);
@ -529,7 +531,8 @@ class ContactsManager : public Actor {
tl_object_ptr<td_api::secretChat> get_secret_chat_object(SecretChatId secret_chat_id);
void on_update_secret_chat(SecretChatId secret_chat_id, int64 access_hash, UserId user_id, SecretChatState state,
bool is_outbound, int32 ttl, int32 date, string key_hash, int32 layer);
bool is_outbound, int32 ttl, int32 date, string key_hash, int32 layer,
FolderId initial_folder_id);
tl_object_ptr<td_api::chatMember> get_chat_member_object(const DialogParticipant &dialog_participant) const;
@ -860,6 +863,7 @@ class ContactsManager : public Actor {
int32 ttl = 0;
int32 date = 0;
int32 layer = 0;
FolderId initial_folder_id;
bool is_outbound = false;

View File

@ -30034,8 +30034,7 @@ MessagesManager::Dialog *MessagesManager::add_new_dialog(unique_ptr<Dialog> &&d,
d->last_new_message_id = MessageId::min();
}
if (!d->notification_settings.is_secret_chat_show_preview_fixed &&
d->dialog_id.get_type() == DialogType::SecretChat) {
if (!d->notification_settings.is_secret_chat_show_preview_fixed) {
d->notification_settings.use_default_show_preview = true;
d->notification_settings.show_preview = false;
d->notification_settings.is_secret_chat_show_preview_fixed = true;
@ -30047,7 +30046,10 @@ MessagesManager::Dialog *MessagesManager::add_new_dialog(unique_ptr<Dialog> &&d,
d->is_last_read_inbox_message_id_inited = true;
d->is_last_read_outbox_message_id_inited = true;
d->is_pinned_message_id_inited = true;
if (!d->is_folder_id_inited) {
d->folder_id = td_->contacts_manager_->get_secret_chat_initial_folder_id(dialog_id.get_secret_chat_id());
d->is_folder_id_inited = true;
}
break;
case DialogType::None:
default:

View File

@ -1835,6 +1835,10 @@ Status SecretChatActor::on_update_chat(telegram_api::encryptedChatRequested &upd
auth_state_.date = context_->unix_time();
TRY_STATUS(save_common_info(update));
auth_state_.handshake.set_g_a(update.g_a_.as_slice());
if ((update.flags_ & telegram_api::encryptedChatRequested::FOLDER_ID_MASK) != 0) {
auth_state_.initial_folder_id = FolderId(update.folder_id_);
}
send_update_secret_chat();
return Status::OK();
}
@ -2014,7 +2018,8 @@ void SecretChatActor::send_update_secret_chat() {
state = SecretChatState::Waiting;
}
context_->on_update_secret_chat(auth_state_.access_hash, get_user_id(), state, auth_state_.x == 0, config_state_.ttl,
auth_state_.date, auth_state_.key_hash, current_layer());
auth_state_.date, auth_state_.key_hash, current_layer(),
auth_state_.initial_folder_id);
}
void SecretChatActor::on_outbound_action(secret_api::decryptedMessageActionSetMessageTTL &set_ttl) {

View File

@ -16,6 +16,7 @@
#include "td/mtproto/DhHandshake.h"
#include "td/telegram/DhConfig.h"
#include "td/telegram/FolderId.h"
#include "td/telegram/logevent/SecretChatEvent.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/net/NetQuery.h"
@ -82,7 +83,8 @@ class SecretChatActor : public NetQueryCallback {
virtual void send_net_query(NetQueryPtr query, ActorShared<NetQueryCallback> callback, bool ordered) = 0;
virtual void on_update_secret_chat(int64 access_hash, UserId user_id, SecretChatState state, bool is_outbound,
int32 ttl, int32 date, string key_hash, int32 layer) = 0;
int32 ttl, int32 date, string key_hash, int32 layer,
FolderId initial_folder_id) = 0;
// Promise must be set only after the update is processed.
//
@ -376,6 +378,8 @@ class SecretChatActor : public NetQueryCallback {
int32 date = 0;
FolderId initial_folder_id;
DhConfig dh_config;
DhHandshake handshake;
@ -385,14 +389,18 @@ class SecretChatActor : public NetQueryCallback {
template <class StorerT>
void store(StorerT &storer) const {
uint32 flags = 0;
bool date_flag = date != 0;
bool key_hash_flag = true;
if (date_flag) {
bool has_date = date != 0;
bool has_key_hash = true;
bool has_initial_folder_id = initial_folder_id != FolderId();
if (has_date) {
flags |= 1;
}
if (key_hash_flag) {
if (has_key_hash) {
flags |= 2;
}
if (has_initial_folder_id) {
flags |= 4;
}
storer.store_int((flags << 8) | static_cast<int32>(state));
storer.store_int(x);
@ -401,16 +409,19 @@ class SecretChatActor : public NetQueryCallback {
storer.store_int(user_id);
storer.store_long(user_access_hash);
storer.store_int(random_id);
if (date_flag) {
if (has_date) {
storer.store_int(date);
}
if (key_hash_flag) {
if (has_key_hash) {
storer.store_string(key_hash);
}
dh_config.store(storer);
if (state == State::SendRequest || state == State::WaitRequestResponse) {
handshake.store(storer);
}
if (has_initial_folder_id) {
initial_folder_id.store(storer);
}
}
template <class ParserT>
@ -418,8 +429,9 @@ class SecretChatActor : public NetQueryCallback {
uint32 tmp = parser.fetch_int();
state = static_cast<State>(tmp & 255);
uint32 flags = tmp >> 8;
bool date_flag = (flags & 1) != 0;
bool key_hash_flag = (flags & 2) != 0;
bool has_date = (flags & 1) != 0;
bool has_key_hash = (flags & 2) != 0;
bool has_initial_folder_id = (flags & 4) != 0;
x = parser.fetch_int();
@ -428,16 +440,19 @@ class SecretChatActor : public NetQueryCallback {
user_id = parser.fetch_int();
user_access_hash = parser.fetch_long();
random_id = parser.fetch_int();
if (date_flag) {
if (has_date) {
date = parser.fetch_int();
}
if (key_hash_flag) {
if (has_key_hash) {
key_hash = parser.template fetch_string<std::string>();
}
dh_config.parse(parser);
if (state == State::SendRequest || state == State::WaitRequestResponse) {
handshake.parse(parser);
}
if (has_initial_folder_id) {
initial_folder_id.parse(parser);
}
}
};

View File

@ -9,6 +9,7 @@
#include "td/telegram/ConfigShared.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DhCache.h"
#include "td/telegram/FolderId.h"
#include "td/telegram/Global.h"
#include "td/telegram/logevent/SecretChatEvent.h"
#include "td/telegram/MessageId.h"
@ -425,9 +426,9 @@ unique_ptr<SecretChatActor::Context> SecretChatsManager::make_secret_chat_contex
}
void on_update_secret_chat(int64 access_hash, UserId user_id, SecretChatState state, bool is_outbound, int32 ttl,
int32 date, string key_hash, int32 layer) override {
int32 date, string key_hash, int32 layer, FolderId initial_folder_id) override {
send_closure(G()->contacts_manager(), &ContactsManager::on_update_secret_chat, secret_chat_id_, access_hash,
user_id, state, is_outbound, ttl, date, key_hash, layer);
user_id, state, is_outbound, ttl, date, key_hash, layer, initial_folder_id);
}
void on_inbound_message(UserId user_id, MessageId message_id, int32 date,

View File

@ -6,14 +6,14 @@
//
#pragma once
#include "td/telegram/secret_api.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/logevent/SecretChatEvent.h"
#include "td/telegram/PtsManager.h"
#include "td/telegram/SecretChatActor.h"
#include "td/telegram/SecretChatId.h"
#include "td/telegram/secret_api.h"
#include "td/telegram/telegram_api.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"

View File

@ -1790,7 +1790,7 @@ class CreateNewSecretChatRequest : public RequestActor<SecretChatId> {
// But since the update may still be on its way, we will update essential fields here.
td->contacts_manager_->on_update_secret_chat(
secret_chat_id_, 0 /* no access_hash */, user_id_, SecretChatState::Unknown, true /* it is outbound chat */,
-1 /* unknown ttl */, 0 /* unknown creation date */, "" /* no key_hash */, 0);
-1 /* unknown ttl */, 0 /* unknown creation date */, "" /* no key_hash */, 0, FolderId());
DialogId dialog_id(secret_chat_id_);
td->messages_manager_->force_create_dialog(dialog_id, "create new secret chat", true);
send_result(td->messages_manager_->get_chat_object(dialog_id));

View File

@ -4,6 +4,7 @@
// 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/FolderId.h"
#include "td/telegram/Global.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/SecretChatActor.h"
@ -528,7 +529,7 @@ class FakeSecretChatContext : public SecretChatActor::Context {
void send_net_query(NetQueryPtr query, ActorShared<NetQueryCallback> callback, bool ordered) override;
void on_update_secret_chat(int64 access_hash, UserId user_id, SecretChatState state, bool is_outbound, int32 ttl,
int32 date, string key_hash, int32 layer) override {
int32 date, string key_hash, int32 layer, FolderId initial_folder_id) override {
}
void on_inbound_message(UserId user_id, MessageId message_id, int32 date,