2017-07-12 18:59:43 -04:00
class DiscourseChat :: Rule < DiscourseChat :: PluginModel
KEY_PREFIX = 'rule:'
2017-07-12 13:28:45 -04:00
# Setup ActiveRecord::Store to use the JSON field to read/write these values
2017-07-28 15:27:49 -04:00
store :value , accessors : [ :channel_id , :group_id , :category_id , :tags , :filter ] , coder : JSON
2017-07-12 13:28:45 -04:00
2017-07-12 18:59:43 -04:00
after_initialize :init_filter
2017-07-12 13:28:45 -04:00
2017-07-12 18:59:43 -04:00
def init_filter
2017-07-12 13:28:45 -04:00
self . filter || = 'watch'
end
validates :filter , :inclusion = > { :in = > %w( watch follow mute ) ,
:message = > " %{value} is not a valid filter " }
2017-07-28 15:27:49 -04:00
validate :channel_valid? , :category_and_group_valid? , :tags_valid?
2017-07-12 13:28:45 -04:00
2017-07-13 11:09:34 -04:00
def channel_valid?
2017-07-28 15:27:49 -04:00
# Validate channel
2017-07-18 11:17:03 -04:00
if not ( DiscourseChat :: Channel . where ( id : channel_id ) . exists? )
2017-07-13 11:09:34 -04:00
errors . add ( :channel_id , " #{ channel_id } is not a valid channel id " )
2017-07-03 12:38:13 -04:00
end
2017-07-12 13:28:45 -04:00
end
2017-07-03 12:38:13 -04:00
2017-07-28 15:27:49 -04:00
def category_and_group_valid?
if category_id and group_id
errors . add ( :category_id , " cannot be specified in addition to group_id " )
return
end
if group_id
# Validate group
if not Group . where ( id : group_id ) . exists?
errors . add ( :group_id , " #{ group_id } is not a valid group id " )
end
return
end
2017-07-12 13:28:45 -04:00
# Validate category
if not ( category_id . nil? or Category . where ( id : category_id ) . exists? )
errors . add ( :category_id , " #{ category_id } is not a valid category id " )
2017-07-03 12:38:13 -04:00
end
2017-07-12 13:28:45 -04:00
end
2017-07-03 12:38:13 -04:00
2017-07-12 13:28:45 -04:00
def tags_valid?
# Validate tags
return if tags . nil?
tags . each do | tag |
if not Tag . where ( name : tag ) . exists?
errors . add ( :tags , " #{ tag } is not a valid tag " )
2017-06-29 12:01:04 -04:00
end
end
2017-07-12 13:28:45 -04:00
end
2017-06-29 12:01:04 -04:00
2017-07-12 13:28:45 -04:00
# We never want an empty array, set it to nil instead
def tags = ( array )
if array . nil? or array . empty?
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
%w( channel_id category_id group_id ) . each do | name |
define_method " #{ name } = " do | val |
if val . nil? or val . blank?
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
2017-07-18 18:08:06 -04:00
DiscourseChat :: Channel . find_by ( id :channel_id )
2017-07-13 11:09:34 -04:00
end
def channel = ( val )
self . channel_id = val . id
end
2017-06-29 15:19:40 -04:00
2017-07-13 11:09:34 -04:00
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-07-03 12:38:13 -04:00
2017-07-13 11:09:34 -04:00
scope :with_category_id , - > ( category_id ) { category_id . nil? ? where ( " (value::json->'category_id') IS NULL OR json_typeof(value::json->'category_id')='null' " ) : where ( " value::json->>'category_id'=? " , category_id . to_s ) }
2017-07-28 15:27:49 -04:00
scope :with_group_ids , - > ( group_id ) { where ( " value::json->>'group_id' IN (?) " , group_id . map ( & :to_s ) ) }
2017-06-29 12:01:04 -04:00
2017-07-18 16:36:07 -04:00
scope :order_by_precedence , - > { order ( " CASE
WHEN value :: json - >> 'filter' = 'mute' THEN 1
WHEN value :: json - >> 'filter' = 'watch' THEN 2
WHEN value :: json - >> 'filter' = 'follow' THEN 3
END " ) }
2017-07-12 13:28:45 -04:00
end