Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pfaffman committed Apr 5, 2024
0 parents commit 664a80a
Show file tree
Hide file tree
Showing 27 changed files with 3,773 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .discourse-compatibility
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
< 3.3.0.beta1-dev: f1c7ada2637c52f4239420ebce8ac2e880d8de30

1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@discourse/lint-configs/eslint");
11 changes: 11 additions & 0 deletions .github/workflows/discourse-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Discourse Plugin

on:
push:
branches:
- main
pull_request:

jobs:
ci:
uses: discourse/.github/.github/workflows/discourse-plugin.yml@v1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
/gems
/auto_generated
1 change: 1 addition & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@discourse/lint-configs/prettier");
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inherit_gem:
rubocop-discourse: stree-compat.yml
2 changes: 2 additions & 0 deletions .streerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--print-width=100
--plugins=plugin/trailing_comma,plugin/disable_auto_ternary
1 change: 1 addition & 0 deletions .template-lintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@discourse/lint-configs/template-lint");
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

group :development do
gem "rubocop-discourse"
gem "syntax_tree"
end
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Civilized Discourse Construction Kit, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# **Geo Customization** Plugin

**Adds geo location to current_user serializer**

If `SiteSetting.geo_include_country_code` is enabled, the ISO country code (as returned by Maxmind)
will be added to the `<body>` class like "country-US". You can then use this class to hide/display
text on the site.

`SiteSetting.geo_include_city` similarly adds something like

Here is a trival example of using this plugin

Create a theme compnent and add this to the CSS:
```
/* Hide both divs by default */
.location-us-content,
.location-default-content {
display: none;
}
/* Show the US div when the "location-US" class is on the body */
body.country-US .location-us-content {
display: block;
}
/* Show the default div when the "location-US" class is NOT on the body */
body:not(.country-US) .location-default-content {
display: block;
}
```

Add this to the `After Header`:
```
<body class="location-US">
<div class="location-us-content">
This is text for the US
</div>
<div class="location-default-content">
This is everyone else's text
</div>
</body>
```
## Testing

To facilitate testing, `SiteSetting.geo_enable_admin_override` and `SiteSetting.geo_admin_ip_address` can
be used to override the actual user IP address. Three examples are included. Anything after a `-` in the ip address setting will be ignored (making it easier to tell what to expect from the sample IP addresses).
Empty file added app/.gitkeep
Empty file.
Empty file added assets/javascripts/.gitkeep
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import User from "discourse/models/user";

export default {
currentUser: User.current()
};
19 changes: 19 additions & 0 deletions assets/javascripts/discourse/initializers/add-location-to-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { withPluginApi } from "discourse/lib/plugin-api";

export default {
initialize(container) {
const siteSettings = container.lookup("service:site-settings");
withPluginApi("0.11.4", (api) => {
const user = api.getCurrentUser();
const body = document.querySelector('body');
if (user) {
if (siteSettings.geo_include_country_code){

Check failure on line 10 in assets/javascripts/discourse/initializers/add-location-to-body.js

View workflow job for this annotation

GitHub Actions / ci / linting

Trailing spaces not allowed
body.classList.add(`country-${user.geo_location.country_code}`)

Check failure on line 11 in assets/javascripts/discourse/initializers/add-location-to-body.js

View workflow job for this annotation

GitHub Actions / ci / linting

Missing semicolon
}
if (siteSettings.geo_include_city) {
body.classList.add(`city-${user.geo_location.city}`)

Check failure on line 14 in assets/javascripts/discourse/initializers/add-location-to-body.js

View workflow job for this annotation

GitHub Actions / ci / linting

Missing semicolon
};

Check failure on line 15 in assets/javascripts/discourse/initializers/add-location-to-body.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unnecessary semicolon
}
})

Check failure on line 17 in assets/javascripts/discourse/initializers/add-location-to-body.js

View workflow job for this annotation

GitHub Actions / ci / linting

Missing semicolon
}
}

Check failure on line 19 in assets/javascripts/discourse/initializers/add-location-to-body.js

View workflow job for this annotation

GitHub Actions / ci / linting

Missing semicolon
Empty file added assets/stylesheets/.gitkeep
Empty file.
19 changes: 19 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
en:
site_settings:
geo_customization_enabled: "Enable Geo Customization Plugin"
geo_include_city: "Include City in <body> tag"
geo_include_country_code: "Include Country Code in <body> tag"
geo_include_country: "Include Country in <body> tag"
geo_admin_ip_address: "Use first of these for Admin IP Address Geo Customization"
geo_enable_admin_override: "Use Admin IP Address for admin users"
admin_js:
admin:
site_settings:
geo_customization_enabled: "Enable Geo Customization"
geo_include_city: "Include City"
geo_include_country_code: "Include Country Code"
geo_include_country: "Include Country"
categories:
geo_customization: "Geo Customization"
js:
discourse_geo_customization:
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
en:
25 changes: 25 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
geo_customization:
geo_customization_enabled:
default: true
client: true
geo_enable_admin_override:
default: false
client: false
geo_admin_ip_address:
# default: "174.50.213.1"
type: list
default: '173.32.159.123' # canada
client: true
allow_any: true
choices:
- 174.50.213.1 - USA
- 175.156.235.1 - Singapore
- 173.32.159.1 - Canada
geo_include_city:
type: bool
default: false
client: true
geo_include_country_code:
type: bool
default: true
client: true
Empty file added db/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions lib/geo_customization_module/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module ::GeoCustomizationModule
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace GeoCustomizationModule
config.autoload_paths << File.join(config.root, "lib")
end
end
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"devDependencies": {
"@discourse/lint-configs": "^1.3.5",
"ember-template-lint": "^5.13.0",
"eslint": "^8.56.0",
"prettier": "^2.8.8"
}
}
36 changes: 36 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

# name: discourse-geo-customization
# about: add geo location to current_user
# meta_topic_id: TODO
# version: 0.0.1
# authors: Jay Pfaffman
# url: GPL
# required_version: 3.1.0

enabled_site_setting :geo_customization_enabled

module ::GeoCustomizationModule
PLUGIN_NAME = "discourse-geo-customization"
end

require_relative "lib/geo_customization_module/engine"

after_initialize do
add_to_serializer(:current_user, :geo_location) do
begin
if SiteSetting.geo_enable_admin_override && object.admin ||
object.ip_address == '127.0.0.1' || object.ip_address == '::1'
object.ip_address = SiteSetting.geo_admin_ip_address.split("|").first.split("-").first.strip
end
rescue => e
Rails.logger.error("Error in geo_customization IP override: #{e}")
end
begin
DiscourseIpInfo.get(object.ip_address)
rescue => e
Rails.logger.error("Error in geo_customization getting IP address: #{e}")
nil
end
end
end
Empty file added spec/.gitkeep
Empty file.
Empty file added test/javascripts/.gitkeep
Empty file.
Loading

0 comments on commit 664a80a

Please sign in to comment.