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

Implement invite targets #1628

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 70 additions & 30 deletions src/main/java/net/dv8tion/jda/api/entities/Invite.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,6 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
@Nonnull
Invite.InviteType getType();

/**
* The target type of this invite.
*
* @return The invite's target type or {@link TargetType#NONE}
*
* @see net.dv8tion.jda.api.entities.Invite.TargetType
*/
@Nonnull
Invite.TargetType getTargetType();

/**
* An {@link net.dv8tion.jda.api.entities.Invite.Channel Invite.Channel} object
* containing information about this invite's origin channel.
Expand All @@ -175,23 +165,15 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
Group getGroup();

/**
* An {@link net.dv8tion.jda.api.entities.Invite.EmbeddedApplication Invite.EmbeddedApplication} object
* containing information about this invite's application.
* An {@link Invite.InviteTarget Invite.InviteTarget} object
* containing information about this invite's target.
*
* @return Information about this invite's application or {@code null} if this invite's {@link #getTargetType() Invite.TargetType} is not {@link Invite.TargetType#EMBEDDED_APPLICATION}
* @return Information about this invite's target
*
* @see net.dv8tion.jda.api.entities.Invite.EmbeddedApplication
* @see net.dv8tion.jda.api.entities.Invite.InviteTarget
*/
@Nullable
EmbeddedApplication getTargetApplication();

/**
* The user to whose stream this invite goes.
*
* @return The user to whose stream this invite goes or {@code null} if this invite's {@link #getTargetType() Invite.TargetType} is not {@link Invite.TargetType#STREAM Invite.TargetType.STREAM}
*/
@Nullable
User getTargetUser();
@Nonnull
InviteTarget getTarget();

