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

Implemented phone verification (only get) and 50 MiB file limit for nitro accounts #445

Merged
merged 10 commits into from
Oct 29, 2017
23 changes: 15 additions & 8 deletions src/main/java/net/dv8tion/jda/core/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,25 @@ public SelfUser createSelfUser(JSONObject self)
selfUser = new SelfUserImpl(id, api);
api.setSelfUser(selfUser);
}

if (!api.getUserMap().containsKey(selfUser.getIdLong()))
{
api.getUserMap().put(selfUser.getIdLong(), selfUser);
}
return (SelfUser) selfUser
.setVerified(self.getBoolean("verified"))

selfUser.setVerified(self.getBoolean("verified"))
.setMfaEnabled(self.getBoolean("mfa_enabled"))
.setEmail(!self.isNull("email") ? self.getString("email") : null)
.setName(self.getString("username"))
.setDiscriminator(self.getString("discriminator"))
.setAvatarId(self.isNull("avatar") ? null : self.getString("avatar"))
.setBot(self.has("bot") && self.getBoolean("bot"));

if (this.api.getAccountType() == AccountType.CLIENT)
selfUser
.setEmail(!self.isNull("email") ? self.getString("email") : null)
.setMobile(!self.isNull("mobile") ? self.getBoolean("mobile") : false)
.setNitro(!self.isNull("premium") ? self.getBoolean("premium") : false)
.setPhoneNumber(!self.isNull("phone") ? self.getString("phone") : null);

return selfUser;
}

public Game createGame(String name, String url, Game.GameType type)
Expand Down Expand Up @@ -989,7 +996,7 @@ public MessageEmbed createMessageEmbed(JSONObject messageEmbed)
imageJson.isNull("height") ? -1 : imageJson.getInt("height")));
}
else embed.setImage(null);

if (messageEmbed.has("footer"))
{
JSONObject footerJson = messageEmbed.getJSONObject("footer");
Expand All @@ -999,7 +1006,7 @@ public MessageEmbed createMessageEmbed(JSONObject messageEmbed)
footerJson.isNull("proxy_icon_url") ? null : footerJson.getString("proxy_icon_url")));
}
else embed.setFooter(null);

if (messageEmbed.has("fields"))
{
JSONArray fieldsJson = messageEmbed.getJSONArray("fields");
Expand All @@ -1016,7 +1023,7 @@ public MessageEmbed createMessageEmbed(JSONObject messageEmbed)
embed.setFields(fields);
}
else embed.setFields(Collections.emptyList());

