Skip to content

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

DEV: Improve user handling and access in post components (#34013)

Sérgio Saquetim4013d1ffb8b9
diff --git a/app/assets/javascripts/discourse/app/components/post/avatar.gjs b/app/assets/javascripts/discourse/app/components/post/avatar.gjs
index f99695283cd..8c471ae4ad8 100644
--- a/app/assets/javascripts/discourse/app/components/post/avatar.gjs
+++ b/app/assets/javascripts/discourse/app/components/post/avatar.gjs
@@ -9,89 +9,96 @@ import lazyHash from "discourse/helpers/lazy-hash";
import { applyValueTransformer } from "discourse/lib/transformer";
export default class PostAvatar extends Component {
- get size() {
- return applyValueTransformer(
- "post-avatar-size",
- this.args.size || "large",
- { post: this.args.post }
- );
- }
-
@cached
get user() {
- const username = this.args.post.username;
- const name = this.args.post.name;
- const path = this.args.post.usernameUrl;
+ const user = this.args.post.user;
+ if (!user) {
+ return null;
+ }
+
const avatarTemplate = applyValueTransformer(
"post-avatar-template",
this.args.post.avatar_template,
- {
- post: this.args.post,
- keyboardSelected: this.args.keyboardSelected,
- username,
- name,
- path,
- decoratorState: this.args.decoratorState,
- }
+ this.#transformerContext(user)
);
+ // returns a proxy object to user which overrides the avatarTemplate
+ return new Proxy(user, {
+ get(target, prop) {
+ if (prop === "avatar_template") {
+ return avatarTemplate;
+ }
+ return target[prop];
+ },
+ });
+ }
- return {
- avatar_template: avatarTemplate,
- username,
- name,
- path,
- };
+ get size() {
+ return applyValueTransformer(
+ "post-avatar-size",
+ this.args.size || "large",
+ this.#transformerContext()
+ );
}
get userWasDeleted() {
- return !this.args.post.user_id;
+ return !this.args.post.user;
}
get additionalClasses() {
- return applyValueTransformer("post-avatar-class", [], {
+ return applyValueTransformer(
+ "post-avatar-class",
+ [],
+ this.#transformerContext()
+ );
+ }
+
+ #transformerContext(user = this.user) {
+ return {
+ decoratorState: this.args.decoratorState,
+ keyboardSelected: this.args.keyboardSelected,
post: this.args.post,
- user: this.user,
+ user,
userWasDeleted: this.userWasDeleted,
- size: this.size,
- });
+ };
}
<template>
<div class={{concatClass "topic-avatar" this.additionalClasses}}>
- <PluginOutlet
- @name="post-avatar"
- @outletArgs={{lazyHash
+ {{#let
+ (lazyHash
post=@post
size=this.size
user=this.user
userWasDeleted=this.userWasDeleted
- }}
- >
- <div class="post-avatar">
- {{#if this.userWasDeleted}}
- {{icon "trash-can" class="deleted-user-avatar"}}
- {{else}}
- <UserAvatar
- class="main-avatar"
- tabindex="-1"
- @hideTitle={{true}}
- @lazy={{true}}
- @size={{this.size}}
- @user={{this.user}}
- />
- <UserAvatarFlair @user={{@post}} />
- <div>
+ )
+ as |avatarOutletArgs|
+ }}
+ <PluginOutlet @name="post-avatar" @outletArgs={{avatarOutletArgs}}>
+ <div class="post-avatar">
+ {{#if this.userWasDeleted}}
+ {{icon "trash-can" class="deleted-user-avatar"}}
+ {{else}}
+ <UserAvatar
+ class="main-avatar"
+ tabindex="-1"
+ @hideTitle={{true}}
+ @lazy={{true}}
+ @size={{this.size}}
+ @user={{this.user}}
+ />
<PluginOutlet
@name="post-avatar-flair"
- @outletArgs={{lazyHash user=this.user}}
- />
- </div>
- {{/if}}
- {{#if @displayPosterName}}
- <div class="post-avatar-user-info"></div>
- {{/if}}
- </div>
- </PluginOutlet>
+ @outletArgs={{avatarOutletArgs}}
+ >
+ <UserAvatarFlair @user={{this.user}} />
+ </PluginOutlet>
+ {{/if}}
+ {{#if @displayPosterName}}
+ <div class="post-avatar-user-info"></div>
+ {{/if}}
+ </div>
+ </PluginOutlet>
+ {{/let}}
</div>
</template>
}
diff --git a/app/assets/javascripts/discourse/app/components/post/meta-data/poster-name.gjs b/app/assets/javascripts/discourse/app/components/post/meta-data/poster-name.gjs
index 4e1a55f0350..831cbc6f4cc 100644
--- a/app/assets/javascripts/discourse/app/components/post/meta-data/poster-name.gjs
+++ b/app/assets/javascripts/discourse/app/components/post/meta-data/poster-name.gjs
@@ -37,32 +37,23 @@ export default class PostMetaDataPosterName extends Component {
});
});
- get suppressSimilarName() {
- return applyValueTransformer(
- "post-meta-data-poster-name-suppress-similar-name",
- true,
- { post: this.args.post, name: this.name }
- );
- }
-
get name() {
- return userPrioritizedName(this.args.post);
+ return userPrioritizedName(this.user);
}
get nameFirst() {
- return this.name === this.args.post.name;
+ return this.name === this.user.name;
}
get primaryGroupHref() {
- return getURL(`/g/${this.args.post.primary_group_name}`);
+ return getURL(`/g/${this.user.primary_group_name}`);
}
get shouldDisplaySecondName() {
return (
- this.args.post.name &&
+ this.user.name &&
this.siteSettings.display_name_on_posts &&
- this.#sanitizeName(this.args.post.name) !==
- this.#sanitizeName(this.args.post.username)
+ !this.#suppressSimilarName
);
}
@@ -71,20 +62,17 @@ export default class PostMetaDataPosterName extends Component {
}
get userTitle() {
- return applyValueTransformer(
- "poster-name-user-title",
- this.args.post.user_title,
- {
- post: this.args.post,
- }
- );
+ return applyValueTransformer("poster-name-user-title", this.user.title, {
+ post: this.args.post,
+ user: this.user,
+ });
}
get titleClassNames() {
const classNames = [this.userTitle];
- if (this.args.post.title_is_group && this.args.post.primary_group_name) {
- classNames.push(this.args.post.primary_group_name);
+ if (this.args.post.title_is_group && this.user.primary_group_name) {
+ classNames.push(this.user.primary_group_name);
}
return classNames.map(
@@ -95,10 +83,15 @@ export default class PostMetaDataPosterName extends Component {
get additionalClasses() {
return applyValueTransformer("poster-name-class", [], {
+ post: this.args.post,
user: this.user,
});
}
+ get shouldShowUserStatus() {
+ return this.userStatus.isEnabled && this.user.status;
+ }
+
@bind
withBadgeDescription(badge) {
// Alter the badge description to show that the badge was granted for this post.
@@ -110,118 +103,142 @@ export default class PostMetaDataPosterName extends Component {
return badge;
}
+ get #suppressSimilarName() {
+ const sanitizedName = this.#sanitizeName(this.user.name);
+ const sanitizedUsername = this.#sanitizeName(this.user.username);
+
+ return applyValueTransformer(
+ "post-meta-data-poster-name-suppress-similar-name",
+ sanitizedName === sanitizedUsername,
+ {
+ name: this.user.name,
+ post: this.args.post,
+ sanitizedName,
+ sanitizedUsername,
+ user: this.user,
+ username: this.user.username,
+ }
+ );
+ }
+
#sanitizeName(name) {
- return this.suppressSimilarName
- ? name.toLowerCase().replace(/[\s._-]/g, "")
- : name;
+ return name?.toLowerCase()?.replace(/[\s._-]/g, "");
}
<template>
- {{this.trackUserStatus user=this.user}}
- <div class="names trigger-user-card">
- <PluginOutlet
- @name="post-meta-data-poster-name"
- @outletArgs={{lazyHash post=@post}}
- >
- <span
- class={{concatClass
- "first"
- (if this.nameFirst "full-name" "username")
- (if @post.staff "staff")
- (if @post.admin "admin")
- (if @post.moderator "moderator")
- (if @post.group_moderator "category-moderator")
- (if @post.new_user "new-user")
- (if
- @post.primary_group_name
- (concat "group--" @post.primary_group_name)
- )
- this.additionalClasses
- }}
+ {{#if this.user}}
+ {{this.trackUserStatus user=this.user}}
+ <div class="names trigger-user-card">
+ <PluginOutlet
+ @name="post-meta-data-poster-name"
+ @outletArgs={{lazyHash post=@post user=this.user}}
>
- {{! use the position argument to choose between the first and second name if needed}}
- <PluginOutlet
- @name="post-meta-data-poster-name-user-link"
- @outletArgs={{lazyHash position="first" name=this.name post=@post}}
+ <span
+ class={{concatClass
+ "first"
+ (if this.nameFirst "full-name" "username")
+ (if this.user.staff "staff")
+ (if this.user.admin "admin")
+ (if this.user.moderator "moderator")
+ (if @post.group_moderator "category-moderator")
+ (if @post.new_user "new-user")
+ (if
+ this.user.primary_group_name
+ (concat "group--" this.user.primary_group_name)
+ )
+ this.additionalClasses
+ }}
>
- <UserLink @user={{@post}}>
- {{this.name}}
- {{#if this.showGlyph}}
- {{#if (or @post.moderator @post.group_moderator)}}
- {{icon
- "shield-halved"
- translatedTitle=(i18n "user.moderator_tooltip")
- }}
- {{/if}}
- {{/if}}
- </UserLink>
- </PluginOutlet>
- </span>
-
- {{#if this.showNameAndGroup}}
- {{#if this.shouldDisplaySecondName}}
- <span
- class={{concatClass
- "second"
- (if this.nameFirst "username" "full-name")
+ {{! use the position argument to choose between the first and second name if needed}}
+ <PluginOutlet
+ @name="post-meta-data-poster-name-user-link"
+ @outletArgs={{lazyHash
+ position="first"
+ name=this.name
+ post=@post
+ user=this.user
}}
>
- {{! use the position argument to choose between the first and second name if needed}}
- <PluginOutlet
- @name="post-meta-data-poster-name-user-link"
- @outletArgs={{lazyHash
- position="second"
- name=this.name
- post=@post
+ <UserLink @user={{this.user}}>
+ {{this.name}}
+ {{#if this.showGlyph}}
+ {{#if (or this.user.moderator @post.group_moderator)}}
+ {{icon
+ "shield-halved"
+ translatedTitle=(i18n "user.moderator_tooltip")
+ }}
+ {{/if}}
+ {{/if}}
+ </UserLink>
+ </PluginOutlet>
+ </span>
+
+ {{#if this.showNameAndGroup}}
+ {{#if this.shouldDisplaySecondName}}
+ <span
+ class={{concatClass
+ "second"
+ (if this.nameFirst "username" "full-name")
}}
>
- <UserLink @user={{@post}}>
- {{#if this.nameFirst}}
- {{formatUsername @post.username}}
- {{else}}
- {{@post.name}}
- {{/if}}
- </UserLink>
- </PluginOutlet>
- </span>
- {{/if}}
-
- {{#if this.userTitle}}
- <span class={{concatClass "user-title" this.titleClassNames}}>
- {{#if (and @post.primary_group_name @post.title_is_group)}}
- <GroupLink
- @name={{@post.primary_group_name}}
- @href={{this.primaryGroupHref}}
+ {{! use the position argument to choose between the first and second name if needed}}
+ <PluginOutlet
+ @name="post-meta-data-poster-name-user-link"
+ @outletArgs={{lazyHash
+ position="second"
+ name=this.name
+ post=@post
+ user=this.user
+ }}
>
+ <UserLink @user={{this.user}}>
+ {{#if this.nameFirst}}
+ {{formatUsername this.user.username}}
+ {{else}}
+ {{this.user.name}}
+ {{/if}}
+ </UserLink>
+ </PluginOutlet>
+ </span>
+ {{/if}}
+
+ {{#if this.userTitle}}
+ <span class={{concatClass "user-title" this.titleClassNames}}>
+ {{#if (and this.user.primary_group_name @post.title_is_group)}}
+ <GroupLink
+ @name={{this.user.primary_group_name}}
+ @href={{this.primaryGroupHref}}
+ >
+ {{this.userTitle}}
+ </GroupLink>
+ {{else}}
{{this.userTitle}}
- </GroupLink>
- {{else}}
- {{this.userTitle}}
- {{/if}}
- </span>
- {{/if}}
-
- {{#if (and this.userStatus.isEnabled this.user.status)}}
- <span class="user-status-message-wrap">
- <UserStatusMessage @status={{this.user.status}} />
- </span>
- {{/if}}
-
- {{#if @post.badgesGranted}}
- <span class="user-badge-buttons">
- {{#each @post.badgesGranted key="id" as |badge|}}
- <span class={{concat "user-badge-button-" badge.slug}}>
- <UserBadge
- @badge={{this.withBadgeDescription badge}}
- @user={{@post.user}}
- @showName={{false}}
- />
- </span>
- {{/each}}
- </span>
+ {{/if}}
+ </span>
+ {{/if}}
+
+ {{#if this.shouldShowUserStatus}}
+ <span class="user-status-message-wrap">
+ <UserStatusMessage @status={{this.user.status}} />
+ </span>
+ {{/if}}
+
+ {{#if @post.badgesGranted}}
+ <span class="user-badge-buttons">
+ {{#each @post.badgesGranted key="id" as |badge|}}
+ <span class={{concat "user-badge-button-" badge.slug}}>
+ <UserBadge
+ @badge={{this.withBadgeDescription badge}}
+ @user={{this.user}}
+ @showName={{false}}
+ />
+ </span>
+ {{/each}}
+ </span>
+ {{/if}}
{{/if}}
- {{/if}}
- </PluginOutlet>
- </div>
+ </PluginOutlet>
+ </div>
+ {{/if}}
</template>
}
diff --git a/app/assets/javascripts/discourse/app/components/post/notice/custom.gjs b/app/assets/javascripts/discourse/app/components/post/notice/custom.gjs
index 842a14cf310..03da8924f78 100644
--- a/app/assets/javascripts/discourse/app/components/post/notice/custom.gjs
+++ b/app/assets/javascripts/discourse/app/components/post/notice/custom.gjs
@@ -28,7 +28,7 @@ export default class PostNoticeCustom extends Component {
<template>
{{icon "user-shield"}}
- <div class="post-notice-message test" {{this.registerCreatedByLink}}>
+ <div class="post-notice-message" {{this.registerCreatedByLink}}>
{{htmlSafe @notice.cooked}}
{{#if this.createdByName}}
{{htmlSafe
diff --git a/app/assets/javascripts/discourse/app/models/post-stream.js b/app/assets/javascripts/discourse/app/models/post-stream.js
index 7532d8a99b9..0d6f7bad98b 100644
--- a/app/assets/javascripts/discourse/app/models/post-stream.js
+++ b/app/assets/javascripts/discourse/app/models/post-stream.js
@@ -1300,16 +1300,6 @@ export default class PostStream extends RestModel {
}
_initUserModels(post) {
- post.user = this.store.createRecord("user", {
- id: post.user_id,
- username: post.username,
- avatar_template: post.avatar_template,
- });
-
- if (post.user_status) {
- post.user.status = post.user_status;
- }
-
if (post.mentioned_users) {
post.mentioned_users = post.mentioned_users.map((u) =>
this.store.createRecord("user", u)
diff --git a/app/assets/javascripts/discourse/app/models/post.js b/app/assets/javascripts/discourse/app/models/post.js
index 5413d2ad76b..21e0033a1b9 100644
--- a/app/assets/javascripts/discourse/app/models/post.js
+++ b/app/assets/javascripts/discourse/app/models/post.js
@@ -203,7 +203,6 @@ export default class Post extends RestModel {
@trackedPostProperty topic_id;
@trackedPostProperty trust_level;
@trackedPostProperty updated_at;
- @trackedPostProperty user;
@trackedPostProperty user_deleted;
@trackedPostProperty user_id;
@trackedPostProperty user_suspended;
@@ -442,6 +441,37 @@ export default class Post extends RestModel {
return this.likeAction && (this.liked || this.canToggleLike);
}
+ @cached
+ get user() {
+ if (!this.user_id || !this.username) {
+ // If we don't have at least user_id and username, we can't create a User instance.
+ return null;
+ }
+
+ // Using store.createRecord can lead to issues when updating existing models in the cache, potentially causing
+ // rendering errors if the cached model is currently being rendered.
+ // Instead, User.create ensures we get a fresh instance every time without affecting the cache.
+ // The @cached decorator ensures this computation only happens once and is cached until dependencies are updated.
+ return User.create({
+ id: this.user_id,
+ username: this.username,
+ name: this.name,
+ admin: this.admin,
+ avatar_template: this.avatar_template,
+ flair_bg_color: this.flair_bg_color,
+ flair_color: this.flair_color,
+ flair_group_id: this.flair_group_id,
+ flair_name: this.flair_name,
+ flair_url: this.flair_url,
+ moderator: this.moderator,
+ primary_group_name: this.primary_group_name,
+ status: this.user_status,
+ title: this.user_title,
+ trust_level: this.trust_level,
+ custom_fields: this.user_custom_fields,
+ });
+ }
+
afterUpdate(res) {
if (res.category) {
this.site.updateCategory(res.category);
diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/meta-data-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/meta-data-test.gjs
index fc6be593a14..2a3f0faebd1 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/post/meta-data-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/post/meta-data-test.gjs
@@ -31,9 +31,6 @@ module("Integration | Component | Post | PostMetaData", function (hooks) {
test("post-meta-data-infos value transformer (DAG API)", async function (assert) {
this.post.username = "eviltrout";
- this.post.user = this.store.createRecord("user", {
- username: "eviltrout",
- });
withPluginApi((api) => {
const TestComponent = <template>
<div class="post-info post-metadata-test"></div>
diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/meta-data/poster-name-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/meta-data/poster-name-test.gjs
index 1ff7594f064..e5b9fdc1390 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/post/meta-data/poster-name-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/post/meta-data/poster-name-test.gjs
@@ -31,6 +31,7 @@ module(
});
test("basic rendering", async function (assert) {
+ this.post.user_id = 1;
this.post.username = "eviltrout";
this.post.name = "Robin Ward";
this.post.user_title = "Trout Master";
@@ -48,6 +49,7 @@ module(
});
test("extra classes and glyphs", async function (assert) {
+ this.post.user_id = 1;
this.post.username = "eviltrout";
this.post.staff = true;
this.post.admin = true;
@@ -67,6 +69,7 @@ module(
test("disable display name on posts", async function (assert) {
this.siteSettings.display_name_on_posts = false;
+ this.post.user_id = 1;
this.post.username = "eviltrout";
this.post.name = "Robin Ward";
@@ -78,6 +81,7 @@ module(
test("doesn't render a name if it's similar to the username", async function (assert) {
this.siteSettings.prioritize_username_in_ux = true;
this.siteSettings.display_name_on_posts = true;
+ this.post.user_id = 1;
this.post.username = "eviltrout";
this.post.name = "evil-trout";
@@ -105,10 +109,8 @@ module(
});
test("renders badges that are passed in", async function (assert) {
+ this.post.user_id = 1;
this.post.username = "eviltrout";
- this.post.user = this.store.createRecord("user", {
- username: "eviltrout",
- });
this.post.badges_granted = [
{ badge: { id: 1, icon: "heart", slug: "badge1", name: "Badge One" } },
{ badge: { id: 2, icon: "target", slug: "badge2", name: "Badge Two" } },
@@ -138,10 +140,8 @@ module(
});
test("api.addPosterIcons", async function (assert) {
+ this.post.user_id = 1;
this.post.username = "eviltrout";
- this.post.user = this.store.createRecord("user", {
- username: "eviltrout",
- });
withPluginApi((api) => {
api.addPosterIcons((_, { username }) => {
@@ -189,10 +189,8 @@ module(
});
test("poster name additional classes", async function (assert) {
+ this.post.user_id = 1;
this.post.username = "eviltrout";
- this.post.user = this.store.createRecord("user", {
- username: "eviltrout",
- });
withPluginApi((api) => {
api.registerValueTransformer(
"poster-name-class",
@@ -211,6 +209,7 @@ module(
});
test("poster-name-user-title transformer", async function (assert) {
+ this.post.user_id = 1;
this.post.username = "eviltrout";
this.post.user_title = "Original Title";
diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/post-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/post-test.gjs
index 6cbefff243a..9384bd45902 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/post/post-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/post/post-test.gjs
@@ -62,6 +62,8 @@ module("Integration | Component | Post", function (hooks) {
like_count: 3,
actions_summary: [{ id: 2, count: 1, hidden: false, can_act: true }],
created_at: new Date(new Date().getTime() - 30 * 60 * 1000),
+ user_id: 1,
+ username: "eviltrout",
});
this.post = post;
@@ -383,20 +385,21 @@ module("Integration | Component | Post", function (hooks) {
test("reply directly above", async function (assert) {
this.siteSettings.suppress_reply_directly_above = false;
- this.post.reply_to_user = {
- username: "eviltrout",
- avatar_template: "/images/avatar.png",
- };
- this.post.post_number = 2;
- this.post.reply_to_post_number = 1;
- const prevPost = this.store.createRecord("post", {
- id: 122,
- post_number: 1,
+ const reply = this.store.createRecord("post", {
+ id: 369,
+ post_number: 2,
+ reply_to_post_number: 1,
topic: this.post.topic,
+ user_id: 2,
+ username: "somebody",
+ reply_to_user: {
+ username: "eviltrout",
+ avatar_template: "/images/avatar.png",
+ },
});
- await renderComponent(this.post, { prevPost });
+ await renderComponent(reply, { prevPost: this.post });
assert.dom(".avoid-tab").exists({ count: 1 }, "has the avoid tab class");
await click("a.reply-to-tab");
@@ -754,13 +757,11 @@ module("Integration | Component | Post", function (hooks) {
test("shows user status if enabled in site settings", async function (assert) {
this.siteSettings.enable_user_status = true;
- const status = {
+ this.post.user_status = {
emoji: "tooth",
description: "off to dentist",
};
- this.post.user = this.store.createRecord("user", { status });
-
await renderComponent(this.post);
assert.dom(".user-status-message").exists();
@@ -768,13 +769,11 @@ module("Integration | Component | Post", function (hooks) {
test("doesn't show user status if disabled in site settings", async function (assert) {
this.siteSettings.enable_user_status = false;
- const status = {
+ this.post.user_status = {
emoji: "tooth",
description: "off to dentist",
};
- this.post.user = this.store.createRecord("user", { status });
-
await renderComponent(this.post);
assert.dom(".user-status-message").doesNotExist();
diff --git a/app/assets/javascripts/discourse/tests/unit/models/post-test.js b/app/assets/javascripts/discourse/tests/unit/models/post-test.js
index eec76caf7ba..0d3a44c3dd4 100644
--- a/app/assets/javascripts/discourse/tests/unit/models/post-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/models/post-test.js
@@ -86,7 +86,7 @@ module("Unit | Model | post", function (hooks) {
id: 1,
can_delete: true,
version: 1,
- user,
+ username: "eviltrout",
});
await post.destroy(user);
@@ -106,12 +106,12 @@ module("Unit | Model | post", function (hooks) {
test("destroy by non-staff", async function (assert) {
const originalCooked = "this is the original cooked value";
- const user = this.store.createRecord("user", { username: "evil trout" });
+ const user = this.store.createRecord("user", { username: "regular-user" });
const post = this.store.createRecord("post", {
id: 1,
can_delete: true,
version: 1,
- user,
+ username: "eviltrout",
cooked: originalCooked,
});
diff --git a/app/serializers/group_post_serializer.rb b/app/serializers/group_post_serializer.rb
index 2ff638e7a70..ec2a87ec3a9 100644
--- a/app/serializers/group_post_serializer.rb
+++ b/app/serializers/group_post_serializer.rb
@@ -16,6 +16,7 @@ class GroupPostSerializer < ApplicationSerializer
:post_number,
:posts_count,
:post_type,
+ :user_id,
:username,
:name,
:avatar_template,
@@ -50,6 +51,10 @@ class GroupPostSerializer < ApplicationSerializer
object.topic.category_id
end
+ def user_id
+ object&.user&.id
+ end
+
def username
object&.user&.username
end
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 a808a77c68a..caac3893d8b 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
@@ -45,13 +45,17 @@ export default class CustomReaction extends RestModel {
return reactions.map((reaction) => {
reaction.user = User.create(reaction.user);
reaction.topic = Topic.create(reaction.post.topic);
- reaction.post_user = User.create(reaction.post.user);
reaction.category = Category.findById(reaction.post.category_id);
const postAttrs = { ...reaction.post };
- delete postAttrs.url; // Auto-calculated by Model implementation
+
+ // Delete fields auto-calculated by the model implementation
+ delete postAttrs.url;
+ delete postAttrs.user;
reaction.post = Post.create(postAttrs);
+ reaction.post_user = reaction.post.user;
+
return EmberObject.create(reaction);
});
});