-
Notifications
You must be signed in to change notification settings - Fork 0
The logic of searching for valid users has been moved to the database #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
subscription-service/src/main/java/tnews/subscription/repository/SubscriptionRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,31 @@ | ||
| package tnews.subscription.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.stereotype.Repository; | ||
| import tnews.subscription.entity.Subscription; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public interface SubscriptionRepository extends JpaRepository<Subscription, Long>, CustomizedSave<Subscription> { | ||
|
|
||
| @Query(value = """ | ||
| SELECT * | ||
| FROM subscription s | ||
| WHERE | ||
| (s.time_interval = 0 AND | ||
| (s.last_send IS NULL OR s.last_send <= now() - INTERVAL '1 minute')) | ||
| OR | ||
| (s.time_interval = 1 AND | ||
| (s.last_send IS NULL OR s.last_send <= now() - INTERVAL '1 day')) | ||
| OR | ||
| (s.time_interval = 2 AND | ||
| (s.last_send IS NULL OR s.last_send <= now() - INTERVAL '1 week')) | ||
| OR | ||
| (s.time_interval = 3 AND | ||
| (s.last_send IS NULL OR s.last_send <= now() - INTERVAL '1 month')) | ||
| """, nativeQuery = true) | ||
| List<Subscription> findValidSubscriptions(); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,10 @@ | |
| import tnews.subscription.bot.Command; | ||
| import tnews.subscription.controllers.BotController; | ||
| import tnews.subscription.entity.Subscription; | ||
| import tnews.subscription.entity.UserAction; | ||
| import tnews.subscription.service.CategoryService; | ||
| import tnews.subscription.service.SubscriptionService; | ||
| import tnews.subscription.service.UserService; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.List; | ||
|
|
||
| @Component | ||
|
|
@@ -37,41 +34,16 @@ public void scheduleCategoryUpdate() { | |
| log.info("Updated categories: " + categories.size()); | ||
| } | ||
|
|
||
| @Scheduled(fixedRate = 600000) // проверка рассылок новостей раз в минуту для теста оптимально мин время рассылки | ||
| @Scheduled(fixedRate = 600000) // проверка рассылок новостей раз в 10 минут | ||
| // нужно резать LocalDateTime, так как из-за милисекунд будут пропуски | ||
| public void sendNewsDigest() { | ||
| log.info("Sending news digest"); | ||
| List<Subscription> subscriptionList = subscriptionService.findAll(); | ||
| List<Subscription> subscriptionList = subscriptionService.findNewsToValidSubscriptions(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Стало на порядок лучше. |
||
|
|
||
| for (Subscription subscription : subscriptionList) { | ||
| if (shouldSend(subscription)) { | ||
| Update update = createFakeCallbackUpdate(subscription.getId()); | ||
| botController.onUpdateReceived(update); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private boolean shouldSend(Subscription subscription) { | ||
| if (userService.findById(subscription.getId()).getCurrentAction().equals(UserAction.UPDATE)) // не очень читабельно, но используется только тут. Стоит ли переписать? | ||
| return false; | ||
| LocalDateTime lastSent = subscription.getLastSend() != null | ||
| ? subscription.getLastSend().truncatedTo(ChronoUnit.MINUTES) | ||
| : null; // скорее всего округление не обязательно, так как отправка раз в час. Но тесты с ним удут более четко | ||
| LocalDateTime now = LocalDateTime.now(); | ||
| log.info("Checking if last sent is: {}", lastSent); | ||
|
|
||
| boolean tmp = switch (subscription.getTimeInterval()) { | ||
| case ONE_HOUR -> lastSent == null || lastSent.plusHours(1).isBefore(now); //TODO: пока раз минуту, должно быть раз в час | ||
| case ONE_DAY -> lastSent == null || lastSent.plusDays(1).isBefore(now); | ||
| case ONE_WEEK -> lastSent == null || lastSent.plusWeeks(1).isBefore(now); | ||
| case ONE_MONTH -> lastSent == null || lastSent.plusMonths(1).isBefore(now); | ||
| }; | ||
| subscription.setLastSend(now); | ||
| subscriptionService.save(subscription); | ||
| if (lastSent != null) | ||
| log.info(lastSent.toString()); | ||
| else log.info("Last sent is null"); | ||
| return tmp; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -360,15 +360,10 @@ private List<BotApiMethod<?>> exit (Long chatId) { | |
| } | ||
| Set<String> sendNews = new HashSet<>(); | ||
|
|
||
| List<NewsDto> newNews = subscriptionService.getActualNews(chatId, 3); // userService.findActualNews(chatId); | ||
| List<NewsDto> newNews = subscriptionService.getActualNews(chatId, 3); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это мы лимит хардкодим? Вынеси тогда в константу, чтобы читалось нормально. |
||
| if (newNews.isEmpty()) { | ||
| return List.of(); | ||
| } | ||
| sendNews.addAll(newNews.stream() | ||
| .map(NewsDto::getId) | ||
| .toList()); | ||
| subscription.setSentNewsIds(sendNews); | ||
| subscriptionService.save(subscription); | ||
|
|
||
| return Stream.concat( | ||
| Stream.concat( | ||
|
|
@@ -388,7 +383,7 @@ private List<BotApiMethod<?>> moreNews(Long chatId, Integer messageId) { | |
| KeyboardFactory.createSubscription()) | ||
| ); | ||
| } | ||
| List<NewsDto> newNews = subscriptionService.getActualNews(chatId, 3); // userService.findActualNews(chatId); | ||
| List<NewsDto> newNews = subscriptionService.getActualNews(chatId, 3); | ||
| if (newNews.isEmpty()) { | ||
| return MessageFactory.createMessage(chatId, | ||
| "Свежих новостей пока нет :( Как появятся что-то новое - уведомим вас", messageId); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Чтобы не забыть надо писать тесты, тогда они начнут падать если забудешь поменять. Все эти комментарии - бесполезны практически, ставь жесткие ограничения.