if (messageEmbed.has("video"))
{
JSONObject videoJson = messageEmbed.getJSONObject("video");
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/net/dv8tion/jda/core/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@
*/
public interface Message extends ISnowflake, Formattable
{
int MAX_FILE_SIZE = 8 << 20; // 8mb
/**
* Useful constant for the max allowed file size for message attachments on normal accounts. Value: {@value #MAX_FILE_SIZE} Bytes (8 MiB)
*/
int MAX_FILE_SIZE = 8 << 20; // 8 MiB

/**
* Useful constant for the max allowed file size for message attachments on nitro accounts. Value: {@value #MAX_FILE_SIZE_NITRO} Bytes (50 MiB)
*/
int MAX_FILE_SIZE_NITRO = 50 << 20; // 50 MiB
Copy link

@DuncanCasteleyn DuncanCasteleyn Oct 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really consistent you are calling your Boolean premium to check if user is a Nitro users but here you are calling you constant MAX_FILE_SIZE_NITRO instead of MAX_FILE_SIZE_PREMIUM. I'd say either rename the Boolean to nitro or rename this constant.


/**
* A immutable list of all mentioned users. if no user was mentioned, this list is empty.
* <br>In {@link net.dv8tion.jda.core.entities.PrivateChannel PrivateChannel's}, this always returns an empty List
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/net/dv8tion/jda/core/entities/MessageChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ protected void handleResponse(Response response, Request<Message> request)
* <li>Provided {@code file} is null.</li>
* <li>Provided {@code file} does not exist.</li>
* <li>Provided {@code file} is unreadable.</li>
* <li>Provided {@code file} is greater than 8MB.</li>
* <li>Provided {@code file} is greater than 8 MiB on a normal or 50 MiB on a nitro account.</li>
* <li>Provided {@link net.dv8tion.jda.core.entities.Message Message} is not {@code null} <b>and</b>
* contains a {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbed} which
* is not {@link net.dv8tion.jda.core.entities.MessageEmbed#isSendable(net.dv8tion.jda.core.AccountType) sendable}</li>
Expand Down Expand Up @@ -482,7 +482,7 @@ default RestAction<Message> sendFile(File file, Message message)
* <li>Provided {@code file} is null.</li>
* <li>Provided {@code file} does not exist.</li>
* <li>Provided {@code file} is unreadable.</li>
* <li>Provided {@code file} is greater than 8MB.</li>
* <li>Provided {@code file} is greater than 8 MiB on a normal or 50 MiB on a nitro account.</li>
* <li>Provided {@link net.dv8tion.jda.core.entities.Message Message} is not {@code null} <b>and</b>
* contains a {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbed} which
* is not {@link net.dv8tion.jda.core.entities.MessageEmbed#isSendable(net.dv8tion.jda.core.AccountType) sendable}</li>
Expand All @@ -507,8 +507,8 @@ default RestAction<Message> sendFile(File file, String fileName, Message message
Checks.notNull(file, "file");
Checks.check(file.exists() && file.canRead(),
"Provided file is either null, doesn't exist or is not readable!");
Checks.check(file.length() <= Message.MAX_FILE_SIZE,// TODO: deal with Discord Nitro allowing 50MB files.
"File is to big! Max file-size is 8MB");
Checks.check(file.length() <= getJDA().getSelfUser().getAllowedFileSize(),
"File is to big! Max file-size is 8 MiB for normal and 50 MiB for nitro users");

Checks.notNull(fileName, "fileName");

Expand Down Expand Up @@ -638,7 +638,7 @@ protected void handleResponse(Response response, Request<Message> request)
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided filename is {@code null} or {@code empty} or the provided data is larger than 8MB.</li>
* <li>If the provided filename is {@code null} or {@code empty} or the provided data is larger than 8 MiB on a normal or 50 MiB on a nitro account.</li>
* <li>If the provided {@link net.dv8tion.jda.core.entities.Message Message}
* contains an {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbed}
* that is not {@link net.dv8tion.jda.core.entities.MessageEmbed#isSendable(net.dv8tion.jda.core.AccountType) sendable}</li>
Expand All @@ -660,11 +660,10 @@ protected void handleResponse(Response response, Request<Message> request)
@CheckReturnValue
default RestAction<Message> sendFile(byte[] data, String fileName, Message message)
{
Checks.notNull(data, "file data[]");
Checks.notNull(data, "data");
Checks.notNull(fileName, "fileName");

Checks.check(data.length <= Message.MAX_FILE_SIZE, //8MB
"Provided data is too large! Max file-size is 8MB (%d)", Message.MAX_FILE_SIZE);
Checks.check(data.length <= getJDA().getSelfUser().getAllowedFileSize(),
"Provided data is too large! Max file-size is 8 MiB for normal and 50 MiB for nitro users");

Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(getId());
MultipartBody.Builder builder = new okhttp3.MultipartBody.Builder()
Expand Down
60 changes: 48 additions & 12 deletions src/main/java/net/dv8tion/jda/core/entities/SelfUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,54 @@ public interface SelfUser extends User
*/
String getEmail() throws AccountTypeException;

/**
* Shows whether there has ever been a mobile app connected to this account.
* <br><b>NOTE:</b> this is a {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} method only!
*
* @throws AccountTypeException
* If this method is called when {@link net.dv8tion.jda.core.JDA#getAccountType() JDA#getAccountType()} does not return
* {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}. E.g: If the logged in account isn't a Client account!
*
* @return {@code true} if the account is linked with a mobile app, otherwise {@code false}
*/
boolean isMobile() throws AccountTypeException;

/**
* The Discord Nitro status of this account.
* <br><b>NOTE:</b> this is a {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} method only!
*
* @throws AccountTypeException
* If this method is called when {@link net.dv8tion.jda.core.JDA#getAccountType() JDA#getAccountType()} does not return
* {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}. E.g: If the logged in account isn't a Client account!
*
* @return The Discord Nitro status of the currently logged in account.
*/
boolean isNitro() throws AccountTypeException;

/**
* Used to get the phone number of the currently logged in account if a phone number has been attached to it.
* <br><b>NOTE:</b> this is a {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} method only!
*
* @throws AccountTypeException
* If this method is called when {@link net.dv8tion.jda.core.JDA#getAccountType() JDA#getAccountType()} does not return
* {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}. E.g: If the logged in account isn't a Client account!
*
* @return The phone of the currently logged in account or null if there's no number associated
*/
String getPhoneNumber() throws AccountTypeException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you specifically throw an AccountTypeException for these methods? I would suggest to actually just return w/e we parsed and document that for bots the value will most likely be false or null. It might be possible that discord will make some of these available to bots in the future (unlikely) so we should just go with that, imo.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this because we already have the same be behavior for getEmail(). I also highly doubt that any of these will be available for bots anytime in the future.


/**
* Returns the maximum size for files that can be uploaded with this account.
* <br>Returns {@value net.dv8tion.jda.core.entities.Message#MAX_FILE_SIZE} for bots and non-nitro client accounts
* and {@value net.dv8tion.jda.core.entities.Message#MAX_FILE_SIZE_NITRO} for client accounts with a active nitro subscription.
*
* @return The maximum size for files that can be uploaded with this account
*
* @see net.dv8tion.jda.core.entities.Message#MAX_FILE_SIZE
* @see net.dv8tion.jda.core.entities.Message#MAX_FILE_SIZE_NITRO
*/
long getAllowedFileSize();

/**
* The {@link net.dv8tion.jda.core.managers.AccountManager AccountManager}
* for the currently logged in account.
Expand All @@ -73,16 +121,4 @@ public interface SelfUser extends User
* @return An AccountManagerUpdatable instance for the current account
*/
AccountManagerUpdatable getManagerUpdatable();

// /**
// * Creates a OAuth invite-link used to invite bot-accounts.<br>
// * This is literally just a shortcut to
// * {@link net.dv8tion.jda.utils.ApplicationUtil#getAuthInvite(net.dv8tion.jda.JDA, net.dv8tion.jda.Permission...) ApplicationUtil.getAuthInvite(JDA, Permission...)}
// *
// * @param perms
// * Possibly empty list of Permissions that should be requested via invite
// * @return
// * The link used to invite the bot
// */
// String getAuthUrl(Permission... perms);
}
14 changes: 11 additions & 3 deletions src/main/java/net/dv8tion/jda/core/entities/impl/GuildImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,20 +516,28 @@ public boolean checkVerification()
return true;
if(canSendVerification)
return true;

if (api.getSelfUser().getPhoneNumber() != null)
Copy link
Collaborator Author

@Almighty-Alpaca Almighty-Alpaca Aug 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MinnDevelopment @Sanduhr32 is this what you mean?

edit: I hate these snippets...

return canSendVerification = true;

switch (verificationLevel)
{
case VERY_HIGH:
break; // we already checked for a verified phone number
case HIGH:
if(ChronoUnit.MINUTES.between(getSelfMember().getJoinDate(), OffsetDateTime.now()) < 10)
if (ChronoUnit.MINUTES.between(getSelfMember().getJoinDate(), OffsetDateTime.now()) < 10)
break;
case MEDIUM:
if(ChronoUnit.MINUTES.between(MiscUtil.getCreationTime(api.getSelfUser()), OffsetDateTime.now()) < 5)
if (ChronoUnit.MINUTES.between(MiscUtil.getCreationTime(api.getSelfUser()), OffsetDateTime.now()) < 5)
break;
case LOW:
if(!api.getSelfUser().isVerified())
if (!api.getSelfUser().isVerified())
break;
case NONE:
canSendVerification = true;
return true;
case UNKNOWN:
return true; // try and let discord decide
}
return false;
}
Expand Down
61 changes: 55 additions & 6 deletions src/main/java/net/dv8tion/jda/core/entities/impl/SelfUserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.dv8tion.jda.core.entities.impl;