/**
* The invite code
Expand Down Expand Up @@ -521,10 +503,68 @@ interface Group extends ISnowflake
List<String> getUsers();
}

/**
* POJO for the target of this invite.
*
* @see #getTarget()
*/
interface InviteTarget {
anweisen marked this conversation as resolved.
Show resolved Hide resolved

/**
* The type of this invite target or {@link TargetType#NONE} if none is given.
*
* @return The type of this invite target
*/
@Nonnull
TargetType getType();

/**
* The Snowflake id of the target entity of this invite.
*
* @return The id of the target entity
*
* @throws IllegalStateException
* If there is no target entity, {@link #getType() TargetType} is {@link TargetType#NONE}
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*/
@Nonnull
String getId();

/**
* The Snowflake id of the target entity of this invite.
*
* @return The id of the target entity
*
* @throws IllegalStateException
* If there is no target entity, {@link #getType() TargetType} is {@link TargetType#NONE}
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*/
long getIdLong();

/**
* The target user of this invite or {@code null} if the {@link #getType() TargeType} is not {@link TargetType#STREAM}
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*
* @return The target user of this invite
*
* @see net.dv8tion.jda.api.entities.User
*/
@Nullable
User getUser();

/**
* The target application of this invite or {@code null} if the {@link #getType() TargeType} is not {@link TargetType#EMBEDDED_APPLICATION}
*
* @return The target application of this invite
*
* @see net.dv8tion.jda.api.entities.Invite.EmbeddedApplication
*/
@Nullable
EmbeddedApplication getApplication();

anweisen marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* POJO for the target application information provided by an invite.
*
* @see #getTargetApplication()
* @see InviteTarget#getApplication()
*/
interface EmbeddedApplication extends ISnowflake
{
Expand Down Expand Up @@ -598,7 +638,7 @@ enum InviteType
*
* Some actions might not be available or show up on certain devices.
*
* @see #getTargetType()
* @see InviteTarget#getType()
*/
enum TargetType
{
Expand All @@ -609,17 +649,17 @@ enum TargetType

/**
* The invite points to a user's stream in a voice channel.
* The user to whose stream the invite goes can be get with {@link Invite#getTargetUser() Invite.getTargetUser} and is not {@code null}.
* The user to whose stream the invite goes can be get with {@link InviteTarget#getUser() InviteTarget.getUser} and is not {@code null}.
*
* @see Invite#getTargetUser()
* @see InviteTarget#getUser()
*/
STREAM(1),

/**
* The invite points to an application in a voice channel.
* The application to which the invite goes can be get with {@link Invite#getTargetApplication() Invite.getTargetApplication} and is not {@code null}.
* The application to which the invite goes can be get with {@link InviteTarget#getApplication() InviteTarget.getApplication} and is not {@code null}.
*
* @see Invite#getTargetApplication()
* @see InviteTarget#getApplication()
*/
EMBEDDED_APPLICATION(2),

Expand Down
33 changes: 13 additions & 20 deletions src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1562,8 +1562,7 @@ public Invite createInvite(DataObject object)
final Invite.Guild guild;
final Invite.Channel channel;
final Invite.Group group;
final Invite.EmbeddedApplication application;
final User targetUser;
final Invite.InviteTarget target;

if (channelType == ChannelType.GROUP)
{
Expand Down Expand Up @@ -1624,26 +1623,20 @@ else if (channelType.isGuild())
switch (targetType)
{
case EMBEDDED_APPLICATION:
final DataObject applicationObject = object.getObject("target_application");

final String applicationIconId = applicationObject.getString("icon", null);
final String applicationName = applicationObject.getString("name");
final String applicationDescription = applicationObject.getString("description");
final String applicationSummary = applicationObject.getString("summary");
final long applicationId = applicationObject.getLong("id");
final int maxApplicationParticipants = applicationObject.getInt("max_participants", -1);

application = new InviteImpl.EmbeddedApplicationImpl(applicationIconId, applicationName, applicationDescription, applicationSummary, applicationId, maxApplicationParticipants);
targetUser = null;
break;
final DataObject applicationObject = object.getObject("target_application");

Invite.EmbeddedApplication application = new InviteImpl.EmbeddedApplicationImpl(
applicationObject.getString("icon", null), applicationObject.getString("name"), applicationObject.getString("description"),
applicationObject.getString("summary"), applicationObject.getLong("id"), applicationObject.getInt("max_participants", -1)
);
target = new InviteImpl.InviteTargetImpl(targetType, application, null);
break;
case STREAM:
final DataObject targetUserObject = object.getObject("target_user");
targetUser = createUser(targetUserObject);
application = null;
target = new InviteImpl.InviteTargetImpl(targetType, null, createUser(targetUserObject));
break;
default:
application = null;
targetUser = null;
target = new InviteImpl.InviteTargetImpl(targetType, null, null);
anweisen marked this conversation as resolved.
Show resolved Hide resolved
}

final int maxAge;
Expand Down Expand Up @@ -1673,8 +1666,8 @@ else if (channelType.isGuild())
}

return new InviteImpl(getJDA(), code, expanded, inviter,
maxAge, maxUses, temporary,
timeCreated, uses, channel, guild, group, application, targetUser, type, targetType);
maxAge, maxUses, temporary, timeCreated,
uses, channel, guild, group, target, type);
}

public ApplicationInfo createApplicationInfo(DataObject object)
Expand Down
88 changes: 63 additions & 25 deletions src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,18 @@ public class InviteImpl implements Invite
private final boolean expanded;
private final Guild guild;
private final Group group;
private final EmbeddedApplication application;
private final User targetUser;
private final InviteTarget target;
private final User inviter;
private final int maxAge;
private final int maxUses;
private final boolean temporary;
private final OffsetDateTime timeCreated;
private final int uses;
private final Invite.InviteType type;
private final Invite.TargetType targetType;

public InviteImpl(final JDAImpl api, final String code, final boolean expanded, final User inviter,
final int maxAge, final int maxUses, final boolean temporary, final OffsetDateTime timeCreated, final int uses,
final Channel channel, final Guild guild, final Group group, final EmbeddedApplication application, final User targetUser,
final Invite.InviteType type, Invite.TargetType targetType)
final Channel channel, final Guild guild, final Group group, final InviteTarget target, final Invite.InviteType type)
{
this.api = api;
this.code = code;
Expand All @@ -76,10 +73,8 @@ public InviteImpl(final JDAImpl api, final String code, final boolean expanded,
this.channel = channel;
this.guild = guild;
this.group = group;
this.application = application;
this.targetUser = targetUser;
this.target = target;
this.type = type;
this.targetType = targetType;
}

public static RestAction<Invite> resolve(final JDA api, final String code, final boolean withCounts)
Expand Down Expand Up @@ -165,13 +160,6 @@ public Invite.InviteType getType()
return this.type;
}

