# API not returning suspended fields properly

**URL:** https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850
**Category:** Bug
**Created:** [October 11, 2017, 3:30pm UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850 "2017-10-11T15:30:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Geoffrey\_Challen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/geoffrey_challen/32/119637_2.png) [@Geoffrey\_Challen](https://meta.discourse.org/u/Geoffrey_Challen)
#### Post date: [October 11, 2017, 3:30pm UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850/1 "2017-10-11T15:30:09Z")

</div>

In the past the API returned a user object with `suspended: true` (or some non-null value) when a user was suspended. A recent change seems to break that behavior. Now, `suspended_at` can be non-null while `suspended` is null.

Here is an example snippet returned by the API:

```json
  "title": null,
  "suspended_at": "2017-10-11T15:11:26.936Z",
  "suspended_till": null,
  "suspended": null,
  "blocked": false,

```

This seems like a recent regression, since scripts that I wrote in August that relied on this behavior only recently stopped working properly. I’m on 1.9.0.beta12.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [October 12, 2017, 1:08am UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850/2 "2017-10-12T01:08:23Z")

</div>

Unlikely to be a regression, the api changed as @eviltrout changed it.

---

<div class="post-metadata">

### Author: ![Geoffrey\_Challen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/geoffrey_challen/32/119637_2.png) [@Geoffrey\_Challen](https://meta.discourse.org/u/Geoffrey_Challen)
#### Post date: [October 12, 2017, 1:59pm UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850/3 "2017-10-12T13:59:59Z")

</div>

OK. What’s the new correct way to interpret these fields? It’s seems counterintuitive for `suspended_at` to be set while `suspended` is `null`.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [October 12, 2017, 8:27pm UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850/4 "2017-10-12T20:27:59Z")

</div>

Can you be more specific about which API you’re using to get this information?

Also do you have steps to reproduce this? Looking at the `unsuspend` method, it should be clearing out `suspended_at`.

---

<div class="post-metadata">

### Author: ![Geoffrey\_Challen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/geoffrey_challen/32/119637_2.png) [@Geoffrey\_Challen](https://meta.discourse.org/u/Geoffrey_Challen)
#### Post date: [October 13, 2017, 12:10pm UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850/5 "2017-10-13T12:10:43Z")

</div>

I’m grabbing the users from `admin/users/list/active.json`. I can provide more of the script if you’d like, but it uses that list to determine if there are users that should be suspended. (I’m using this for a class, so as students drop we suspend them indefinitely so that they can’t continue using the forum.) I use `admin/users/<id>/suspend` to suspend these users.

Back in August when I first wrote this, a check for `user.suspended == null` on the list of active users was sufficient to determine if a user was suspended. But then I noticed that some users were being repeatedly suspended, and realized that `suspended` was not being set to a non-null value as reported above.

Note that `unsuspend` probably has nothing to do with what I’m seeing—I never unsuspend users, since students don’t typically come back when they drop. (This did happen once, but I handled it manually.) The problem could be in the `suspend` method.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [October 13, 2017, 4:19pm UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850/6 "2017-10-13T16:19:54Z")

</div>

I reviewed the code and it turns out `suspended?` is simply defined as `suspended_till && suspended_till > DateTime.now`.

A user could be returned as not `suspended` if the period of time simply expired. `suspnded_at` in that case would still have a value because they _were_ suspended then. I don’t think this is strictly a bug, as `suspended` would still be correct.

I did make two changes to make the API less confusing though:

1. `suspended` will always be `true` or `false`, you don’t have to worry about `null` anymore.
2. `suspended_at` and `suspended_till` will not be returned unless `suspended` is true. This shrinks our JSON payload which is nice.

> <https://github.com/discourse/discourse/commit/f73a3cc0d4b9d7e8c1fbf2a4309397074ce4ec02>

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [October 13, 2017, 7:41pm UTC](https://meta.discourse.org/t/api-not-returning-suspended-fields-properly/71850/7 "2017-10-13T19:41:08Z")

</div>