import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.PrivateChannel;
import net.dv8tion.jda.core.entities.SelfUser;
import net.dv8tion.jda.core.exceptions.AccountTypeException;
Expand All @@ -34,6 +35,9 @@ public class SelfUserImpl extends UserImpl implements SelfUser

//Client only
private String email;
private String phoneNumber;
private boolean mobile;
private boolean nitro;

public SelfUserImpl(long id, JDAImpl api)
{
Expand Down Expand Up @@ -78,6 +82,39 @@ public String getEmail() throws AccountTypeException
return email;
}

@Override
public String getPhoneNumber() throws AccountTypeException
{
if (api.getAccountType() != AccountType.CLIENT)
throw new AccountTypeException(AccountType.CLIENT, "Phone number retrieval can only be done on CLIENT accounts!");
return this.phoneNumber;
}

@Override
public boolean isMobile() throws AccountTypeException
{
if (api.getAccountType() != AccountType.CLIENT)
throw new AccountTypeException(AccountType.CLIENT, "Mobile app retrieval can only be done on CLIENT accounts!");
return this.mobile;
}

@Override
public boolean isNitro() throws AccountTypeException
{
if (api.getAccountType() != AccountType.CLIENT)
throw new AccountTypeException(AccountType.CLIENT, "Nitro status retrieval can only be done on CLIENT accounts!");
return this.nitro;
}

