Locally update reaction counts when changing story reaction.

This commit is contained in:
levlam 2023-09-19 13:42:13 +03:00
parent 5a19de8faa
commit 48607e18b5
4 changed files with 51 additions and 4 deletions

View File

@ -65,6 +65,7 @@ StoryInteractionInfo::StoryInteractionInfo(Td *td, telegram_api::object_ptr<tele
}
reaction_counts_.emplace_back(std::move(reaction_type), reaction->count_);
}
std::sort(reaction_counts_.begin(), reaction_counts_.end());
}
void StoryInteractionInfo::add_dependencies(Dependencies &dependencies) const {
@ -73,6 +74,33 @@ void StoryInteractionInfo::add_dependencies(Dependencies &dependencies) const {
}
}
void StoryInteractionInfo::set_chosen_reaction_type(const ReactionType &new_reaction_type,
const ReactionType &old_reaction_type) {
if (!old_reaction_type.is_empty()) {
for (auto it = reaction_counts_.begin(); it != reaction_counts_.end(); ++it) {
if (it->first == old_reaction_type) {
it->second--;
if (it->second == 0) {
reaction_counts_.erase(it);
}
}
}
}
if (!new_reaction_type.is_empty()) {
bool is_found = false;
for (auto it = reaction_counts_.begin(); it != reaction_counts_.end(); ++it) {
if (it->first == old_reaction_type) {
it->second++;
is_found = true;
}
}
if (!is_found) {
reaction_counts_.emplace_back(new_reaction_type, 1);
}
}
std::sort(reaction_counts_.begin(), reaction_counts_.end());
}
bool StoryInteractionInfo::set_recent_viewer_user_ids(vector<UserId> &&user_ids) {
if (recent_viewer_user_ids_.empty() && view_count_ > 0) {
// don't update recent viewers for stories with expired viewers

View File

@ -57,6 +57,8 @@ class StoryInteractionInfo {
return false;
}
void set_chosen_reaction_type(const ReactionType &new_reaction_type, const ReactionType &old_reaction_type);
int32 get_view_count() const {
return view_count_;
}

View File

@ -2667,6 +2667,15 @@ void StoryManager::on_story_replied(StoryFullId story_full_id, UserId replier_us
}
}
bool StoryManager::has_suggested_reaction(const Story *story, const ReactionType &reaction_type) {
if (reaction_type.is_empty()) {
return false;
}
CHECK(story != nullptr);
return std::any_of(story->areas_.begin(), story->areas_.end(),
[&reaction_type](const MediaArea &area) { return area.has_reaction_type(reaction_type); });
}
bool StoryManager::can_use_story_reaction(const Story *story, const ReactionType &reaction_type) const {
if (reaction_type.is_empty()) {
return true;
@ -2675,10 +2684,8 @@ bool StoryManager::can_use_story_reaction(const Story *story, const ReactionType
if (td_->option_manager_->get_option_boolean("is_premium")) {
return true;
}
for (auto &area : story->areas_) {
if (area.has_reaction_type(reaction_type)) {
return true;
}
if (has_suggested_reaction(story, reaction_type)) {
return true;
}
return false;
}
@ -2718,6 +2725,14 @@ void StoryManager::set_story_reaction(StoryFullId story_full_id, ReactionType re
td_->reaction_manager_->add_recent_reaction(reaction_type);
}
if (owner_dialog_id.get_type() != DialogType::User) {
bool need_add = has_suggested_reaction(story, reaction_type);
bool need_remove = has_suggested_reaction(story, story->chosen_reaction_type_);
if (need_add || need_remove) {
story->interaction_info_.set_chosen_reaction_type(need_add ? reaction_type : ReactionType(),
story->chosen_reaction_type_);
}
}
story->chosen_reaction_type_ = reaction_type;
on_story_changed(story_full_id, story, true, true);

View File

@ -573,6 +573,8 @@ class StoryManager final : public Actor {
void read_stories_on_server(DialogId owner_dialog_id, StoryId story_id, uint64 log_event_id);
static bool has_suggested_reaction(const Story *story, const ReactionType &reaction_type);
bool can_use_story_reaction(const Story *story, const ReactionType &reaction_type) const;
void schedule_interaction_info_update();