This repository has been archived by the owner on May 27, 2022. It is now read-only.
forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: adds support for bannered until (discourse#13417)
ATM it only implements server side of it, as my need is for automation purposes. However it should probably be added in the UI too as it's unexpected to have pinned_until and no bannered_until.
- Loading branch information
Showing
8 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Jobs | ||
|
||
class RemoveBanner < ::Jobs::Base | ||
|
||
def execute(args) | ||
topic_id = args[:topic_id] | ||
|
||
return unless topic_id.present? | ||
|
||
topic = Topic.find_by(id: topic_id) | ||
topic.remove_banner!(Discourse.system_user) if topic.present? | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddBanneredUntil < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :topics, :bannered_until, :datetime, null: true | ||
|
||
add_index :topics, :bannered_until, where: 'bannered_until IS NOT NULL' | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20210624080131_add_partial_index_pinned_until.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddPartialIndexPinnedUntil < ActiveRecord::Migration[6.1] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
add_index :topics, :pinned_until, | ||
where: 'pinned_until IS NOT NULL', | ||
algorithm: :concurrently | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe Jobs::RemoveBanner do | ||
fab!(:topic) { Fabricate(:topic) } | ||
fab!(:user) { topic.user } | ||
|
||
context 'topic is not bannered until' do | ||
it 'doesn’t enqueue a future job to remove it' do | ||
expect do | ||
topic.make_banner!(user) | ||
end.to change { Jobs::RemoveBanner.jobs.size }.by(0) | ||
end | ||
end | ||
|
||
context 'topic is bannered until' do | ||
context 'bannered_until is a valid date' do | ||
it 'enqueues a future job to remove it' do | ||
bannered_until = 5.days.from_now | ||
|
||
expect(topic.archetype).to eq(Archetype.default) | ||
|
||
expect do | ||
topic.make_banner!(user, bannered_until.to_s) | ||
end.to change { Jobs::RemoveBanner.jobs.size }.by(1) | ||
|
||
topic.reload | ||
expect(topic.archetype).to eq(Archetype.banner) | ||
|
||
job = Jobs::RemoveBanner.jobs[0] | ||
expect(Time.at(job['at'])).to be_within_one_minute_of(bannered_until) | ||
expect(job['args'][0]['topic_id']).to eq(topic.id) | ||
|
||
job['class'].constantize.new.perform(*job['args']) | ||
topic.reload | ||
expect(topic.archetype).to eq(Archetype.default) | ||
end | ||
end | ||
|
||
context 'bannered_until is an invalid date' do | ||
it 'doesn’t enqueue a future job to remove it' do | ||
expect do | ||
expect do | ||
topic.make_banner!(user, 'xxx') | ||
end.to raise_error(Discourse::InvalidParameters) | ||
end.to change { Jobs::RemoveBanner.jobs.size }.by(0) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters