FIX: Only include last_posted_at if there's a topic_user object. (#11011)

Trying to include this attribute when topic_user is nil causes an error when visiting a topic as anon. Additionally, we don't display the slow mode banner for these users.
This commit is contained in:
Roman Rizzi 2020-10-23 11:31:59 -03:00 committed by GitHub
parent f85f5eb179
commit c0848a5cc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -11,9 +11,9 @@ export default Component.extend({
return durationTextFromSeconds(seconds);
},
@discourseComputed("topic.slow_mode_seconds", "topic.closed")
showSlowModeNotice(seconds, closed) {
return seconds > 0 && !closed;
@discourseComputed("user", "topic.slow_mode_seconds", "topic.closed")
showSlowModeNotice(user, seconds, closed) {
return user && seconds > 0 && !closed;
},
@action

View File

@ -288,6 +288,6 @@ class TopicViewSerializer < ApplicationSerializer
end
def include_user_last_posted_at?
object.topic.slow_mode_seconds.to_i > 0
has_topic_user? && object.topic.slow_mode_seconds.to_i > 0
end
end

View File

@ -423,4 +423,38 @@ describe TopicViewSerializer do
end
end
describe '#user_last_posted_at' do
context 'When the slow mode is disabled' do
it 'returns nil' do
Fabricate(:topic_user, user: user, topic: topic, last_posted_at: 6.hours.ago)
json = serialize_topic(topic, user)
expect(json[:user_last_posted_at]).to be_nil
end
end
context 'Wwhen the slow mode is enabled' do
before { topic.update!(slow_mode_seconds: 1000) }
it 'returns nil if no user is given' do
json = serialize_topic(topic, nil)
expect(json[:user_last_posted_at]).to be_nil
end
it "returns nil if there's no topic_user association" do
json = serialize_topic(topic, user)
expect(json[:user_last_posted_at]).to be_nil
end
it 'returns the last time the user posted' do
Fabricate(:topic_user, user: user, topic: topic, last_posted_at: 6.hours.ago)
json = serialize_topic(topic, user)
expect(json[:user_last_posted_at]).to be_present
end
end
end
end