diff --git a/Find_New_Articles_WF.bpmn b/Find_New_Posts_WF.bpmn similarity index 86% rename from Find_New_Articles_WF.bpmn rename to Find_New_Posts_WF.bpmn index 8e37d56..50ab201 100644 --- a/Find_New_Articles_WF.bpmn +++ b/Find_New_Posts_WF.bpmn @@ -1,10 +1,10 @@ - + - + Flow_0yrxqwq - + Flow_0yrxqwq @@ -14,7 +14,7 @@ PT24H - + Flow_150dsht Flow_1g56vij @@ -24,12 +24,12 @@ Flow_14geq43 - + Flow_0lqyzo8 Flow_0xev53g - + Flow_14geq43 Flow_0lqyzo8 Flow_0ux9vtc @@ -42,16 +42,12 @@ - - Active group subscription is  subscription, which linked to active User. - - Runs every 15 minutes - Find new articles for each group subscription since last time + Find new posts for each group subscription since last time @@ -97,7 +93,7 @@ - + @@ -109,6 +105,12 @@ + + + + + + @@ -118,31 +120,18 @@ - - - - - - - - - - - - - diff --git a/HELP.md b/HELP.md deleted file mode 100644 index e91da64..0000000 --- a/HELP.md +++ /dev/null @@ -1,9 +0,0 @@ -# Getting Started - -### Reference Documentation -For further reference, please consider the following sections: - -* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) -* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.3.7.RELEASE/maven-plugin/reference/html/) -* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.3.7.RELEASE/maven-plugin/reference/html/#build-image) - diff --git a/README.md b/README.md index 45fd6bd..1065efa 100644 --- a/README.md +++ b/README.md @@ -4,22 +4,21 @@ JavaRush Telegram bot from community for community. Written by developers, who learned in [Javarush](https://javarush.ru). ## Idea The main idea is to show how to create real application, which can be used by someone else. -There are [set of articles](https://javarush.ru/groups/posts/2935-java-proekt-ot-a-do-ja-pishem-realjhnihy-proekt-dlja-portfolio), which are describing step by step guidelines of how it was created. +There are [set of posts](https://javarush.ru/groups/posts/2935-java-proekt-ot-a-do-ja-pishem-realjhnihy-proekt-dlja-portfolio), which are describing step by step guidelines of how it was created. ## MVP Scope -As a user, I want to subscribe on group of articles and get notification via telegram-bot every time, -when new article, related to group subscriptions, has come. +As a user, I want to subscribe on group of posts and get notification via telegram-bot every time, +when new post, related to group subscriptions, has come. -# How it would work +# How it works Based on MVP Scope, we can specify next behaviours (here and after Telegram User, which is using JavaRush Telgegram bot will call User): -- User can subscribe on group of articles -- User can view list of gorup subscriptions on which user subscribes -- User can unsubscribe from gorup of articles +- User can subscribe on group of posts +- User can view list of group subscriptions on which user subscribes +- User can unsubscribe from group of posts - User can set an inactive bot and do not receive notifications - User can restart getting notifications -## Find new articles workflow -The workflow of finding new articles and send them to subscribers can be viewed here: -![Find_New_Articles_WF](https://user-images.githubusercontent.com/16310793/103340221-62bb5400-4a38-11eb-947f-c28ce8ecad1b.png) - +## Find new posts workflow +The workflow of finding new posts and send them to subscribers can be viewed here: +![Find_New_Posts_WF](https://user-images.githubusercontent.com/16310793/119827993-6c22ec80-bf02-11eb-8759-83bea483db93.png) ## Deployment Deployment process as easy as possible: Required software: diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 7f355cb..658263b 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,14 @@ # Release Notes +## 1.0.0 +Implemented all the logic, planned up to MVP: +* User can subscribe on group of posts +* User can view list of group subscriptions on which user subscribes +* User can unsubscribe from group of posts +* User can set an inactive bot and do not receive notifications +* User can restart getting notifications +* Admin has ability to see bot statistics + ## 0.8.0-SNAPSHOT * JRTB-10: extended bot statistics for admins. diff --git a/pom.xml b/pom.xml index ab75cbc..cf238d6 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.github.javarushcommunity javarush-telegrambot - 0.8.0-SNAPSHOT + 1.0.0 Javarush TelegramBot Telegram bot for Javarush from community to community diff --git a/src/main/java/com/github/javarushcommunity/jrtb/bot/JavarushTelegramBot.java b/src/main/java/com/github/javarushcommunity/jrtb/bot/JavarushTelegramBot.java index 4ca48c8..dc03df2 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/bot/JavarushTelegramBot.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/bot/JavarushTelegramBot.java @@ -48,9 +48,9 @@ public void onUpdateReceived(Update update) { String username = update.getMessage().getFrom().getUserName(); if (message.startsWith(COMMAND_PREFIX)) { String commandIdentifier = message.split(" ")[0].toLowerCase(); - commandContainer.retrieveCommand(commandIdentifier, username).execute(update); + commandContainer.findCommand(commandIdentifier, username).execute(update); } else { - commandContainer.retrieveCommand(NO.getCommandName(), username).execute(update); + commandContainer.findCommand(NO.getCommandName(), username).execute(update); } } } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/AddGroupSubCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/AddGroupSubCommand.java index 1939773..37d4afd 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/AddGroupSubCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/AddGroupSubCommand.java @@ -41,7 +41,7 @@ public void execute(Update update) { return; } String groupId = getMessage(update).split(SPACE)[1]; - String chatId = getChatId(update); + Long chatId = getChatId(update); if (isNumeric(groupId)) { GroupDiscussionInfo groupById = javaRushGroupClient.getGroupById(Integer.parseInt(groupId)); if (isNull(groupById.getId())) { @@ -54,17 +54,17 @@ public void execute(Update update) { } } - private void sendGroupNotFound(String chatId, String groupId) { + private void sendGroupNotFound(Long chatId, String groupId) { String groupNotFoundMessage = "Нет группы с ID = \"%s\""; sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId)); } - private void sendNotValidGroupID(String chatId, String groupId) { + private void sendNotValidGroupID(Long chatId, String groupId) { String groupNotFoundMessage = "Неправильный ID группы = \"%s\""; sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId)); ; } - private void sendGroupIdList(String chatId) { + private void sendGroupIdList(Long chatId) { String groupIds = javaRushGroupClient.getGroupList(GroupRequestArgs.builder().build()).stream() .map(group -> String.format("%s - %s \n", group.getTitle(), group.getId())) .collect(Collectors.joining()); diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/AdminHelpCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/AdminHelpCommand.java index 05b7e7e..37e1e42 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/AdminHelpCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/AdminHelpCommand.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.meta.api.objects.Update; import static com.github.javarushcommunity.jrtb.command.CommandName.STAT; +import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; import static java.lang.String.format; /** @@ -24,6 +25,6 @@ public AdminHelpCommand(SendBotMessageService sendBotMessageService) { @Override public void execute(Update update) { - sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), ADMIN_HELP_MESSAGE); + sendBotMessageService.sendMessage(getChatId(update), ADMIN_HELP_MESSAGE); } } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/CommandContainer.java b/src/main/java/com/github/javarushcommunity/jrtb/command/CommandContainer.java index 6c154ef..3df8b9b 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/CommandContainer.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/CommandContainer.java @@ -45,7 +45,7 @@ public CommandContainer(SendBotMessageService sendBotMessageService, TelegramUse unknownCommand = new UnknownCommand(sendBotMessageService); } - public Command retrieveCommand(String commandIdentifier, String username) { + public Command findCommand(String commandIdentifier, String username) { Command orDefault = commandMap.getOrDefault(commandIdentifier, unknownCommand); if (isAdminCommand(orDefault)) { if (admins.contains(username)) { diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/CommandUtils.java b/src/main/java/com/github/javarushcommunity/jrtb/command/CommandUtils.java index c598849..5469ca2 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/CommandUtils.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/CommandUtils.java @@ -8,17 +8,17 @@ public class CommandUtils { /** - * Retrieve chatId from {@link Update} object. + * Get chatId from {@link Update} object. * * @param update provided {@link Update} * @return chatID from the provided {@link Update} object. */ - public static String getChatId(Update update) { - return update.getMessage().getChatId().toString(); + public static Long getChatId(Update update) { + return update.getMessage().getChatId(); } /** - * Retrieve text of the message from {@link Update} object. + * Get text of the message from {@link Update} object. * * @param update provided {@link Update} * @return the text of the message from the provided {@link Update} object. diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommand.java index 24f9e4f..97d320c 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommand.java @@ -43,7 +43,7 @@ public void execute(Update update) { return; } String groupId = getMessage(update).split(SPACE)[1]; - String chatId = getChatId(update); + Long chatId = getChatId(update); if (isNumeric(groupId)) { Optional optionalGroupSub = groupSubService.findById(Integer.valueOf(groupId)); if (optionalGroupSub.isPresent()) { @@ -61,7 +61,7 @@ public void execute(Update update) { } } - private void sendGroupIdList(String chatId) { + private void sendGroupIdList(Long chatId) { String message; List groupSubs = telegramUserService.findByChatId(chatId) .orElseThrow(NotFoundException::new) diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/HelpCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/HelpCommand.java index 811e016..a5916c4 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/HelpCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/HelpCommand.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.meta.api.objects.Update; import static com.github.javarushcommunity.jrtb.command.CommandName.*; +import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; /** * Help {@link Command}. @@ -33,6 +34,6 @@ public HelpCommand(SendBotMessageService sendBotMessageService) { @Override public void execute(Update update) { - sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), HELP_MESSAGE); + sendBotMessageService.sendMessage(getChatId(update), HELP_MESSAGE); } } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/NoCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/NoCommand.java index 28245c5..be9a570 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/NoCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/NoCommand.java @@ -3,6 +3,8 @@ import com.github.javarushcommunity.jrtb.service.SendBotMessageService; import org.telegram.telegrambots.meta.api.objects.Update; +import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; + /** * No {@link Command}. */ @@ -19,6 +21,6 @@ public NoCommand(SendBotMessageService sendBotMessageService) { @Override public void execute(Update update) { - sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), NO_MESSAGE); + sendBotMessageService.sendMessage(getChatId(update), NO_MESSAGE); } } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/StartCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/StartCommand.java index ffe1299..4a7cc22 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/StartCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/StartCommand.java @@ -5,6 +5,8 @@ import com.github.javarushcommunity.jrtb.service.TelegramUserService; import org.telegram.telegrambots.meta.api.objects.Update; +import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; + /** * Start {@link Command}. */ @@ -25,7 +27,7 @@ public StartCommand(SendBotMessageService sendBotMessageService, TelegramUserSer @Override public void execute(Update update) { - String chatId = update.getMessage().getChatId().toString(); + Long chatId = getChatId(update); telegramUserService.findByChatId(chatId).ifPresentOrElse( user -> { diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/StatCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/StatCommand.java index e7cc2ad..8b3addd 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/StatCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/StatCommand.java @@ -9,6 +9,8 @@ import java.util.stream.Collectors; +import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; + /** * Statistics {@link Command}. */ @@ -39,7 +41,7 @@ public void execute(Update update) { .map(it -> String.format("%s (id = %s) - %s подписчиков", it.getTitle(), it.getId(), it.getActiveUserCount())) .collect(Collectors.joining("\n")); - sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), String.format(STAT_MESSAGE, + sendBotMessageService.sendMessage(getChatId(update), String.format(STAT_MESSAGE, statisticDTO.getActiveUserCount(), statisticDTO.getInactiveUserCount(), statisticDTO.getAverageGroupCountByUser(), diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/StopCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/StopCommand.java index 3a328de..4f5a634 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/StopCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/StopCommand.java @@ -7,6 +7,8 @@ import java.util.Optional; +import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; + /** * Stop {@link Command}. */ @@ -25,8 +27,8 @@ public StopCommand(SendBotMessageService sendBotMessageService, TelegramUserServ @Override public void execute(Update update) { - sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), STOP_MESSAGE); - telegramUserService.findByChatId(update.getMessage().getChatId().toString()) + sendBotMessageService.sendMessage(getChatId(update), STOP_MESSAGE); + telegramUserService.findByChatId(getChatId(update)) .ifPresent(it -> { it.setActive(false); telegramUserService.save(it); diff --git a/src/main/java/com/github/javarushcommunity/jrtb/command/UnknownCommand.java b/src/main/java/com/github/javarushcommunity/jrtb/command/UnknownCommand.java index ba12389..659f48b 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/command/UnknownCommand.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/command/UnknownCommand.java @@ -3,6 +3,8 @@ import com.github.javarushcommunity.jrtb.service.SendBotMessageService; import org.telegram.telegrambots.meta.api.objects.Update; +import static com.github.javarushcommunity.jrtb.command.CommandUtils.getChatId; + /** * Unknown {@link Command}. */ @@ -18,6 +20,6 @@ public UnknownCommand(SendBotMessageService sendBotMessageService) { @Override public void execute(Update update) { - sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), UNKNOWN_MESSAGE); + sendBotMessageService.sendMessage(getChatId(update), UNKNOWN_MESSAGE); } } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClient.java b/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClient.java index 02da562..3e61bca 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClient.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClient.java @@ -44,5 +44,5 @@ public interface JavaRushGroupClient { */ GroupDiscussionInfo getGroupById(Integer id); - Integer findLastArticleId(Integer groupSub); + Integer findLastPostId(Integer groupSub); } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClientImpl.java b/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClientImpl.java index fb1eb5d..16447fa 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClientImpl.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/javarushclient/JavaRushGroupClientImpl.java @@ -62,7 +62,7 @@ public GroupDiscussionInfo getGroupById(Integer id) { } @Override - public Integer findLastArticleId(Integer groupSubId) { + public Integer findLastPostId(Integer groupSubId) { List posts = Unirest.get(getJavarushApiPostPath) .queryString("order", "NEW") .queryString("groupKid", groupSubId.toString()) diff --git a/src/main/java/com/github/javarushcommunity/jrtb/job/FindNewArticlesJob.java b/src/main/java/com/github/javarushcommunity/jrtb/job/FindNewArticlesJob.java deleted file mode 100644 index b9b770b..0000000 --- a/src/main/java/com/github/javarushcommunity/jrtb/job/FindNewArticlesJob.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.github.javarushcommunity.jrtb.job; - -import com.github.javarushcommunity.jrtb.service.FindNewArticleService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -import java.time.LocalDateTime; -import java.time.ZoneOffset; - -/** - * Job for finding new articles. - */ -@Slf4j -@Component -public class FindNewArticlesJob { - - private final FindNewArticleService findNewArticleService; - - @Autowired - public FindNewArticlesJob(FindNewArticleService findNewArticleService) { - this.findNewArticleService = findNewArticleService; - } - - @Scheduled(fixedRateString = "${bot.recountNewArticleFixedRate}") - public void findNewArticles() { - LocalDateTime start = LocalDateTime.now(); - - log.info("Find new article job started."); - - findNewArticleService.findNewArticles(); - - LocalDateTime end = LocalDateTime.now(); - - log.info("Find new articles job finished. Took seconds: {}", - end.toEpochSecond(ZoneOffset.UTC) - start.toEpochSecond(ZoneOffset.UTC)); - } -} diff --git a/src/main/java/com/github/javarushcommunity/jrtb/job/FindNewPostsJob.java b/src/main/java/com/github/javarushcommunity/jrtb/job/FindNewPostsJob.java new file mode 100644 index 0000000..5c62cd6 --- /dev/null +++ b/src/main/java/com/github/javarushcommunity/jrtb/job/FindNewPostsJob.java @@ -0,0 +1,39 @@ +package com.github.javarushcommunity.jrtb.job; + +import com.github.javarushcommunity.jrtb.service.FindNewPostsService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +/** + * Job for finding new posts. + */ +@Slf4j +@Component +public class FindNewPostsJob { + + private final FindNewPostsService findNewPostsService; + + @Autowired + public FindNewPostsJob(FindNewPostsService findNewPostsService) { + this.findNewPostsService = findNewPostsService; + } + + @Scheduled(fixedRateString = "${bot.recountNewPostFixedRate}") + public void findNewPosts() { + LocalDateTime start = LocalDateTime.now(); + + log.info("Find new posts job started."); + + findNewPostsService.findNewPosts(); + + LocalDateTime end = LocalDateTime.now(); + + log.info("Find new posts job finished. Took seconds: {}", + end.toEpochSecond(ZoneOffset.UTC) - start.toEpochSecond(ZoneOffset.UTC)); + } +} diff --git a/src/main/java/com/github/javarushcommunity/jrtb/repository/TelegramUserRepository.java b/src/main/java/com/github/javarushcommunity/jrtb/repository/TelegramUserRepository.java index 8f442da..39c7b90 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/repository/TelegramUserRepository.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/repository/TelegramUserRepository.java @@ -10,7 +10,7 @@ * {@link Repository} for handling with {@link TelegramUser} entity. */ @Repository -public interface TelegramUserRepository extends JpaRepository { +public interface TelegramUserRepository extends JpaRepository { List findAllByActiveTrue(); List findAllByActiveFalse(); } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/GroupSub.java b/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/GroupSub.java index d8d7792..1472bd8 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/GroupSub.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/GroupSub.java @@ -21,8 +21,8 @@ public class GroupSub { @Column(name = "title") private String title; - @Column(name = "last_article_id") - private Integer lastArticleId; + @Column(name = "last_post_id") + private Integer lastPostId; @ManyToMany(fetch = FetchType.EAGER) @JoinTable( diff --git a/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/TelegramUser.java b/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/TelegramUser.java index fbd84b7..75bcabe 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/TelegramUser.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/repository/entity/TelegramUser.java @@ -17,7 +17,7 @@ public class TelegramUser { @Id @Column(name = "chat_id") - private String chatId; + private Long chatId; @Column(name = "active") private boolean active; diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewArticleService.java b/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewArticleService.java deleted file mode 100644 index 9797642..0000000 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewArticleService.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.javarushcommunity.jrtb.service; - -/** - * Service for finding new articles. - */ -public interface FindNewArticleService { - - /** - * Find new articles and notify subscribers about it. - */ - void findNewArticles(); -} diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewPostsService.java b/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewPostsService.java new file mode 100644 index 0000000..9b6d022 --- /dev/null +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewPostsService.java @@ -0,0 +1,12 @@ +package com.github.javarushcommunity.jrtb.service; + +/** + * Service for finding new posts. + */ +public interface FindNewPostsService { + + /** + * Find new posts and notify subscribers about it. + */ + void findNewPosts(); +} diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewArticleServiceImpl.java b/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewPostsServiceImpl.java similarity index 71% rename from src/main/java/com/github/javarushcommunity/jrtb/service/FindNewArticleServiceImpl.java rename to src/main/java/com/github/javarushcommunity/jrtb/service/FindNewPostsServiceImpl.java index ecb18a0..bfba595 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewArticleServiceImpl.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/FindNewPostsServiceImpl.java @@ -12,7 +12,7 @@ import java.util.stream.Collectors; @Service -public class FindNewArticleServiceImpl implements FindNewArticleService { +public class FindNewPostsServiceImpl implements FindNewPostsService { public static final String JAVARUSH_WEB_POST_FORMAT = "https://javarush.ru/groups/posts/%s"; @@ -21,9 +21,9 @@ public class FindNewArticleServiceImpl implements FindNewArticleService { private final SendBotMessageService sendMessageService; @Autowired - public FindNewArticleServiceImpl(GroupSubService groupSubService, - JavaRushPostClient javaRushPostClient, - SendBotMessageService sendMessageService) { + public FindNewPostsServiceImpl(GroupSubService groupSubService, + JavaRushPostClient javaRushPostClient, + SendBotMessageService sendMessageService) { this.groupSubService = groupSubService; this.javaRushPostClient = javaRushPostClient; this.sendMessageService = sendMessageService; @@ -31,19 +31,19 @@ public FindNewArticleServiceImpl(GroupSubService groupSubService, @Override - public void findNewArticles() { + public void findNewPosts() { groupSubService.findAll().forEach(gSub -> { - List newPosts = javaRushPostClient.findNewPosts(gSub.getId(), gSub.getLastArticleId()); + List newPosts = javaRushPostClient.findNewPosts(gSub.getId(), gSub.getLastPostId()); - setNewLastArticleId(gSub, newPosts); + setNewLastPostId(gSub, newPosts); - notifySubscribersAboutNewArticles(gSub, newPosts); + notifySubscribersAboutNewPosts(gSub, newPosts); }); } - private void notifySubscribersAboutNewArticles(GroupSub gSub, List newPosts) { + private void notifySubscribersAboutNewPosts(GroupSub gSub, List newPosts) { Collections.reverse(newPosts); - List messagesWithNewArticles = newPosts.stream() + List messagesWithNewPosts = newPosts.stream() .map(post -> String.format("✨Вышла новая статья %s в группе %s.✨\n\n" + "Описание: %s\n\n" + "Ссылка: %s\n", @@ -52,13 +52,13 @@ private void notifySubscribersAboutNewArticles(GroupSub gSub, List new gSub.getUsers().stream() .filter(TelegramUser::isActive) - .forEach(it -> sendMessageService.sendMessage(it.getChatId(), messagesWithNewArticles)); + .forEach(it -> sendMessageService.sendMessage(it.getChatId(), messagesWithNewPosts)); } - private void setNewLastArticleId(GroupSub gSub, List newPosts) { + private void setNewLastPostId(GroupSub gSub, List newPosts) { newPosts.stream().mapToInt(PostInfo::getId).max() .ifPresent(id -> { - gSub.setLastArticleId(id); + gSub.setLastPostId(id); groupSubService.save(gSub); }); } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubService.java b/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubService.java index 0721e40..15d25fb 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubService.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubService.java @@ -11,7 +11,7 @@ */ public interface GroupSubService { - GroupSub save(String chatId, GroupDiscussionInfo groupDiscussionInfo); + GroupSub save(Long chatId, GroupDiscussionInfo groupDiscussionInfo); GroupSub save(GroupSub groupSub); diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceImpl.java b/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceImpl.java index bc043c8..10c5dde 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceImpl.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceImpl.java @@ -27,7 +27,7 @@ public GroupSubServiceImpl(GroupSubRepository groupSubRepository, TelegramUserSe } @Override - public GroupSub save(String chatId, GroupDiscussionInfo groupDiscussionInfo) { + public GroupSub save(Long chatId, GroupDiscussionInfo groupDiscussionInfo) { TelegramUser telegramUser = telegramUserService.findByChatId(chatId).orElseThrow(NotFoundException::new); //TODO add exception handling GroupSub groupSub; @@ -35,7 +35,7 @@ public GroupSub save(String chatId, GroupDiscussionInfo groupDiscussionInfo) { if (groupSubFromDB.isPresent()) { groupSub = groupSubFromDB.get(); Optional first = groupSub.getUsers().stream() - .filter(it -> it.getChatId().equalsIgnoreCase(chatId)) + .filter(it -> it.getChatId().equals(chatId)) .findFirst(); if (first.isEmpty()) { groupSub.addUser(telegramUser); @@ -43,7 +43,7 @@ public GroupSub save(String chatId, GroupDiscussionInfo groupDiscussionInfo) { } else { groupSub = new GroupSub(); groupSub.addUser(telegramUser); - groupSub.setLastArticleId(javaRushGroupClient.findLastArticleId(groupDiscussionInfo.getId())); + groupSub.setLastPostId(javaRushGroupClient.findLastPostId(groupDiscussionInfo.getId())); groupSub.setId(groupDiscussionInfo.getId()); groupSub.setTitle(groupDiscussionInfo.getTitle()); } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageService.java b/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageService.java index 2abb489..bee6d6b 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageService.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageService.java @@ -10,10 +10,16 @@ public interface SendBotMessageService { /** * Send message via telegram bot. * - * @param chatId provided chatId in which would be sent. + * @param chatId provided chatId in which would be sent. * @param message provided message to be sent. */ - void sendMessage(String chatId, String message); + void sendMessage(Long chatId, String message); - void sendMessage(String chatId, List message); + /** + * Send messages via telegram bot. + * + * @param chatId provided chatId in which would be sent. + * @param message collection of provided messages to be sent. + */ + void sendMessage(Long chatId, List message); } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceImpl.java b/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceImpl.java index cfb33f4..7b2646a 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceImpl.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceImpl.java @@ -25,11 +25,11 @@ public SendBotMessageServiceImpl(JavarushTelegramBot javarushBot) { } @Override - public void sendMessage(String chatId, String message) { + public void sendMessage(Long chatId, String message) { if (isBlank(message)) return; SendMessage sendMessage = new SendMessage(); - sendMessage.setChatId(chatId); + sendMessage.setChatId(chatId.toString()); sendMessage.enableHtml(true); sendMessage.setText(message); @@ -42,7 +42,7 @@ public void sendMessage(String chatId, String message) { } @Override - public void sendMessage(String chatId, List messages) { + public void sendMessage(Long chatId, List messages) { if (isEmpty(messages)) return; messages.forEach(m -> sendMessage(chatId, m)); diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserService.java b/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserService.java index e5e0a3b..aa2e372 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserService.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserService.java @@ -19,14 +19,14 @@ public interface TelegramUserService { void save(TelegramUser telegramUser); /** - * Retrieve all active {@link TelegramUser}. + * Find all active {@link TelegramUser}. * * @return the collection of the active {@link TelegramUser} objects. */ List findAllActiveUsers(); /** - * Retrieve all inactive {@link TelegramUser} + * Find all inactive {@link TelegramUser} * * @return the collection of the inactive {@link TelegramUser} objects. */ @@ -38,5 +38,5 @@ public interface TelegramUserService { * @param chatId provided Chat ID * @return {@link TelegramUser} with provided chat ID or null otherwise. */ - Optional findByChatId(String chatId); + Optional findByChatId(Long chatId); } diff --git a/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserServiceImpl.java b/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserServiceImpl.java index 715b1fb..d7be972 100644 --- a/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserServiceImpl.java +++ b/src/main/java/com/github/javarushcommunity/jrtb/service/TelegramUserServiceImpl.java @@ -37,7 +37,7 @@ public List findAllInActiveUsers() { } @Override - public Optional findByChatId(String chatId) { + public Optional findByChatId(Long chatId) { return telegramUserRepository.findById(chatId); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 78c03c2..e56a975 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,5 +9,5 @@ javarush.api.path=https://javarush.ru/api/1.0/rest # TelegramBot configurations: bot.username=test.javarush_community_bot bot.token=1375780501:AAE4A6Rz0BSnIGzeu896OjQnjzsMEG6_uso -bot.recountNewArticleFixedRate = 900000 +bot.recountNewPostFixedRate = 900000 bot.admins: robeskman,romankh3 \ No newline at end of file diff --git a/src/main/resources/db/migration/V00003__rename_last_article_id.sql b/src/main/resources/db/migration/V00003__rename_last_article_id.sql new file mode 100644 index 0000000..0203eb2 --- /dev/null +++ b/src/main/resources/db/migration/V00003__rename_last_article_id.sql @@ -0,0 +1 @@ +ALTER TABLE group_sub CHANGE COLUMN last_article_id last_post_id INT; \ No newline at end of file diff --git a/src/main/resources/db/migration/V00004_change_chat_Id_type_to_Long.sql b/src/main/resources/db/migration/V00004_change_chat_Id_type_to_Long.sql new file mode 100644 index 0000000..a1e5076 --- /dev/null +++ b/src/main/resources/db/migration/V00004_change_chat_Id_type_to_Long.sql @@ -0,0 +1 @@ +ALTER TABLE tg_user MODIFY chat_id INT; \ No newline at end of file diff --git a/src/test/java/com/github/javarushcommunity/jrtb/command/CommandContainerTest.java b/src/test/java/com/github/javarushcommunity/jrtb/command/CommandContainerTest.java index c2088a4..d1a8e1b 100644 --- a/src/test/java/com/github/javarushcommunity/jrtb/command/CommandContainerTest.java +++ b/src/test/java/com/github/javarushcommunity/jrtb/command/CommandContainerTest.java @@ -40,7 +40,7 @@ public void shouldGetAllTheExistingCommands() { //when-then Arrays.stream(CommandName.values()) .forEach(commandName -> { - Command command = commandContainer.retrieveCommand(commandName.getCommandName(), "username"); + Command command = commandContainer.findCommand(commandName.getCommandName(), "username"); Assertions.assertNotEquals(UnknownCommand.class, command.getClass()); }); } @@ -51,7 +51,7 @@ public void shouldReturnUnknownCommand() { String unknownCommand = "/fgjhdfgdfg"; //when - Command command = commandContainer.retrieveCommand(unknownCommand, "username"); + Command command = commandContainer.findCommand(unknownCommand, "username"); //then Assertions.assertEquals(UnknownCommand.class, command.getClass()); diff --git a/src/test/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommandTest.java b/src/test/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommandTest.java index 9229f3e..f8e1835 100644 --- a/src/test/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommandTest.java +++ b/src/test/java/com/github/javarushcommunity/jrtb/command/DeleteGroupSubCommandTest.java @@ -42,7 +42,7 @@ public void shouldProperlyReturnEmptySubscriptionList() { Long chatId = 23456L; Update update = prepareUpdate(chatId, DELETE_GROUP_SUB.getCommandName()); - Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) + Mockito.when(telegramUserService.findByChatId(chatId)) .thenReturn(Optional.of(new TelegramUser())); String expectedMessage = "Пока нет подписок на группы. Чтобы добавить подписку напиши /addGroupSub"; @@ -51,7 +51,7 @@ public void shouldProperlyReturnEmptySubscriptionList() { command.execute(update); //then - Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); + Mockito.verify(sendBotMessageService).sendMessage(chatId, expectedMessage); } @Test @@ -64,7 +64,7 @@ public void shouldProperlyReturnSubscriptionLit() { gs1.setId(123); gs1.setTitle("GS1 Title"); telegramUser.setGroupSubs(singletonList(gs1)); - Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) + Mockito.when(telegramUserService.findByChatId(chatId)) .thenReturn(Optional.of(telegramUser)); String expectedMessage = "Чтобы удалить подписку на группу - передай комадну вместе с ID группы. \n" + @@ -77,7 +77,7 @@ public void shouldProperlyReturnSubscriptionLit() { command.execute(update); //then - Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); + Mockito.verify(sendBotMessageService).sendMessage(chatId, expectedMessage); } @Test @@ -90,7 +90,7 @@ public void shouldRejectByInvalidGroupId() { gs1.setId(123); gs1.setTitle("GS1 Title"); telegramUser.setGroupSubs(singletonList(gs1)); - Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) + Mockito.when(telegramUserService.findByChatId(chatId)) .thenReturn(Optional.of(telegramUser)); String expectedMessage = "неправильный формат ID группы.\n " + @@ -100,7 +100,7 @@ public void shouldRejectByInvalidGroupId() { command.execute(update); //then - Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); + Mockito.verify(sendBotMessageService).sendMessage(chatId, expectedMessage); } @Test @@ -117,13 +117,13 @@ public void shouldProperlyDeleteByGroupId() { gs1.setId(123); gs1.setTitle("GS1 Title"); TelegramUser telegramUser = new TelegramUser(); - telegramUser.setChatId(chatId.toString()); + telegramUser.setChatId(chatId); telegramUser.setGroupSubs(singletonList(gs1)); ArrayList users = new ArrayList<>(); users.add(telegramUser); gs1.setUsers(users); Mockito.when(groupSubService.findById(groupId)).thenReturn(Optional.of(gs1)); - Mockito.when(telegramUserService.findByChatId(String.valueOf(chatId))) + Mockito.when(telegramUserService.findByChatId(chatId)) .thenReturn(Optional.of(telegramUser)); String expectedMessage = "Удалил подписку на группу: GS1 Title"; @@ -134,7 +134,7 @@ public void shouldProperlyDeleteByGroupId() { //then users.remove(telegramUser); Mockito.verify(groupSubService).save(gs1); - Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); + Mockito.verify(sendBotMessageService).sendMessage(chatId, expectedMessage); } @Test @@ -154,6 +154,6 @@ public void shouldDoesNotExistByGroupId() { //then Mockito.verify(groupSubService).findById(groupId); - Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), expectedMessage); + Mockito.verify(sendBotMessageService).sendMessage(chatId, expectedMessage); } } diff --git a/src/test/java/com/github/javarushcommunity/jrtb/command/ListGroupSubCommandTest.java b/src/test/java/com/github/javarushcommunity/jrtb/command/ListGroupSubCommandTest.java index 31109de..cb61c33 100644 --- a/src/test/java/com/github/javarushcommunity/jrtb/command/ListGroupSubCommandTest.java +++ b/src/test/java/com/github/javarushcommunity/jrtb/command/ListGroupSubCommandTest.java @@ -26,7 +26,7 @@ public void shouldProperlyShowsListGroupSub() { //given TelegramUser telegramUser = new TelegramUser(); telegramUser.setActive(true); - telegramUser.setChatId("1"); + telegramUser.setChatId(1L); List groupSubList = new ArrayList<>(); groupSubList.add(populateGroupSub(1, "gs1")); diff --git a/src/test/java/com/github/javarushcommunity/jrtb/command/StatCommandTest.java b/src/test/java/com/github/javarushcommunity/jrtb/command/StatCommandTest.java index b59711a..3e523ba 100644 --- a/src/test/java/com/github/javarushcommunity/jrtb/command/StatCommandTest.java +++ b/src/test/java/com/github/javarushcommunity/jrtb/command/StatCommandTest.java @@ -42,7 +42,7 @@ public void shouldProperlySendMessage() { statCommand.execute(prepareUpdate(chatId, CommandName.STAT.getCommandName())); //then - Mockito.verify(sendBotMessageService).sendMessage(chatId.toString(), format(STAT_MESSAGE, + Mockito.verify(sendBotMessageService).sendMessage(chatId, format(STAT_MESSAGE, statisticDTO.getActiveUserCount(), statisticDTO.getInactiveUserCount(), statisticDTO.getAverageGroupCountByUser(), diff --git a/src/test/java/com/github/javarushcommunity/jrtb/repository/GroupSubRepositoryIT.java b/src/test/java/com/github/javarushcommunity/jrtb/repository/GroupSubRepositoryIT.java index 1a39715..596c514 100644 --- a/src/test/java/com/github/javarushcommunity/jrtb/repository/GroupSubRepositoryIT.java +++ b/src/test/java/com/github/javarushcommunity/jrtb/repository/GroupSubRepositoryIT.java @@ -37,7 +37,7 @@ public void shouldProperlyGetAllUsersForGroupSub() { Assertions.assertEquals(1, groupSubFromDB.get().getId()); List users = groupSubFromDB.get().getUsers(); for(int i=0; i userFromDB = telegramUserRepository.findById("1"); + Optional userFromDB = telegramUserRepository.findById(1L); //then Assertions.assertTrue(userFromDB.isPresent()); @@ -65,7 +65,7 @@ public void shouldProperlyGetAllGroupSubsForUser() { for (int i = 0; i < groupSubs.size(); i++) { Assertions.assertEquals(String.format("g%s", (i + 1)), groupSubs.get(i).getTitle()); Assertions.assertEquals(i + 1, groupSubs.get(i).getId()); - Assertions.assertEquals(i + 1, groupSubs.get(i).getLastArticleId()); + Assertions.assertEquals(i + 1, groupSubs.get(i).getLastPostId()); } } } diff --git a/src/test/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceTest.java b/src/test/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceTest.java index b927830..1694e6b 100644 --- a/src/test/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceTest.java +++ b/src/test/java/com/github/javarushcommunity/jrtb/service/GroupSubServiceTest.java @@ -20,9 +20,9 @@ public class GroupSubServiceTest { private JavaRushGroupClient javaRushGroupClient; private TelegramUser newUser; - private final static String CHAT_ID = "1234234"; + private final static Long CHAT_ID = 1234234L; private final static Integer GROUP_ID = 1123; - private final static Integer LAST_ARTICLE_ID = 310; + private final static Integer LAST_POST_ID = 310; @BeforeEach public void init() { @@ -37,7 +37,7 @@ public void init() { Mockito.when(telegramUserService.findByChatId(CHAT_ID)).thenReturn(Optional.of(newUser)); - Mockito.when(javaRushGroupClient.findLastArticleId(GROUP_ID)).thenReturn(LAST_ARTICLE_ID); + Mockito.when(javaRushGroupClient.findLastPostId(GROUP_ID)).thenReturn(LAST_POST_ID); } @Test @@ -51,7 +51,7 @@ public void shouldProperlySaveGroup() { GroupSub expectedGroupSub = new GroupSub(); expectedGroupSub.setId(groupDiscussionInfo.getId()); expectedGroupSub.setTitle(groupDiscussionInfo.getTitle()); - expectedGroupSub.setLastArticleId(LAST_ARTICLE_ID); + expectedGroupSub.setLastPostId(LAST_POST_ID); expectedGroupSub.addUser(newUser); //when @@ -65,7 +65,7 @@ public void shouldProperlySaveGroup() { public void shouldProperlyAddUserToExistingGroup() { //given TelegramUser oldTelegramUser = new TelegramUser(); - oldTelegramUser.setChatId("2"); + oldTelegramUser.setChatId(2L); oldTelegramUser.setActive(true); GroupDiscussionInfo groupDiscussionInfo = new GroupDiscussionInfo(); diff --git a/src/test/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceTest.java b/src/test/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceTest.java index 8a69485..3702862 100644 --- a/src/test/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceTest.java +++ b/src/test/java/com/github/javarushcommunity/jrtb/service/SendBotMessageServiceTest.java @@ -23,12 +23,12 @@ public void init() { @Test public void shouldProperlySendMessage() throws TelegramApiException { //given - String chatId = "test_chat_id"; + Long chatId = 123L; String message = "test_message"; SendMessage sendMessage = new SendMessage(); sendMessage.setText(message); - sendMessage.setChatId(chatId); + sendMessage.setChatId(chatId.toString()); sendMessage.enableHtml(true); //when