Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Banned user support #252

Closed
bodand opened this issue May 18, 2018 · 7 comments
Closed

Banned user support #252

bodand opened this issue May 18, 2018 · 7 comments

Comments

@bodand
Copy link

bodand commented May 18, 2018

Whenever a user is banned or doesn't exist it just returns null. A banned user should be created with an isSuspended or something field, to be able to check it. Because Reddit returns this for banned users:

{"kind": "t2", "data": {"is_suspended": true, "name": "TheFlintASteel"}}

And just a 404 for not existent ones:

{"message": "Not Found", "error": 404}

EDIT:
I was being stupid, non-existent ones generate a different error, but this feature still wouldn't be bad for easier management of suspended users.

@mattbdean
Copy link
Owner

it just returns null

What is "it"? I don't understand what you're asking here. Can you provide the code you're using and a sample use case?

@bodand
Copy link
Author

bodand commented May 19, 2018

I got a NullPointerException so I assumed that reddit.user("") returned null, but it did not(the .about() call threw the exception) however, there don't seem to be a direct distinction between non-existent and banned users only with the exceptions they throw(NullPointer for banned and ApiException for not existing ones).
Here is the code in Scala(it's a Discord bot, but I mainly removed the parts that are not connected to JRAW)

val user = reddit.user(name)
if (user.isSelf) {
  BotUtils.sendMsg(event.getChannel, "Oh, that is me! Hello!")
} else try {
  val about = user.about() //this throws the exceptions
  /*...*/
} catch {
  case _: ApiException =>
    /*...If the user doesn't exist and reddit returns 404...*/
  case _: NullPointerException =>
    /*...If the user is banned...*/
  case e: Exception =>
    logger.error(s"Unknown error occurred: ${e.getClass.toString}: ${e.getMessage}")
}

I suggested that instead of throwing a NullPointerException the .about() call could still create a user and it should have a suspended field that can be checked
Like this

val user = reddit.user(name)
val about = user.about()
if (about.isSuspended) {
  /*The user is suspended*/
} else (about.isNotExistent) { //needs a better naming
  /*User doesn't exist and reddit returned 404*/
} else {
  /* The user is normal a user and about can be used safely */
}

@mattbdean
Copy link
Owner

Can you post the stacktrace of the NPE?

@bodand
Copy link
Author

bodand commented May 21, 2018

Sorry for late response, here it is.

java.lang.NullPointerException: Null created
	at net.dean.jraw.models.$AutoValue_Account.<init>($AutoValue_Account.java:36)
	at net.dean.jraw.models.AutoValue_Account.<init>(AutoValue_Account.java:26)
	at net.dean.jraw.models.AutoValue_Account$MoshiJsonAdapter.fromJson(AutoValue_Account.java:110)
	at net.dean.jraw.models.AutoValue_Account$MoshiJsonAdapter.fromJson(AutoValue_Account.java:29)
	at net.dean.jraw.models.internal.AutoValue_RedditModelEnvelope$MoshiJsonAdapter.fromJson(AutoValue_RedditModelEnvelope.java:38)
	at net.dean.jraw.models.internal.AutoValue_RedditModelEnvelope$MoshiJsonAdapter.fromJson(AutoValue_RedditModelEnvelope.java:17)
	at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:128)
	at net.dean.jraw.databind.RedditModelAdapterFactory$StaticAdapter.fromJson(RedditModelAdapterFactory.kt:112)
	at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:35)
	at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:39)
	at net.dean.jraw.references.UserReference.about(UserReferences.kt:36)
	at com.infosoftcorp.wordpress.redditbot.eventhandle.events.UserMentionedEvent$.handleEvent(UserMentionedEvent.scala:18)
	at com.infosoftcorp.wordpress.redditbot.eventhandle.RedditEvents.onMessageRecieved(RedditEvents.scala:64)
	at sx.blah.discord.api.events.EventDispatcher$MethodEventHandler.handle(EventDispatcher.java:680)
	at sx.blah.discord.api.events.EventDispatcher.lambda$null$17(EventDispatcher.java:610)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

@mattbdean
Copy link
Owner

Can you given an example of a user that's banned? That NPE is definitely a bug

@bodand
Copy link
Author

bodand commented May 21, 2018

I always use u/TheFlintASteel if that is what you are asking for.
Reddit calls it "suspended".
image

@mattbdean
Copy link
Owner

Fixed with commit 54dda0e.

Use UserReference.query() instead of UserReference.about():

AccountQuery query = redditClient.user("TheFlintASteel").query();
if (query.getStatus() == AccountStatus.SUSPENDED) {
    // do something
} else if (query.getStatus() == AccountStatus.NON_EXISTENT) {
    // do something
} else {
    Account account = query.getAccount();
    // do something
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants