mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 23:31:18 +08:00
FEATURE: Merge discourse-automation (#26432)
Automation (previously known as discourse-automation) is now a core plugin.
This commit is contained in:
@ -0,0 +1,96 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseAutomation
|
||||
class AdminAutomationsController < ::Admin::AdminController
|
||||
requires_plugin DiscourseAutomation::PLUGIN_NAME
|
||||
|
||||
def index
|
||||
automations = DiscourseAutomation::Automation.order(:name).all
|
||||
serializer =
|
||||
ActiveModel::ArraySerializer.new(
|
||||
automations,
|
||||
each_serializer: DiscourseAutomation::AutomationSerializer,
|
||||
root: "automations",
|
||||
).as_json
|
||||
render_json_dump(serializer)
|
||||
end
|
||||
|
||||
def show
|
||||
automation = DiscourseAutomation::Automation.find(params[:id])
|
||||
render_serialized_automation(automation)
|
||||
end
|
||||
|
||||
def create
|
||||
automation_params = params.require(:automation).permit(:name, :script, :trigger)
|
||||
|
||||
automation =
|
||||
DiscourseAutomation::Automation.new(
|
||||
automation_params.merge(last_updated_by_id: current_user.id),
|
||||
)
|
||||
if automation.scriptable.forced_triggerable
|
||||
automation.trigger = scriptable.forced_triggerable[:triggerable].to_s
|
||||
end
|
||||
|
||||
automation.save!
|
||||
|
||||
render_serialized_automation(automation)
|
||||
end
|
||||
|
||||
def update
|
||||
params.require(:automation)
|
||||
|
||||
automation = DiscourseAutomation::Automation.find(params[:id])
|
||||
if automation.scriptable.forced_triggerable
|
||||
params[:trigger] = automation.scriptable.forced_triggerable[:triggerable].to_s
|
||||
end
|
||||
|
||||
attributes =
|
||||
request.parameters[:automation].slice(:name, :id, :script, :trigger, :enabled).merge(
|
||||
last_updated_by_id: current_user.id,
|
||||
)
|
||||
|
||||
if automation.trigger != params[:automation][:trigger]
|
||||
params[:automation][:fields] = []
|
||||
attributes[:enabled] = false
|
||||
automation.fields.destroy_all
|
||||
end
|
||||
|
||||
if automation.script != params[:automation][:script]
|
||||
attributes[:trigger] = nil
|
||||
params[:automation][:fields] = []
|
||||
attributes[:enabled] = false
|
||||
automation.fields.destroy_all
|
||||
automation.tap { |r| r.assign_attributes(attributes) }.save!(validate: false)
|
||||
else
|
||||
Array(params[:automation][:fields])
|
||||
.reject(&:empty?)
|
||||
.each do |field|
|
||||
automation.upsert_field!(
|
||||
field[:name],
|
||||
field[:component],
|
||||
field[:metadata],
|
||||
target: field[:target],
|
||||
)
|
||||
end
|
||||
|
||||
automation.tap { |r| r.assign_attributes(attributes) }.save!
|
||||
end
|
||||
|
||||
render_serialized_automation(automation)
|
||||
end
|
||||
|
||||
def destroy
|
||||
automation = DiscourseAutomation::Automation.find(params[:id])
|
||||
automation.destroy!
|
||||
render json: success_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_serialized_automation(automation)
|
||||
serializer =
|
||||
DiscourseAutomation::AutomationSerializer.new(automation, root: "automation").as_json
|
||||
render_json_dump(serializer)
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseAutomation
|
||||
class AdminController < ::Admin::AdminController
|
||||
requires_plugin DiscourseAutomation::PLUGIN_NAME
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseAutomation
|
||||
class AdminScriptablesController < ::Admin::AdminController
|
||||
requires_plugin DiscourseAutomation::PLUGIN_NAME
|
||||
|
||||
def index
|
||||
scriptables =
|
||||
DiscourseAutomation::Scriptable.all.map do |s|
|
||||
id = s.to_s.gsub(/^__scriptable_/, "")
|
||||
{
|
||||
id: id,
|
||||
name: I18n.t("discourse_automation.scriptables.#{id}.title"),
|
||||
description: I18n.t("discourse_automation.scriptables.#{id}.description", default: ""),
|
||||
doc: I18n.t("discourse_automation.scriptables.#{id}.doc", default: ""),
|
||||
}
|
||||
end
|
||||
|
||||
scriptables.sort_by! { |s| s[:name] }
|
||||
render_json_dump(scriptables: scriptables)
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseAutomation
|
||||
class AdminTriggerablesController < ::Admin::AdminController
|
||||
requires_plugin DiscourseAutomation::PLUGIN_NAME
|
||||
|
||||
def index
|
||||
if params[:automation_id].present?
|
||||
automation = DiscourseAutomation::Automation.find(params[:automation_id])
|
||||
scriptable = automation.scriptable
|
||||
triggerables = scriptable.triggerables
|
||||
else
|
||||
triggerables = DiscourseAutomation::Triggerable.all
|
||||
end
|
||||
|
||||
triggerables =
|
||||
triggerables.map do |s|
|
||||
id = s.to_s.gsub(/^__triggerable_/, "")
|
||||
{
|
||||
id: id,
|
||||
name: I18n.t("discourse_automation.triggerables.#{id}.title"),
|
||||
description: I18n.t("discourse_automation.triggerables.#{id}.description", default: ""),
|
||||
doc: I18n.t("discourse_automation.triggerables.#{id}.doc", default: ""),
|
||||
}
|
||||
end
|
||||
|
||||
render_json_dump(triggerables: triggerables)
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseAutomation
|
||||
class AppendLastCheckedByController < ApplicationController
|
||||
requires_plugin DiscourseAutomation::PLUGIN_NAME
|
||||
requires_login
|
||||
|
||||
def post_checked
|
||||
post = Post.find(params[:post_id])
|
||||
guardian.ensure_can_edit!(post)
|
||||
|
||||
topic = post.topic
|
||||
raise Discourse::NotFound if topic.blank?
|
||||
|
||||
topic.custom_fields[DiscourseAutomation::TOPIC_LAST_CHECKED_BY] = current_user.username
|
||||
topic.custom_fields[DiscourseAutomation::TOPIC_LAST_CHECKED_AT] = Time.zone.now.to_s
|
||||
topic.save_custom_fields
|
||||
|
||||
post.rebake!
|
||||
|
||||
render json: success_json
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseAutomation
|
||||
class AutomationsController < ApplicationController
|
||||
requires_plugin DiscourseAutomation::PLUGIN_NAME
|
||||
before_action :ensure_admin
|
||||
|
||||
def trigger
|
||||
automation = DiscourseAutomation::Automation.find(params[:id])
|
||||
automation.trigger_in_background!(params.merge(kind: DiscourseAutomation::Triggers::API_CALL))
|
||||
render json: success_json
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseAutomation
|
||||
class UserGlobalNoticesController < ApplicationController
|
||||
requires_plugin DiscourseAutomation::PLUGIN_NAME
|
||||
requires_login
|
||||
|
||||
def destroy
|
||||
notice =
|
||||
DiscourseAutomation::UserGlobalNotice.find_by(user_id: current_user.id, id: params[:id])
|
||||
|
||||
raise Discourse::NotFound unless notice
|
||||
|
||||
notice.destroy!
|
||||
|
||||
head :no_content
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user