Skip to content

Commit 4509cd5

Browse files
authored
DEV: Autoload app files (#179)
1 parent 5194a8b commit 4509cd5

File tree

16 files changed

+275
-369
lines changed

16 files changed

+275
-369
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
module ::DiscourseTranslator
4+
class TranslatorController < ::ApplicationController
5+
requires_plugin PLUGIN_NAME
6+
7+
before_action :ensure_logged_in
8+
9+
def translate
10+
if !current_user.staff?
11+
RateLimiter.new(
12+
current_user,
13+
"translate_post",
14+
SiteSetting.max_translations_per_minute,
15+
1.minute,
16+
).performed!
17+
end
18+
19+
params.require(:post_id)
20+
post = Post.find_by(id: params[:post_id])
21+
raise Discourse::InvalidParameters.new(:post_id) if post.blank?
22+
guardian.ensure_can_see!(post)
23+
24+
if !guardian.user_group_allow_translate?
25+
raise Discourse::InvalidAccess.new(
26+
"not_in_group",
27+
SiteSetting.restrict_translation_by_group,
28+
custom_message: "not_in_group.user_not_in_group",
29+
group: current_user.groups.pluck(:id),
30+
)
31+
end
32+
33+
if !guardian.poster_group_allow_translate?(post)
34+
raise Discourse::InvalidAccess.new(
35+
"not_in_group",
36+
SiteSetting.restrict_translation_by_poster_group,
37+
custom_message: "not_in_group.poster_not_in_group",
38+
)
39+
end
40+
41+
begin
42+
title_json = {}
43+
detected_lang, translation =
44+
"DiscourseTranslator::#{SiteSetting.translator}".constantize.translate(post)
45+
if post.is_first_post?
46+
_, title_translation =
47+
"DiscourseTranslator::#{SiteSetting.translator}".constantize.translate(post.topic)
48+
title_json = { title_translation: title_translation }
49+
end
50+
render json: { translation: translation, detected_lang: detected_lang }.merge(title_json),
51+
status: 200
52+
rescue ::DiscourseTranslator::TranslatorError => e
53+
render_json_error e.message, status: 422
54+
end
55+
end
56+
end
57+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
module ::Jobs
4+
class DetectPostsLanguage < ::Jobs::Scheduled
5+
sidekiq_options retry: false
6+
every 5.minutes
7+
8+
BATCH_SIZE = 100
9+
MAX_QUEUE_SIZE = 1000
10+
11+
def execute(args)
12+
return unless SiteSetting.translator_enabled
13+
14+
post_ids = Discourse.redis.spop(DiscourseTranslator::LANG_DETECT_NEEDED, MAX_QUEUE_SIZE)
15+
return if post_ids.blank?
16+
17+
post_ids.each_slice(BATCH_SIZE) { |batch| process_batch(batch) }
18+
end
19+
20+
private
21+
22+
def process_batch(post_ids)
23+
posts = Post.where(id: post_ids).to_a
24+
posts.each do |post|
25+
DistributedMutex.synchronize("detect_translation_#{post.id}") do
26+
begin
27+
translator = "DiscourseTranslator::#{SiteSetting.translator}".constantize
28+
translator.detect(post)
29+
if !post.custom_fields_clean?
30+
post.save_custom_fields
31+
post.publish_change_to_clients!(:revised)
32+
end
33+
rescue ::DiscourseTranslator::ProblemCheckedTranslationError
34+
# problem-checked translation errors gracefully
35+
end
36+
end
37+
end
38+
end
39+
end
40+
end
File renamed without changes.

config/routes.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
DiscourseTranslator::Engine.routes.draw do
4+
post "/translate" => "translator#translate", :format => :json
5+
end
6+
7+
Discourse::Application.routes.draw { mount ::DiscourseTranslator::Engine, at: "/translator" }

lib/discourse_translator/engine.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
module ::DiscourseTranslator
3+
class Engine < ::Rails::Engine
4+
engine_name PLUGIN_NAME
5+
isolate_namespace DiscourseTranslator
6+
config.autoload_paths << File.join(config.root, "lib")
7+
scheduled_job_dir = "#{config.root}/app/jobs/scheduled"
8+
config.to_prepare do
9+
Rails.autoloaders.main.eager_load_dir(scheduled_job_dir) if Dir.exist?(scheduled_job_dir)
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)