FIX: anonymous users cannot load topics with mentions with a user status that has an end date (#20660)

Steps to reproduce:
1. Create a post with a mention of a user that has user status with an end date
2. Try to load the topic with that post as an anonymous user

You'll see a topic with blank content.
This commit is contained in:
Andrei Prigorshnev 2023-03-13 19:57:09 +04:00 committed by GitHub
parent f91af69ec4
commit 72726830b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 31 deletions

View File

@ -421,11 +421,11 @@ export default class PostCooked {
return status.description;
}
const until_ = until(
status.ends_at,
this.currentUser.timezone,
this.currentUser.locale
);
const timezone = this.currentUser
? this.currentUser.user_option?.timezone
: moment.tz.guess();
const until_ = until(status.ends_at, timezone, this.currentUser?.locale);
return escapeExpression(`${status.description} ${until_}`);
}

View File

@ -10,7 +10,29 @@ import { cloneJSON } from "discourse-common/lib/object";
import topicFixtures from "../fixtures/topic";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
acceptance("Post inline mentions test", function (needs) {
function topicWithoutUserStatus(topicId, mentionedUserId) {
const topic = cloneJSON(topicFixtures[`/t/${topicId}.json`]);
topic.archetype = "regular";
const firstPost = topic.post_stream.posts[0];
firstPost.cooked =
'<p>I am mentioning <a class="mention" href="/u/user1">@user1</a> again.</p>';
firstPost.mentioned_users = [
{
id: mentionedUserId,
username: "user1",
avatar_template: "/letter_avatar_proxy/v4/letter/a/bbce88/{size}.png",
},
];
return topic;
}
function topicWithUserStatus(topicId, mentionedUserId, status) {
const topic = topicWithoutUserStatus(topicId, mentionedUserId);
topic.post_stream.posts[0].mentioned_users[0].status = status;
return topic;
}
acceptance("Post inline mentions", function (needs) {
needs.user();
const topicId = 130;
@ -21,30 +43,9 @@ acceptance("Post inline mentions test", function (needs) {
ends_at: null,
};
function topicWithoutUserStatus() {
const topic = cloneJSON(topicFixtures[`/t/${topicId}.json`]);
const firstPost = topic.post_stream.posts[0];
firstPost.cooked =
'<p>I am mentioning <a class="mention" href="/u/user1">@user1</a> again.</p>';
firstPost.mentioned_users = [
{
id: mentionedUserId,
username: "user1",
avatar_template: "/letter_avatar_proxy/v4/letter/a/bbce88/{size}.png",
},
];
return topic;
}
function topicWithUserStatus() {
const topic = topicWithoutUserStatus();
topic.post_stream.posts[0].mentioned_users[0].status = status;
return topic;
}
test("shows user status on inline mentions", async function (assert) {
pretender.get(`/t/${topicId}.json`, () => {
return response(topicWithUserStatus());
return response(topicWithUserStatus(topicId, mentionedUserId, status));
});
await visit(`/t/lorem-ipsum-dolor-sit-amet/${topicId}`);
@ -67,7 +68,7 @@ acceptance("Post inline mentions test", function (needs) {
test("inserts user status on message bus message", async function (assert) {
pretender.get(`/t/${topicId}.json`, () => {
return response(topicWithoutUserStatus());
return response(topicWithoutUserStatus(topicId, mentionedUserId));
});
await visit(`/t/lorem-ipsum-dolor-sit-amet/${topicId}`);
@ -101,7 +102,7 @@ acceptance("Post inline mentions test", function (needs) {
test("updates user status on message bus message", async function (assert) {
pretender.get(`/t/${topicId}.json`, () => {
return response(topicWithUserStatus());
return response(topicWithUserStatus(topicId, mentionedUserId, status));
});
await visit(`/t/lorem-ipsum-dolor-sit-amet/${topicId}`);
@ -139,7 +140,7 @@ acceptance("Post inline mentions test", function (needs) {
test("removes user status on message bus message", async function (assert) {
pretender.get(`/t/${topicId}.json`, () => {
return response(topicWithUserStatus());
return response(topicWithUserStatus(topicId, mentionedUserId, status));
});
await visit(`/t/lorem-ipsum-dolor-sit-amet/${topicId}`);
@ -158,3 +159,46 @@ acceptance("Post inline mentions test", function (needs) {
);
});
});
acceptance("Post inline mentions as an anonymous user", function () {
const topicId = 130;
const mentionedUserId = 1;
const status = {
description: "Surfing",
emoji: "surfing_man",
ends_at: null,
};
test("an anonymous user can see user status on mentions", async function (assert) {
pretender.get(`/t/${topicId}.json`, () => {
const topic = topicWithUserStatus(topicId, mentionedUserId, status);
return response(topic);
});
await visit(`/t/lorem-ipsum-dolor-sit-amet/${topicId}`);
assert.ok(
exists(".topic-post .cooked .mention .user-status"),
"user status is shown"
);
});
test("an anonymous user can see user status with an end date on mentions", async function (assert) {
pretender.get(`/t/${topicId}.json`, () => {
const statusWithEndDate = Object.assign(status, {
ends_at: "2100-02-01T09:00:00.000Z",
});
const topic = topicWithUserStatus(
topicId,
mentionedUserId,
statusWithEndDate
);
return response(topic);
});
await visit(`/t/lorem-ipsum-dolor-sit-amet/${topicId}`);
assert.ok(
exists(".topic-post .cooked .mention .user-status"),
"user status is shown"
);
});
});