generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.rb
44 lines (33 loc) · 1.06 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
# frozen_string_literal: true
# name: discourse-ip-location
# about: show ip location in front
# version: 0.0.1
# authors: Jiajun Du
# url: https://github.com/ShuiyuanSJTU/discourse-ip-location
# required_version: 2.7.0
enabled_site_setting :plugin_ip_location_enabled
module ::IpLocationPluginModule
PLUGIN_NAME = "discourse-ip-location"
end
after_initialize do
# Code which should run after Rails has finished booting
ip_location_block = Proc.new {
if object.ip_address == nil
return I18n.t("discourse_ip_location.unknown")
end
if object.ip_address.private?
return I18n.t("discourse_ip_location.private")
end
info = DiscourseIpInfo.get(object.ip_address, locale: "zh_CN")
if info == {}
return I18n.t("discourse_ip_location.unknown")
end
if info[:country_code] != "CN"
return info[:country]
end
return info[:region] if info[:region] != nil
return info[:country]
}
add_to_serializer(:user, :ip_location, &ip_location_block)
add_to_serializer(:user_card, :ip_location, &ip_location_block)
end