Skip to content

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

FIX: Show the last poster's avatar in the user menu messages tab

Régis Hanold36d416d20e6
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 680fa9126c2..bc1295ba157 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -2120,15 +2120,7 @@ class UsersController < ApplicationController
end
if messages_list
- serialized_messages =
- serialize_data(messages_list, TopicListSerializer, scope: guardian, root: false)[:topics]
- serialized_users =
- if SiteSetting.show_user_menu_avatars || !SiteSetting.prioritize_username_in_ux
- users = messages_list.topics.map { |t| t.posters.last.user }.flatten.compact.uniq(&:id)
- serialize_data(users, BasicUserSerializer, scope: guardian, root: false)
- else
- []
- end
+ serialized_messages = serialize_data(messages_list.topics, UserMenuMessageSerializer)
end
if read_notifications.present?
@@ -2145,7 +2137,6 @@ class UsersController < ApplicationController
unread_notifications: serialized_unread_notifications || [],
read_notifications: serialized_read_notifications || [],
topics: serialized_messages || [],
- users: serialized_users || [],
}
end
diff --git a/app/serializers/user_menu_message_serializer.rb b/app/serializers/user_menu_message_serializer.rb
new file mode 100644
index 00000000000..dd731a0bca7
--- /dev/null
+++ b/app/serializers/user_menu_message_serializer.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+class UserMenuMessageSerializer < ApplicationSerializer
+ include LocalizedFancyTopicTitleMixin
+
+ attributes :id, :slug, :bumped_at, :highest_post_number, :last_read_post_number, :unread_posts
+
+ has_one :last_poster, serializer: BasicUserSerializer, embed: :objects
+
+ def highest_post_number
+ (scope.is_whisperer? && object.highest_staff_post_number) || object.highest_post_number
+ end
+
+ def last_read_post_number
+ object.user_data.last_read_post_number
+ end
+
+ def unread_posts
+ Unread.new(object, object.user_data, scope).unread_posts
+ end
+
+ def has_user_data
+ object.user_data.present?
+ end
+
+ alias include_last_read_post_number? has_user_data
+ alias include_unread_posts? has_user_data
+end
diff --git a/frontend/discourse/app/components/user-menu/messages-list.js b/frontend/discourse/app/components/user-menu/messages-list.js
index 2a8437b0bf4..4c6dfc055d4 100644
--- a/frontend/discourse/app/components/user-menu/messages-list.js
+++ b/frontend/discourse/app/components/user-menu/messages-list.js
@@ -79,19 +79,6 @@ export default class UserMenuMessagesList extends UserMenuNotificationsList {
const topics = data.topics.map((t) => this.store.createRecord("topic", t));
await Topic.applyTransformations(topics);
- if (
- this.siteSettings.show_user_menu_avatars ||
- this.siteSettings.prioritize_full_name_in_ux
- ) {
- // Populate avatar_template for lastPoster
- const usersById = new Map(data.users.map((u) => [u.id, u]));
- topics.forEach((t) => {
- const lastPoster = usersById.get(t.lastPoster.user_id);
- t.last_poster_avatar_template = lastPoster?.avatar_template;
- t.last_poster_name = lastPoster?.name;
- });
- }
-
const readNotifications = await Notification.initializeNotifications(
data.read_notifications
);
diff --git a/frontend/discourse/app/lib/user-menu/message-item.js b/frontend/discourse/app/lib/user-menu/message-item.js
index 7436f5ff3ea..b02050210df 100644
--- a/frontend/discourse/app/lib/user-menu/message-item.js
+++ b/frontend/discourse/app/lib/user-menu/message-item.js
@@ -36,10 +36,10 @@ export default class UserMenuMessageItem extends UserMenuBaseItem {
get label() {
if (!this.siteSettings.prioritize_full_name_in_ux) {
- return this.message.last_poster_username;
+ return this.message.last_poster?.username;
}
- return this.message.last_poster_name || this.message.last_poster_username;
+ return this.message.last_poster?.name || this.message.last_poster?.username;
}
get description() {
@@ -51,6 +51,6 @@ export default class UserMenuMessageItem extends UserMenuBaseItem {
}
get avatarTemplate() {
- return this.message.last_poster_avatar_template;
+ return this.message.last_poster?.avatar_template;
}
}
diff --git a/frontend/discourse/tests/acceptance/user-menu-test.js b/frontend/discourse/tests/acceptance/user-menu-test.js
index 6f8b32748f8..31dba9da73a 100644
--- a/frontend/discourse/tests/acceptance/user-menu-test.js
+++ b/frontend/discourse/tests/acceptance/user-menu-test.js
@@ -1056,7 +1056,7 @@ acceptance("User menu - avatars", function (needs) {
await click("#user-menu-button-messages");
assert.dom("li.notification.private-message .icon-avatar").exists();
- assert.dom("li.message .icon-avatar").exists();
+ assert.dom("li.message .icon-avatar.user-avatar").exists();
});
test("shows user avatars for bookmark items and bookmark reminder notification items", async function (assert) {
diff --git a/frontend/discourse/tests/fixtures/user-menu.js b/frontend/discourse/tests/fixtures/user-menu.js
index c10e5c786ad..d397d7dda03 100644
--- a/frontend/discourse/tests/fixtures/user-menu.js
+++ b/frontend/discourse/tests/fixtures/user-menu.js
@@ -96,61 +96,21 @@ const userMenuFixtures = {
topics: [
{
id: 8092,
- title: "BUG: Can not render emoji properly :/",
- fancy_title: "BUG: Can not render emoji properly :confused:",
slug: "bug-can-not-render-emoji-properly",
- posts_count: 1,
- reply_count: 0,
- highest_post_number: 2,
- image_url: null,
- created_at: "2019-07-26T01:29:24.008Z",
- last_posted_at: "2019-07-26T01:29:24.177Z",
- bumped: true,
+ fancy_title: "BUG: Can not render emoji properly :confused:",
bumped_at: "2019-07-26T01:29:24.177Z",
- unseen: false,
+ highest_post_number: 2,
last_read_post_number: 2,
unread_posts: 0,
- pinned: false,
- unpinned: null,
- visible: true,
- closed: false,
- archived: false,
- notification_level: 3,
- bookmarked: false,
- bookmarks: [],
- liked: false,
- views: 5,
- like_count: 0,
- has_summary: false,
- archetype: "private_message",
- last_poster_username: "mixtape",
- category_id: null,
- pinned_globally: false,
- featured_link: null,
- posters: [
- {
- extras: "latest single",
- description: "Original Poster, Most Recent Poster",
- user_id: 13,
- primary_group_id: null,
- },
- ],
- participants: [
- {
- extras: "latest",
- description: null,
- user_id: 13,
- primary_group_id: null,
- },
- ],
- }
+ last_poster: {
+ id: 13,
+ username: "mixtape",
+ name: "Mix Tape",
+ avatar_template: "/letter_avatar_proxy/v4/letter/o/f05b48/{size}.png",
+ },
+ },
],
read_notifications: [],
- users: [{id: 13,
- username: "mixtape",
- name: "Mix Tape",
- avatar_template: "/letter_avatar_proxy/v4/letter/o/f05b48/{size}.png"
- }]
}
};
diff --git a/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs b/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs
index 98981b8b79c..8308c2b32b3 100644
--- a/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs
+++ b/frontend/discourse/tests/integration/components/user-menu/menu-item-test.gjs
@@ -10,7 +10,7 @@ import UserMenuReviewableItem from "discourse/lib/user-menu/reviewable-item";
import Notification from "discourse/models/notification";
import UserMenuReviewable from "discourse/models/user-menu-reviewable";
import { NOTIFICATION_TYPES } from "discourse/tests/fixtures/concerns/notification-types";
-import PrivateMessagesFixture from "discourse/tests/fixtures/private-messages-fixtures";
+import UserMenuFixtures from "discourse/tests/fixtures/user-menu";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { i18n } from "discourse-i18n";
@@ -427,8 +427,7 @@ module(
function getMessage(overrides = {}, siteSettings, site) {
const message = deepMerge(
cloneJSON(
- PrivateMessagesFixture["/topics/private-messages/eviltrout.json"]
- .topic_list.topics[0]
+ UserMenuFixtures["/u/:username/user-menu-private-messages"].topics[0]
),
overrides
);
@@ -457,6 +456,15 @@ module(
.dom("li.message .item-description b")
.hasText("safe", "fancy title is not escaped");
});
+
+ test("item label is the last poster's name when prioritizing full names", async function (assert) {
+ this.siteSettings.prioritize_full_name_in_ux = true;
+ const item = getMessage({}, this.siteSettings, this.site);
+
+ await render(<template><MenuItem @item={{item}} /></template>);
+
+ assert.dom("li.message .item-label").hasText("Mix Tape");
+ });
}
);
diff --git a/frontend/discourse/tests/integration/components/user-menu/messages-list-test.gjs b/frontend/discourse/tests/integration/components/user-menu/messages-list-test.gjs
index c064560b71b..3ed3211f57d 100644
--- a/frontend/discourse/tests/integration/components/user-menu/messages-list-test.gjs
+++ b/frontend/discourse/tests/integration/components/user-menu/messages-list-test.gjs
@@ -10,56 +10,9 @@ import { i18n } from "discourse-i18n";
function getMessage(overrides = {}) {
return deepMerge(
- {
- id: 8092,
- title: "Test ToPic 4422",
- fancy_title: "Test topic 4422",
- slug: "test-topic-4422",
- posts_count: 1,
- reply_count: 0,
- highest_post_number: 2,
- image_url: null,
- created_at: "2019-07-26T01:29:24.008Z",
- last_posted_at: "2019-07-26T01:29:24.177Z",
- bumped: true,
- bumped_at: "2019-07-26T01:29:24.177Z",
- unseen: false,
- last_read_post_number: 2,
- unread_posts: 0,
- pinned: false,
- unpinned: null,
- visible: true,
- closed: false,
- archived: false,
- notification_level: 3,
- bookmarked: false,
- bookmarks: [],
- liked: false,
- views: 5,
- like_count: 0,
- has_summary: false,
- archetype: "private_message",
- last_poster_username: "mixtape",
- category_id: null,
- pinned_globally: false,
- featured_link: null,
- posters: [
- {
- extras: "latest single",
- description: "Original Poster, Most Recent Poster",
- user_id: 13,
- primary_group_id: null,
- },
- ],
- participants: [
- {
- extras: "latest",
- description: null,
- user_id: 13,
- primary_group_id: null,
- },
- ],
- },
+ cloneJSON(
+ UserMenuFixtures["/u/:username/user-menu-private-messages"].topics[0]
+ ),
overrides
);
}
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 88dbf99c8c8..22edd22d958 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -9168,6 +9168,15 @@ RSpec.describe UsersController do
)
end
+ it "includes the last poster of each message" do
+ get "/u/#{user.username}/user-menu-private-messages"
+ expect(response.status).to eq(200)
+
+ topic =
+ response.parsed_body["topics"].find { |t| t["id"] == message_without_notification.id }
+ expect(topic["last_poster"]["id"]).to eq(message_without_notification.last_post_user_id)
+ end
+
it "fills up the remaining of the USER_MENU_LIST_LIMIT limit with PM topics" do
stub_const(UsersController, "USER_MENU_LIST_LIMIT", 3) do
get "/u/#{user.username}/user-menu-private-messages"