Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement beta charity api (#628)
* feat(eventsub): add charity campaign donate type * feat(helix): add get charity campaign endpoint * feat(chat): add charity donation event * feat(pubsub): add unofficial charity topic * refactor(pubsub): avoid wildcard import
- Loading branch information
Showing
17 changed files
with
446 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
chat/src/main/java/com/github/twitch4j/chat/events/channel/CharityDonationEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.github.twitch4j.chat.events.channel; | ||
|
||
import com.github.twitch4j.chat.events.AbstractChannelEvent; | ||
import com.github.twitch4j.common.annotation.Unofficial; | ||
import com.github.twitch4j.common.enums.CommandPermission; | ||
import com.github.twitch4j.common.events.domain.EventUser; | ||
import com.github.twitch4j.common.util.DonationAmount; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Set; | ||
|
||
@Value | ||
@Unofficial | ||
@EqualsAndHashCode(callSuper = true) | ||
public class CharityDonationEvent extends AbstractChannelEvent { | ||
|
||
@NotNull | ||
@EqualsAndHashCode.Exclude | ||
IRCMessageEvent messageEvent; | ||
|
||
String userId; | ||
|
||
String userLogin; | ||
|
||
String userName; | ||
|
||
Set<CommandPermission> badges; | ||
|
||
String charityName; | ||
|
||
DonationAmount amount; | ||
|
||
String systemMessage; | ||
|
||
public CharityDonationEvent(@NotNull IRCMessageEvent rawEvent) { | ||
super(rawEvent.getChannel()); | ||
this.messageEvent = rawEvent; | ||
this.userId = rawEvent.getTagValue("user-id").orElse(null); | ||
this.userLogin = rawEvent.getTagValue("login").orElse(null); | ||
this.userName = rawEvent.getTagValue("display-name").orElse(userLogin); | ||
this.badges = rawEvent.getClientPermissions(); | ||
this.charityName = rawEvent.getTagValue("msg-param-charity-name").orElse(null); | ||
|
||
Long amount = Long.parseLong(rawEvent.getTags().get("msg-param-donation-amount")); | ||
String currency = rawEvent.getTagValue("msg-param-donation-currency").orElse("USD"); | ||
Integer decimals = Integer.parseInt(rawEvent.getTags().getOrDefault("msg-param-exponent", "2")); | ||
this.amount = new DonationAmount(amount, decimals, currency); | ||
|
||
this.systemMessage = rawEvent.getTagValue("system-msg").orElseGet( | ||
() -> String.format("%s donated %s %s to support %s", userName, currency, this.amount.getParsedValue().toPlainString(), charityName) | ||
); | ||
} | ||
|
||
public EventUser getUser() { | ||
return new EventUser(userId, userLogin); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
common/src/main/java/com/github/twitch4j/common/util/DonationAmount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.github.twitch4j.common.util; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Currency; | ||
|
||
@Data | ||
@Setter(AccessLevel.PRIVATE) | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DonationAmount { | ||
|
||
/** | ||
* The monetary amount. | ||
* <p> | ||
* The amount is specified in the currency’s minor unit. | ||
* <p> | ||
* For example, the minor units for USD is cents, so if the amount is $5.50 USD, value is set to 550. | ||
*/ | ||
private Long value; | ||
|
||
/** | ||
* The number of decimal places used by the currency. | ||
* <p> | ||
* For example, USD uses two decimal places. | ||
*/ | ||
private Integer decimalPlaces; | ||
|
||
/** | ||
* The ISO-4217 three-letter currency code that identifies the type of currency in {@link #getValue()}. | ||
*/ | ||
private String currency; | ||
|
||
/** | ||
* The {@link Currency} corresponding to the ISO-4217 code contained in {@link #getCurrency()}. | ||
*/ | ||
@JsonIgnore | ||
@Getter(lazy = true) | ||
@EqualsAndHashCode.Exclude | ||
private final Currency parsedCurrency = Currency.getInstance(getCurrency()); | ||
|
||
/** | ||
* The donation amount, with the appropriate decimals, based on {@link #getValue()}. | ||
*/ | ||
@JsonIgnore | ||
@Getter(lazy = true) | ||
@EqualsAndHashCode.Exclude | ||
private final BigDecimal parsedValue = BigDecimal.valueOf(getValue(), getDecimalPlaces()); | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...src/main/java/com/github/twitch4j/eventsub/condition/ChannelCharityCampaignCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.github.twitch4j.eventsub.condition; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.ToString; | ||
import lombok.experimental.SuperBuilder; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@SuperBuilder | ||
@EqualsAndHashCode(callSuper = true) | ||
@ToString(callSuper = true) | ||
@Jacksonized | ||
public class ChannelCharityCampaignCondition extends ChannelEventSubCondition {} |
30 changes: 30 additions & 0 deletions
30
...b-common/src/main/java/com/github/twitch4j/eventsub/events/ChannelCharityDonateEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.twitch4j.eventsub.events; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.github.twitch4j.common.util.DonationAmount; | ||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@Setter(AccessLevel.PRIVATE) | ||
@NoArgsConstructor | ||
@ToString(callSuper = true) | ||
@EqualsAndHashCode(callSuper = true) | ||
public class ChannelCharityDonateEvent extends EventSubUserChannelEvent { | ||
|
||
/** | ||
* An ID that uniquely identifies the charity campaign. | ||
*/ | ||
@JsonProperty("id") | ||
private String campaignId; | ||
|
||
/** | ||
* An object that contains the amount of the user’s donation. | ||
*/ | ||
private DonationAmount amount; | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...on/src/main/java/com/github/twitch4j/eventsub/subscriptions/ChannelCharityDonateType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.twitch4j.eventsub.subscriptions; | ||
|
||
import com.github.twitch4j.common.annotation.Unofficial; | ||
import com.github.twitch4j.eventsub.condition.ChannelCharityCampaignCondition; | ||
import com.github.twitch4j.eventsub.events.ChannelCharityDonateEvent; | ||
|
||
/** | ||
* Channel Charity Campaign Donate | ||
* <p> | ||
* Sends an event notification when a user donates to the broadcaster’s charity campaign. | ||
* <p> | ||
* This subscription type is currently in open beta. | ||
* | ||
* @see com.github.twitch4j.auth.domain.TwitchScopes#HELIX_CHANNEL_CHARITY_READ | ||
*/ | ||
@Unofficial | ||
public class ChannelCharityDonateType implements SubscriptionType<ChannelCharityCampaignCondition, ChannelCharityCampaignCondition.ChannelCharityCampaignConditionBuilder<?, ?>, ChannelCharityDonateEvent> { | ||
|
||
@Override | ||
public String getName() { | ||
return "channel.charity_campaign.donate"; | ||
} | ||
|
||
@Override | ||
public String getVersion() { | ||
return "1"; | ||
} | ||
|
||
@Override | ||
public ChannelCharityCampaignCondition.ChannelCharityCampaignConditionBuilder<?, ?> getConditionBuilder() { | ||
return ChannelCharityCampaignCondition.builder(); | ||
} | ||
|
||
@Override | ||
public Class<ChannelCharityDonateEvent> getEventClass() { | ||
return ChannelCharityDonateEvent.class; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
pubsub/src/main/java/com/github/twitch4j/pubsub/domain/CharityCampaignStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.twitch4j.pubsub.domain; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import lombok.experimental.Accessors; | ||
|
||
@Data | ||
@Setter(AccessLevel.PRIVATE) | ||
@EqualsAndHashCode(callSuper = true) | ||
@ToString(callSuper = true) | ||
public class CharityCampaignStatus extends CharityDonationData { | ||
|
||
@Accessors(fluent = true) | ||
@JsonProperty("is_active") | ||
private Boolean isActive; | ||
|
||
@JsonProperty("campaign_name") | ||
private String charityName; | ||
|
||
@JsonProperty("campaign_description") | ||
private String charityDescription; | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
pubsub/src/main/java/com/github/twitch4j/pubsub/domain/CharityDonationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.twitch4j.pubsub.domain; | ||
|
||
import com.github.twitch4j.common.util.DonationAmount; | ||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.Setter; | ||
|
||
import java.util.Optional; | ||
|
||
@Data | ||
@Setter(AccessLevel.PRIVATE) | ||
public class CharityDonationData { | ||
|
||
private String campaignId; | ||
|
||
private String campaignCurrency = "USD"; | ||
|
||
private Long donationTotal; | ||
|
||
private Long goalTarget; | ||
|
||
public DonationAmount getTotal() { | ||
return new DonationAmount(donationTotal, 2, campaignCurrency); | ||
} | ||
|
||
public Optional<DonationAmount> getTarget() { | ||
return Optional.ofNullable(goalTarget) | ||
.filter(l -> l > 0) | ||
.map(target -> new DonationAmount(target, 2, campaignCurrency)); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
pubsub/src/main/java/com/github/twitch4j/pubsub/events/CharityCampaignDonationEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.github.twitch4j.pubsub.events; | ||
|
||
import com.github.twitch4j.common.events.TwitchEvent; | ||
import com.github.twitch4j.pubsub.domain.CharityDonationData; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class CharityCampaignDonationEvent extends TwitchEvent { | ||
String channelId; | ||
CharityDonationData data; | ||
} |
Oops, something went wrong.