2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
class DiscourseChatIntegration::Rule < DiscourseChatIntegration::PluginModel
|
2017-07-12 13:28:45 -04:00
|
|
|
# Setup ActiveRecord::Store to use the JSON field to read/write these values
|
2022-12-29 07:31:05 -05:00
|
|
|
store :value, accessors: %i[channel_id type group_id category_id tags filter], coder: JSON
|
2017-07-12 13:28:45 -04:00
|
|
|
|
2017-09-28 04:32:38 -04:00
|
|
|
scope :with_type, ->(type) { where("value::json->>'type'=?", type.to_s) }
|
|
|
|
scope :with_channel, ->(channel) { with_channel_id(channel.id) }
|
|
|
|
scope :with_channel_id, ->(channel_id) { where("value::json->>'channel_id'=?", channel_id.to_s) }
|
2017-10-03 03:30:38 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
scope :with_category_id,
|
2023-11-29 17:01:31 -05:00
|
|
|
->(category_id) do
|
2022-12-29 07:31:05 -05:00
|
|
|
if category_id.nil?
|
|
|
|
where(
|
|
|
|
"(value::json->'category_id') IS NULL OR json_typeof(value::json->'category_id')='null'",
|
|
|
|
)
|
|
|
|
else
|
|
|
|
where("value::json->>'category_id'=?", category_id.to_s)
|
|
|
|
end
|
2023-11-29 17:01:31 -05:00
|
|
|
end
|
2022-12-29 07:31:05 -05:00
|
|
|
|
|
|
|
scope :with_group_ids,
|
|
|
|
->(group_id) { where("value::json->>'group_id' IN (?)", group_id.map!(&:to_s)) }
|
|
|
|
|
|
|
|
scope :order_by_precedence,
|
2023-11-29 17:01:31 -05:00
|
|
|
-> do
|
2022-12-29 07:31:05 -05:00
|
|
|
order(
|
|
|
|
"
|
2017-10-03 03:30:38 -04:00
|
|
|
CASE
|
|
|
|
WHEN value::json->>'type' = 'group_mention' THEN 1
|
|
|
|
WHEN value::json->>'type' = 'group_message' THEN 2
|
|
|
|
ELSE 3
|
|
|
|
END
|
|
|
|
",
|
2022-12-29 07:31:05 -05:00
|
|
|
"
|
2017-10-03 03:30:38 -04:00
|
|
|
CASE
|
|
|
|
WHEN value::json->>'filter' = 'mute' THEN 1
|
2020-06-15 11:45:25 -04:00
|
|
|
WHEN value::json->>'filter' = 'thread' THEN 2
|
|
|
|
WHEN value::json->>'filter' = 'watch' THEN 3
|
2023-04-06 07:55:47 -04:00
|
|
|
WHEN value::json->>'filter' = 'tag_added' THEN 4
|
|
|
|
WHEN value::json->>'filter' = 'follow' THEN 5
|
2017-10-03 03:30:38 -04:00
|
|
|
END
|
2022-12-29 07:31:05 -05:00
|
|
|
",
|
|
|
|
)
|
2023-11-29 17:01:31 -05:00
|
|
|
end
|
2017-09-28 04:32:38 -04:00
|
|
|
|
|
|
|
after_initialize :init_filter
|
2017-07-12 13:28:45 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
validates :filter,
|
|
|
|
inclusion: {
|
2023-04-06 07:55:47 -04:00
|
|
|
in: %w[thread watch follow tag_added mute],
|
2022-12-29 07:31:05 -05:00
|
|
|
message: "%{value} is not a valid filter",
|
|
|
|
}
|
2017-07-12 13:28:45 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
validates :type,
|
|
|
|
inclusion: {
|
|
|
|
in: %w[normal group_message group_mention],
|
|
|
|
message: "%{value} is not a valid filter",
|
|
|
|
}
|
2017-08-01 10:20:00 -04:00
|
|
|
|
|
|
|
validate :channel_valid?, :category_valid?, :group_valid?, :tags_valid?
|
2017-07-12 13:28:45 -04:00
|
|
|
|
2017-10-03 03:35:27 -04:00
|
|
|
def self.key_prefix
|
2022-12-29 07:31:05 -05:00
|
|
|
"rule:".freeze
|
2017-10-03 03:35:27 -04:00
|
|
|
end
|
|
|
|
|
2017-07-12 13:28:45 -04:00
|
|
|
# We never want an empty array, set it to nil instead
|
|
|
|
def tags=(array)
|
2017-08-01 15:53:39 -04:00
|
|
|
if array.nil? || array.empty?
|
2017-07-12 13:28:45 -04:00
|
|
|
super(nil)
|
|
|
|
else
|
|
|
|
super(array)
|
2017-06-29 12:01:04 -04:00
|
|
|
end
|
2017-07-12 13:28:45 -04:00
|
|
|
end
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2017-07-31 12:09:21 -04:00
|
|
|
# These are only allowed to be integers
|
2022-12-29 07:31:05 -05:00
|
|
|
%w[channel_id category_id group_id].each do |name|
|
2017-07-31 12:09:21 -04:00
|
|
|
define_method "#{name}=" do |val|
|
2017-08-01 15:53:39 -04:00
|
|
|
if val.nil? || val.blank?
|
2017-07-31 12:09:21 -04:00
|
|
|
super(nil)
|
|
|
|
else
|
|
|
|
super(val.to_i)
|
|
|
|
end
|
2017-07-18 13:23:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
# Mock foreign key
|
2017-07-18 18:08:06 -04:00
|
|
|
# Could return nil
|
2017-07-13 11:09:34 -04:00
|
|
|
def channel
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Channel.find_by(id: channel_id)
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
2017-10-03 03:30:38 -04:00
|
|
|
|
2017-07-13 11:09:34 -04:00
|
|
|
def channel=(val)
|
|
|
|
self.channel_id = val.id
|
|
|
|
end
|
2017-06-29 15:19:40 -04:00
|
|
|
|
2017-09-28 04:32:38 -04:00
|
|
|
private
|
2017-08-01 10:20:00 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def channel_valid?
|
2021-07-13 15:36:16 -04:00
|
|
|
if !(DiscourseChatIntegration::Channel.where(id: channel_id).exists?)
|
2018-06-07 21:52:35 -04:00
|
|
|
errors.add(:channel_id, "#{channel_id} is not a valid channel id")
|
2017-09-28 04:32:38 -04:00
|
|
|
end
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-07-03 12:38:13 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def category_valid?
|
2022-12-29 07:31:05 -05:00
|
|
|
if type != "normal" && !category_id.nil?
|
2018-06-07 21:52:35 -04:00
|
|
|
errors.add(:category_id, "cannot be specified for that type of rule")
|
|
|
|
end
|
2017-06-29 12:01:04 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
return unless type == "normal"
|
2017-09-28 04:32:38 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
if !(category_id.nil? || Category.where(id: category_id).exists?)
|
|
|
|
errors.add(:category_id, "#{category_id} is not a valid category id")
|
2017-09-28 04:32:38 -04:00
|
|
|
end
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-09-28 04:32:38 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def group_valid?
|
2022-12-29 07:31:05 -05:00
|
|
|
if type == "normal" && !group_id.nil?
|
2018-06-07 21:52:35 -04:00
|
|
|
errors.add(:group_id, "cannot be specified for that type of rule")
|
|
|
|
end
|
2017-07-18 16:36:07 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
return if type == "normal"
|
2017-09-28 04:32:38 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
if !Group.where(id: group_id).exists?
|
|
|
|
errors.add(:group_id, "#{group_id} is not a valid group id")
|
2017-09-28 04:32:38 -04:00
|
|
|
end
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-09-28 04:32:38 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def tags_valid?
|
|
|
|
return if tags.nil?
|
2017-10-03 03:30:38 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
tags.each do |tag|
|
2022-12-29 07:31:05 -05:00
|
|
|
errors.add(:tags, "#{tag} is not a valid tag") if !Tag.where(name: tag).exists?
|
2017-09-28 04:32:38 -04:00
|
|
|
end
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-09-28 04:32:38 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def init_filter
|
2022-12-29 07:31:05 -05:00
|
|
|
self.filter ||= "watch"
|
|
|
|
self.type ||= "normal"
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|