From 32ac05693b5f970a8e08c6e4e08b5b74e55dbe43 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 6 Mar 2019 00:12:04 +0300 Subject: [PATCH] Hide voter count in non-voted polls. GitOrigin-RevId: d15190256271ad0e63ebaa83805778f8c8716df1 --- td/generate/scheme/td_api.tl | 2 +- td/telegram/PollManager.cpp | 55 +++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 5cacc0b8..dbe71f0b 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -192,7 +192,7 @@ maskPointChin = MaskPoint; maskPosition point:MaskPoint x_shift:double y_shift:double scale:double = MaskPosition; -//@description Describes one answer option of a poll @text Option text, 1-100 characters @voter_count Number of voters for this option @vote_percentage The percentage of votes for this option, 0-100 @is_chosen True, if the option was chosen by the user +//@description Describes one answer option of a poll @text Option text, 1-100 characters @voter_count Number of voters for this option, available only for closed or voted polls @vote_percentage The percentage of votes for this option, 0-100 @is_chosen True, if the option was chosen by the user pollOption text:string voter_count:int32 vote_percentage:int32 is_chosen:Bool = PollOption; diff --git a/td/telegram/PollManager.cpp b/td/telegram/PollManager.cpp index 76ccf0a2..c8addb7a 100644 --- a/td/telegram/PollManager.cpp +++ b/td/telegram/PollManager.cpp @@ -312,18 +312,24 @@ td_api::object_ptr PollManager::get_poll_option_object(const } vector PollManager::get_vote_percentage(const vector &voter_counts, int32 total_voter_count) { - vector result(voter_counts.size(), 0); - if (total_voter_count == 0 || voter_counts.empty()) { - return result; - } - int32 sum = 0; for (auto voter_count : voter_counts) { CHECK(0 <= voter_count); CHECK(voter_count <= std::numeric_limits::max() - sum); sum += voter_count; } - CHECK(total_voter_count <= sum); + if (total_voter_count > sum) { + if (sum != 0) { + LOG(ERROR) << "Have total_voter_count = " << total_voter_count << ", but votes sum = " << sum << ": " + << voter_counts; + } + total_voter_count = sum; + } + + vector result(voter_counts.size(), 0); + if (total_voter_count == 0) { + return result; + } if (total_voter_count != sum) { // just round to the nearest for (size_t i = 0; i < result.size(); i++) { @@ -358,7 +364,7 @@ vector PollManager::get_vote_percentage(const vector &voter_counts std::unordered_map options; for (size_t i = 0; i < result.size(); i++) { auto &option = options[voter_counts[i]]; - option.pos = i; + option.pos = narrow_cast(i); option.count++; } vector