forked from erickguan/discourse-chinese-localization-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
96 lines (75 loc) · 3.05 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# name: Discourse 中文本地化服务集合
# about: 为 Discourse 增加了各种本地化的功能。
# version: 2.0.1
# authors: Erick Guan
# url: https://github.com/fantasticfears/discourse-chinese-localization-pack
enabled_site_setting :zh_l10n_enabled
gem('omniauth-douban-oauth2', '0.0.7') # https://github.com/liluo/omniauth-douban-oauth2
gem('omniauth-qq', '0.3.0') # https://github.com/beenhero/omniauth-qq
gem('omniauth-weibo-oauth2', '0.5.2') # https://github.com/beenhero/omniauth-weibo-oauth2
register_svg_icon 'fab-weibo'
register_svg_icon 'fab-qq'
# register_svg_icon 'zhl10n-douban'
# load oauth providers
Dir[File.expand_path('../lib/auth/*.rb', __FILE__)].each { |f| require f }
require 'active_support/inflector'
require "ostruct"
PROVIDERS = ['Weibo', 'QQ', 'Douban']
PLUGIN_PREFIX = 'zh_l10n_'.freeze
SITE_SETTING_NAME = 'zh_l10n_enabled'.freeze
ONEBOX_SETTING_NAME = 'zh_l10n_http_onebox_override'.freeze
def provider_icon(provider_name)
provider_name = provider_name.downcase
if provider_name == "douban"
nil
else
"fab-#{provider_name}"
end
end
PROVIDERS.each { |name| auth_provider(authenticator: "#{name}Authenticator".constantize.new, icon: provider_icon(name)) }
Dir[File.expand_path('../lib/onebox_override/*.rb', __FILE__)].each { |f| require f }
register_asset "stylesheets/buttons.scss"
after_initialize do
next unless SiteSetting.zh_l10n_enabled
Dir[File.expand_path('../lib/onebox/*.rb', __FILE__)].each { |f| require f }
PROVIDERS.each do |name|
provider_name = name.downcase
enable_setting = "#{PLUGIN_PREFIX}enable_#{provider_name}_logins"
check = "#{provider_name}_config_check".to_sym
AdminDashboardData.class_eval do
define_method(check) do
if SiteSetting.public_send(enable_setting) && (
SiteSetting.public_send("#{PLUGIN_PREFIX}#{provider_name}_client_id").blank? ||
SiteSetting.public_send("#{PLUGIN_PREFIX}#{provider_name}_client_secret").blank?)
I18n.t("dashboard.#{PLUGIN_PREFIX}#{provider_name}_config_warning")
end
end
end
AdminDashboardData.add_problem_check check
end
DiscourseEvent.on(:site_setting_changed) do |site_setting|
if site_setting.name == SITE_SETTING_NAME && site_setting.value_changed? && site_setting.value == "f" # false
PROVIDERS.each { |provider| SiteSetting.public_send("#{PLUGIN_PREFIX}enable_#{provider[0].downcase}_logins=", false) }
end
end
module ::DisableUsernameSuggester
def to_client_hash
hash = defined?(super) ? super : nil
return nil unless hash
# only catch when a oauth login and a username is random
if hash[:auth_provider]
match = (hash[:username] || '').match(/^\d+$/i)
if SiteSetting.zh_l10n_disable_random_username_sugeestion && match
hash[:username] = nil
if SiteSetting.enable_names? && SiteSetting.zh_l10n_disable_random_username_sugeestion && match
hash[:name] = nil
end
end
end
hash
end
end
Auth::Result.class_eval do
prepend ::DisableUsernameSuggester
end
end