Retrieving user information via REST API

Hello!

I have been looking for some specific user data. The users flagged data:

  • number_of_flagged_posts
  • number_of_suspensions
  • warnings_received_count

I found that this information is part of the user class

https://github.com/discourse/discourse/blob/bc52bdfa1280ba08e6fb1931c26e18aa7fee9f7f/app/models/user.rb

but is not part of the ‘user’ that the REST API returns

https://docs.discourse.org/#tag/Tags%2Fpaths%2F~1tags~1{tag}%2Fget
https://docs.discourse.org/#tag/Users%2Fpaths%2F~1admin~1users~1list~1{flag}.json%2Fget

I am wondering how I can get the user’s

  • number_of_flagged_posts
  • number_of_suspensions
  • warnings_received_count

through the REST API. And if it’s not possible, then is a plugin REST API that surfaces this information the enxt best option?

1 Like

Apparently the admin user route isn’t documented in the API Docs. I’ll make sure that route is added.

If you GET /admin/users/{id}.json it will return these fields which are close to what you are after:

flags_given_count
flags_received_count
warnings_received_count
suspended (boolean)

I would start with the Data Explorer Plugin where you can create a saved query that you can send an API call to and pass it a user id to return exactly what you are after. But yes, creating a plugin that creates a new route to expose those fields is also an option.

6 Likes

AHH THANK YOU! This is what I was looking for! You’re awesome :slight_smile:

4 Likes

I just added the GET /admin/users/{id}.json route to the api docs.

7 Likes

To add to this, I also noticed that the silence endpoint isn’t visible in the API docs as well. I found it by taking a guess :stuck_out_tongue:

Endpoint:
PUT /admin/users/{id}/silence

3 Likes

Hmm I have also noticed a few differences between the two endpoints (in case @blake you were going to add the silence user to the API docs)

suspend silence differences
PUT /admin/users/{id}/suspend PUT /admin/users/{id}/silence endpoints
suspend_until silenced_till request body
Yes No Update while silenced /suspended

The last column in the table is causing me some trouble at the moment. I am trying to silence a user programmatically, but if they have already been silenced before, and I make a new silencing request with a later silenced_till date the date doesn’t get updated.

Almost as if the endpoint is saying “do nothing if the user is already silenced”

The suspend endpoint doesn’t behave like this. Rather it updates the suspend_until time.

Any advice?

4 Likes

It is. You can see where it happens here: https://github.com/discourse/discourse/blob/master/app/services/user_silencer.rb#L21

It’s possible to update the silenced_till parameter directly on the user, but there’s no API endpoint for that, so doing it that way would require a plugin.

3 Likes