-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: move post flags into database (#26951)
This is preparation for a feature that will allow admins to define their custom flags. Current behaviour should stay untouched.
- Loading branch information
Showing
28 changed files
with
346 additions
and
83 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
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,61 @@ | ||
# frozen_string_literal: true | ||
|
||
class Flag < ActiveRecord::Base | ||
scope :enabled, -> { where(enabled: true) } | ||
scope :system, -> { where("id < 1000") } | ||
|
||
before_save :set_position | ||
before_save :set_name_key | ||
after_save :reset_flag_settings! | ||
after_destroy :reset_flag_settings! | ||
|
||
def used? | ||
PostAction.exists?(post_action_type_id: self.id) || | ||
ReviewableScore.exists?(reviewable_score_type: self.id) | ||
end | ||
|
||
def self.reset_flag_settings! | ||
PostActionType.reload_types | ||
ReviewableScore.reload_types | ||
end | ||
|
||
def system? | ||
self.id < 1000 | ||
end | ||
|
||
def applies_to?(type) | ||
self.applies_to.include?(type) | ||
end | ||
|
||
private | ||
|
||
def reset_flag_settings! | ||
self.class.reset_flag_settings! | ||
end | ||
|
||
def set_position | ||
self.position = Flag.maximum(:position).to_i + 1 if !self.position | ||
end | ||
|
||
def set_name_key | ||
self.name_key = self.name.squeeze(" ").gsub(" ", "_").gsub(/[^\w]/, "").downcase | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: flags | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# name_key :string | ||
# description :text | ||
# notify_type :boolean default(FALSE), not null | ||
# auto_action_type :boolean default(FALSE), not null | ||
# custom_type :boolean default(FALSE), not null | ||
# applies_to :string not null, is an Array | ||
# position :integer not null | ||
# enabled :boolean default(TRUE), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# |
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
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
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,56 @@ | ||
# frozen_string_literal: true | ||
|
||
Flag.seed do |s| | ||
s.id = 3 | ||
s.name = "off_topic" | ||
s.position = 2 | ||
s.notify_type = true | ||
s.auto_action_type = true | ||
s.custom_type = false | ||
s.applies_to = %w[Post Chat::Message] | ||
end | ||
Flag.seed do |s| | ||
s.id = 4 | ||
s.name = "inappropriate" | ||
s.position = 3 | ||
s.notify_type = true | ||
s.auto_action_type = true | ||
s.custom_type = false | ||
s.applies_to = %w[Post Topic Chat::Message] | ||
end | ||
Flag.seed do |s| | ||
s.id = 8 | ||
s.name = "spam" | ||
s.position = 4 | ||
s.notify_type = true | ||
s.auto_action_type = true | ||
s.custom_type = false | ||
s.applies_to = %w[Post Topic Chat::Message] | ||
end | ||
Flag.seed do |s| | ||
s.id = 6 | ||
s.name = "notify_user" | ||
s.position = 0 | ||
s.notify_type = false | ||
s.auto_action_type = false | ||
s.custom_type = true | ||
s.applies_to = %w[Post Chat::Message] | ||
end | ||
Flag.seed do |s| | ||
s.id = 7 | ||
s.name = "notify_moderators" | ||
s.position = 1 | ||
s.notify_type = true | ||
s.auto_action_type = false | ||
s.custom_type = true | ||
s.applies_to = %w[Post Topic Chat::Message] | ||
end | ||
Flag.seed do |s| | ||
s.id = 10 | ||
s.name = "illegal" | ||
s.position = 5 | ||
s.notify_type = true | ||
s.auto_action_type = false | ||
s.custom_type = true | ||
s.applies_to = %w[Post Topic Chat::Message] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateFlags < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :flags do |t| | ||
t.string :name, unique: true | ||
t.string :name_key, unique: true | ||
t.text :description | ||
t.boolean :notify_type, default: false, null: false | ||
t.boolean :auto_action_type, default: false, null: false | ||
t.boolean :custom_type, default: false, null: false | ||
t.string :applies_to, array: true, null: false | ||
t.integer :position, null: false | ||
t.boolean :enabled, default: true, null: false | ||
t.timestamps | ||
end | ||
DB.exec("SELECT setval('flags_id_seq', 1001, FALSE);") | ||
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module FlagGuardian | ||
def can_edit_flag?(flag) | ||
@user.admin? && !flag.system? && !flag.used? | ||
end | ||
end |
Oops, something went wrong.