@Nonnull
@Override
public TargetType getTargetType()
{
return this.targetType;
}

@Override
public Channel getChannel()
{
Expand Down Expand Up @@ -207,18 +195,11 @@ public Group getGroup()
return this.group;
}

@Nullable
@Override
public EmbeddedApplication getTargetApplication()
{
return this.application;
}

@Nullable
@Nonnull
@Override
public User getTargetUser()
public InviteTarget getTarget()
{
return this.targetUser;
return target;
}

@Override
Expand Down Expand Up @@ -482,6 +463,63 @@ public List<String> getUsers()
}
}

public static class InviteTargetImpl implements InviteTarget
{
private final TargetType type;
private final EmbeddedApplication targetApplication;
private final User targetUser;

public InviteTargetImpl(TargetType type, EmbeddedApplication targetApplication, User targetUser)
{
this.type = type;
this.targetApplication = targetApplication;
this.targetUser = targetUser;
}

@Nonnull
@Override
public TargetType getType()
{
return type;
}

@Nonnull
@Override
public String getId()
{
return getTargetEntity().getId();
}

@Override
public long getIdLong()
{
return getTargetEntity().getIdLong();
}

@Nullable
@Override
public User getUser()
{
return targetUser;
}

@Nullable
@Override
public EmbeddedApplication getApplication()
{
return targetApplication;
}

@Nonnull
private ISnowflake getTargetEntity()
{
if (targetUser != null) return targetUser;
if (targetApplication != null) return targetApplication;
throw new IllegalStateException("No target entity");
}

}

public static class EmbeddedApplicationImpl implements EmbeddedApplication
{
private final String iconId, name, description, summary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,29 @@ protected Long handleInternally(DataObject content)
InviteImpl.GuildImpl guild = new InviteImpl.GuildImpl(realGuild);

final Invite.TargetType targetType = Invite.TargetType.fromId(content.getInt("target_type", 0));
final User targetUser;
final InviteImpl.EmbeddedApplicationImpl application;
final Invite.InviteTarget target;

switch (targetType)
{
case STREAM:
DataObject targetUserObject = content.getObject("target_user");
targetUser = getJDA().getEntityBuilder().createUser(targetUserObject);
application = null;
target = new InviteImpl.InviteTargetImpl(targetType, null, getJDA().getEntityBuilder().createUser(targetUserObject));
break;
case EMBEDDED_APPLICATION:
DataObject applicationObject = content.getObject("target_application");
application = new InviteImpl.EmbeddedApplicationImpl(
Invite.EmbeddedApplication application = new InviteImpl.EmbeddedApplicationImpl(
applicationObject.getString("icon", null), applicationObject.getString("name"), applicationObject.getString("description"),
applicationObject.getString("summary"), applicationObject.getLong("id"), applicationObject.getInt("max_participants", -1)
);
targetUser = null;
target = new InviteImpl.InviteTargetImpl(targetType, application, null);
break;
default:
application = null;
targetUser = null;
target = new InviteImpl.InviteTargetImpl(targetType, null, null);
}

Invite invite = new InviteImpl(getJDA(), code, expanded, inviter, maxAge, maxUses, temporary, creationTime, 0, channel, guild, null, application, targetUser, Invite.InviteType.GUILD, targetType);
Invite invite = new InviteImpl(getJDA(), code, expanded, inviter,
maxAge, maxUses, temporary, creationTime,
0, channel, guild, null, target, Invite.InviteType.GUILD);
getJDA().handleEvent(
new GuildInviteCreateEvent(
getJDA(), responseNumber,
Expand Down