diff options
Diffstat (limited to 'src/main/java/com/juick/service/PatreonService.java')
-rw-r--r-- | src/main/java/com/juick/service/PatreonService.java | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/main/java/com/juick/service/PatreonService.java b/src/main/java/com/juick/service/PatreonService.java index fdd6d3dd..8f21f389 100644 --- a/src/main/java/com/juick/service/PatreonService.java +++ b/src/main/java/com/juick/service/PatreonService.java @@ -20,20 +20,27 @@ package com.juick.service; import com.fasterxml.jackson.databind.ObjectMapper; import okhttp3.OkHttpClient; import okhttp3.Request; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.util.UriComponentsBuilder; -import javax.inject.Inject; +import jakarta.inject.Inject; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.stream.StreamSupport; @Component public class PatreonService { + private static final Logger logger = LoggerFactory.getLogger("Patreon"); + + @Inject + private UserService userService; @Inject private OkHttpClient httpClient; @Inject @@ -89,4 +96,30 @@ public class PatreonService { } return List.of(); } + + public void updateStatus(List<String> updatedEmails) { + var campainsResponse = fetchCampaigns(); + List<String> activeEmails = new ArrayList<>(); + campainsResponse.forEach(campaign -> { + var pledgesResponse = fetchPledges(campaign); + pledgesResponse.forEach(pledge -> { + logger.debug("Pledge email: {}", pledge); + activeEmails.add(pledge); + }); + }); + activeEmails.forEach(email -> { + var user = userService.getUserByEmail(email); + if (!user.isAnonymous()) { + userService.setPremium(user.getUid(), true); + } + }); + updatedEmails.stream().filter(email -> !activeEmails.contains(email)) + .forEach(deleted -> { + var user = userService.getUserByEmail(deleted); + if (!user.isAnonymous() && user.isPremium()) { + logger.debug("User is not a patron anymore: {}", deleted); + userService.setPremium(user.getUid(), false); + } + }); + } } |