FIX: Staff can create and edit posts even if a topic is in slow mode. (#11057)

Additionally, ninja edits are no longer restricted.
This commit is contained in:
Roman Rizzi 2020-10-28 16:47:50 -03:00 committed by GitHub
parent e3de45359f
commit 2f32336081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 73 additions and 6 deletions

View File

@ -648,9 +648,19 @@ export default Controller.extend({
} }
const topic = composer.topic; const topic = composer.topic;
const slowModePost =
topic && topic.slow_mode_seconds && topic.user_last_posted_at;
const notEditing = this.get("model.action") !== "edit";
if (topic && topic.slow_mode_seconds && topic.user_last_posted_at) { // Editing a topic in slow mode is directly handled by the backend.
if (cannotPostAgain(topic.slow_mode_seconds, topic.user_last_posted_at)) { if (slowModePost && notEditing) {
if (
cannotPostAgain(
this.currentUser,
topic.slow_mode_seconds,
topic.user_last_posted_at
)
) {
const message = I18n.t("composer.slow_mode.error"); const message = I18n.t("composer.slow_mode.error");
bootbox.alert(message); bootbox.alert(message);

View File

@ -29,9 +29,9 @@ export function durationTextFromSeconds(seconds) {
return moment.duration(seconds, "seconds").humanize(); return moment.duration(seconds, "seconds").humanize();
} }
export function cannotPostAgain(duration, last_posted_at) { export function cannotPostAgain(user, duration, last_posted_at) {
let threshold = new Date(last_posted_at); let threshold = new Date(last_posted_at);
threshold = new Date(threshold.getTime() + duration * 1000); threshold = new Date(threshold.getTime() + duration * 1000);
return new Date() < threshold; return !user.staff && new Date() < threshold;
} }

View File

@ -383,6 +383,7 @@ en:
too_late_to_edit: "That post was created too long ago. It can no longer be edited or deleted." too_late_to_edit: "That post was created too long ago. It can no longer be edited or deleted."
edit_conflict: "That post was edited by another user and your changes can no longer be saved." edit_conflict: "That post was edited by another user and your changes can no longer be saved."
revert_version_same: "The current version is same as the version you are trying to revert to." revert_version_same: "The current version is same as the version you are trying to revert to."
cannot_edit_on_slow_mode: "The topic is in slow mode and that post can no longer be edited."
excerpt_image: "image" excerpt_image: "image"

View File

@ -219,4 +219,8 @@ module TopicGuardian
can_perform_action_available_to_group_moderators?(topic) can_perform_action_available_to_group_moderators?(topic)
end end
def affected_by_slow_mode?(topic)
topic&.slow_mode_seconds.to_i > 0 && @user.human? && !is_staff?
end
end end

View File

@ -159,7 +159,7 @@ class PostCreator
return false return false
end end
if @topic&.slow_mode_seconds.to_i > 0 if guardian.affected_by_slow_mode?(@topic)
tu = TopicUser.find_by(user: @user, topic: @topic) tu = TopicUser.find_by(user: @user, topic: @topic)
if tu&.last_posted_at if tu&.last_posted_at

View File

@ -144,6 +144,11 @@ class PostRevisor
@revised_at = @opts[:revised_at] || Time.now @revised_at = @opts[:revised_at] || Time.now
@last_version_at = @post.last_version_at || Time.now @last_version_at = @post.last_version_at || Time.now
if guardian.affected_by_slow_mode?(@topic) && !ninja_edit?
@post.errors.add(:base, I18n.t("cannot_edit_on_slow_mode"))
return false
end
@version_changed = false @version_changed = false
@post_successfully_saved = true @post_successfully_saved = true
@ -194,7 +199,7 @@ class PostRevisor
# We log staff/group moderator edits to posts # We log staff/group moderator edits to posts
if ( if (
(@editor.staff? || (@post.is_category_description? && Guardian.new(@editor).can_edit_category_description?(@post.topic.category))) && (@editor.staff? || (@post.is_category_description? && guardian.can_edit_category_description?(@post.topic.category))) &&
@editor.id != @post.user_id && @editor.id != @post.user_id &&
@fields.has_key?('raw') && @fields.has_key?('raw') &&
!@opts[:skip_staff_log] !@opts[:skip_staff_log]
@ -639,4 +644,8 @@ class PostRevisor
@post_successfully_saved && !@topic_changes.errored? @post_successfully_saved && !@topic_changes.errored?
end end
def guardian
@guardian ||= Guardian.new(@editor)
end
end end

View File

@ -747,6 +747,17 @@ describe PostCreator do
expect(post).to be_present expect(post).to be_present
expect(creator.errors.count).to be_zero expect(creator.errors.count).to be_zero
end end
it 'creates the topic if the user is a staff member' do
admin = Fabricate(:admin)
post_creator = PostCreator.new(admin, raw: 'test reply', topic_id: topic.id, reply_to_post_number: 4)
TopicUser.create!(user: admin, topic: topic, last_posted_at: 10.minutes.ago)
post = post_creator.create
expect(post).to be_present
expect(post_creator.errors.count).to be_zero
end
end end
end end

View File

@ -128,6 +128,38 @@ describe PostRevisor do
end end
end end
describe 'topic is in slow mode' do
before do
topic.update!(slow_mode_seconds: 1000)
end
it 'regular edit' do
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.minutes)
expect(post.errors.present?).to eq(true)
expect(post.errors.messages[:base].first).to be I18n.t("cannot_edit_on_slow_mode")
end
it 'ninja editing is allowed' do
SiteSetting.editing_grace_period = 1.minute
subject.revise!(post.user, { raw: 'updated body' }, revised_at: post.updated_at + 10.seconds)
post.reload
expect(post.errors).to be_empty
end
it 'staff is allowed to edit posts even if the topic is in slow mode' do
admin = Fabricate(:admin)
subject.revise!(admin, { raw: 'updated body' }, revised_at: post.updated_at + 10.minutes)
post.reload
expect(post.errors).to be_empty
end
end
describe 'ninja editing' do describe 'ninja editing' do
it 'correctly applies edits' do it 'correctly applies edits' do
SiteSetting.editing_grace_period = 1.minute SiteSetting.editing_grace_period = 1.minute