Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package net.dv8tion.jda.api.entities.subscription;

import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.utils.EntityString;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.OffsetDateTime;
import java.util.List;

/**
* Representation of a Discord Subscription
* <br> This class is immutable
*/
public class Subscription
Copy link
Contributor

Choose a reason for hiding this comment

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

Implement ISnowflake

{
private final JDAImpl api;
private final long id;
private final long subscriberId;
private final List<Long> skuIDs;
private final List<Long> entitlementIDs;
private final List<Long> renewalSkuIDs;
private final OffsetDateTime currentPeriodStart;
private final OffsetDateTime currentPeriodEnd;
private final OffsetDateTime canceledAt;
private final SubscriptionStatus status;

public Subscription(@Nonnull final JDAImpl api, final long id, final long subscriberId, @Nonnull final List<Long> skuIDs,
@Nonnull final List<Long> entitlementIDs, @Nullable final List<Long> renewalSkuIDs,
@Nonnull final OffsetDateTime currentPeriodStart, @Nonnull final OffsetDateTime currentPeriodEnd, @Nullable final OffsetDateTime canceledAt,
@Nonnull final SubscriptionStatus status)
{

this.api = api;
this.id = id;
this.subscriberId = subscriberId;
this.skuIDs = skuIDs;
this.entitlementIDs = entitlementIDs;
this.renewalSkuIDs = renewalSkuIDs;
this.currentPeriodStart = currentPeriodStart;
this.currentPeriodEnd = currentPeriodEnd;
this.canceledAt = canceledAt;
this.status = status;
}

public Long getId()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public Long getId()
public long getId()

{
return id;
}

public Long getSubscriberId()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public Long getSubscriberId()
public long getSubscriberId()

{
return subscriberId;
}

public List<Long> getSkuIDs()
{
return skuIDs;
}

public List<Long> getEntitlementIDs()
{
return entitlementIDs;
}

public List<Long> getRenewalSkuIDs()
{
return renewalSkuIDs;
}

public OffsetDateTime getCurrentPeriodStart()
{
return currentPeriodStart;
}

public OffsetDateTime getCurrentPeriodEnd()
{
return currentPeriodEnd;
}

public OffsetDateTime getCanceledAt()
{
return canceledAt;
}

public SubscriptionStatus getStatus()
{
return status;
}


public JDAImpl getApi()
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed imo, and also don't expose internals in API

{
return api;
}

@Override
public int hashCode()
{
return Long.hashCode(id);
}

@Override
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(!(obj instanceof Subscription))
{
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
{
return false;
}
return false;

Subscription other = (Subscription) obj;
return other.id == this.id;
}

@Override
public String toString(){
return new EntityString(this)
.addMetadata("id", id)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.dv8tion.jda.api.entities.subscription;

import javax.annotation.Nonnull;

/**
* Representation of a Discord Subscription Status
*/
public enum SubscriptionStatus
Copy link
Contributor

Choose a reason for hiding this comment

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

Run the formatter on this class

{
UNKNOWN(-1),ACTIVE(0), ENDING(1), INACTIVE(2);

private final int id;

SubscriptionStatus(int id){
this.id = id;
}

@Nonnull
public static SubscriptionStatus fromKey(int id){
for (SubscriptionStatus status : values()){
if(status.id == id){
return status;
}
}
return UNKNOWN;
}

public int getId(){
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import net.dv8tion.jda.api.entities.messages.MessagePoll;
import net.dv8tion.jda.api.entities.messages.MessageSnapshot;
import net.dv8tion.jda.api.entities.sticker.*;
import net.dv8tion.jda.api.entities.subscription.Subscription;
import net.dv8tion.jda.api.entities.subscription.SubscriptionStatus;
import net.dv8tion.jda.api.entities.templates.Template;
import net.dv8tion.jda.api.entities.templates.TemplateChannel;
import net.dv8tion.jda.api.entities.templates.TemplateGuild;
Expand Down Expand Up @@ -392,7 +394,8 @@ public GuildImpl createGuild(long guildId, DataObject guildJson, TLongObjectMap<
LOG.error("Guild is missing a SelfMember. GuildId: {}", guildId);
LOG.debug("Guild is missing a SelfMember. GuildId: {} JSON: \n{}", guildId, guildJson);
// This is actually a gateway request
guildObj.retrieveMembersByIds(api.getSelfUser().getIdLong()).onSuccess(m -> {
guildObj.retrieveMembersByIds(api.getSelfUser().getIdLong()).onSuccess(m ->
{
if (m.isEmpty())
LOG.warn("Was unable to recover SelfMember for guild with id {}. This guild might be corrupted!", guildId);
else
Expand Down Expand Up @@ -2613,6 +2616,42 @@ public Entitlement createEntitlement(DataObject object)
);
}

public Subscription createSubscription(DataObject object)
{
DataArray skuIDs = object.getArray("sku_ids");
DataArray entitlementsIDs = object.getArray("entitlement_ids");
DataArray renewalSkuIDs = object.optArray("renewal_sku_ids").orElse(null);
OffsetDateTime canceledAt = object.getOffsetDateTime("canceled_at", null);


List<Long> mappedSkuIds = mapDataArrayToLongList(skuIDs);
List<Long> mappedEntitlementsIds = mapDataArrayToLongList(entitlementsIDs);
List<Long> mappedRenewalSkuIds = Optional.ofNullable(renewalSkuIDs)
.map(this::mapDataArrayToLongList)
.orElse(null);


return new Subscription(
getJDA(),
object.getUnsignedLong("id"),
object.getUnsignedLong("user_id", 0),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
object.getUnsignedLong("user_id", 0),
object.getUnsignedLong("user_id"),

mappedSkuIds,
mappedEntitlementsIds,
mappedRenewalSkuIds,
object.getOffsetDateTime("current_period_start"),
object.getOffsetDateTime("current_period_end"),
canceledAt,
SubscriptionStatus.fromKey(object.getInt("status"))
);
}

public List<Long> mapDataArrayToLongList(DataArray dataArray)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public List<Long> mapDataArrayToLongList(DataArray dataArray)
private List<Long> mapDataArrayToLongList(DataArray dataArray)

Maybe rename to mapToLongList

{
return IntStream.range(0, dataArray.length())
.mapToObj(dataArray::getLong)
.collect(Collectors.toList());
}

private Map<String, AuditLogChange> changeToMap(Set<AuditLogChange> changesList)
{
return changesList.stream().collect(Collectors.toMap(AuditLogChange::getKey, UnaryOperator.identity()));
Expand Down