Search in sources :

Example 1 with PatreonEvent

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();
}
Also used : WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) PatreonEvent(com.sx4.bot.events.patreon.PatreonEvent) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Document(org.bson.Document) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1 WebhookMessageBuilder (club.minnced.discord.webhook.send.WebhookMessageBuilder)1 PatreonEvent (com.sx4.bot.events.patreon.PatreonEvent)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Document (org.bson.Document)1