DEV: Convert some files to autoloading and various improvements (#26860)

This commit is contained in:
Osama Sayegh
2024-05-06 23:12:55 +03:00
committed by GitHub
parent 8bbcd409e3
commit 2f2355b0ad
63 changed files with 90 additions and 186 deletions

View File

@ -4,6 +4,11 @@ module ::DiscourseAutomation
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseAutomation
config.autoload_paths << File.join(config.root, "lib")
scheduled_job_dir = "#{config.root}/app/jobs/scheduled"
config.to_prepare do
Rails.autoloaders.main.eager_load_dir(scheduled_job_dir) if Dir.exist?(scheduled_job_dir)
end
end
def self.filter_by_trigger(items, trigger)

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
module DiscourseAutomation
module PluginInstanceExtension
def add_automation_scriptable(name, &block)
reloadable_patch { DiscourseAutomation::Scriptable.add(name, &block) }
end
def add_automation_triggerable(name, &block)
reloadable_patch { DiscourseAutomation::Triggerable.add(name, &block) }
end
def add_triggerable_to_scriptable(triggerable, scriptable)
reloadable_patch do
DiscourseAutomation::Scriptable.add_plugin_triggerable(triggerable, scriptable)
end
end
end
end

View File

@ -16,7 +16,7 @@ DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::ZAPIER_WEBHOOK
end
Jobs.enqueue(
:discourse_automation_call_zapier_webhook,
Jobs::DiscourseAutomation::CallZapierWebhook,
webhook_url: webhook_url,
context: context,
)

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
class DiscourseAutomation::StalledTopicFinder
def self.call(stalled_date, tags: nil, categories: nil)
sql = <<~SQL
SELECT t.id
FROM topics t
SQL
sql += <<~SQL if tags
JOIN topic_tags ON topic_tags.topic_id = t.id
JOIN tags
ON tags.name IN (:tags)
AND tags.id = topic_tags.tag_id
SQL
sql += <<~SQL
WHERE t.deleted_at IS NULL
AND t.posts_count > 0
AND t.archetype != 'private_message'
AND NOT t.closed
AND NOT t.archived
AND NOT EXISTS (
SELECT p.id
FROM posts p
WHERE t.id = p.topic_id
AND p.deleted_at IS NULL
AND t.user_id = p.user_id
AND p.created_at > :stalled_date
LIMIT 1
)
SQL
sql += <<~SQL if categories
AND t.category_id IN (:categories)
SQL
sql += <<~SQL
LIMIT 250
SQL
DB.query(sql, categories: categories, tags: tags, stalled_date: stalled_date)
end
end

View File

@ -1,17 +0,0 @@
# frozen_string_literal: true
class Plugin::Instance
def add_automation_scriptable(name, &block)
reloadable_patch { DiscourseAutomation::Scriptable.add(name, &block) }
end
def add_automation_triggerable(name, &block)
reloadable_patch { DiscourseAutomation::Triggerable.add(name, &block) }
end
def add_triggerable_to_scriptable(triggerable, scriptable)
reloadable_patch do
DiscourseAutomation::Scriptable.add_plugin_triggerable(triggerable, scriptable)
end
end
end