Retrieving user information via REST API


(Sarah) #1

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

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?


(Blake Erickson) #2

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.


(Sarah) #3

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


(Blake Erickson) #4

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


(Sarah) #5

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


(Sarah) #6

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?


(Simon Cossar) #7

It is. You can see where it happens here: discourse/user_silencer.rb at master · discourse/discourse · GitHub

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.