2024-05-22 22:19:07 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Flag < ActiveRecord::Base
|
2024-07-18 00:00:27 -04:00
|
|
|
# TODO(2025-01-15): krisk remove
|
|
|
|
self.ignored_columns = ["custom_type"]
|
|
|
|
|
2024-07-02 18:45:37 -04:00
|
|
|
DEFAULT_VALID_APPLIES_TO = %w[Post Topic]
|
2024-05-22 22:19:07 -04:00
|
|
|
MAX_SYSTEM_FLAG_ID = 1000
|
2024-07-02 18:45:37 -04:00
|
|
|
MAX_NAME_LENGTH = 200
|
|
|
|
MAX_DESCRIPTION_LENGTH = 1000
|
2024-05-22 22:19:07 -04:00
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
scope :system, -> { where("id < 1000") }
|
|
|
|
|
|
|
|
before_save :set_position
|
|
|
|
before_save :set_name_key
|
2024-07-15 05:45:25 -04:00
|
|
|
after_commit :reset_flag_settings!
|
2024-05-22 22:19:07 -04:00
|
|
|
|
2024-06-04 23:27:06 -04:00
|
|
|
default_scope { order(:position).where(score_type: false) }
|
2024-05-22 22:19:07 -04:00
|
|
|
|
|
|
|
def used?
|
|
|
|
PostAction.exists?(post_action_type_id: self.id) ||
|
|
|
|
ReviewableScore.exists?(reviewable_score_type: self.id)
|
|
|
|
end
|
|
|
|
|
2024-07-02 18:45:37 -04:00
|
|
|
def self.valid_applies_to_types
|
|
|
|
Set.new(DEFAULT_VALID_APPLIES_TO | DiscoursePluginRegistry.flag_applies_to_types)
|
|
|
|
end
|
|
|
|
|
2024-05-22 22:19:07 -04:00
|
|
|
def self.reset_flag_settings!
|
|
|
|
# Flags are memoized for better performance. After the update, we need to reload them in all processes.
|
|
|
|
PostActionType.reload_types
|
2024-07-15 05:45:25 -04:00
|
|
|
MessageBus.publish("/reload_post_action_types", {})
|
2024-05-22 22:19:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def system?
|
|
|
|
self.id < MAX_SYSTEM_FLAG_ID
|
|
|
|
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
|
2024-07-17 20:10:22 -04:00
|
|
|
# require_message :boolean default(FALSE), not null
|
2024-05-22 22:19:07 -04:00
|
|
|
# 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
|
2024-05-29 00:39:58 -04:00
|
|
|
# score_type :boolean default(FALSE), not null
|
2024-05-22 22:19:07 -04:00
|
|
|
#
|