2020-12-08 15:29:25 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2020-12-08 15:29:25 +01:00
|
|
|
//
|
|
|
|
// 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/GroupCallParticipant.h"
|
|
|
|
|
2021-03-15 17:32:33 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2021-03-05 12:40:54 +01:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
2021-03-05 10:50:39 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2020-12-08 15:29:25 +01:00
|
|
|
|
2020-12-22 13:51:57 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2021-03-27 03:19:22 +01:00
|
|
|
#include <limits>
|
|
|
|
|
2020-12-08 15:29:25 +01:00
|
|
|
namespace td {
|
|
|
|
|
2021-03-19 00:44:20 +01:00
|
|
|
GroupCallParticipant::GroupCallParticipant(const tl_object_ptr<telegram_api::groupCallParticipant> &participant,
|
|
|
|
int32 call_version) {
|
2020-12-08 15:29:25 +01:00
|
|
|
CHECK(participant != nullptr);
|
2021-03-05 12:40:54 +01:00
|
|
|
dialog_id = DialogId(participant->peer_);
|
2021-10-19 17:11:16 +02:00
|
|
|
about = participant->about_;
|
2021-01-11 13:02:02 +01:00
|
|
|
audio_source = participant->source_;
|
2021-02-11 18:34:24 +01:00
|
|
|
server_is_muted_by_themselves = participant->can_self_unmute_;
|
|
|
|
server_is_muted_by_admin = participant->muted_ && !participant->can_self_unmute_;
|
|
|
|
server_is_muted_locally = participant->muted_by_you_;
|
2021-03-05 10:39:57 +01:00
|
|
|
is_self = participant->self_;
|
2020-12-31 01:10:41 +01:00
|
|
|
if ((participant->flags_ & telegram_api::groupCallParticipant::VOLUME_MASK) != 0) {
|
|
|
|
volume_level = participant->volume_;
|
2021-01-02 20:59:48 +01:00
|
|
|
if (volume_level < MIN_VOLUME_LEVEL || volume_level > MAX_VOLUME_LEVEL) {
|
2020-12-31 01:10:41 +01:00
|
|
|
LOG(ERROR) << "Receive " << to_string(participant);
|
|
|
|
volume_level = 10000;
|
|
|
|
}
|
2021-11-01 19:53:23 +01:00
|
|
|
is_volume_level_local = !participant->volume_by_admin_;
|
2020-12-31 01:10:41 +01:00
|
|
|
}
|
2020-12-08 15:29:25 +01:00
|
|
|
if (!participant->left_) {
|
|
|
|
joined_date = participant->date_;
|
|
|
|
if ((participant->flags_ & telegram_api::groupCallParticipant::ACTIVE_DATE_MASK) != 0) {
|
|
|
|
active_date = participant->active_date_;
|
|
|
|
}
|
2021-04-13 01:33:02 +02:00
|
|
|
if (joined_date <= 0 || active_date < 0) {
|
2021-03-15 16:53:51 +01:00
|
|
|
LOG(ERROR) << "Receive invalid active_date/joined_date in " << to_string(participant);
|
2021-04-13 01:33:02 +02:00
|
|
|
joined_date = 1;
|
2020-12-11 15:43:23 +01:00
|
|
|
active_date = 0;
|
|
|
|
}
|
2021-03-05 14:44:43 +01:00
|
|
|
if ((participant->flags_ & telegram_api::groupCallParticipant::RAISE_HAND_RATING_MASK) != 0) {
|
|
|
|
raise_hand_rating = participant->raise_hand_rating_;
|
2021-03-15 16:53:51 +01:00
|
|
|
if (raise_hand_rating < 0) {
|
|
|
|
LOG(ERROR) << "Receive invalid raise_hand_rating in " << to_string(participant);
|
|
|
|
raise_hand_rating = 0;
|
|
|
|
}
|
2021-03-05 14:44:43 +01:00
|
|
|
}
|
2020-12-08 15:29:25 +01:00
|
|
|
}
|
2020-12-11 15:43:23 +01:00
|
|
|
is_just_joined = participant->just_joined_;
|
2021-03-05 10:39:57 +01:00
|
|
|
is_min = participant->min_;
|
2021-03-19 00:44:20 +01:00
|
|
|
version = call_version;
|
2021-01-12 15:05:25 +01:00
|
|
|
|
2021-04-30 16:07:01 +02:00
|
|
|
if (participant->video_ != nullptr) {
|
2021-07-11 00:45:30 +02:00
|
|
|
video_payload = GroupCallVideoPayload(participant->video_.get());
|
2021-05-03 15:30:14 +02:00
|
|
|
}
|
|
|
|
if (participant->presentation_ != nullptr) {
|
2021-07-06 01:33:42 +02:00
|
|
|
if (participant->presentation_->flags_ & telegram_api::groupCallParticipantVideo::AUDIO_SOURCE_MASK) {
|
|
|
|
presentation_audio_source = participant->presentation_->audio_source_;
|
|
|
|
}
|
2021-07-11 00:45:30 +02:00
|
|
|
presentation_payload = GroupCallVideoPayload(participant->presentation_.get());
|
2021-01-12 15:05:25 +01:00
|
|
|
}
|
2021-07-12 19:21:34 +02:00
|
|
|
|
2021-07-13 02:17:46 +02:00
|
|
|
if (is_just_joined && get_has_video()) {
|
2021-07-12 19:21:34 +02:00
|
|
|
video_diff++;
|
|
|
|
}
|
2020-12-08 15:29:25 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 13:20:05 +01:00
|
|
|
bool GroupCallParticipant::is_versioned_update(const tl_object_ptr<telegram_api::groupCallParticipant> &participant) {
|
2021-03-18 00:36:31 +01:00
|
|
|
// updates about new and left participants must be applyed as versioned, even they don't increase version
|
2020-12-18 13:20:05 +01:00
|
|
|
return participant->just_joined_ || participant->left_ || participant->versioned_;
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:48:41 +02:00
|
|
|
GroupCallParticipantOrder GroupCallParticipant::get_real_order(bool can_self_unmute, bool joined_date_asc,
|
2021-03-18 22:03:34 +01:00
|
|
|
bool keep_active_date) const {
|
2021-03-18 00:36:31 +01:00
|
|
|
auto sort_active_date = td::max(active_date, local_active_date);
|
2021-03-18 22:03:34 +01:00
|
|
|
if (!keep_active_date && sort_active_date < G()->unix_time() - 300) {
|
2021-03-18 00:36:31 +01:00
|
|
|
sort_active_date = 0;
|
2021-03-15 17:32:33 +01:00
|
|
|
}
|
2021-04-06 00:48:41 +02:00
|
|
|
auto sort_raise_hand_rating = can_self_unmute ? raise_hand_rating : 0;
|
2021-03-18 00:36:31 +01:00
|
|
|
auto sort_joined_date = joined_date_asc ? std::numeric_limits<int32>::max() - joined_date : joined_date;
|
2021-07-11 00:55:44 +02:00
|
|
|
bool has_video = !video_payload.is_empty() || !presentation_payload.is_empty();
|
2021-07-09 17:48:10 +02:00
|
|
|
return GroupCallParticipantOrder(has_video, sort_active_date, sort_raise_hand_rating, sort_joined_date);
|
2021-03-15 16:32:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 13:41:20 +01:00
|
|
|
bool GroupCallParticipant::get_is_muted_by_themselves() const {
|
|
|
|
return have_pending_is_muted ? pending_is_muted_by_themselves : server_is_muted_by_themselves;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupCallParticipant::get_is_muted_by_admin() const {
|
|
|
|
return have_pending_is_muted ? pending_is_muted_by_admin : server_is_muted_by_admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupCallParticipant::get_is_muted_locally() const {
|
|
|
|
return have_pending_is_muted ? pending_is_muted_locally : server_is_muted_locally;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupCallParticipant::get_is_muted_for_all_users() const {
|
|
|
|
return get_is_muted_by_admin() || get_is_muted_by_themselves();
|
2021-02-11 18:34:24 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 22:40:40 +01:00
|
|
|
int32 GroupCallParticipant::get_volume_level() const {
|
|
|
|
return pending_volume_level != 0 ? pending_volume_level : volume_level;
|
|
|
|
}
|
|
|
|
|
2021-03-05 14:44:43 +01:00
|
|
|
bool GroupCallParticipant::get_is_hand_raised() const {
|
|
|
|
return have_pending_is_hand_raised ? pending_is_hand_raised : raise_hand_rating != 0;
|
|
|
|
}
|
|
|
|
|
2021-07-13 02:14:48 +02:00
|
|
|
int32 GroupCallParticipant::get_has_video() const {
|
2021-07-13 02:17:46 +02:00
|
|
|
return video_payload.is_empty() && presentation_payload.is_empty() ? 0 : 1;
|
2021-07-13 02:14:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-01 23:06:49 +01:00
|
|
|
void GroupCallParticipant::update_from(const GroupCallParticipant &old_participant) {
|
|
|
|
CHECK(!old_participant.is_min);
|
|
|
|
if (joined_date < old_participant.joined_date) {
|
2021-03-18 21:18:16 +01:00
|
|
|
LOG(ERROR) << "Join date of " << old_participant.dialog_id << " decreased from " << old_participant.joined_date
|
|
|
|
<< " to " << joined_date;
|
2021-02-01 23:06:49 +01:00
|
|
|
joined_date = old_participant.joined_date;
|
|
|
|
}
|
|
|
|
if (active_date < old_participant.active_date) {
|
|
|
|
active_date = old_participant.active_date;
|
|
|
|
}
|
|
|
|
local_active_date = old_participant.local_active_date;
|
|
|
|
is_speaking = old_participant.is_speaking;
|
|
|
|
if (is_min) {
|
2021-02-11 18:34:24 +01:00
|
|
|
server_is_muted_locally = old_participant.server_is_muted_locally;
|
|
|
|
|
|
|
|
if (old_participant.is_volume_level_local && !is_volume_level_local) {
|
|
|
|
is_volume_level_local = true;
|
|
|
|
volume_level = old_participant.volume_level;
|
|
|
|
}
|
2021-03-05 10:39:57 +01:00
|
|
|
|
|
|
|
if (audio_source == old_participant.audio_source) {
|
|
|
|
is_self = old_participant.is_self;
|
|
|
|
}
|
2021-02-01 23:06:49 +01:00
|
|
|
}
|
2021-02-11 18:34:24 +01:00
|
|
|
is_min = false;
|
|
|
|
|
2021-02-01 23:06:49 +01:00
|
|
|
pending_volume_level = old_participant.pending_volume_level;
|
|
|
|
pending_volume_level_generation = old_participant.pending_volume_level_generation;
|
2021-02-12 13:41:20 +01:00
|
|
|
|
|
|
|
have_pending_is_muted = old_participant.have_pending_is_muted;
|
|
|
|
pending_is_muted_by_themselves = old_participant.pending_is_muted_by_themselves;
|
|
|
|
pending_is_muted_by_admin = old_participant.pending_is_muted_by_admin;
|
|
|
|
pending_is_muted_locally = old_participant.pending_is_muted_locally;
|
|
|
|
pending_is_muted_generation = old_participant.pending_is_muted_generation;
|
2021-03-05 14:44:43 +01:00
|
|
|
|
|
|
|
have_pending_is_hand_raised = old_participant.have_pending_is_hand_raised;
|
|
|
|
pending_is_hand_raised = old_participant.pending_is_hand_raised;
|
|
|
|
pending_is_hand_raised_generation = old_participant.pending_is_hand_raised_generation;
|
2021-02-01 23:06:49 +01:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:39:57 +01:00
|
|
|
bool GroupCallParticipant::update_can_be_muted(bool can_manage, bool is_admin) {
|
2021-02-12 13:41:20 +01:00
|
|
|
bool is_muted_by_admin = get_is_muted_by_admin();
|
|
|
|
bool is_muted_by_themselves = get_is_muted_by_themselves();
|
|
|
|
bool is_muted_locally = get_is_muted_locally();
|
|
|
|
|
|
|
|
CHECK(!is_muted_by_admin || !is_muted_by_themselves);
|
2021-02-11 18:34:24 +01:00
|
|
|
|
2020-12-31 00:48:45 +01:00
|
|
|
bool new_can_be_muted_for_all_users = false;
|
|
|
|
bool new_can_be_unmuted_for_all_users = false;
|
2021-02-11 18:34:24 +01:00
|
|
|
bool new_can_be_muted_only_for_self = !can_manage && !is_muted_locally;
|
|
|
|
bool new_can_be_unmuted_only_for_self = !can_manage && is_muted_locally;
|
2020-12-17 00:07:05 +01:00
|
|
|
if (is_self) {
|
2021-02-12 13:41:20 +01:00
|
|
|
// current user can be muted if !is_muted_by_themselves && !is_muted_by_admin; after that is_muted_by_themselves
|
2021-02-11 18:34:24 +01:00
|
|
|
// current user can be unmuted if is_muted_by_themselves; after that !is_muted
|
2021-02-12 13:41:20 +01:00
|
|
|
new_can_be_muted_for_all_users = !is_muted_by_themselves && !is_muted_by_admin;
|
2021-02-11 18:34:24 +01:00
|
|
|
new_can_be_unmuted_for_all_users = is_muted_by_themselves;
|
2020-12-31 00:48:45 +01:00
|
|
|
new_can_be_muted_only_for_self = false;
|
|
|
|
new_can_be_unmuted_only_for_self = false;
|
2020-12-17 00:07:05 +01:00
|
|
|
} else if (is_admin) {
|
2021-02-12 13:41:20 +01:00
|
|
|
// admin user can be muted if can_manage && !is_muted_by_themselves; after that is_muted_by_themselves
|
2020-12-17 00:07:05 +01:00
|
|
|
// admin user can't be unmuted
|
2021-02-12 13:41:20 +01:00
|
|
|
new_can_be_muted_for_all_users = can_manage && !is_muted_by_themselves;
|
2020-12-17 00:07:05 +01:00
|
|
|
} else {
|
2021-02-11 18:34:24 +01:00
|
|
|
// other users can be muted if can_manage && !is_muted_by_admin; after that is_muted_by_admin
|
|
|
|
// other users can be unmuted if can_manage && is_muted_by_admin; after that is_muted_by_themselves
|
|
|
|
new_can_be_muted_for_all_users = can_manage && !is_muted_by_admin;
|
|
|
|
new_can_be_unmuted_for_all_users = can_manage && is_muted_by_admin;
|
2020-12-17 00:07:05 +01:00
|
|
|
}
|
2021-02-12 13:41:20 +01:00
|
|
|
CHECK(static_cast<int>(new_can_be_muted_for_all_users) + static_cast<int>(new_can_be_unmuted_for_all_users) +
|
|
|
|
static_cast<int>(new_can_be_muted_only_for_self) + static_cast<int>(new_can_be_unmuted_only_for_self) <=
|
|
|
|
1);
|
2020-12-31 00:48:45 +01:00
|
|
|
if (new_can_be_muted_for_all_users != can_be_muted_for_all_users ||
|
|
|
|
new_can_be_unmuted_for_all_users != can_be_unmuted_for_all_users ||
|
|
|
|
new_can_be_muted_only_for_self != can_be_muted_only_for_self ||
|
|
|
|
new_can_be_unmuted_only_for_self != can_be_unmuted_only_for_self) {
|
|
|
|
can_be_muted_for_all_users = new_can_be_muted_for_all_users;
|
|
|
|
can_be_unmuted_for_all_users = new_can_be_unmuted_for_all_users;
|
|
|
|
can_be_muted_only_for_self = new_can_be_muted_only_for_self;
|
|
|
|
can_be_unmuted_only_for_self = new_can_be_unmuted_only_for_self;
|
2020-12-17 00:07:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-05 10:39:57 +01:00
|
|
|
bool GroupCallParticipant::set_pending_is_muted(bool is_muted, bool can_manage, bool is_admin) {
|
|
|
|
update_can_be_muted(can_manage, is_admin);
|
2021-02-12 13:41:20 +01:00
|
|
|
if (is_muted) {
|
|
|
|
if (!can_be_muted_for_all_users && !can_be_muted_only_for_self) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CHECK(!can_be_muted_for_all_users || !can_be_muted_only_for_self);
|
|
|
|
} else {
|
|
|
|
if (!can_be_unmuted_for_all_users && !can_be_unmuted_only_for_self) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CHECK(!can_be_unmuted_for_all_users || !can_be_unmuted_only_for_self);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_self) {
|
|
|
|
pending_is_muted_by_themselves = is_muted;
|
|
|
|
pending_is_muted_by_admin = false;
|
|
|
|
pending_is_muted_locally = false;
|
|
|
|
} else {
|
|
|
|
pending_is_muted_by_themselves = get_is_muted_by_themselves();
|
|
|
|
pending_is_muted_by_admin = get_is_muted_by_admin();
|
|
|
|
pending_is_muted_locally = get_is_muted_locally();
|
|
|
|
if (is_muted) {
|
|
|
|
if (can_be_muted_only_for_self) {
|
|
|
|
// local mute
|
|
|
|
pending_is_muted_locally = true;
|
|
|
|
} else {
|
|
|
|
// admin mute
|
|
|
|
CHECK(can_be_muted_for_all_users);
|
|
|
|
CHECK(can_manage);
|
|
|
|
if (is_admin) {
|
|
|
|
CHECK(!pending_is_muted_by_themselves);
|
|
|
|
pending_is_muted_by_admin = false;
|
|
|
|
pending_is_muted_by_themselves = true;
|
|
|
|
} else {
|
|
|
|
CHECK(!pending_is_muted_by_admin);
|
|
|
|
pending_is_muted_by_admin = true;
|
|
|
|
pending_is_muted_by_themselves = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (can_be_unmuted_only_for_self) {
|
|
|
|
// local unmute
|
|
|
|
pending_is_muted_locally = false;
|
|
|
|
} else {
|
|
|
|
// admin unmute
|
|
|
|
CHECK(can_be_unmuted_for_all_users);
|
|
|
|
CHECK(can_manage);
|
|
|
|
CHECK(!is_admin);
|
|
|
|
pending_is_muted_by_admin = false;
|
|
|
|
pending_is_muted_by_themselves = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
have_pending_is_muted = true;
|
2021-03-05 10:39:57 +01:00
|
|
|
update_can_be_muted(can_manage, is_admin);
|
2021-02-12 13:41:20 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-05 10:50:39 +01:00
|
|
|
td_api::object_ptr<td_api::groupCallParticipant> GroupCallParticipant::get_group_call_participant_object(Td *td) const {
|
2020-12-08 15:29:25 +01:00
|
|
|
if (!is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return td_api::make_object<td_api::groupCallParticipant>(
|
2021-08-03 00:58:02 +02:00
|
|
|
td->messages_manager_->get_message_sender_object(dialog_id, "get_group_call_participant_object"), audio_source,
|
|
|
|
presentation_audio_source, video_payload.get_group_call_participant_video_info_object(),
|
2021-07-11 00:45:30 +02:00
|
|
|
presentation_payload.get_group_call_participant_video_info_object(), about, is_self, is_speaking,
|
2021-05-03 15:30:14 +02:00
|
|
|
get_is_hand_raised(), can_be_muted_for_all_users, can_be_unmuted_for_all_users, can_be_muted_only_for_self,
|
|
|
|
can_be_unmuted_only_for_self, get_is_muted_for_all_users(), get_is_muted_locally(), get_is_muted_by_themselves(),
|
|
|
|
get_volume_level(), order.get_group_call_participant_order_object());
|
2020-12-11 15:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const GroupCallParticipant &lhs, const GroupCallParticipant &rhs) {
|
2021-05-03 15:30:14 +02:00
|
|
|
return lhs.dialog_id == rhs.dialog_id && lhs.audio_source == rhs.audio_source &&
|
2021-07-08 18:59:16 +02:00
|
|
|
lhs.presentation_audio_source == rhs.presentation_audio_source && lhs.video_payload == rhs.video_payload &&
|
2021-06-01 22:57:04 +02:00
|
|
|
lhs.presentation_payload == rhs.presentation_payload && lhs.about == rhs.about && lhs.is_self == rhs.is_self &&
|
|
|
|
lhs.is_speaking == rhs.is_speaking && lhs.get_is_hand_raised() == rhs.get_is_hand_raised() &&
|
2020-12-31 00:48:45 +01:00
|
|
|
lhs.can_be_muted_for_all_users == rhs.can_be_muted_for_all_users &&
|
|
|
|
lhs.can_be_unmuted_for_all_users == rhs.can_be_unmuted_for_all_users &&
|
|
|
|
lhs.can_be_muted_only_for_self == rhs.can_be_muted_only_for_self &&
|
2021-02-11 18:34:24 +01:00
|
|
|
lhs.can_be_unmuted_only_for_self == rhs.can_be_unmuted_only_for_self &&
|
2021-02-12 13:41:20 +01:00
|
|
|
lhs.get_is_muted_for_all_users() == rhs.get_is_muted_for_all_users() &&
|
|
|
|
lhs.get_is_muted_locally() == rhs.get_is_muted_locally() &&
|
2021-03-05 14:44:43 +01:00
|
|
|
lhs.get_is_muted_by_themselves() == rhs.get_is_muted_by_themselves() &&
|
2021-02-01 22:40:40 +01:00
|
|
|
lhs.get_volume_level() == rhs.get_volume_level() && lhs.order == rhs.order;
|
2020-12-11 15:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const GroupCallParticipant &lhs, const GroupCallParticipant &rhs) {
|
|
|
|
return !(lhs == rhs);
|
2020-12-08 15:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const GroupCallParticipant &group_call_participant) {
|
2021-05-17 18:11:59 +02:00
|
|
|
return string_builder << "GroupCallParticipant[" << group_call_participant.dialog_id << " with source "
|
2021-01-11 13:02:02 +01:00
|
|
|
<< group_call_participant.audio_source << " and order " << group_call_participant.order << ']';
|
2020-12-08 15:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|