Hacking on Discourse

Tags: #<Tag:0x00007fa49788a818>

Does anybody fancy trying to wrap their head round the Discourse API?

In theory, anything you can do with the regular site can be done via your own code; all you have to do is keep an eye on the Network tab of browser devtools and see what calls are made as you do things.

So they say. It’s all Greek to me!

What I’d really like to see is an enhancement for our membership system that automatically creates/invites a Discourse account when creating a new member, and automatically promotes that account to the “Members” user group when their payment goes through (and demotes them when they cancel). We have to do this manually right now, which is a pain.

Useful links:

Our members code: https://github.com/southlondonmakerspace
Discourse source: https://github.com/discourse/discourse
Discourse API “documentation”: https://meta.discourse.org/t/discourse-api-documentation/22706

Down the road, what would be really amazing is an Android/iOS app for receiving notifications etc. but that’s a far-off dream for now :smile:

@richard_aw How complex would it be to do something like Tom is talking about for iOS?

I can have a look at the membership system modifications.

Hello, I’m not sure to be honest it depends on discourse API I guess. I’m not that experienced on developing these kinds of applications more on the visual and interactive side of things. I’ll have a little search around though to get a better idea.

I have been looking into apps using PhoneGap in the past. Got a basic app working.

I don’t think it would be too hard, PhoneGap just turns a javascript web app into a phone app. I will look into it.

Yeah, wrapping the existing site is quite easy. But tying into system-level notifications is the tricky bit.

I’ll have a look into this too. Might be a fun challenge. What is the main functionality we would need on the app? Purely the membership control and notifications?

They’re separate projects - the membership <-> usergroup link is purely a server(s)-side thing.

The app is so we can have our phones go off for @mentions like Slack.

EDIT: If said app can be made to work with any/multiple Discourse site, there’d be a lot of happy people out there!

For membership it also kind of needs to revoke access but we could do with that in Slack too… for now it’s not a big deal.

Client-side

I’m taking baby steps in customising our Discourse installation by attempting to write a plugin that will allow custom content in the main header, eg this big expanse of empty space:

Watch me fail here:

I’m using these simple howtos:

Server Side

I have successfully added and removed discourse users from the Members usergroup, from the command line.

Here’s the users and groups api for reference

https://github.com/discourse/discourse_api/blob/master/lib/discourse_api/api/users.rb https://github.com/discourse/discourse_api/blob/master/lib/discourse_api/api/groups.rb

So first of all, the group id for Members is 41 (you could verify this out with https://discourse.southlondonmakerspace.org/admin/groups.json lookup if you like.

Now get member info based on their email address

https://discourse.southlondonmakerspace.org/admin/users/list/active.json?filter=tom.newsom%40gmail.com?api_key=ApiKeyGoesHere&api_username=Makerspace

which returns a bunch of key:values. we’re just interested in the second one.

[{"id":1,"username":"tomnewsom","uploaded_avatar_id":2,"avatar_template":"/user_avatar/discourse.southlondonmakerspace.org/tomnewsom/{size}/2_1.png","active":true,"admin":true,"moderator":true,"last_seen_at":"2015-08-20T09:51:22.309Z","last_emailed_at":"2015-08-19T11:55:47.051Z","created_at":"2015-05-15T09:32:07.051Z","last_seen_age":"< 1m","last_emailed_age":"22h","created_at_age":"3mon","username_lower":"tomnewsom","trust_level":4,"trust_level_locked":false,"flag_level":0,"title":"Trustee","suspended_at":null,"suspended_till":null,"suspended":null,"blocked":false,"time_read":"2d","days_visited":90,"posts_read_count":2207,"topics_entered":300,"post_count":595}]

So now you have the username and the group id you can set group 41 for that user

curl -X PUT -d usernames=username https://discourse.southlondonmakerspace.org/admin/groups/41/members.json?api_key=ApiKeyGoesHere&api_username=Makerspace

to remove the group, do

curl -X DELETE -d username=username https://discourse.southlondonmakerspace.org/admin/groups/41/members.json?api_key=ApiKeyGoesHere&api_username=Makerspace

Note the plural “usernames=” for the adding process (will accept a comma seperated list of usernames to promote) and the singular “username=” for the group removal (one user at a time)

Auto-creating a discourse account during signup (if none exists) would be nice too. For that you use

curl -X POST --data "name=FullName&username=UserName&email=Name@Domain.TLD&password=Password&active=true" https://discourse.southlondonmakerspace.org/users?api_key=ApiKeyGoesHere&api_username=Makerspace

The latest advice from the Discourse devs is that I should

be putting the api_key in the form data if possible.

Like this?

curl -X PUT -F api_key=blahblahbalh -F api_user=username -d usernames=username https://etc.etc.

But I haven’t tested that yet


It should be a simple matter for someone to string this all together into a script. Further intelligence will be required for edge cases, eg. SLMS member email not matching Discourse email. If anyone fancies having a play, let me know and I’ll give you the API key

That should not have taken so long. Apologies if you were trying to use discourse in the last half hour.

Doesn’t do anything. Yet.

You really should work on a local instance, not live.

I did :smile:
(and then installed it on live to double check, but made it difficult for myself with typos >_<)