Skip to content

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

FIX: /my/notifications/reactions-received view (#36163)

Régis Hanol51daf75123b0
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-emoji.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-emoji.gjs
new file mode 100644
index 00000000000..a58a9712272
--- /dev/null
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-emoji.gjs
@@ -0,0 +1,30 @@
+import Component from "@glimmer/component";
+import avatar from "discourse/helpers/avatar";
+import { emojiUrlFor } from "discourse/lib/text";
+
+export default class DiscourseReactionsReactionEmoji extends Component {
+ get emojiUrl() {
+ const reactionValue = this.args.reaction.reaction?.reaction_value;
+ return reactionValue ? emojiUrlFor(reactionValue) : null;
+ }
+
+ <template>
+ {{#if @reaction.reaction.reaction_users_count}}
+ <div class="discourse-reactions-my-reaction">
+ <img src={{this.emojiUrl}} class="reaction-emoji" />
+ <a
+ href={{@reaction.user.userUrl}}
+ data-user-card={{@reaction.user.username}}
+ class="avatar-link"
+ >
+ {{avatar
+ @reaction.user
+ imageSize="tiny"
+ extraClasses="actor"
+ ignoreTitle="true"
+ }}
+ </a>
+ </div>
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-post.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-post.gjs
deleted file mode 100644
index febf18248fb..00000000000
--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-reaction-post.gjs
+++ /dev/null
@@ -1,156 +0,0 @@
-import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { hash } from "@ember/helper";
-import { action } from "@ember/object";
-import { service } from "@ember/service";
-import UserStreamItem from "discourse/components/user-stream-item";
-import avatar from "discourse/helpers/avatar";
-import { ajax } from "discourse/lib/ajax";
-import getURL from "discourse/lib/get-url";
-import { emojiUrlFor } from "discourse/lib/text";
-
-let cachedNames = new Map();
-let pendingSearch = null;
-
-export default class DiscourseReactionsReactionPost extends Component {
- @service siteSettings;
-
- @tracked updatedExcerpt = this.args.reaction.post.excerpt;
- @tracked updatedExpandedExcerpt = this.args.reaction.post.expandedExcerpt;
-
- constructor() {
- super(...arguments);
- this.updateMentionedUsernames();
- }
-
- @action
- async updateMentionedUsernames() {
- this.updatedExcerpt = await this.replaceMentionsWithFullNames(
- this.args.reaction.post.excerpt
- );
- this.updatedExpandedExcerpt = await this.replaceMentionsWithFullNames(
- this.args.reaction.post.expandedExcerpt
- );
- }
-
- async replaceMentionsWithFullNames(text) {
- if (!text) {
- return text;
- }
-
- const mentionRegex = /@([\p{L}\d._]+)/gu;
-
- const replacedText = await Promise.all(
- text.split(mentionRegex).map(async (part, index) => {
- if (index % 2 === 1) {
- const fullName = await this.searchUsername(part);
- return `@${fullName || part}`;
- }
- return part;
- })
- );
-
- return replacedText.join("");
- }
-
- async searchUsername(username) {
- username = username.toLowerCase();
-
- if (cachedNames.has(username)) {
- return cachedNames.get(username);
- }
-
- if (pendingSearch?.usernames.size <= 50) {
- pendingSearch.usernames.add(username);
- const results = await pendingSearch.search;
-
- if (results.searchedUsernames.includes(username)) {
- const fullName =
- results.data.users?.find(
- (user) => user.username.toLowerCase() === username
- )?.name ||
- results.data.groups?.find(
- (group) => group.name.toLowerCase() === username
- )?.full_name;
-
- cachedNames.set(username, fullName);
- return fullName;
- }
- }
-
- return this.deferSearch(username);
- }
-
- async deferSearch(username) {
- const usernamesList = new Set([username]);
-
- pendingSearch = {
- search: new Promise((resolve) => {
- setTimeout(async () => {
- const searchedUsernames = Array.from(usernamesList);
- pendingSearch = null;
-
- const data = await ajax("/u/search/users.json", {
- data: {
- usernames: searchedUsernames.join(","),
- include_groups: this.siteSettings.show_fullname_for_groups,
- },
- });
-
- resolve({ searchedUsernames, data });
- }, 20);
- }),
- usernames: usernamesList,
- };
-
- return this.searchUsername(username);
- }
-
- get postUrl() {
- return getURL(this.args.reaction.post.url);
- }
-
- get emojiUrl() {
- const reactionValue = this.args.reaction.reaction.reaction_value;
- return reactionValue ? emojiUrlFor(reactionValue) : null;
- }
-
- <template>
- <UserStreamItem
- @item={{hash
- [email protected]_user.username
- [email protected]_user.name
- [email protected]_user.avatar_template
- postUrl=this.postUrl
- expandedExcerpt=this.updatedExpandedExcerpt
- excerpt=this.updatedExcerpt
- }}
- >
- <:bottom>
- {{#if @reaction.reaction.reaction_users_count}}
- <div class="discourse-reactions-my-reaction">
- <img src={{this.emojiUrl}} class="reaction-emoji" />
- <a
- href={{@reaction.user.userUrl}}
- data-user-card={{@reaction.user.username}}
- class="avatar-link"
- >
- {{avatar
- @reaction.user
- imageSize="tiny"
- extraClasses="actor"
- ignoreTitle="true"
- }}
- </a>
- </div>
- {{/if}}
- </:bottom>
- </UserStreamItem>
- </template>
-}
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js b/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js
index f19b8c5d766..ea177c686b5 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js
@@ -15,7 +15,7 @@ export default class UserActivityReactions extends Controller {
@tracked beforeReactionUserId = null;
#getLastIdFrom(array) {
- return array.length ? array[array.length - 1].get("id") : null;
+ return array.length ? array[array.length - 1].id : null;
}
#updateBeforeIds(reactionUsers) {
@@ -45,7 +45,7 @@ export default class UserActivityReactions extends Controller {
@action
async loadMore() {
if (!this.canLoadMore || this.loading) {
- return;
+ return [];
}
this.loading = true;
@@ -69,12 +69,18 @@ export default class UserActivityReactions extends Controller {
opts
);
- addUniqueValuesToArray(reactionUsers, newReactionUsers);
- this.#updateBeforeIds(newReactionUsers);
+ const flattened = newReactionUsers.map((r) =>
+ CustomReaction.flattenForPostList(r)
+ );
+
+ addUniqueValuesToArray(reactionUsers, flattened);
+ this.#updateBeforeIds(flattened);
- if (newReactionUsers.length === 0) {
+ if (flattened.length === 0) {
this.canLoadMore = false;
}
+
+ return flattened;
} finally {
this.loading = false;
}
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/models/discourse-reactions-custom-reaction.js b/plugins/discourse-reactions/assets/javascripts/discourse/models/discourse-reactions-custom-reaction.js
index 103d97a0651..3d5236c7fa0 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/models/discourse-reactions-custom-reaction.js
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/models/discourse-reactions-custom-reaction.js
@@ -19,6 +19,29 @@ export default class CustomReaction extends RestModel {
});
}
+ static flattenForPostList(reaction) {
+ return {
+ // Original reaction data
+ ...reaction,
+ // Flatten post fields to top level for PostListItem
+ id: reaction.post.id,
+ user_id: reaction.post.user_id,
+ username: reaction.post.username,
+ name: reaction.post.name,
+ avatar_template: reaction.post.avatar_template,
+ user_title: reaction.post.user_title,
+ primary_group_name: reaction.post.primary_group_name,
+ excerpt: reaction.post.excerpt,
+ expandedExcerpt: reaction.post.expandedExcerpt,
+ topic_id: reaction.post.topic_id,
+ post_type: reaction.post.post_type,
+ url: reaction.post.url,
+ title: reaction.topic.title,
+ category: reaction.category,
+ created_at: reaction.created_at,
+ };
+ }
+
static findReactions(url, username, opts) {
opts = opts || {};
const data = { username };
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js b/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js
index e2c03929519..82325ea5f13 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js
@@ -9,7 +9,9 @@ export default class UserActivityReactions extends DiscourseRoute {
this.modelFor("user").username
);
- return new TrackedArray(list);
+ return new TrackedArray(
+ list.map((reaction) => CustomReaction.flattenForPostList(reaction))
+ );
}
setupController(controller, model) {
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-notifications/reactions-received.js b/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-notifications/reactions-received.js
index 1a93a4ad1e2..bf7bd254fc5 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-notifications/reactions-received.js
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-notifications/reactions-received.js
@@ -21,11 +21,14 @@ export default class UserNotificationsReactionsReceived extends DiscourseRoute {
}
);
- return new TrackedArray(list);
+ return new TrackedArray(
+ list.map((reaction) => CustomReaction.flattenForPostList(reaction))
+ );
}
setupController(controller, model) {
- let loadedAll = model.length < 20;
+ const loadedAll = model.length < 20;
+
this.controllerFor("user-activity.reactions").setProperties({
model,
canLoadMore: !loadedAll,
@@ -34,6 +37,7 @@ export default class UserNotificationsReactionsReceived extends DiscourseRoute {
actingUsername: controller.acting_username,
includeLikes: controller.include_likes,
});
+
this.controllerFor("application").set("showFooter", loadedAll);
}
}
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/templates/user-activity/reactions.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/templates/user-activity/reactions.gjs
index a45ae48b8ea..fd8d7f7ef8b 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/templates/user-activity/reactions.gjs
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/templates/user-activity/reactions.gjs
@@ -1,18 +1,18 @@
-import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
-import LoadMore from "discourse/components/load-more";
+import PostList from "discourse/components/post-list";
import { i18n } from "discourse-i18n";
-import DiscourseReactionsReactionPost from "../../components/discourse-reactions-reaction-post";
+import DiscourseReactionsReactionEmoji from "../../components/discourse-reactions-reaction-emoji";
export default <template>
- <LoadMore @selector=".user-stream-item" @action={{@controller.loadMore}}>
- <div class="user-stream">
- {{#each @model as |reaction|}}
- <DiscourseReactionsReactionPost @reaction={{reaction}} />
- {{else}}
- <div class="alert alert-info">{{i18n "notifications.empty"}}</div>
- {{/each}}
- </div>
-
- <ConditionalLoadingSpinner @condition={{@controller.loading}} />
- </LoadMore>
+ <PostList
+ @posts={{@model}}
+ @fetchMorePosts={{@controller.loadMore}}
+ @emptyText={{i18n "notifications.empty"}}
+ @additionalItemClasses="user-stream-item"
+ @showUserInfo={{false}}
+ class="user-stream"
+ >
+ <:belowPostItem as |reaction|>
+ <DiscourseReactionsReactionEmoji @reaction={{reaction}} />
+ </:belowPostItem>
+ </PostList>
</template>
diff --git a/plugins/discourse-reactions/spec/system/reactions_activity_spec.rb b/plugins/discourse-reactions/spec/system/reactions_activity_spec.rb
index 996308e163d..4c018260934 100644
--- a/plugins/discourse-reactions/spec/system/reactions_activity_spec.rb
+++ b/plugins/discourse-reactions/spec/system/reactions_activity_spec.rb
@@ -25,62 +25,6 @@ describe "Reactions | Activity", type: :system do
expect(page).to have_css(".user-stream-item [data-post-id='#{post_1.id}']")
end
- context "when unicode usernames is enabled " do
- before do
- SiteSetting.external_system_avatars_enabled = true
- SiteSetting.external_system_avatars_url =
- "/letter_avatar_proxy/v4/letter/{first_letter}/{color}/{size}.png"
- SiteSetting.unicode_usernames = true
- end
-
- context "when prioritize_full_name_in_ux SiteSetting is true" do
- before do
- SiteSetting.prioritize_full_name_in_ux = true
- stub_request(
- :get,
- "https://avatars.discourse-cdn.com/v4/letter/b/dbc845/48.png",
- ).to_return(status: 200, body: "image", headers: {})
-
- stub_request(
- :get,
- "https://avatars.discourse-cdn.com/v4/letter/b/90ced4/48.png",
- ).to_return(status: 200, body: "image", headers: {})
-
- stub_request(
- :get,
- "https://avatars.discourse-cdn.com/v4/letter/b/90ced4/24.png",
- ).to_return(status: 200, body: "image", headers: {})
-
- stub_request(
- :get,
- "https://avatars.discourse-cdn.com/v4/letter/b/90ced4/144.png",
- ).to_return(status: 200, body: "image", headers: {})
-
- stub_request(
- :get,
- "https://avatars.discourse-cdn.com/v4/letter/b/9de053/48.png",
- ).to_return(status: 200, body: "image", headers: {})
- end
-
- it "shows the name of the mentioned user instead of the username" do
- unicode_user = Fabricate(:user)
- post_2 =
- Fabricate(:post, raw: "This is a test post with a mention @#{unicode_user.username}")
- DiscourseReactions::ReactionManager.new(
- reaction_value: "clap",
- user: current_user,
- post: post_2,
- ).toggle!
-
- visit("/u/#{current_user.username}/activity/reactions")
-
- post = find(".user-stream-item [data-post-id='#{post_2.id}']")
- expect(page).to have_css(".user-stream-item [data-post-id='#{post_2.id}']")
- expect(post).to have_content("@Bruce Wayne")
- end
- end
- end
-
context "when the associated post is deleted" do
before { post_1.trash! }
diff --git a/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-received-test.js b/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-received-test.js
new file mode 100644
index 00000000000..1e6d2af0c3e
--- /dev/null
+++ b/plugins/discourse-reactions/test/javascripts/acceptance/discourse-reactions-received-test.js
@@ -0,0 +1,101 @@
+import { visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("Discourse Reactions - Reactions Received Page", function (needs) {
+ needs.user();
+
+ needs.settings({
+ discourse_reactions_enabled: true,
+ discourse_reactions_enabled_reactions: "otter|open_mouth|heart",
+ discourse_reactions_reaction_for_like: "heart",
+ });
+
+ needs.pretender((server, helper) => {
+ server.get("/discourse-reactions/posts/reactions-received.json", () => {
+ return helper.response([
+ {
+ id: 1,
+ user_id: 1,
+ post_id: 123,
+ reaction: {
+ reaction_value: "heart",
+ reaction_users_count: 1,
+ },
+ post: {
+ id: 123,
+ created_at: "2024-01-01T00:00:00.000Z",
+ topic_id: 1,
+ topic_title: "Test Topic",
+ topic_slug: "test-topic",
+ topic_html_title: "Test Topic",
+ post_number: 1,
+ posts_count: 5,
+ post_type: 1,
+ excerpt:
+ 'This is a post with a mention <span class="mention">@keegan</span> in it.',
+ expandedExcerpt:
+ 'This is a post with a mention <span class="mention">@keegan</span> in it. More content here.',
+ url: "/t/test-topic/1/1",
+ category_id: 1,
+ user_id: 3,
+ username: "testuser",
+ name: "Test User",
+ avatar_template:
+ "/letter_avatar_proxy/v4/letter/t/3be4f8/{size}.png",
+ user: {
+ id: 3,
+ username: "testuser",
+ name: "Test User",
+ avatar_template:
+ "/letter_avatar_proxy/v4/letter/t/3be4f8/{size}.png",
+ },
+ topic: {
+ id: 1,
+ title: "Test Topic",
+ fancy_title: "Test Topic",
+ slug: "test-topic",
+ posts_count: 5,
+ },
+ },
+ user: {
+ id: 2,
+ username: "reactor",
+ name: "Reactor User",
+ avatar_template:
+ "/letter_avatar_proxy/v4/letter/r/3be4f8/{size}.png",
+ userUrl: "/u/reactor",
+ },
+ created_at: "2024-01-01T00:00:00.000Z",
+ },
+ ]);
+ });
+ });
+
+ test("mentions display with username, not full name", async function (assert) {
+ await visit("/u/eviltrout/notifications/reactions-received");
+
+ assert
+ .dom(".post-list-item .excerpt .mention")
+ .hasText("@keegan", "mention displays username, not full name");
+ });
+
+ test("mentions have proper structure for user cards", async function (assert) {
+ await visit("/u/eviltrout/notifications/reactions-received");
+
+ assert
+ .dom(".post-list-item .excerpt .mention")
+ .exists("mention span exists");
+
+ const mentionText = document
+ .querySelector(".post-list-item .excerpt .mention")
+ ?.textContent?.trim();
+
+ assert.true(mentionText?.startsWith("@"), "mention starts with @");
+
+ assert.false(
+ mentionText?.includes(" "),
+ "mention does not contain spaces (which would indicate a full name)"
+ );
+ });
+});