aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2024-07-19 13:25:06 +0300
committerGravatar Vitaly Takmazov2024-07-20 22:58:16 +0300
commite14d4612d313b7424b2163ba247843655c4dfca1 (patch)
treee9cf4ce93be54f77fe500019325e3d3463bab2f6 /src/main/java/com/juick/service
parent5c71aa619e5e64babaf52aec7c01204ed02ffe69 (diff)
Update premium status on login
Diffstat (limited to 'src/main/java/com/juick/service')
-rw-r--r--src/main/java/com/juick/service/PatreonService.java33
-rw-r--r--src/main/java/com/juick/service/VKService.java10
2 files changed, 39 insertions, 4 deletions
diff --git a/src/main/java/com/juick/service/PatreonService.java b/src/main/java/com/juick/service/PatreonService.java
index 08a828db..8f21f389 100644
--- a/src/main/java/com/juick/service/PatreonService.java
+++ b/src/main/java/com/juick/service/PatreonService.java
@@ -20,6 +20,8 @@ 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;
@@ -28,12 +30,17 @@ import org.springframework.web.util.UriComponentsBuilder;
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);
+ }
+ });
+ }
}
diff --git a/src/main/java/com/juick/service/VKService.java b/src/main/java/com/juick/service/VKService.java
index 19fa5b58..b9b2483f 100644
--- a/src/main/java/com/juick/service/VKService.java
+++ b/src/main/java/com/juick/service/VKService.java
@@ -56,7 +56,7 @@ public class VKService {
.build(VkontakteApi.instance()));
}
- public void updatePremiumStatus(Integer userId) {
+ public boolean updatePremiumStatus(Integer userId) {
var vkUser = userService.getVkTokens(userId);
if (vkUser != null) {
OAuth2AccessToken token = new OAuth2AccessToken(vkUser.getRight());
@@ -65,12 +65,13 @@ public class VKService {
getVkAuthService().signRequest(token, donRequest);
try (Response vkResponse = getVkAuthService().execute(donRequest)) {
if (vkResponse.isSuccessful()) {
- logger.info(vkResponse.getBody());
+ logger.debug(vkResponse.getBody());
var response = jsonMapper.readTree(vkResponse.getBody());
if (response.has("response")) {
var isDon = response.get("response").intValue() > 0;
- logger.info("{} is Don: {}", vkUser.getLeft(), isDon);
+ logger.debug("{} is Don: {}", vkUser.getLeft(), isDon);
userService.setPremium(userId, isDon);
+ return isDon;
} else {
// token is expired or does not have "groups" permissions
userService.updateVkToken(userId, "");
@@ -80,8 +81,9 @@ public class VKService {
logger.error("Don request error", e);
}
} else {
- logger.warn("User is not connected to VK: {}", userId);
+ logger.debug("User is not connected to VK: {}", userId);
}
+ return false;
}
public OAuth20Service getVkAuthService() {