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:
parent
e3de45359f
commit
2f32336081
|
@ -648,9 +648,19 @@ export default Controller.extend({
|
|||
}
|
||||
|
||||
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) {
|
||||
if (cannotPostAgain(topic.slow_mode_seconds, topic.user_last_posted_at)) {
|
||||
// Editing a topic in slow mode is directly handled by the backend.
|
||||
if (slowModePost && notEditing) {
|
||||
if (
|
||||
cannotPostAgain(
|
||||
this.currentUser,
|
||||
topic.slow_mode_seconds,
|
||||
topic.user_last_posted_at
|
||||
)
|
||||
) {
|
||||
const message = I18n.t("composer.slow_mode.error");
|
||||
|
||||
bootbox.alert(message);
|
||||
|
|
|
@ -29,9 +29,9 @@ export function durationTextFromSeconds(seconds) {
|
|||
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);
|
||||
threshold = new Date(threshold.getTime() + duration * 1000);
|
||||
|
||||
return new Date() < threshold;
|
||||
return !user.staff && new Date() < threshold;
|
||||
}
|
||||
|
|
|
@ -383,6 +383,7 @@ en:
|
|||
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."
|
||||
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"
|
||||
|
||||
|
|
|
@ -219,4 +219,8 @@ module TopicGuardian
|
|||
can_perform_action_available_to_group_moderators?(topic)
|
||||
end
|
||||
|
||||
def affected_by_slow_mode?(topic)
|
||||
topic&.slow_mode_seconds.to_i > 0 && @user.human? && !is_staff?
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -159,7 +159,7 @@ class PostCreator
|
|||
return false
|
||||
end
|
||||
|
||||
if @topic&.slow_mode_seconds.to_i > 0
|
||||
if guardian.affected_by_slow_mode?(@topic)
|
||||
tu = TopicUser.find_by(user: @user, topic: @topic)
|
||||
|
||||
if tu&.last_posted_at
|
||||
|
|
|
@ -144,6 +144,11 @@ class PostRevisor
|
|||
@revised_at = @opts[:revised_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
|
||||
@post_successfully_saved = true
|
||||
|
||||
|
@ -194,7 +199,7 @@ class PostRevisor
|
|||
|
||||
# We log staff/group moderator edits to posts
|
||||
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 &&
|
||||
@fields.has_key?('raw') &&
|
||||
!@opts[:skip_staff_log]
|
||||
|
@ -639,4 +644,8 @@ class PostRevisor
|
|||
@post_successfully_saved && !@topic_changes.errored?
|
||||
end
|
||||
|
||||
def guardian
|
||||
@guardian ||= Guardian.new(@editor)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -747,6 +747,17 @@ describe PostCreator do
|
|||
expect(post).to be_present
|
||||
expect(creator.errors.count).to be_zero
|
||||
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
|
||||
|
||||
|
|
|
@ -128,6 +128,38 @@ describe PostRevisor do
|
|||
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
|
||||
it 'correctly applies edits' do
|
||||
SiteSetting.editing_grace_period = 1.minute
|
||||
|
|
Loading…
Reference in New Issue