@Override
public long getAllowedFileSize()
{
if (this.nitro) // by directly accessing the field we don't need to check the account type
return Message.MAX_FILE_SIZE_NITRO;
else
return Message.MAX_FILE_SIZE;
}

@Override
public AccountManager getManager()
{
Expand Down Expand Up @@ -110,12 +147,6 @@ public AccountManagerUpdatable getManagerUpdatable()
return mng;
}

// @Override
// public String getAuthUrl(Permission... perms)
// {
// return ApplicationUtil.getAuthInvite(getJDA(), perms);
// }

public SelfUserImpl setVerified(boolean verified)
{
this.verified = verified;
Expand All @@ -133,4 +164,22 @@ public SelfUserImpl setEmail(String email)
this.email = email;
return this;
}

public SelfUserImpl setPhoneNumber(String phoneNumber)
{
this.phoneNumber = phoneNumber;
return this;
}

public SelfUserImpl setMobile(boolean mobile)
{
this.mobile = mobile;
return this;
}

public SelfUserImpl setNitro(boolean nitro)
{
this.nitro = nitro;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter & Florian Spieß
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.core.events.self;

import net.dv8tion.jda.core.JDA;

/**
* <b><u>SelfUpdateMobileEvent</u></b><br>
* Fired if you login to your discord account with a mobile device for the first time.<br>
*/
public class SelfUpdateMobileEvent extends GenericSelfUpdateEvent
{
private final boolean wasMobile;

public SelfUpdateMobileEvent(JDA api, long responseNumber, boolean wasMobile)
{
super(api, responseNumber);
this.wasMobile = wasMobile;
}

/**
* Returns the old mobile status. <i>Should</i> always be {@code false}.
*
* @return The mobile status.
*/
public boolean wasMobile()
{
return wasMobile;
}
}
Loading