use of com.sx4.bot.events.patreon.PatreonEvent in project Sx4 by sx4-discord-bot.
the class PatreonEndpoint method postPatreon.
@POST
@Path("patreon")
public Response postPatreon(final String body, @HeaderParam("X-Patreon-Signature") final String signature, @HeaderParam("X-Patreon-Event") final String event) {
String hash;
try {
hash = HmacUtility.getSignatureHex(this.bot.getConfig().getPatreonWebhookSecret(), body, HmacUtility.HMAC_MD5);
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
return Response.status(500).build();
}
if (!hash.equals(signature)) {
return Response.status(401).build();
}
Document document = Document.parse(body);
WebhookMessage message = new WebhookMessageBuilder().setContent("Patreon payload received").addFile("patreon.json", document.toJson(MongoDatabase.PRETTY_JSON).getBytes(StandardCharsets.UTF_8)).build();
this.webhook.send(message);
int totalAmount = document.getEmbedded(List.of("data", "attributes", "lifetime_support_cents"), 0);
if (totalAmount == 0) {
return Response.status(204).build();
}
Document user = document.getList("included", Document.class).stream().filter(included -> included.getString("type").equals("user")).findFirst().orElse(null);
if (user != null) {
String discordId = user.getEmbedded(List.of("attributes", "social_connections", "discord", "user_id"), String.class);
if (discordId != null) {
this.bot.getPatreonManager().onPatreonEvent(new PatreonEvent(Long.parseLong(discordId), totalAmount));
}
}
return Response.status(204).build();
}
Aggregations