Skip to content

A platform for community discussion. Free, open, simple.

FIX: Count reactors in Rewind Reactions

Régis Hanol6bacee0bbfbf
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/base_report.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/base_report.rb
index 193e3933e33..618afb45030 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/base_report.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/base_report.rb
@@ -22,6 +22,10 @@ module DiscourseRewind
true
end
+ def self.plugin_enabled?(name)
+ Discourse.plugins_by_name[name]&.enabled?
+ end
+
def should_use_fake_data?
return false if ENV["DISCOURSE_REWIND_USE_REAL_DATA"] == "1"
Rails.env.development?
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb
index a83ee909daa..1cd2ed3f7fd 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/reactions.rb
@@ -4,89 +4,62 @@
module DiscourseRewind
module Action
class Reactions < BaseReport
+ MAX_REACTIONS = 5
+ FAKE_REACTIONS = [
+ { emoji: "grinning", count: 82 },
+ { emoji: "heart", count: 45 },
+ { emoji: "dog", count: 34 },
+ { emoji: "cat", count: 32 },
+ { emoji: "open_mouth", count: 2 },
+ ].freeze
+
FakeData = {
data: {
- post_received_reactions: {
- "open_mouth" => 2,
- "cat" => 32,
- "dog" => 34,
- "heart" => 45,
- "grinning" => 82,
- },
- post_used_reactions: {
- "open_mouth" => 2,
- "cat" => 32,
- "dog" => 34,
- "heart" => 45,
- "grinning" => 82,
- },
- chat_used_reactions: {
- "open_mouth" => 2,
- "cat" => 32,
- "dog" => 34,
- "heart" => 45,
- "grinning" => 82,
- },
- chat_received_reactions: {
- "open_mouth" => 2,
- "cat" => 32,
- "dog" => 34,
- "heart" => 45,
- "grinning" => 82,
- },
+ post_received_reactions: FAKE_REACTIONS,
+ post_used_reactions: FAKE_REACTIONS,
+ post_used_reactions_total: FAKE_REACTIONS.sum { |reaction| reaction[:count] },
},
identifier: "reactions",
}
def call
return FakeData if should_use_fake_data?
- return if !enabled?
-
- data = {}
- if defined?(DiscourseReactions::Reaction)
- # This is missing heart reactions (default like)
- data[:post_used_reactions] = sort_and_limit(
- DiscourseReactions::Reaction
- .by_user(user)
- .where(created_at: date)
- .group(:reaction_value)
- .count,
- )
+ return if !self.class.enabled?
- data[:post_received_reactions] = sort_and_limit(
- DiscourseReactions::Reaction
- .includes(:post)
- .where(posts: { user_id: user.id })
- .where(created_at: date)
- .group(:reaction_value)
- .count,
+ used = count_by_emoji(DiscourseReactions::Reaction.by_user(user))
+ received =
+ count_by_emoji(
+ DiscourseReactions::Reaction.joins(:reaction_users, :post).where(
+ posts: {
+ user_id: user.id,
+ },
+ ),
)
- end
+ return if used.empty? && received.empty?
- if Discourse.plugins_by_name["chat"]&.enabled?
- data[:chat_used_reactions] = sort_and_limit(
- Chat::MessageReaction.where(user: user).where(created_at: date).group(:emoji).count,
- )
-
- data[:chat_received_reactions] = sort_and_limit(
- Chat::MessageReaction
- .includes(:chat_message)
- .where(chat_message: { user_id: user.id })
- .where(created_at: date)
- .group(:emoji)
- .count,
- )
- end
-
- { data:, identifier: "reactions" }
+ {
+ data: {
+ post_used_reactions: used.first(MAX_REACTIONS),
+ post_used_reactions_total: used.sum { |reaction| reaction[:count] },
+ post_received_reactions: received.first(MAX_REACTIONS),
+ },
+ identifier: "reactions",
+ }
end
- def enabled?
- Discourse.plugins_by_name["discourse-reactions"]&.enabled?
+ def self.enabled?
+ plugin_enabled?("discourse-reactions")
end
- def sort_and_limit(reactions)
- reactions.sort_by { |_, value| -value }.take(5).to_h
+ private
+
+ def count_by_emoji(reactions)
+ reactions
+ .where(discourse_reactions_reaction_users: { created_at: date })
+ .group(:reaction_value)
+ .order("COUNT(*) DESC, reaction_value")
+ .count
+ .map { |emoji, count| { emoji:, count: } }
end
end
end
diff --git a/plugins/discourse-rewind/assets/javascripts/discourse/components/reports/reactions.gjs b/plugins/discourse-rewind/assets/javascripts/discourse/components/reports/reactions.gjs
index 4b2ffc24e08..16c3ac79a7e 100644
--- a/plugins/discourse-rewind/assets/javascripts/discourse/components/reports/reactions.gjs
+++ b/plugins/discourse-rewind/assets/javascripts/discourse/components/reports/reactions.gjs
@@ -1,34 +1,14 @@
import Component from "@glimmer/component";
-import { concat, get } from "@ember/helper";
import { action } from "@ember/object";
import { trustHTML } from "@ember/template";
-import dReplaceEmoji from "discourse/ui-kit/helpers/d-replace-emoji";
+import dEmoji from "discourse/ui-kit/helpers/d-emoji";
import { i18n } from "discourse-i18n";
export default class Reactions extends Component {
- get totalPostUsedReactions() {
- return Object.values(
- this.args.report.data.post_used_reactions ?? {}
- ).reduce((acc, count) => acc + count, 0);
- }
-
- get receivedReactions() {
- return this.args.report.data.post_received_reactions ?? {};
- }
-
- get sortedUsedReactions() {
- const reactions = this.args.report.data.post_used_reactions ?? {};
- return Object.entries(reactions).sort((a, b) => b[1] - a[1]);
- }
-
- @action
- cleanEmoji(emojiName) {
- return emojiName.replaceAll(/_/g, " ");
- }
-
@action
computePercentage(count) {
- return `${((count / this.totalPostUsedReactions) * 100).toFixed(2)}%`;
+ const total = this.args.report.data.post_used_reactions_total;
+ return `${((count / total) * 100).toFixed(2)}%`;
}
@action
@@ -37,52 +17,56 @@ export default class Reactions extends Component {
}
<template>
- <div class="rewind-report-page --post-received-reactions">
- <h2 class="rewind-report-title">
- {{i18n "discourse_rewind.reports.post_received_reactions.title"}}
- </h2>
- <div class="rewind-report-container">
- {{#each-in this.receivedReactions as |emojiName count|}}
- <div class="rewind-card scale">
- <span class="rewind-card__emoji">
- {{dReplaceEmoji (concat ":" emojiName ":")}}
- </span>
- <span class="rewind-card__data">{{count}}</span>
- </div>
- {{/each-in}}
- </div>
- </div>
-
- <div class="rewind-report-page --post-used-reactions">
- <h2 class="rewind-report-title">
- {{i18n "discourse_rewind.reports.post_used_reactions.title"}}
- </h2>
- <div class="rewind-card">
- <div class="rewind-reactions-chart">
- {{#each this.sortedUsedReactions as |reaction|}}
- <div class="rewind-reactions-row">
- <span class="emoji">
- {{dReplaceEmoji (concat ":" (get reaction "0") ":")}}
+ {{#if @report.data.post_received_reactions.length}}
+ <div class="rewind-report-page --post-received-reactions">
+ <h2 class="rewind-report-title">
+ {{i18n "discourse_rewind.reports.post_received_reactions.title"}}
+ </h2>
+ <div class="rewind-report-container">
+ {{#each @report.data.post_received_reactions as |reaction|}}
+ <div class="rewind-card scale">
+ <span class="rewind-card__emoji">
+ {{dEmoji reaction.emoji}}
</span>
- <span class="percentage">{{this.computePercentage
- (get reaction "1")
- }}</span>
- <div
- class="rewind-reactions-bar"
- style={{this.computePercentageStyle (get reaction "1")}}
- title={{get reaction "1"}}
- ></div>
+ <span class="rewind-card__data">{{reaction.count}}</span>
</div>
{{/each}}
+ </div>
+ </div>
+ {{/if}}
+
+ {{#if @report.data.post_used_reactions.length}}
+ <div class="rewind-report-page --post-used-reactions">
+ <h2 class="rewind-report-title">
+ {{i18n "discourse_rewind.reports.post_used_reactions.title"}}
+ </h2>
+ <div class="rewind-card">
+ <div class="rewind-reactions-chart">
+ {{#each @report.data.post_used_reactions as |reaction|}}
+ <div class="rewind-reactions-row">
+ <span class="emoji">
+ {{dEmoji reaction.emoji}}
+ </span>
+ <span class="percentage">{{this.computePercentage
+ reaction.count
+ }}</span>
+ <div
+ class="rewind-reactions-bar"
+ style={{this.computePercentageStyle reaction.count}}
+ title={{reaction.count}}
+ ></div>
+ </div>
+ {{/each}}
- <span class="rewind-total-reactions">
- {{i18n
- "discourse_rewind.reports.post_used_reactions.total_number"
- count=this.totalPostUsedReactions
- }}
- </span>
+ <span class="rewind-total-reactions">
+ {{i18n
+ "discourse_rewind.reports.post_used_reactions.total_number"
+ [email protected]_used_reactions_total
+ }}
+ </span>
+ </div>
</div>
</div>
- </div>
+ {{/if}}
</template>
}
diff --git a/plugins/discourse-rewind/spec/actions/reactions_spec.rb b/plugins/discourse-rewind/spec/actions/reactions_spec.rb
new file mode 100644
index 00000000000..edd84f8f203
--- /dev/null
+++ b/plugins/discourse-rewind/spec/actions/reactions_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseRewind::Action::Reactions do
+ fab!(:user)
+
+ before { SiteSetting.discourse_reactions_enabled = true }
+
+ describe ".call" do
+ it "returns nothing when the user has no post reactions" do
+ expect(call_report).to be_nil
+ end
+
+ it "counts the reactions used and received during the year" do
+ own_post = Fabricate(:post, user:)
+ received = Fabricate(:reaction, post: own_post, created_at: 2.years.ago)
+ 3.times do
+ Fabricate(:reaction_user, reaction: received, post: own_post, created_at: random_datetime)
+ end
+ Fabricate(:reaction_user, reaction: received, post: own_post, created_at: 2.years.ago)
+ used = Fabricate(:reaction, post: Fabricate(:post), reaction_value: "heart")
+ Fabricate(:reaction_user, reaction: used, user:, post: used.post, created_at: random_datetime)
+
+ other_received = Fabricate(:reaction, post: Fabricate(:post, user:), reaction_value: "heart")
+ Fabricate(
+ :reaction_user,
+ reaction: other_received,
+ post: other_received.post,
+ created_at: random_datetime,
+ )
+
+ expect(call_report[:data]).to eq(
+ post_used_reactions: [{ emoji: "heart", count: 1 }],
+ post_used_reactions_total: 1,
+ post_received_reactions: [{ emoji: "otter", count: 3 }, { emoji: "heart", count: 1 }],
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-rewind/test/javascripts/integration/components/reports/reactions-test.gjs b/plugins/discourse-rewind/test/javascripts/integration/components/reports/reactions-test.gjs
new file mode 100644
index 00000000000..5fd9d00af94
--- /dev/null
+++ b/plugins/discourse-rewind/test/javascripts/integration/components/reports/reactions-test.gjs
@@ -0,0 +1,46 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import Reactions from "discourse/plugins/discourse-rewind/discourse/components/reports/reactions";
+
+module("Integration | Component | Reports | Reactions", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("without received reactions", async function (assert) {
+ const report = {
+ data: {
+ post_used_reactions: [{ emoji: "heart", count: 2 }],
+ post_used_reactions_total: 2,
+ post_received_reactions: [],
+ },
+ };
+
+ await render(<template><Reactions @report={{report}} /></template>);
+
+ assert
+ .dom(".rewind-report-page.--post-used-reactions")
+ .exists("the used reactions render");
+ assert
+ .dom(".rewind-report-page.--post-received-reactions")
+ .doesNotExist("the received reactions are not rendered");
+ });
+
+ test("without used reactions", async function (assert) {
+ const report = {
+ data: {
+ post_used_reactions: [],
+ post_used_reactions_total: 0,
+ post_received_reactions: [{ emoji: "otter", count: 1 }],
+ },
+ };
+
+ await render(<template><Reactions @report={{report}} /></template>);
+
+ assert
+ .dom(".rewind-report-page.--post-used-reactions")
+ .doesNotExist("the used reactions are not rendered");
+ assert
+ .dom(".rewind-report-page.--post-received-reactions")
+ .exists("the received reactions render");
+ });
+});