Better readHistory in secret chats.

GitOrigin-RevId: 9ecb838683600488b8c6fbc0ec1a0574851c3ad3
This commit is contained in:
levlam 2018-04-02 04:14:47 +03:00
parent e9490eb6e1
commit ca5d9a5ceb
2 changed files with 39 additions and 9 deletions

View File

@ -105,6 +105,8 @@ void SecretChatActor::on_result_resendable(NetQueryPtr net_query, Promise<NetQue
return on_update_chat(std::move(net_query)); return on_update_chat(std::move(net_query));
case static_cast<uint8>(QueryType::Message): case static_cast<uint8>(QueryType::Message):
return on_outbound_send_message_result(std::move(net_query), std::move(promise)), Status::OK(); return on_outbound_send_message_result(std::move(net_query), std::move(promise)), Status::OK();
case static_cast<uint8>(QueryType::ReadHistory):
return on_read_history(std::move(net_query));
case static_cast<uint8>(QueryType::Ignore): case static_cast<uint8>(QueryType::Ignore):
return Status::OK(); return Status::OK();
} }
@ -360,12 +362,27 @@ void SecretChatActor::send_read_history(int32 date, Promise<> promise) {
promise.set_error(Status::Error(400, "Can't access the chat")); promise.set_error(Status::Error(400, "Can't access the chat"));
return; return;
} }
// TODO: use promise
context_->send_net_query(context_->net_query_creator().create( if (date <= last_read_history_date_) {
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::Ignore)), return promise.set_value(Unit());
create_storer(telegram_api::messages_readEncryptedHistory(get_input_chat(), date))),
actor_shared(this), false);
} }
if (read_history_promise_) {
LOG(INFO) << "Cancel previous read history request in secret chat " << auth_state_.id;
read_history_promise_.set_value(Unit());
cancel_query(read_history_query_);
}
auto net_query = context_->net_query_creator().create(
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::ReadHistory)),
create_storer(telegram_api::messages_readEncryptedHistory(get_input_chat(), date)));
read_history_query_ = net_query.get_weak();
last_read_history_date_ = date;
read_history_promise_ = std::move(promise);
LOG(INFO) << "Send read history request with date " << date << " in secret chat " << auth_state_.id;
context_->send_net_query(std::move(net_query), actor_shared(this), false);
}
void SecretChatActor::send_open_message(int64 random_id, Promise<> promise) { void SecretChatActor::send_open_message(int64 random_id, Promise<> promise) {
if (close_flag_) { if (close_flag_) {
promise.set_error(Status::Error(400, "Chat is closed")); promise.set_error(Status::Error(400, "Chat is closed"));
@ -1834,6 +1851,14 @@ Status SecretChatActor::on_update_chat(telegram_api::object_ptr<telegram_api::En
return res; return res;
} }
Status SecretChatActor::on_read_history(NetQueryPtr query) {
if (query.generation() == read_history_query_.generation()) {
read_history_query_ = NetQueryRef();
read_history_promise_.set_value(Unit());
}
return Status::OK();
}
// DH CONFIG // DH CONFIG
// messages.dhConfigNotModified#c0e24635 random:bytes = messages.DhConfig; // messages.dhConfigNotModified#c0e24635 random:bytes = messages.DhConfig;
// messages.dhConfig#2c221edd g:int p:bytes version:int random:bytes = messages.DhConfig; // messages.dhConfig#2c221edd g:int p:bytes version:int random:bytes = messages.DhConfig;

View File

@ -436,13 +436,13 @@ class SecretChatActor : public NetQueryCallback {
LogEvent::Id create_logevent_id_ = 0; LogEvent::Id create_logevent_id_ = 0;
enum class QueryType : uint8 { DhConfig, EncryptedChat, Message, Ignore, DiscardEncryption }; enum class QueryType : uint8 { DhConfig, EncryptedChat, Message, Ignore, DiscardEncryption, ReadHistory };
bool can_be_empty_; bool can_be_empty_;
AuthState auth_state_; AuthState auth_state_;
ConfigState config_state_; ConfigState config_state_;
// Turns out, that all changes should be made made through StateChange. // Turns out, that all changes should be made through StateChange.
// //
// The problem is the time between the moment we made decision about change and // The problem is the time between the moment we made decision about change and
// the moment we actually apply the change to memory. // the moment we actually apply the change to memory.
@ -457,12 +457,12 @@ class SecretChatActor : public NetQueryCallback {
// 2. We SEND CHANGE to db only after correspoiding EVENT is SAVED to the binlog. // 2. We SEND CHANGE to db only after correspoiding EVENT is SAVED to the binlog.
// 3. The we are able to ERASE EVENT just AFTER the CHANGE is SAVED to the binlog. // 3. The we are able to ERASE EVENT just AFTER the CHANGE is SAVED to the binlog.
// //
// Actually the change will be saved do binlog too. // Actually the change will be saved to binlog too.
// So we can do it immidiatelly after EVENT is SENT to the binlog, because SEND CHANGE and ERASE EVENT will be // So we can do it immidiatelly after EVENT is SENT to the binlog, because SEND CHANGE and ERASE EVENT will be
// ordered automatically. // ordered automatically.
// //
// We will use common ChangeProcessor for all changes (inside one SecretChatActor). // We will use common ChangeProcessor for all changes (inside one SecretChatActor).
// So all changes will be saved in exactly the same order as they are applied // So all changes will be saved in exactly the same order as they are applied.
template <class StateT> template <class StateT>
class Change { class Change {
@ -572,6 +572,9 @@ class SecretChatActor : public NetQueryCallback {
Container<OutboundMessageState> outbound_message_states_; Container<OutboundMessageState> outbound_message_states_;
NetQueryRef set_typing_query_; NetQueryRef set_typing_query_;
NetQueryRef read_history_query_;
int32 last_read_history_date_ = -1;
Promise<Unit> read_history_promise_;
enum SendFlag { enum SendFlag {
None = 0, None = 0,
@ -649,6 +652,8 @@ class SecretChatActor : public NetQueryCallback {
Status on_update_chat(NetQueryPtr query) TD_WARN_UNUSED_RESULT; Status on_update_chat(NetQueryPtr query) TD_WARN_UNUSED_RESULT;
Status on_update_chat(telegram_api::object_ptr<telegram_api::EncryptedChat> chat) TD_WARN_UNUSED_RESULT; Status on_update_chat(telegram_api::object_ptr<telegram_api::EncryptedChat> chat) TD_WARN_UNUSED_RESULT;
Status on_read_history(NetQueryPtr query) TD_WARN_UNUSED_RESULT;
void on_promise_error(Status error, string desc); void on_promise_error(Status error, string desc);
void get_dh_config(); void get_dh_config();