Backend code

Hello,

I’m trying to find a way to execute some code in the backend.

The ultimate goal is to call the Maxmind API and redirect users coming from outside of Quebec to another site.

I found this post and I understand that to do this I would have to use a self-hosted Discourse.

Is there a way to do what I am trying to do using standard Discourse?

Cheers,

Julien

I don’t know the answer, sorry, but you should think twice such solution. Geo-IP is really unreliable.

You will need to write a plugin for that. See Developing Discourse Plugins - Part 1 - Create a basic plugin.

No need for API calls, as Discourse ships with MaxMind integration out of the box, and you can simply call DiscourseIpInfo.get(ip) from your plugin.

If you are on our hosting, please contact our support. We can help you with custom plugins in our Enterprise plan.

3 Likes

You will need to write a plugin for that. See Developing Discourse Plugins - Part 1 - Create a basic plugin.

But will I be able to use that plugin with the SaaS version of Discourse (with the standard plan)?

No need for API calls, as Discourse ships with MaxMind integration out of the box, and you can simply call DiscourseIpInfo.get(ip) from your plugin.

Is this precise enough to target user from Quebec, Canada only. Most GeoIP services I found had a country precision but were not able to target a specific province in Canada.

If you are on our hosting, please contact our support. We can help you with custom plugins in our Enterprise plan.

So not possible with the Standard plan? My guess is the Enterprise plan is at least 3x the price of the Standard plan.

Hard to tell here’s what I find out about my IP:

=> {:country=>"United States",
 :country_code=>"US",
 :region=>"Tennessee",
 :city=>"Nashville",
 :latitude=>36.1315,
 :longitude=>-86.8019,
 :location=>"Nashville, Tennessee, United States",
 :geoname_ids=>[6255149, 6252001, 4644585, 4662168],
 :asn=>7922,
 :organization=>"COMCAST-7922"}

There will be a bunch of ways that this could have false positives or negatives. Your best bet is some other solution to the problem that you’re solving with the redirect.

That looks precise enough, but a lot of GeoIP services are more precise for IPs from USA than IPs from Canada.

It doesn’t have to be 100% positive or negative, it is a way to have a first fence since the service is offered to people in Quebec only.

1 Like

Instead of a full redirect you might be able to use a theme-component that for example if your looking to redirect french users. It could maybe use Local to populate a popup in the login screen for the prefered site for francophones.

1 Like

I’d recommend that you just include a user custom field that says “I am in Quebec” that they must click when they sign up.

If you’d like a plugin, though, I might be able to help.

1 Like

I’d welcome a PR to the Locations plugin (as a site setting) to determine a User’s location automatically and set this in their profile which in this mode could be read only (another setting) to prevent them changing it themselves (but might be by admin on request).

Then, as a bonus, you would be able to see everyone on a map :slight_smile:

You could turn the logic in our geo blocking plugin around and instead of blocking certain regions, you could block all except certain regions. Shouldn’t be too hard - bonus points for the person that can do it with a one character edit. Hint: line 37 !

Or you can host with us on the Professional plan or up. In that case we’ll even extend the plugin for your use case.

2 Likes