Bring users online locally after typing or sending/editing a message.

GitOrigin-RevId: 2bc4e0c29083f1dca6277498118f1dd6932ccda3
This commit is contained in:
levlam 2019-03-15 02:29:36 +03:00
parent f0d6892453
commit 3e0fb81e09
6 changed files with 75 additions and 12 deletions

View File

@ -3455,8 +3455,14 @@ int32 ContactsManager::get_user_was_online(const User *u, UserId user_id) const
}
int32 was_online = u->was_online;
if (user_id == get_my_id() && my_was_online_local_ != 0) {
was_online = my_was_online_local_;
if (user_id == get_my_id()) {
if (my_was_online_local_ != 0) {
was_online = my_was_online_local_;
}
} else {
if (u->local_was_online != 0 && u->local_was_online > was_online) {
was_online = u->local_was_online;
}
}
return was_online;
}
@ -7005,6 +7011,9 @@ void ContactsManager::on_update_user_online(User *u, UserId user_id, tl_object_p
bool new_is_online = new_online > G()->unix_time_cached();
u->was_online = new_online;
u->is_status_changed = true;
if (u->was_online > 0) {
u->local_was_online = 0;
}
if (user_id == get_my_id()) {
if (my_was_online_local_ != 0 || old_is_online != new_is_online) {
@ -7020,6 +7029,49 @@ void ContactsManager::on_update_user_online(User *u, UserId user_id, tl_object_p
}
}
void ContactsManager::on_update_user_local_was_online(UserId user_id, int32 local_was_online) {
CHECK(user_id.is_valid());
User *u = get_user_force(user_id);
if (u == nullptr) {
return;
}
on_update_user_local_was_online(u, user_id, local_was_online);
update_user(u, user_id);
}
void ContactsManager::on_update_user_local_was_online(User *u, UserId user_id, int32 local_was_online) {
CHECK(u != nullptr);
if (u->is_deleted || u->is_bot || user_id == get_my_id()) {
return;
}
if (u->was_online > G()->unix_time_cached()) {
// if user is currently online, ignore local online
return;
}
if (u->was_online > 0) {
// bring users with accessible status online for 1 minute
local_was_online += 60;
} else {
// bring users with inaccessible status online for 5 minutes
local_was_online += 5 * 60;
}
if (local_was_online < G()->unix_time_cached() + 2 || local_was_online <= u->local_was_online) {
return;
}
LOG(DEBUG) << "Update " << user_id << " local online from " << u->local_was_online << " to " << local_was_online;
bool old_is_online = u->local_was_online > G()->unix_time_cached();
u->local_was_online = local_was_online;
u->is_status_changed = true;
if (!old_is_online) {
u->is_online_status_changed = true;
}
}
void ContactsManager::on_update_user_blocked(UserId user_id, bool is_blocked) {
LOG(INFO) << "Receive update user blocked with " << user_id << " and is_blocked = " << is_blocked;
if (!user_id.is_valid()) {

View File

@ -153,6 +153,7 @@ class ContactsManager : public Actor {
void on_update_user_phone_number(UserId user_id, string &&phone_number);
void on_update_user_photo(UserId user_id, tl_object_ptr<telegram_api::UserProfilePhoto> &&photo_ptr);
void on_update_user_online(UserId user_id, tl_object_ptr<telegram_api::UserStatus> &&status);
void on_update_user_local_was_online(UserId user_id, int32 local_was_online);
void on_update_user_links(UserId user_id, tl_object_ptr<telegram_api::ContactLink> &&outbound,
tl_object_ptr<telegram_api::ContactLink> &&inbound);
void on_update_user_blocked(UserId user_id, bool is_blocked);
@ -468,6 +469,7 @@ class ContactsManager : public Actor {
int32 bot_info_version = -1;
int32 was_online = 0;
int32 local_was_online = 0;
string language_code;
@ -860,6 +862,7 @@ class ContactsManager : public Actor {
void on_update_user_phone_number(User *u, UserId user_id, string &&phone_number);
void on_update_user_photo(User *u, UserId user_id, tl_object_ptr<telegram_api::UserProfilePhoto> &&photo);
void on_update_user_online(User *u, UserId user_id, tl_object_ptr<telegram_api::UserStatus> &&status);
void on_update_user_local_was_online(User *u, UserId user_id, int32 local_was_online);
void on_update_user_links(User *u, UserId user_id, LinkState outbound, LinkState inbound);
void do_update_user_photo(User *u, UserId user_id, tl_object_ptr<telegram_api::UserProfilePhoto> &&photo);

View File

@ -5307,12 +5307,16 @@ bool MessagesManager::need_cancel_user_dialog_action(int32 action_id, MessageCon
}
void MessagesManager::on_user_dialog_action(DialogId dialog_id, UserId user_id,
tl_object_ptr<td_api::ChatAction> &&action,
tl_object_ptr<td_api::ChatAction> &&action, int32 date,
MessageContentType message_content_type) {
if (td_->auth_manager_->is_bot() || !user_id.is_valid() || is_broadcast_channel(dialog_id)) {
return;
}
if (action != nullptr || message_content_type != MessageContentType::None) {
td_->contacts_manager_->on_update_user_local_was_online(user_id, date);
}
bool is_canceled = action == nullptr || action->get_id() == td_api::chatActionCancel::ID;
if (is_canceled) {
auto actions_it = active_dialog_actions_.find(dialog_id);
@ -5397,7 +5401,7 @@ void MessagesManager::cancel_user_dialog_action(DialogId dialog_id, const Messag
return;
}
on_user_dialog_action(dialog_id, m->sender_user_id, nullptr, m->content->get_type());
on_user_dialog_action(dialog_id, m->sender_user_id, nullptr, m->date, m->content->get_type());
}
void MessagesManager::add_pending_channel_update(DialogId dialog_id, tl_object_ptr<telegram_api::Update> &&update,
@ -20540,7 +20544,7 @@ void MessagesManager::on_active_dialog_action_timeout(DialogId dialog_id) {
auto now = Time::now();
while (actions_it->second[0].start_time + DIALOG_ACTION_TIMEOUT < now + 0.1) {
on_user_dialog_action(dialog_id, actions_it->second[0].user_id, nullptr);
on_user_dialog_action(dialog_id, actions_it->second[0].user_id, nullptr, 0);
actions_it = active_dialog_actions_.find(dialog_id);
if (actions_it == active_dialog_actions_.end()) {
@ -20559,7 +20563,7 @@ void MessagesManager::clear_active_dialog_actions(DialogId dialog_id) {
auto actions_it = active_dialog_actions_.find(dialog_id);
while (actions_it != active_dialog_actions_.end()) {
CHECK(!actions_it->second.empty());
on_user_dialog_action(dialog_id, actions_it->second[0].user_id, nullptr);
on_user_dialog_action(dialog_id, actions_it->second[0].user_id, nullptr, 0);
actions_it = active_dialog_actions_.find(dialog_id);
}
}

View File

@ -309,7 +309,7 @@ class MessagesManager : public Actor {
void on_update_include_sponsored_dialog_to_unread_count();
void on_user_dialog_action(DialogId dialog_id, UserId user_id, tl_object_ptr<td_api::ChatAction> &&action,
void on_user_dialog_action(DialogId dialog_id, UserId user_id, tl_object_ptr<td_api::ChatAction> &&action, int32 date,
MessageContentType message_content_type = MessageContentType::None);
void delete_messages(DialogId dialog_id, const vector<MessageId> &message_ids, bool revoke, Promise<Unit> &&promise);

View File

@ -4332,6 +4332,8 @@ void Td::send_update(tl_object_ptr<td_api::Update> &&object) {
case td_api::updateChatReadInbox::ID * 2:
case td_api::updateUnreadMessageCount::ID * 2:
case td_api::updateUnreadChatCount::ID * 2:
case td_api::updateChatOnlineMemberCount::ID * 2:
case td_api::updateUserChatAction::ID * 2:
LOG(ERROR) << "Sending update: " << oneline(to_string(object));
break;
default:

View File

@ -1624,8 +1624,9 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateUserTyping> upd
LOG(DEBUG) << "Ignore user typing in unknown " << dialog_id;
return;
}
td_->messages_manager_->on_user_dialog_action(dialog_id, user_id,
convert_send_message_action(std::move(update->action_)));
// TODO date
td_->messages_manager_->on_user_dialog_action(
dialog_id, user_id, convert_send_message_action(std::move(update->action_)), G()->unix_time_cached());
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateChatUserTyping> update, bool /*force_apply*/) {
@ -1644,8 +1645,8 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateChatUserTyping>
return;
}
}
td_->messages_manager_->on_user_dialog_action(dialog_id, user_id,
convert_send_message_action(std::move(update->action_)));
td_->messages_manager_->on_user_dialog_action(
dialog_id, user_id, convert_send_message_action(std::move(update->action_)), G()->unix_time_cached());
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateEncryptedChatTyping> update, bool /*force_apply*/) {
@ -1663,7 +1664,8 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateEncryptedChatTy
return;
}
td_->messages_manager_->on_user_dialog_action(dialog_id, user_id, make_tl_object<td_api::chatActionTyping>());
td_->messages_manager_->on_user_dialog_action(dialog_id, user_id, make_tl_object<td_api::chatActionTyping>(),
G()->unix_time_cached());
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateUserStatus> update, bool /*force_apply*/) {