From 47a7bc05e2464a62caf2d90c1a6cead4542ee98f Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 11 Apr 2021 05:37:53 +0300 Subject: [PATCH] Use service messages to synchronize active group call state. --- td/telegram/MessageContent.cpp | 6 ++++++ td/telegram/MessageContent.h | 3 +++ td/telegram/MessagesManager.cpp | 24 ++++++++++++++++++++++-- td/telegram/MessagesManager.h | 2 ++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index feebce40e..feb7c0c59 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -2593,6 +2593,12 @@ FullMessageId get_message_content_replied_message_id(DialogId dialog_id, const M } } +std::pair get_message_content_group_call_info(const MessageContent *content) { + CHECK(content->get_type() == MessageContentType::GroupCall); + auto message_group_call = static_cast(content); + return {message_group_call->input_group_call_id, message_group_call->duration >= 0}; +} + vector get_message_content_added_user_ids(const MessageContent *content) { CHECK(content->get_type() == MessageContentType::ChatAddUsers); return static_cast(content)->user_ids; diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index d0186e277..5d3148e0c 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -9,6 +9,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/files/FileId.h" #include "td/telegram/FullMessageId.h" +#include "td/telegram/InputGroupCallId.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/MessageContentType.h" #include "td/telegram/MessageCopyOptions.h" @@ -132,6 +133,8 @@ MessageId get_message_content_pinned_message_id(const MessageContent *content); FullMessageId get_message_content_replied_message_id(DialogId dialog_id, const MessageContent *content); +std::pair get_message_content_group_call_info(const MessageContent *content); + vector get_message_content_added_user_ids(const MessageContent *content); UserId get_message_content_deleted_user_id(const MessageContent *content); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 26cc52e12..3d7d2afcb 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -29861,11 +29861,10 @@ void MessagesManager::on_update_dialog_group_call_id(DialogId dialog_id, InputGr } if (d->active_group_call_id != input_group_call_id) { + LOG(INFO) << "Update active group call in " << dialog_id << " to " << input_group_call_id; d->active_group_call_id = input_group_call_id; bool has_active_group_call = input_group_call_id.is_valid(); if (has_active_group_call != d->has_active_group_call) { - LOG(ERROR) << "Receive invalid has_active_group_call flag " << d->has_active_group_call << ", but have " - << input_group_call_id << " in " << dialog_id; d->has_active_group_call = has_active_group_call; if (!has_active_group_call) { d->is_group_call_empty = false; @@ -32607,6 +32606,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } if (from_update) { + speculatively_update_active_group_call_id(d, m); speculatively_update_channel_participants(dialog_id, m); update_sent_message_contents(dialog_id, m); update_used_hashtags(dialog_id, m); @@ -36055,6 +36055,26 @@ void MessagesManager::reget_message_from_server_if_needed(DialogId dialog_id, co } } +void MessagesManager::speculatively_update_active_group_call_id(Dialog *d, const Message *m) { + CHECK(m != nullptr); + if (!m->message_id.is_any_server() || m->content->get_type() != MessageContentType::GroupCall) { + return; + } + + InputGroupCallId input_group_call_id; + bool is_ended; + std::tie(input_group_call_id, is_ended) = get_message_content_group_call_info(m->content.get()); + if (is_ended) { + if (d->active_group_call_id == input_group_call_id) { + on_update_dialog_group_call_id(d->dialog_id, InputGroupCallId()); + } + } else { + if (d->active_group_call_id != input_group_call_id) { + repair_dialog_active_group_call_id(d->dialog_id); + } + } +} + void MessagesManager::speculatively_update_channel_participants(DialogId dialog_id, const Message *m) { CHECK(m != nullptr); if (!m->message_id.is_any_server() || dialog_id.get_type() != DialogType::Channel || !m->sender_user_id.is_valid()) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 8cfb6a616..418f1841a 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -2881,6 +2881,8 @@ class MessagesManager : public Actor { void reget_message_from_server_if_needed(DialogId dialog_id, const Message *m); + void speculatively_update_active_group_call_id(Dialog *d, const Message *m); + void speculatively_update_channel_participants(DialogId dialog_id, const Message *m); void update_sent_message_contents(DialogId dialog_id, const Message *m);