mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 03:29:31 +08:00
FEATURE: Allow moderators to change topic timestamps (#7053)
This commit is contained in:
parent
433f07fcb3
commit
d1bad881ea
|
@ -201,7 +201,7 @@ export default createWidget("topic-admin-menu", {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.currentUser.admin) {
|
if (this.currentUser.get("staff")) {
|
||||||
buttons.push({
|
buttons.push({
|
||||||
className: "topic-admin-change-timestamp",
|
className: "topic-admin-change-timestamp",
|
||||||
buttonClass: "btn-default",
|
buttonClass: "btn-default",
|
||||||
|
|
|
@ -676,17 +676,22 @@ class TopicsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def change_timestamps
|
def change_timestamps
|
||||||
params.require(:topic_id)
|
topic_id = params.require(:topic_id).to_i
|
||||||
params.require(:timestamp)
|
timestamp = params.require(:timestamp).to_f
|
||||||
|
|
||||||
guardian.ensure_can_change_post_timestamps!
|
guardian.ensure_can_change_post_timestamps!
|
||||||
|
|
||||||
|
topic = Topic.with_deleted.find(topic_id)
|
||||||
|
previous_timestamp = topic.first_post.created_at
|
||||||
|
|
||||||
begin
|
begin
|
||||||
TopicTimestampChanger.new(
|
TopicTimestampChanger.new(
|
||||||
topic_id: params[:topic_id].to_i,
|
topic: topic,
|
||||||
timestamp: params[:timestamp].to_f
|
timestamp: timestamp
|
||||||
).change!
|
).change!
|
||||||
|
|
||||||
|
StaffActionLogger.new(current_user).log_topic_timestamps_changed(topic, Time.zone.at(timestamp), previous_timestamp)
|
||||||
|
|
||||||
render json: success_json
|
render json: success_json
|
||||||
rescue ActiveRecord::RecordInvalid, TopicTimestampChanger::InvalidTimestampError
|
rescue ActiveRecord::RecordInvalid, TopicTimestampChanger::InvalidTimestampError
|
||||||
render json: failed_json, status: 422
|
render json: failed_json, status: 422
|
||||||
|
|
|
@ -83,7 +83,8 @@ class UserHistory < ActiveRecord::Base
|
||||||
post_rejected: 64,
|
post_rejected: 64,
|
||||||
merge_user: 65,
|
merge_user: 65,
|
||||||
entity_export: 66,
|
entity_export: 66,
|
||||||
change_password: 67
|
change_password: 67,
|
||||||
|
topic_timestamps_changed: 68
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -145,7 +146,8 @@ class UserHistory < ActiveRecord::Base
|
||||||
:post_rejected,
|
:post_rejected,
|
||||||
:merge_user,
|
:merge_user,
|
||||||
:entity_export,
|
:entity_export,
|
||||||
:change_name
|
:change_name,
|
||||||
|
:topic_timestamps_changed
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -117,6 +117,16 @@ class StaffActionLogger
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def log_topic_timestamps_changed(topic, new_timestamp, previous_timestamp, opts = {})
|
||||||
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
||||||
|
UserHistory.create!(params(opts).merge(
|
||||||
|
action: UserHistory.actions[:topic_timestamps_changed],
|
||||||
|
topic_id: topic.id,
|
||||||
|
new_value: new_timestamp,
|
||||||
|
previous_value: previous_timestamp)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def log_post_lock(post, opts = {})
|
def log_post_lock(post, opts = {})
|
||||||
raise Discourse::InvalidParameters.new(:post) unless post && post.is_a?(Post)
|
raise Discourse::InvalidParameters.new(:post) unless post && post.is_a?(Post)
|
||||||
UserHistory.create!(params(opts).merge(
|
UserHistory.create!(params(opts).merge(
|
||||||
|
|
|
@ -3669,6 +3669,7 @@ en:
|
||||||
merge_user: "merge user"
|
merge_user: "merge user"
|
||||||
entity_export: "export entity"
|
entity_export: "export entity"
|
||||||
change_name: "change name"
|
change_name: "change name"
|
||||||
|
topic_timestamps_changed: "topic timestamps changed"
|
||||||
screened_emails:
|
screened_emails:
|
||||||
title: "Screened Emails"
|
title: "Screened Emails"
|
||||||
description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."
|
description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."
|
||||||
|
|
|
@ -226,7 +226,7 @@ module PostGuardian
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_change_post_timestamps?
|
def can_change_post_timestamps?
|
||||||
is_admin?
|
is_staff?
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_wiki?(post)
|
def can_wiki?(post)
|
||||||
|
|
|
@ -572,19 +572,17 @@ RSpec.describe TopicsController do
|
||||||
expect(response.status).to eq(403)
|
expect(response.status).to eq(403)
|
||||||
end
|
end
|
||||||
|
|
||||||
[:moderator, :trust_level_4].each do |user|
|
describe "forbidden to trust_level_4" do
|
||||||
describe "forbidden to #{user}" do
|
let!(:trust_level_4) { sign_in(Fabricate(:trust_level_4)) }
|
||||||
let!(user) { sign_in(Fabricate(user)) }
|
|
||||||
|
|
||||||
it 'correctly denies' do
|
it 'correctly denies' do
|
||||||
put "/t/1/change-timestamp.json", params: params
|
put "/t/1/change-timestamp.json", params: params
|
||||||
expect(response).to be_forbidden
|
expect(response).to be_forbidden
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe 'changing timestamps' do
|
describe 'changing timestamps' do
|
||||||
let!(:admin) { sign_in(Fabricate(:admin)) }
|
let!(:moderator) { sign_in(Fabricate(:moderator)) }
|
||||||
let(:old_timestamp) { Time.zone.now }
|
let(:old_timestamp) { Time.zone.now }
|
||||||
let(:new_timestamp) { old_timestamp - 1.day }
|
let(:new_timestamp) { old_timestamp - 1.day }
|
||||||
let!(:topic) { Fabricate(:topic, created_at: old_timestamp) }
|
let!(:topic) { Fabricate(:topic, created_at: old_timestamp) }
|
||||||
|
@ -605,6 +603,18 @@ RSpec.describe TopicsController do
|
||||||
expect(p1.reload.created_at).to be_within_one_second_of(new_timestamp)
|
expect(p1.reload.created_at).to be_within_one_second_of(new_timestamp)
|
||||||
expect(p2.reload.created_at).to be_within_one_second_of(old_timestamp)
|
expect(p2.reload.created_at).to be_within_one_second_of(old_timestamp)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should create a staff log entry' do
|
||||||
|
put "/t/#{topic.id}/change-timestamp.json", params: {
|
||||||
|
timestamp: new_timestamp.to_f
|
||||||
|
}
|
||||||
|
|
||||||
|
log = UserHistory.last
|
||||||
|
expect(log.acting_user_id).to eq(moderator.id)
|
||||||
|
expect(log.topic_id).to eq(topic.id)
|
||||||
|
expect(log.new_value).to eq(new_timestamp.utc.to_s)
|
||||||
|
expect(log.previous_value).to eq(old_timestamp.utc.to_s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user