2020-03-30 08:16:03 +02:00
|
|
|
class CustomWizard::Action
|
|
|
|
attr_accessor :data,
|
|
|
|
:action,
|
|
|
|
:user,
|
2020-07-09 04:19:36 +02:00
|
|
|
:guardian,
|
2020-04-14 16:10:26 +02:00
|
|
|
:result
|
2020-03-30 08:16:03 +02:00
|
|
|
|
|
|
|
def initialize(params)
|
2020-04-15 02:46:44 +02:00
|
|
|
@wizard = params[:wizard]
|
2020-03-30 08:16:03 +02:00
|
|
|
@action = params[:action]
|
|
|
|
@user = params[:user]
|
2020-07-09 04:19:36 +02:00
|
|
|
@guardian = Guardian.new(@user)
|
2020-03-30 08:16:03 +02:00
|
|
|
@data = params[:data]
|
2020-04-15 02:46:44 +02:00
|
|
|
@log = []
|
2020-05-04 11:02:49 +02:00
|
|
|
@result = CustomWizard::ActionResult.new
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
|
2020-05-04 11:02:49 +02:00
|
|
|
def perform
|
2020-04-14 16:10:26 +02:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
self.send(action['type'].to_sym)
|
2020-04-15 02:46:44 +02:00
|
|
|
end
|
2020-05-04 11:02:49 +02:00
|
|
|
|
2020-07-17 09:02:48 +02:00
|
|
|
if @result.success? && @result.output.present?
|
|
|
|
data[action['id']] = @result.output
|
|
|
|
end
|
|
|
|
|
2020-05-04 11:02:49 +02:00
|
|
|
save_log
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def mapper
|
|
|
|
@mapper ||= CustomWizard::Mapper.new(user: user, data: data)
|
|
|
|
end
|
|
|
|
|
2020-03-31 08:29:59 +02:00
|
|
|
def create_topic
|
2020-07-20 06:26:11 +02:00
|
|
|
params = basic_topic_params.merge(public_topic_params)
|
2020-04-14 16:10:26 +02:00
|
|
|
|
2020-04-14 12:21:54 +02:00
|
|
|
if params[:title].present? && params[:raw].present?
|
2020-12-03 15:45:10 +01:00
|
|
|
manager = NewPostManager.new(user, params)
|
|
|
|
post_result = manager.perform
|
|
|
|
|
|
|
|
if post_result.errors.present?
|
|
|
|
messages = post_result.errors.full_messages.join(" ")
|
2020-04-15 02:46:44 +02:00
|
|
|
log_error("failed to create", messages)
|
2020-04-02 07:21:57 +02:00
|
|
|
elsif action['skip_redirect'].blank?
|
2020-12-03 15:45:10 +01:00
|
|
|
data['redirect_on_complete'] = post_result.post.topic.url
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
2020-04-14 16:10:26 +02:00
|
|
|
|
2020-12-03 15:45:10 +01:00
|
|
|
if post_result.errors.blank?
|
|
|
|
log_success("created topic", "id: #{post_result.post.topic.id}")
|
|
|
|
result.handler = manager
|
|
|
|
result.output = post_result.post.topic.id
|
2020-04-14 16:10:26 +02:00
|
|
|
end
|
|
|
|
else
|
2020-04-15 03:05:26 +02:00
|
|
|
log_error("invalid topic params", "title: #{params[:title]}; post: #{params[:raw]}")
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_message
|
2020-10-20 07:40:23 +02:00
|
|
|
|
|
|
|
if action['required'].present?
|
|
|
|
required = CustomWizard::Mapper.new(
|
|
|
|
inputs: action['required'],
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
|
|
|
|
|
|
|
if required.blank?
|
|
|
|
log_error("required input not present")
|
|
|
|
return
|
|
|
|
end
|
2020-04-15 03:05:26 +02:00
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
|
2020-04-02 07:21:57 +02:00
|
|
|
params = basic_topic_params
|
2020-10-05 04:41:18 +02:00
|
|
|
|
|
|
|
targets = CustomWizard::Mapper.new(
|
2020-04-02 07:21:57 +02:00
|
|
|
inputs: action['recipient'],
|
|
|
|
data: data,
|
2020-10-05 04:41:18 +02:00
|
|
|
user: user,
|
|
|
|
multiple: true
|
2020-04-06 10:36:38 +02:00
|
|
|
).perform
|
2020-10-05 04:41:18 +02:00
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
if targets.blank?
|
|
|
|
log_error("no recipients", "send_message has no recipients")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-10-05 04:41:18 +02:00
|
|
|
targets.each do |target|
|
|
|
|
if Group.find_by(name: target)
|
|
|
|
params[:target_group_names] = target
|
|
|
|
elsif User.find_by_username(target)
|
|
|
|
params[:target_usernames] = target
|
|
|
|
else
|
|
|
|
#
|
|
|
|
end
|
|
|
|
end
|
2020-04-02 10:21:03 +02:00
|
|
|
|
2020-10-05 04:41:18 +02:00
|
|
|
if params[:title].present? &&
|
|
|
|
params[:raw].present? &&
|
|
|
|
(params[:target_usernames].present? ||
|
|
|
|
params[:target_group_names].present?)
|
|
|
|
|
2020-04-02 07:21:57 +02:00
|
|
|
params[:archetype] = Archetype.private_message
|
|
|
|
|
2020-12-03 15:45:10 +01:00
|
|
|
manager = NewPostManager.new(user, params)
|
|
|
|
post_result = manager.perform
|
2020-03-30 08:16:03 +02:00
|
|
|
|
2020-12-03 15:45:10 +01:00
|
|
|
if post_result.errors.present?
|
|
|
|
messages = post_result.errors.full_messages.join(" ")
|
2020-04-15 02:46:44 +02:00
|
|
|
log_error("failed to create message", messages)
|
2020-04-02 07:21:57 +02:00
|
|
|
elsif action['skip_redirect'].blank?
|
2020-12-03 15:45:10 +01:00
|
|
|
data['redirect_on_complete'] = post_result.post.topic.url
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
2020-04-14 16:10:26 +02:00
|
|
|
|
2020-12-03 15:45:10 +01:00
|
|
|
if post_result.errors.blank?
|
|
|
|
log_success("created message", "id: #{post_result.post.topic.id}")
|
|
|
|
result.handler = manager
|
|
|
|
result.output = post_result.post.topic.id
|
2020-04-14 16:10:26 +02:00
|
|
|
end
|
|
|
|
else
|
2020-04-15 03:05:26 +02:00
|
|
|
log_error(
|
|
|
|
"invalid message params",
|
|
|
|
"title: #{params[:title]}; post: #{params[:raw]}; recipients: #{params[:target_usernames]}"
|
|
|
|
)
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_profile
|
2020-04-07 15:33:11 +02:00
|
|
|
params = {}
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-07-02 02:02:40 +02:00
|
|
|
if (profile_updates = action['profile_updates'])
|
|
|
|
profile_updates.first[:pairs].each do |pair|
|
|
|
|
if allowed_profile_field?(pair['key'])
|
|
|
|
key = cast_profile_key(pair['key'])
|
2020-10-20 01:15:03 +02:00
|
|
|
value = cast_profile_value(
|
|
|
|
mapper.map_field(
|
|
|
|
pair['value'],
|
|
|
|
pair['value_type']
|
|
|
|
),
|
|
|
|
pair['key']
|
|
|
|
)
|
2020-07-02 02:02:40 +02:00
|
|
|
|
|
|
|
if user_field?(pair['key'])
|
|
|
|
params[:custom_fields] ||= {}
|
|
|
|
params[:custom_fields][key] = value
|
|
|
|
else
|
|
|
|
params[key.to_sym] = value
|
|
|
|
end
|
2020-04-19 11:48:58 +02:00
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 15:33:11 +02:00
|
|
|
params = add_custom_fields(params)
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-04-07 15:33:11 +02:00
|
|
|
if params.present?
|
2020-04-14 16:10:26 +02:00
|
|
|
result = UserUpdater.new(Discourse.system_user, user).update(params)
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-04-07 15:33:11 +02:00
|
|
|
if params[:avatar].present?
|
2020-04-14 16:10:26 +02:00
|
|
|
result = update_avatar(params[:avatar])
|
|
|
|
end
|
|
|
|
|
|
|
|
if result
|
2020-04-15 05:34:54 +02:00
|
|
|
log_success("updated profile fields", "fields: #{params.keys.map(&:to_s).join(',')}")
|
2020-04-14 16:10:26 +02:00
|
|
|
else
|
2020-04-15 03:42:24 +02:00
|
|
|
log_error("failed to update profile fields", "result: #{result.inspect}")
|
2020-04-07 15:33:11 +02:00
|
|
|
end
|
2020-04-14 16:10:26 +02:00
|
|
|
else
|
2020-04-15 03:42:24 +02:00
|
|
|
log_error("invalid profile fields params", "params: #{params.inspect}")
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-23 00:42:26 +02:00
|
|
|
def watch_categories
|
2020-05-24 12:23:17 +02:00
|
|
|
watched_categories = CustomWizard::Mapper.new(
|
2020-05-24 12:20:15 +02:00
|
|
|
inputs: action['categories'],
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
2020-07-20 05:06:36 +02:00
|
|
|
|
2020-07-20 05:51:11 +02:00
|
|
|
watched_categories = [*watched_categories].map(&:to_i)
|
2020-05-24 12:20:15 +02:00
|
|
|
|
2020-05-25 15:59:31 +02:00
|
|
|
notification_level = action['notification_level']
|
|
|
|
|
2020-05-26 11:59:11 +02:00
|
|
|
if notification_level.blank?
|
2020-07-20 05:06:36 +02:00
|
|
|
log_error("Notifcation Level was not set. Exiting wizard action")
|
2020-05-26 11:59:11 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-05-24 12:20:15 +02:00
|
|
|
mute_remainder = CustomWizard::Mapper.new(
|
|
|
|
inputs: action['mute_remainder'],
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
2020-07-20 05:06:36 +02:00
|
|
|
|
|
|
|
users = []
|
|
|
|
|
|
|
|
if action['usernames']
|
|
|
|
mapped_users = CustomWizard::Mapper.new(
|
|
|
|
inputs: action['usernames'],
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
|
|
|
|
|
|
|
if mapped_users.present?
|
|
|
|
mapped_users = mapped_users.split(',')
|
|
|
|
.map { |username| User.find_by(username: username) }
|
|
|
|
users.push(*mapped_users)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if ActiveRecord::Type::Boolean.new.cast(action['wizard_user'])
|
|
|
|
users.push(user)
|
|
|
|
end
|
2020-05-24 12:20:15 +02:00
|
|
|
|
2020-07-20 05:06:36 +02:00
|
|
|
category_ids = Category.all.pluck(:id)
|
|
|
|
set_level = CategoryUser.notification_levels[notification_level.to_sym]
|
|
|
|
mute_level = CategoryUser.notification_levels[:muted]
|
|
|
|
|
|
|
|
users.each do |user|
|
|
|
|
category_ids.each do |category_id|
|
|
|
|
new_level = nil
|
|
|
|
|
|
|
|
if watched_categories.include?(category_id) && set_level != nil
|
|
|
|
new_level = set_level
|
|
|
|
elsif mute_remainder
|
|
|
|
new_level = mute_level
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_level
|
|
|
|
CategoryUser.set_notification_level_for_category(user, new_level, category_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if watched_categories.any?
|
|
|
|
log_success("#{user.username} notifications for #{watched_categories} set to #{set_level}")
|
|
|
|
end
|
|
|
|
|
|
|
|
if mute_remainder
|
|
|
|
log_success("#{user.username} notifications for all other categories muted")
|
2020-05-23 00:42:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-30 08:16:03 +02:00
|
|
|
def send_to_api
|
|
|
|
api_body = nil
|
|
|
|
|
|
|
|
if action['api_body'] != ""
|
|
|
|
begin
|
|
|
|
api_body_parsed = JSON.parse(action['api_body'])
|
|
|
|
rescue JSON::ParserError
|
|
|
|
raise Discourse::InvalidParameters, "Invalid API body definition: #{action['api_body']} for #{action['title']}"
|
|
|
|
end
|
|
|
|
api_body = JSON.parse(mapper.interpolate(JSON.generate(api_body_parsed)))
|
|
|
|
end
|
|
|
|
|
|
|
|
result = CustomWizard::Api::Endpoint.request(user, action['api'], action['api_endpoint'], api_body)
|
|
|
|
|
|
|
|
if error = result['error'] || (result[0] && result[0]['error'])
|
|
|
|
error = error['message'] || error
|
2020-04-15 05:34:54 +02:00
|
|
|
log_error("api request failed", "message: #{error}")
|
2020-03-30 08:16:03 +02:00
|
|
|
else
|
2020-04-15 05:34:54 +02:00
|
|
|
log_success("api request succeeded", "result: #{result}")
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-14 11:51:58 +02:00
|
|
|
def open_composer
|
2020-04-15 05:52:02 +02:00
|
|
|
params = basic_topic_params
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-04-15 05:52:02 +02:00
|
|
|
if params[:title].present? && params[:raw].present?
|
|
|
|
url = "/new-topic?title=#{params[:title]}"
|
|
|
|
url += "&body=#{params[:raw]}"
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-04-15 05:52:02 +02:00
|
|
|
if category_id = action_category
|
2020-11-03 01:24:20 +01:00
|
|
|
if category = Category.find_by(id: category_id)
|
2020-04-15 05:52:02 +02:00
|
|
|
url += "&category=#{category.full_slug('/')}"
|
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
2020-04-15 05:52:02 +02:00
|
|
|
|
|
|
|
if tags = action_tags
|
|
|
|
url += "&tags=#{tags.join(',')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
route_to = Discourse.base_uri + URI.encode(url)
|
2020-10-20 01:15:03 +02:00
|
|
|
data['route_to'] = route_to
|
2020-04-15 05:52:02 +02:00
|
|
|
|
|
|
|
log_info("route: #{route_to}")
|
|
|
|
else
|
|
|
|
log_error("invalid composer params", "title: #{params[:title]}; post: #{params[:raw]}")
|
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_to_group
|
2020-04-15 03:42:24 +02:00
|
|
|
group_map = CustomWizard::Mapper.new(
|
2020-04-14 11:51:58 +02:00
|
|
|
inputs: action['group'],
|
2020-03-30 08:16:03 +02:00
|
|
|
data: data,
|
|
|
|
user: user,
|
|
|
|
opts: {
|
|
|
|
multiple: true
|
|
|
|
}
|
2020-04-06 10:36:38 +02:00
|
|
|
).perform
|
2020-10-20 01:15:03 +02:00
|
|
|
|
|
|
|
group_map = group_map.flatten.compact
|
|
|
|
|
|
|
|
unless group_map.present?
|
|
|
|
log_error("invalid group map")
|
|
|
|
return
|
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
groups = group_map.reduce([]) do |groups, g|
|
2020-03-30 08:16:03 +02:00
|
|
|
begin
|
2020-04-15 03:42:24 +02:00
|
|
|
groups.push(Integer(g))
|
2020-03-30 08:16:03 +02:00
|
|
|
rescue ArgumentError
|
|
|
|
group = Group.find_by(name: g)
|
2020-04-15 03:42:24 +02:00
|
|
|
groups.push(group.id) if group
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
|
2020-04-15 03:42:24 +02:00
|
|
|
groups
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
|
2020-04-15 03:42:24 +02:00
|
|
|
result = nil
|
|
|
|
|
2020-03-30 08:16:03 +02:00
|
|
|
if groups.present?
|
|
|
|
groups.each do |group_id|
|
|
|
|
group = Group.find(group_id) if group_id
|
2020-04-14 16:10:26 +02:00
|
|
|
result = group.add(user) if group
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
end
|
2020-04-14 16:10:26 +02:00
|
|
|
|
|
|
|
if result
|
2020-04-15 03:42:24 +02:00
|
|
|
log_success("added to groups", "groups: #{groups.map(&:to_s).join(',')}")
|
2020-04-14 16:10:26 +02:00
|
|
|
else
|
2020-04-15 03:42:24 +02:00
|
|
|
detail = groups.present? ? "groups: #{groups.map(&:to_s).join(',')}" : nil
|
|
|
|
log_error("failed to add to groups", detail)
|
2020-04-14 16:10:26 +02:00
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def route_to
|
2020-04-29 03:47:08 +02:00
|
|
|
return unless (url_input = action['url']).present?
|
|
|
|
|
|
|
|
if url_input.is_a?(String)
|
|
|
|
url = mapper.interpolate(url_input)
|
|
|
|
else
|
|
|
|
url = CustomWizard::Mapper.new(
|
|
|
|
inputs: url_input,
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
|
|
|
end
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-03-30 08:16:03 +02:00
|
|
|
if action['code']
|
|
|
|
data[action['code']] = SecureRandom.hex(8)
|
|
|
|
url += "&#{action['code']}=#{data[action['code']]}"
|
|
|
|
end
|
2020-04-02 07:21:57 +02:00
|
|
|
|
2020-04-15 03:05:26 +02:00
|
|
|
route_to = URI.encode(url)
|
|
|
|
data['route_to'] = route_to
|
|
|
|
|
|
|
|
log_info("route: #{route_to}")
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
def create_group
|
2020-07-09 04:19:36 +02:00
|
|
|
group =
|
|
|
|
begin
|
2020-10-20 01:15:03 +02:00
|
|
|
Group.new(new_group_params.except(:usernames, :owner_usernames))
|
2020-07-09 04:19:36 +02:00
|
|
|
rescue ArgumentError => e
|
|
|
|
raise Discourse::InvalidParameters, "Invalid group params"
|
|
|
|
end
|
2020-07-16 09:50:09 +02:00
|
|
|
|
|
|
|
if group.save
|
2020-10-20 01:15:03 +02:00
|
|
|
def get_user_ids(username_string)
|
|
|
|
User.where(username: username_string.split(",")).pluck(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_group_params[:owner_usernames].present?
|
|
|
|
owner_ids = get_user_ids(new_group_params[:owner_usernames])
|
|
|
|
owner_ids.each { |user_id| group.group_users.build(user_id: user_id, owner: true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_group_params[:usernames].present?
|
|
|
|
user_ids = get_user_ids(new_group_params[:usernames])
|
|
|
|
user_ids -= owner_ids if owner_ids
|
|
|
|
user_ids.each { |user_id| group.group_users.build(user_id: user_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
GroupActionLogger.new(user, group, skip_guardian: true).log_change_group_settings
|
2020-07-09 04:19:36 +02:00
|
|
|
log_success("Group created", group.name)
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-07-17 09:02:48 +02:00
|
|
|
result.output = group.name
|
2020-07-09 04:19:36 +02:00
|
|
|
else
|
2020-10-20 01:15:03 +02:00
|
|
|
log_error("Group creation failed", group.errors.messages)
|
2020-07-09 04:19:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-24 09:47:07 +02:00
|
|
|
def create_category
|
2020-07-09 04:19:36 +02:00
|
|
|
category =
|
|
|
|
begin
|
|
|
|
Category.new(new_category_params.merge(user: user))
|
|
|
|
rescue ArgumentError => e
|
|
|
|
raise Discourse::InvalidParameters, "Invalid category params"
|
|
|
|
end
|
|
|
|
|
|
|
|
if category.save
|
|
|
|
StaffActionLogger.new(user).log_category_creation(category)
|
|
|
|
log_success("Category created", category.name)
|
2020-07-17 09:02:48 +02:00
|
|
|
result.output = category.id
|
2020-07-09 04:19:36 +02:00
|
|
|
else
|
2020-10-20 01:15:03 +02:00
|
|
|
log_error("Category creation failed", category.errors.messages)
|
2020-07-09 04:19:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-02 07:21:57 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def action_category
|
|
|
|
output = CustomWizard::Mapper.new(
|
|
|
|
inputs: action['category'],
|
|
|
|
data: data,
|
|
|
|
user: user
|
2020-04-06 10:36:38 +02:00
|
|
|
).perform
|
2020-11-03 01:24:20 +01:00
|
|
|
|
|
|
|
return false unless output.present?
|
2020-10-20 01:15:03 +02:00
|
|
|
|
2020-04-02 07:21:57 +02:00
|
|
|
if output.is_a?(Array)
|
|
|
|
output.first
|
|
|
|
elsif output.is_a?(Integer)
|
|
|
|
output
|
|
|
|
elsif output.is_a?(String)
|
|
|
|
output.to_i
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_tags
|
2020-04-02 07:21:57 +02:00
|
|
|
output = CustomWizard::Mapper.new(
|
|
|
|
inputs: action['tags'],
|
|
|
|
data: data,
|
|
|
|
user: user,
|
2020-04-06 10:36:38 +02:00
|
|
|
).perform
|
2020-11-03 01:24:20 +01:00
|
|
|
|
|
|
|
return false unless output.present?
|
2020-04-07 13:17:20 +02:00
|
|
|
|
2020-04-02 07:21:57 +02:00
|
|
|
if output.is_a?(Array)
|
|
|
|
output.flatten
|
2020-04-07 13:17:20 +02:00
|
|
|
else output.is_a?(String)
|
2020-04-02 07:21:57 +02:00
|
|
|
[*output]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_custom_fields(params = {})
|
|
|
|
if (custom_fields = action['custom_fields']).present?
|
2020-04-06 10:36:38 +02:00
|
|
|
field_map = CustomWizard::Mapper.new(
|
|
|
|
inputs: custom_fields,
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
|
|
|
|
2020-10-20 07:40:23 +02:00
|
|
|
registered_fields = CustomWizard::CustomField.list
|
|
|
|
|
2020-04-06 10:36:38 +02:00
|
|
|
field_map.each do |field|
|
|
|
|
keyArr = field[:key].split('.')
|
|
|
|
value = field[:value]
|
2020-04-02 07:21:57 +02:00
|
|
|
|
2020-10-20 07:40:23 +02:00
|
|
|
if keyArr.length > 1
|
|
|
|
klass = keyArr.first
|
|
|
|
name = keyArr.last
|
|
|
|
else
|
|
|
|
name = keyArr.first
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
registered = registered_fields.select { |f| f.name == name }
|
|
|
|
if registered.first.present?
|
|
|
|
klass = registered.first.klass
|
|
|
|
end
|
|
|
|
|
|
|
|
if klass === 'topic'
|
2020-04-06 10:36:38 +02:00
|
|
|
params[:topic_opts] ||= {}
|
|
|
|
params[:topic_opts][:custom_fields] ||= {}
|
2020-10-20 07:40:23 +02:00
|
|
|
params[:topic_opts][:custom_fields][name] = value
|
2020-04-07 15:33:11 +02:00
|
|
|
else
|
2020-04-06 10:36:38 +02:00
|
|
|
params[:custom_fields] ||= {}
|
2020-10-20 07:40:23 +02:00
|
|
|
params[:custom_fields][name] = value
|
2020-04-02 07:21:57 +02:00
|
|
|
end
|
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
2020-04-02 07:21:57 +02:00
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def basic_topic_params
|
|
|
|
params = {
|
|
|
|
skip_validations: true
|
|
|
|
}
|
|
|
|
|
|
|
|
params[:title] = CustomWizard::Mapper.new(
|
|
|
|
inputs: action['title'],
|
|
|
|
data: data,
|
|
|
|
user: user
|
2020-04-06 10:36:38 +02:00
|
|
|
).perform
|
2020-04-02 07:21:57 +02:00
|
|
|
|
2020-04-15 03:23:41 +02:00
|
|
|
params[:raw] = action['post_builder'] ?
|
2020-04-02 07:21:57 +02:00
|
|
|
mapper.interpolate(action['post_template']) :
|
|
|
|
data[action['post']]
|
|
|
|
|
2020-10-20 02:42:10 +02:00
|
|
|
params[:import_mode] = ActiveRecord::Type::Boolean.new.cast(action['suppress_notifications'])
|
|
|
|
|
2020-04-07 15:33:11 +02:00
|
|
|
add_custom_fields(params)
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|
2020-04-07 16:18:12 +02:00
|
|
|
|
2020-07-20 06:26:11 +02:00
|
|
|
def public_topic_params
|
|
|
|
params = {}
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
if category = action_category
|
2020-07-20 06:26:11 +02:00
|
|
|
params[:category] = category
|
|
|
|
end
|
|
|
|
|
2020-11-03 01:24:20 +01:00
|
|
|
if tags = action_tags
|
2020-07-20 06:26:11 +02:00
|
|
|
params[:tags] = tags
|
|
|
|
end
|
|
|
|
|
|
|
|
if public_topic_fields.any?
|
|
|
|
public_topic_fields.each do |field|
|
|
|
|
unless action[field].nil? || action[field] == ""
|
|
|
|
params[field.to_sym] = CustomWizard::Mapper.new(
|
|
|
|
inputs: action[field],
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2020-07-09 04:19:36 +02:00
|
|
|
def new_group_params
|
|
|
|
params = {}
|
|
|
|
|
|
|
|
%w(
|
|
|
|
name
|
|
|
|
full_name
|
|
|
|
title
|
2020-07-16 09:50:09 +02:00
|
|
|
bio_raw
|
|
|
|
owner_usernames
|
|
|
|
usernames
|
2020-07-09 04:19:36 +02:00
|
|
|
mentionable_level
|
|
|
|
messageable_level
|
|
|
|
visibility_level
|
|
|
|
members_visibility_level
|
|
|
|
grant_trust_level
|
|
|
|
).each do |attr|
|
2020-07-17 03:08:25 +02:00
|
|
|
input = action[attr]
|
|
|
|
|
|
|
|
if attr === "name" && input.blank?
|
|
|
|
raise ArgumentError.new
|
|
|
|
end
|
|
|
|
|
|
|
|
if attr === "full_name" && input.blank?
|
|
|
|
input = action["name"]
|
|
|
|
end
|
|
|
|
|
|
|
|
if input.present?
|
2020-07-16 09:50:09 +02:00
|
|
|
value = CustomWizard::Mapper.new(
|
2020-07-17 03:08:25 +02:00
|
|
|
inputs: input,
|
2020-07-09 04:19:36 +02:00
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
2020-07-16 09:50:09 +02:00
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
if value
|
|
|
|
value = value.parameterize(separator: '_') if attr === "name"
|
|
|
|
value = value.to_i if attr.include?("_level")
|
|
|
|
|
|
|
|
params[attr.to_sym] = value
|
|
|
|
end
|
2020-07-09 04:19:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_custom_fields(params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_category_params
|
|
|
|
params = {}
|
|
|
|
|
|
|
|
%w(
|
|
|
|
name
|
|
|
|
slug
|
|
|
|
color
|
|
|
|
text_color
|
|
|
|
parent_category_id
|
|
|
|
permissions
|
|
|
|
).each do |attr|
|
2020-07-16 09:50:09 +02:00
|
|
|
if action[attr].present?
|
|
|
|
value = CustomWizard::Mapper.new(
|
2020-07-09 04:19:36 +02:00
|
|
|
inputs: action[attr],
|
|
|
|
data: data,
|
|
|
|
user: user
|
|
|
|
).perform
|
2020-07-16 09:50:09 +02:00
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
if value
|
|
|
|
if attr === "parent_category_id" && value.is_a?(Array)
|
|
|
|
value = value[0]
|
|
|
|
end
|
2020-07-16 09:50:09 +02:00
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
if attr === "permissions" && value.is_a?(Array)
|
|
|
|
permissions = value
|
|
|
|
value = {}
|
2020-07-16 09:50:09 +02:00
|
|
|
|
2020-10-20 01:15:03 +02:00
|
|
|
permissions.each do |p|
|
|
|
|
k = p[:key]
|
|
|
|
v = p[:value].to_i
|
|
|
|
|
|
|
|
if k.is_a?(Array)
|
|
|
|
group = Group.find_by(id: k[0])
|
|
|
|
k = group.name
|
|
|
|
else
|
|
|
|
k = k.parameterize(separator: '_')
|
|
|
|
end
|
|
|
|
|
|
|
|
value[k] = v
|
2020-07-16 09:50:09 +02:00
|
|
|
end
|
|
|
|
end
|
2020-10-20 01:15:03 +02:00
|
|
|
|
|
|
|
if attr === 'slug'
|
|
|
|
value = value.parameterize(separator: '-')
|
|
|
|
end
|
|
|
|
|
|
|
|
params[attr.to_sym] = value
|
2020-07-16 09:50:09 +02:00
|
|
|
end
|
2020-07-09 04:19:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_custom_fields(params)
|
|
|
|
end
|
|
|
|
|
2020-05-04 11:02:49 +02:00
|
|
|
def creates_post?
|
|
|
|
[:create_topic, :send_message].include?(action['type'].to_sym)
|
|
|
|
end
|
|
|
|
|
2020-07-20 06:26:11 +02:00
|
|
|
def public_topic_fields
|
|
|
|
['visible']
|
|
|
|
end
|
|
|
|
|
2020-04-07 16:18:12 +02:00
|
|
|
def profile_url_fields
|
|
|
|
['profile_background', 'card_background']
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_profile_key(key)
|
|
|
|
if profile_url_fields.include?(key)
|
|
|
|
"#{key}_upload_url"
|
|
|
|
else
|
|
|
|
key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_profile_value(value, key)
|
2020-10-20 01:15:03 +02:00
|
|
|
return value if value.nil?
|
|
|
|
|
2020-04-07 16:18:12 +02:00
|
|
|
if profile_url_fields.include?(key)
|
|
|
|
value['url']
|
|
|
|
elsif key === 'avatar'
|
|
|
|
value['id']
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 10:51:01 +02:00
|
|
|
def profile_excluded_fields
|
2020-04-14 12:46:53 +02:00
|
|
|
['username', 'email', 'trust_level'].freeze
|
2020-04-10 10:51:01 +02:00
|
|
|
end
|
|
|
|
|
2020-04-19 11:48:58 +02:00
|
|
|
def allowed_profile_field?(field)
|
|
|
|
allowed_profile_fields.include?(field) || user_field?(field)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_field?(field)
|
|
|
|
field.to_s.include?(::User::USER_FIELD_PREFIX) &&
|
|
|
|
::UserField.exists?(field.split('_').last.to_i)
|
|
|
|
end
|
|
|
|
|
2020-04-07 16:18:12 +02:00
|
|
|
def allowed_profile_fields
|
2020-04-10 10:51:01 +02:00
|
|
|
CustomWizard::Mapper.user_fields.select { |f| profile_excluded_fields.exclude?(f) } +
|
2020-04-07 16:18:12 +02:00
|
|
|
profile_url_fields +
|
|
|
|
['avatar']
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_avatar(upload_id)
|
|
|
|
user.create_user_avatar unless user.user_avatar
|
|
|
|
user.user_avatar.custom_upload_id = upload_id
|
|
|
|
user.uploaded_avatar_id = upload_id
|
|
|
|
user.save!
|
|
|
|
user.user_avatar.save!
|
|
|
|
end
|
2020-04-15 02:46:44 +02:00
|
|
|
|
|
|
|
def log_success(message, detail = nil)
|
2020-04-15 03:42:24 +02:00
|
|
|
@log.push("success: #{message} - #{detail}")
|
2020-05-04 11:02:49 +02:00
|
|
|
@result.success = true
|
2020-04-15 02:46:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_error(message, detail = nil)
|
2020-04-15 03:42:24 +02:00
|
|
|
@log.push("error: #{message} - #{detail}")
|
2020-05-04 11:02:49 +02:00
|
|
|
@result.success = false
|
2020-04-15 02:46:44 +02:00
|
|
|
end
|
2020-04-15 03:05:26 +02:00
|
|
|
|
2020-04-15 03:27:19 +02:00
|
|
|
def log_info(message, detail = nil)
|
2020-04-15 03:42:24 +02:00
|
|
|
@log.push("info: #{message} - #{detail}")
|
2020-04-15 03:05:26 +02:00
|
|
|
end
|
2020-05-04 11:02:49 +02:00
|
|
|
|
|
|
|
def save_log
|
|
|
|
log = "wizard: #{@wizard.id}; action: #{action['type']}; user: #{user.username}"
|
|
|
|
|
|
|
|
if @log.any?
|
|
|
|
@log.each do |item|
|
|
|
|
log << "; #{item.to_s}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
CustomWizard::Log.create(log)
|
|
|
|
end
|
2020-03-30 08:16:03 +02:00
|
|
|
end
|