From 843da4b57fff72f12f3294efa497af5e26ea1abc Mon Sep 17 00:00:00 2001 From: Paul Huerkamp <35733278+PAException@users.noreply.github.com> Date: Fri, 29 Mar 2024 22:35:36 +0100 Subject: [PATCH] Minor Bugfixes --- .../FirebaseCloudMessagingImpl.java | 5 +++- .../notification/NotificationService.java | 3 ++- .../scheduled/ArticleUpdateService.java | 23 ++++++++++++++----- .../scheduled/CafeteriaUpdateService.java | 5 ++-- .../service/scheduled/EventUpdateService.java | 3 ++- .../scheduled/SolarSystemUpdateService.java | 3 ++- .../scheduled/SubstituteUpdateService.java | 2 +- .../engelsburg/api/util/LoggingComponent.java | 4 ++++ 8 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/notification/FirebaseCloudMessagingImpl.java b/src/main/java/io/github/paexception/engelsburg/api/service/notification/FirebaseCloudMessagingImpl.java index 38ba0d0..23aa10e 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/notification/FirebaseCloudMessagingImpl.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/notification/FirebaseCloudMessagingImpl.java @@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; + import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; @@ -114,7 +115,9 @@ public List sendMulticastNotification(String title, String body, SendResponse response = responsesResponses.get(i); if (response.isSuccessful()) continue; - if (response.getException().getMessagingErrorCode().equals(MessagingErrorCode.UNREGISTERED)) + if (response.getException() != null + && response.getException().getMessagingErrorCode() != null + && response.getException().getMessagingErrorCode().equals(MessagingErrorCode.UNREGISTERED)) invalidTokens.add(tokens.get(i)); } diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/notification/NotificationService.java b/src/main/java/io/github/paexception/engelsburg/api/service/notification/NotificationService.java index a4213fb..53af2d5 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/notification/NotificationService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/notification/NotificationService.java @@ -139,8 +139,9 @@ private static List combineDTOs(List d dtos.get(same.get(same.size() - 1)), low + " - " + high )); + int indexOffset = 0; for (Integer element : same) { - dtos.remove(element.intValue()); + dtos.remove(element - indexOffset++); } } else { notificationDTOs.add(SubstituteNotificationDTO.fromSubstituteDTO(dto, null)); diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java index 405cc16..e4e31f6 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/ArticleUpdateService.java @@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; + import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -39,6 +40,8 @@ public class ArticleUpdateService extends JsonFetchingService implements Logging private static final Logger LOGGER = LoggerFactory.getLogger(ArticleUpdateService.class); private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); private static int counter = 0; + private static long lastArticleTime = 0; + private final ArticleController articleController; private final NotificationService notificationService; @@ -51,7 +54,7 @@ public void fetchNewArticles() { if ("false".equals(System.getProperty("app.scheduling.enable"))) return; LOGGER.debug("[ARTICLE] Fetching..."); - List articles = this.updateArticles(DATE_FORMAT.format(System.currentTimeMillis() - 60 * 1000), 1); + List articles = this.updateArticles(DATE_FORMAT.format(lastArticleTime + 1), 1); if (articles.isEmpty()) LOGGER.debug("[ARTICLE] Not updated"); else LOGGER.debug("[ARTICLE] Fetched " + articles.size()); @@ -107,12 +110,19 @@ private List updateArticles(String date, int page) { if (json.toString().startsWith("[")) { //Errors start with { LOGGER.info("[ARTICLE] Still fetching articles (current count: " + counter + ")"); - this.updateArticles(date, page + 1).forEach(this.articleController::createOrUpdateArticle); + if (jsonArticles.size() == 100) { + this.updateArticles(date, page + 1).forEach(this.articleController::createOrUpdateArticle); + } } } } catch (IOException | ParseException e) { - this.logError("[ARTICLE] Couldn't fetch", e, LOGGER); + this.logExpectedError("[ARTICLE] Couldn't fetch", e, LOGGER); } + + if (!dtos.isEmpty()) { + lastArticleTime = dtos.get(0).getDate(); + } + return dtos; } @@ -134,8 +144,8 @@ private ArticleDTO createArticleDTO(int articleId, JsonElement article) throws I try { mediaUrl = WordPressAPI.getFeaturedMedia(article.getAsJsonObject().get("featured_media").getAsInt(), content); - } catch (Exception e) { - this.logError("[ARTICLE] Couldn't load media: " + articleId, e, LOGGER); + } catch (IOException e) { + this.logExpectedError("[ARTICLE] Couldn't load media: " + articleId, e, LOGGER); } if (Environment.BLURHASH) { @@ -144,7 +154,7 @@ private ArticleDTO createArticleDTO(int articleId, JsonElement article) throws I //content = WordpressAPI.applyBlurHashToAllImages(Jsoup.parse(content)).toString(); --> not needed blurHash = mediaUrl != null ? WordPressAPI.getBlurHash(mediaUrl) : null; } catch (IOException e) { - this.logError("[ARTICLE] Couldn't load blur hash of image", e, LOGGER); + this.logExpectedError("[ARTICLE] Couldn't load blur hash of image", e, LOGGER); } } @@ -168,6 +178,7 @@ public void loadPastArticles() { LOGGER.debug("[ARTICLE] Starting fetching past articles"); Result lastArticle = this.articleController.getArticlesAfter(-1, new Paging(0, 1)); if (lastArticle.isResultPresent()) { //Empty would be an error + lastArticleTime = lastArticle.getResult().getArticles().get(0).getDate(); this.updateArticles(DATE_FORMAT.format(lastArticle.getResult().getArticles().get(0).getDate()), 1) .forEach(this.articleController::createOrUpdateArticle); } else { diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java index c94db30..2b6d08c 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/CafeteriaUpdateService.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; + import java.io.IOException; @Service @@ -46,14 +47,14 @@ public void updateCafeteriaInformation() { content = WordPressAPI.applyBlurHashToAllImages(Jsoup.parse(content)).toString(); blurHash = mediaUrl != null ? WordPressAPI.getBlurHash(mediaUrl) : null; } catch (IOException e) { - this.logError("[CAFETERIA] Couldn't load blur hash of image", e, LOGGER); + this.logExpectedError("[CAFETERIA] Couldn't load blur hash of image", e, LOGGER); } this.cafeteriaController.update(new CafeteriaInformationDTO(content, link, mediaUrl, blurHash)); LOGGER.info("[CAFETERIA] Updated"); } else LOGGER.debug("[CAFETERIA] Not changed"); } catch (IOException e) { - this.logError("[CAFETERIA] Couldn't fetch", e, LOGGER); + this.logExpectedError("[CAFETERIA] Couldn't fetch", e, LOGGER); } } diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java index fdc0605..b801314 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/EventUpdateService.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; + import java.io.IOException; import java.sql.Date; import java.text.ParseException; @@ -60,7 +61,7 @@ public void updateEvents() { LOGGER.info("[EVENT] Updated"); } else LOGGER.debug("[EVENT] Not changed"); } catch (IOException e) { - this.logError("[EVENT] Couldn't fetch", e, LOGGER); + this.logExpectedError("[EVENT] Couldn't fetch", e, LOGGER); } } diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java index 534c5a4..b5219c6 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SolarSystemUpdateService.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; + import java.io.DataInputStream; import java.net.URL; @@ -74,7 +75,7 @@ public void updateSolarSystemInfo() { if (this.checkChanges(html, "text")) this.solarSystemController.updateText(html); } catch (Exception e) { //IO and NullPointer - this.logError("[SOLAR] Couldn't fetch", e, LOGGER); + this.logExpectedError("[SOLAR] Couldn't fetch", e, LOGGER); } } diff --git a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java index c22566a..c1133ac 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java +++ b/src/main/java/io/github/paexception/engelsburg/api/service/scheduled/SubstituteUpdateService.java @@ -263,7 +263,7 @@ else if (entry.text().startsWith("Abwesende Klassen")) if (count > 0) LOGGER.info("[SUBSTITUTE] Fetched " + count); else LOGGER.debug("[SUBSTITUTE] Not changed"); } catch (IOException | ParseException e) { - this.logError("[SUBSTITUTE] Couldn't fetch", e, LOGGER); + this.logExpectedError("[SUBSTITUTE] Couldn't fetch", e, LOGGER); } } diff --git a/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java b/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java index 3184282..f14313b 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java +++ b/src/main/java/io/github/paexception/engelsburg/api/util/LoggingComponent.java @@ -24,4 +24,8 @@ public interface LoggingComponent { default void logError(String msg, Throwable throwable, Logger logger) { logger.error(msg, throwable); } + + default void logExpectedError(String msg, Exception exception, Logger logger) { + logger.error(msg + " because of " + exception.getClass().getCanonicalName() + ": " + exception.getMessage()); + } }