User page with custom data from external DB

We use Discourse as a forum for our game and I’d like to create custom user pages with data from the game.
Such as rankings, stats, achievements etc.

I’ve followed the setup tutorials and managed to setup the development environment and I’ve started the plugin.
Since there isn’t really a good place with documentation I already have several questions.
And in general I’d like to know if this is even possible or if it’s too ambitious.


What I have is a forum with SSO login and a MySQL database for where I need to get the data from.
If it’s not possible to get it directly from the DB in ruby I could write a REST API or something to fetch the data of users by external ID.

What I’d like to make is a page on the profile with custom data.
And maybe display some values on other places too such as league icons etc but I’m sure I’ll be able to figure this all out once I get the basics.

  1. Is it possible to fetch user data from our MySQL DB?
    If so, should I just follow a tutorial such as MySQL Ruby tutorial - programming MySQL in Ruby or is there an alternative way?

  2. I couldn’t find any handle to add a new user page is there support for this?
    The plugin that shows handles doesn’t really show any on the user pages.

  3. General question if it’s possible to override stuff in Discourse through plugins or is it only possible to add extra functionality with handles and JS etc?

(4) Also, is it really necessary to exit and start the bundle for each change?
I’ve done some experimenting already of course but none of my changes were on the forum after a refresh.
I had to restart the bundle for each change.
Restarting it isn’t so bad but the first load takes like 15 seconds.

(5) Finally, in general I’d like to know if there is any documentation I might have missed because it’s quite hard to get started on this even though I have quite a lot of experience as developer.
All the documentation I could find was the http://learndiscourse.org and some posts on here but they only explain the basics how to set the plugin up but then don’t go in any depth how to actually make stuff.
Of course I’m also looking in the soure code of already made plugins but to get started it’d be pretty nice to have some basic documenation.

Topics such as controllers, widgets, database, custom locale, models, services, views etc.
I see a lot of plugins with folder structures with things mentioned above but nowhere is really explained what it’s all about.

1 Like

Totally possible, if you’re willing to persevere a little bit.

It is, but connecting Discourse to two databases makes me wince a bit.

It might help if you understood how all the bits of Discourse link together. You basically have to think of Discourse as two seperate applications - the frontend, running an Ember app, and the backend, running a Rails app.

The frontend and the backend talk through an API, but otherwise, they’re basically entirely self contained.

So whatever you end up doing, you’re going to have to write a REST API to get the data from the backend to the frontend *. So now the question becomes, are you going to want to do anything on the backend with this data? (Such as assigning people to groups, or awarding badges, based on it.) Or is it just stuff you want to show on the frontend?

If you do want to to do stuff on the backend with it, you’ll have to have that data in some form on the backend (so this is where you’d connect to your MySQL db from ruby) and then write your API in ruby to actually get the data to the frontend.

If you don’t, then you might as well write your API in whatever language you feel most comfortable, and then query that from the frontend.

* Unless its very simple data, in which case it’s easiest to pass it to the frontend by adding it as a user.custom_fields, which you can then access through the user object in your plugin outlet.

There should be, soon:

Only server-side changes (so generally those done in Ruby). Generally anything changed client side just needs a refresh of the page (although sometimes you need to kill your rails server and do a rm -rf tmp/).

Yes, almost certainly. I’m always stumbling across topics on here which I realised would’ve helped me out massively when I was trying to figure out how to do something (both at the beginning, and more recently).

The Rails and Ember guides have a wealth of general information, especially with the latter when it comes to this:

And there’s this wonderful topic:

Good luck, have fun, and I’ve found there’s nothing that can replace a good ol’ bit of messing about just to see what happens! :smile:

7 Likes

Thanks a lot for all the answers! :smiley:
I’ve just started reading through the ember guides and I realized that all those terms I mentioned earlier are from Ember and not Discourse itself so I’ll find my away around that stuff after I read through the ember docs.

I don’t really think I’m gonna do stuff with the data apart from displaying it but maybe in the future I would.
So then it might be better to just make the API in ruby (even though I’m still new with ruby).
It’d be stupid if I made it in golang or php or something and then remake it later in ruby because I wanna make more advanced things.

For the front end stuff, you linked the PR that adds an outlet on the user nav.
But is this the only way to add content to the forum?
Would it be possible for example to just add a button somewhere on a user page that links to a sub page for a user?

Might be a bit vague but basically I’d just want a /users//stats page or something and render custom data on that page but still have the user header and such.
So it’d be something like this with custom content in that blank space.

Thanks for that link with tips and tricks btw haven’t read it all yet but it seems like a good place to start.
I’ll just go read some more ember and ruby guides.
I thought most of this stuff was Discourse (API) but seems like most of it is just Ember basics.

Thanks once more for all the useful answers. :smile:

2 Likes

You’d want to use the outlet to add a link to a route which you define in your plugin using a route map, with the route map being done a lot like this:

https://meta.discourse.org/t/multiple-routes-in-one-plugin/38120?u=leomca

That then allows you to define templates, controllers and routes with the same name as your resources. As some examples of defining things in a route map, this would probably be the simplest I’ve written and this the most complex.

You’ll also want to install the ember inspector to debug any routing problems you have.

4 Likes

Sweet thanks!
That seems quite nice and not that bad.

And thanks for the tip on the debugger I’ll make sure to install that.

1 Like

I’ve setup all routes and everything is working correct but now I’m at the part where I actually have to hook up the routes with my custom data and I’m not sure how to connect with the MySQL database.
I’ve created settings for the host/username/password etc and now I wanna create a database connection.
When I google it I only get results that you have to create a database config file but I don’t really get this working because I assume that it doesn’t work for plugins?
I did find some Ruby tutorials for MySQL databases but I’m not sure if this is what I’m looking for.

I tried this to get the connection but I don’t think it’s the correct way and it’s also giving me errors.

[details=Summary]I put the following code in plugin.rb

  ActiveRecord::Base.establish_connection(
  :adapter  => 'mysql',
  :database => SiteSetting.curvefever_mysql_database,
  :host     => SiteSetting.curvefever_mysql_host,
  :username => SiteSetting.curvefever_mysql_username,
  :password => SiteSetting.curvefever_mysql_password
  )

It gives me this error.
Specified 'mysql' for database adapter, but the gem is not loaded. Add `gem 'mysql'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
The error is quite obvious but is it possible to add them gem in a plugin?[/details]

EDIT: So I’ve found out I can load a gem in the plugin but I’m unable to get it installed.
I managed to install the gem with ruby locally but when I put it in my plugin it won’t install.
ERROR: Error installing mysql2: ERROR: Failed to build gem native extension.
When I google the error It tells me to install mysql and ruby etc and I did all that and now it lets me install it with gem install mysql2 but this still doesn’t fix the install for the plugin.
I just put gem 'mysql2', '0.4.5' in my plugin.rb.

full log file: https://gist.github.com/Rojoss/367c8e473317d0b7a65845c00ef7a9f4

It seems kinda weird that I have to install MySQL and stuff locally if I wanna connect to an external database.
When I load the plugin on my forum in production it also needs to work without having to install a bunch of stuff.


Another question, once I have the database hooked up and I’m able to fetch custom user data how does caching work?
I found some guides on making API’s in rails apps but will this work as discourse plugin in the controllers?
The user data is pretty dynamic but I don’t want users to be able to spam refresh and send a ton of queries.
Something like 5 minute caching for user data would be great.
Would I have to write my own caching for this or is there something I can use in rails?

In general I’d like to know to what extend I can follow up guides online for rails apps will everything just work fine in a plugin? Are there restrictions and things that can’t be done in plugins?

2 Likes