diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java index aef37af..b8ea8e2 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java @@ -12,12 +12,13 @@ public class ConflictMrNotify extends MrNotify { @Builder private ConflictMrNotify( + Long mrId, String name, String url, String projectKey, String sourceBranch ) { - super(projectKey, name, url); + super(mrId, projectKey, name, url); this.sourceBranch = sourceBranch; } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/MrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/MrNotify.java index 38f8bfd..7fb9e4e 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/MrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/MrNotify.java @@ -6,15 +6,18 @@ import lombok.Getter; @Getter public abstract class MrNotify implements Notify { + protected final Long mrId; protected final String projectName; protected final String title; protected final String url; protected MrNotify( + Long mrId, String projectName, String title, String url ) { + this.mrId = mrId; this.projectName = projectName; this.title = title; this.url = url; diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForAssignee.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForAssignee.java index f19fbcb..1b04c80 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForAssignee.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForAssignee.java @@ -18,6 +18,7 @@ public class NewMrForAssignee extends NewMrNotify { @Builder private NewMrForAssignee( + Long mrId, String title, String url, String description, @@ -31,6 +32,7 @@ public class NewMrForAssignee extends NewMrNotify { String newAssigneeName ) { super( + mrId, title, url, description, diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForReview.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForReview.java index 23f580b..2e5d19e 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForReview.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrForReview.java @@ -14,6 +14,7 @@ public class NewMrForReview extends NewMrNotify { @Builder private NewMrForReview( + Long mrId, String title, String url, String description, @@ -25,6 +26,7 @@ public class NewMrForReview extends NewMrNotify { String assignee ) { super( + mrId, title, url, description, diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrNotify.java index 4b99c5d..20c17ca 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/NewMrNotify.java @@ -14,6 +14,7 @@ public abstract class NewMrNotify extends MrNotify { protected final Set labels; protected NewMrNotify( + Long mrId, String title, String url, String description, @@ -23,7 +24,7 @@ public abstract class NewMrNotify extends MrNotify { String sourceBranch, Set labels ) { - super(projectName, title, url); + super(mrId, projectName, title, url); this.description = description; this.author = author; this.targetBranch = targetBranch; diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusMrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusMrNotify.java index dc7488d..cf8d1e9 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusMrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/StatusMrNotify.java @@ -14,13 +14,14 @@ public class StatusMrNotify extends MrNotify { @Builder private StatusMrNotify( + Long mrId, String name, String url, String projectName, MergeRequestState oldStatus, MergeRequestState newStatus ) { - super(projectName, name, url); + super(mrId, projectName, name, url); this.oldStatus = oldStatus; this.newStatus = newStatus; } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdateMrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdateMrNotify.java index d8f2ab1..68845eb 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdateMrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/UpdateMrNotify.java @@ -16,6 +16,7 @@ public class UpdateMrNotify extends MrNotify { @Builder private UpdateMrNotify( + Long mrId, String name, String url, String author, @@ -25,7 +26,7 @@ public class UpdateMrNotify extends MrNotify { Long personTasks, Long personResolvedTasks ) { - super(projectKey, name, url); + super(mrId, projectKey, name, url); this.author = author; this.allTasks = allTasks; this.allResolvedTasks = allResolvedTasks; diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java index 57a6e3a..d82fd7c 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/repository/MergeRequestRepository.java @@ -28,4 +28,6 @@ public interface MergeRequestRepository { Set findAllIds(); + void disableNotify(Long mrId); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java index aceaa4b..2569a06 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/service/MergeRequestsService.java @@ -38,4 +38,6 @@ public interface MergeRequestsService { Set getAllIds(); + void disableNotify(@NonNull Long mrId); + } diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java index 2a0c8c3..eef35ed 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/utils/Icons.java @@ -22,6 +22,9 @@ public class Icons { public static final String LINK = "\uD83D\uDD17"; public static final String REVIEWER = "\uD83D\uDD0E"; public static final String PROJECT = "\uD83C\uDFD7"; + public static final String DISABLE_NOTIFY = "\uD83D\uDD15"; + public static final String YES = "✅"; + public static final String NO = "❌"; private Icons() { utilityClass(); diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java index c7312db..8a94a5d 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/MergeRequestsServiceImpl.java @@ -116,6 +116,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { private void sendNotifyNewMrReview(MergeRequest mergeRequest, String projectName) { notifyService.send( NewMrForReview.builder() + .mrId(mergeRequest.getId()) .projectName(projectName) .labels(mergeRequest.getLabels()) .author(mergeRequest.getAuthor().getName()) @@ -131,6 +132,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { private void sendNotifyNewAssignee(MergeRequest mergeRequest, String projectName, String oldAssigneeName) { final NewMrForAssignee.NewMrForAssigneeBuilder builder = NewMrForAssignee.builder() + .mrId(mergeRequest.getId()) .projectName(projectName) .labels(mergeRequest.getLabels()) .author(mergeRequest.getAuthor().getName()) @@ -264,6 +266,12 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { return repository.findAllIds(); } + @Override + @Transactional + public void disableNotify(@NonNull Long mrId) { + repository.disableNotify(mrId); + } + private void notifyAboutUpdate(MergeRequest oldMergeRequest, MergeRequest mergeRequest, Project project) { final Long botUserGitlabId = personInformation.getId(); @@ -295,6 +303,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { } notifyService.send( UpdateMrNotify.builder() + .mrId(oldMergeRequest.getId()) .author(oldMergeRequest.getAuthor().getName()) .name(oldMergeRequest.getTitle()) .projectKey(project.getName()) @@ -317,6 +326,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { ) { notifyService.send( ConflictMrNotify.builder() + .mrId(oldMergeRequest.getId()) .sourceBranch(oldMergeRequest.getSourceBranch()) .name(mergeRequest.getTitle()) .url(mergeRequest.getWebUrl()) @@ -336,6 +346,7 @@ public class MergeRequestsServiceImpl implements MergeRequestsService { ) { notifyService.send( StatusMrNotify.builder() + .mrId(oldMergeRequest.getId()) .name(newMergeRequest.getTitle()) .url(oldMergeRequest.getWebUrl()) .projectName(project.getName()) diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java index 55b83c6..b0ffc72 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/impl/MergeRequestRepositoryImpl.java @@ -70,4 +70,9 @@ public class MergeRequestRepositoryImpl implements MergeRequestRepository { return jpaRepository.findAllIds(); } + @Override + public void disableNotify(Long mrId) { + jpaRepository.disableNotify(mrId); + } + } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java index 217e8c0..ea89ab2 100644 --- a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/MergeRequestJpaRepository.java @@ -3,6 +3,7 @@ package dev.struchkov.bot.gitlab.data.jpa; import dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr; import dev.struchkov.bot.gitlab.context.domain.MergeRequestState; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation; import org.springframework.data.repository.query.Param; @@ -27,4 +28,8 @@ public interface MergeRequestJpaRepository extends JpaRepositoryImplementation findAllIds(); + @Modifying + @Query("UPDATE MergeRequest mr SET mr.notification = false WHERE mr.id = :mrId") + void disableNotify(@Param("mrId") Long mrId); + } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/AdditionalConfig.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/AdditionalConfig.java new file mode 100644 index 0000000..300ef12 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/AdditionalConfig.java @@ -0,0 +1,17 @@ +package dev.struchkov.bot.gitlab.telegram.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +@Configuration +public class AdditionalConfig { + + @Bean + public ScheduledExecutorService scheduledExecutorService() { + return Executors.newScheduledThreadPool(2); + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java index 4b3a53b..9c045b8 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/config/TelegramBotConfig.java @@ -1,13 +1,17 @@ package dev.struchkov.bot.gitlab.telegram.config; import dev.struchkov.bot.gitlab.telegram.service.ReplaceUrlLocalhost; -import dev.struchkov.bot.gitlab.telegram.unit.CommandUnit; import dev.struchkov.bot.gitlab.telegram.unit.MenuConfig; +import dev.struchkov.bot.gitlab.telegram.unit.command.AnswerNoteUnit; +import dev.struchkov.bot.gitlab.telegram.unit.command.DeleteMessageUnit; +import dev.struchkov.bot.gitlab.telegram.unit.command.DisableNotifyMrUnit; import dev.struchkov.bot.gitlab.telegram.unit.flow.InitSettingFlow; +import dev.struchkov.godfather.main.core.unit.TypeUnit; import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.simple.context.service.EventHandler; import dev.struchkov.godfather.simple.context.service.PersonSettingService; import dev.struchkov.godfather.simple.context.service.UnitPointerService; +import dev.struchkov.godfather.simple.core.action.cmd.RollBackCmdAction; import dev.struchkov.godfather.simple.core.provider.StoryLineHandler; import dev.struchkov.godfather.simple.core.service.PersonSettingServiceImpl; import dev.struchkov.godfather.simple.core.service.StorylineContextMapImpl; @@ -72,9 +76,11 @@ public class TelegramBotConfig { MenuConfig menuConfig, InitSettingFlow unitConfig, - CommandUnit commandUnit + AnswerNoteUnit commandUnit, + DeleteMessageUnit deleteMessageUnit, + DisableNotifyMrUnit disableNotifyMrUnit ) { - final List config = List.of(menuConfig, unitConfig, commandUnit); + final List config = List.of(menuConfig, unitConfig, commandUnit, deleteMessageUnit, disableNotifyMrUnit); return new StorylineMailService( unitPointerService, @@ -89,11 +95,12 @@ public class TelegramBotConfig { TelegramSending sending, PersonSettingService personSettingService, - StorylineService mailStorylineService + StorylineService storylineService ) { final MailAutoresponderTelegram autoresponder = new MailAutoresponderTelegram( - sending, personSettingService, mailStorylineService + sending, personSettingService, storylineService ); + autoresponder.initActionUnit(TypeUnit.BACK_CMD, new RollBackCmdAction<>(storylineService)); autoresponder.setExecutorService(executorService); return autoresponder; } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForAssigneeNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForAssigneeNotifyGenerator.java index cdec770..f1f9c00 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForAssigneeNotifyGenerator.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForAssigneeNotifyGenerator.java @@ -8,6 +8,10 @@ import org.springframework.stereotype.Component; import java.util.List; import java.util.stream.Collectors; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_CONFIRMATION; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_DISABLE_NOTIFY_MR_ID; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_VALUE_FALSE; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DELETE_MESSAGE; import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; @@ -59,8 +63,9 @@ public class NewMrForAssigneeNotifyGenerator implements NotifyBoxAnswerGenerator notifyMessage, inlineKeyBoard( simpleLine( - simpleButton(Icons.VIEW, "DELETE_MESSAGE"), - urlButton(Icons.LINK, notify.getUrl()) + simpleButton(Icons.VIEW, DELETE_MESSAGE), + urlButton(Icons.LINK, notify.getUrl()), + simpleButton(Icons.DISABLE_NOTIFY, "[" + BUTTON_ARG_DISABLE_NOTIFY_MR_ID + ":" + notify.getMrId() + ";" + BUTTON_ARG_CONFIRMATION + ":" + BUTTON_VALUE_FALSE + "]") ) ) ); diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForReviewNotifyGenerator.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForReviewNotifyGenerator.java index deda557..bdfc0be 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForReviewNotifyGenerator.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/service/notify/NewMrForReviewNotifyGenerator.java @@ -7,6 +7,10 @@ import org.springframework.stereotype.Component; import java.util.stream.Collectors; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_CONFIRMATION; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_DISABLE_NOTIFY_MR_ID; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_VALUE_FALSE; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DELETE_MESSAGE; import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; @@ -52,8 +56,9 @@ public class NewMrForReviewNotifyGenerator implements NotifyBoxAnswerGenerator storylineService; + + @EventListener + public void link(ContextStartedEvent event) { + storylineService.lazyLink(DISABLE_NOTIFY_MR, DISABLE_NOTIFY_MR); + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/CommandUnit.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/AnswerNoteUnit.java similarity index 71% rename from telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/CommandUnit.java rename to telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/AnswerNoteUnit.java index cab416b..0dcc772 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/CommandUnit.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/AnswerNoteUnit.java @@ -1,4 +1,4 @@ -package dev.struchkov.bot.gitlab.telegram.unit; +package dev.struchkov.bot.gitlab.telegram.unit.command; import dev.struchkov.bot.gitlab.context.domain.PersonInformation; import dev.struchkov.bot.gitlab.context.domain.entity.Note; @@ -10,18 +10,15 @@ import dev.struchkov.godfather.main.domain.annotation.Unit; import dev.struchkov.godfather.main.domain.content.Attachment; import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.simple.core.unit.AnswerText; -import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; import dev.struchkov.godfather.telegram.main.core.util.Attachments; -import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; import dev.struchkov.haiti.utils.Checker; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import java.text.MessageFormat; import java.util.List; -import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -30,7 +27,7 @@ import static dev.struchkov.godfather.main.domain.BoxAnswer.boxAnswer; @Component @RequiredArgsConstructor -public class CommandUnit { +public class AnswerNoteUnit { private static final Pattern NOTE_LINK = Pattern.compile("#note_\\d+$"); @@ -38,7 +35,6 @@ public class CommandUnit { private final AppSettingService settingService; private final NoteService noteService; private final DiscussionService discussionService; - private final TelegramSending telegramSending; @Unit(value = ANSWER_NOTE, main = true) public AnswerText answerNote() { @@ -82,29 +78,4 @@ public class CommandUnit { .build(); } - @Unit(value = "DELETE_MESSAGE", main = true) - public AnswerText deleteMessage() { - return AnswerText.builder() - .triggerCheck(mail -> { - final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); - if (isAccess) { - final boolean isFirstStart = settingService.isFirstStart(); - if (!isFirstStart) { - final Optional optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments()); - if (optButtonClick.isPresent()) { - final ButtonClickAttachment buttonClick = optButtonClick.get(); - final String rawData = buttonClick.getRawCallBackData(); - return rawData.equals("DELETE_MESSAGE"); - } - } - } - return false; - }) - .answer(mail -> { - final ButtonClickAttachment clickButton = Attachments.findFirstButtonClick(mail.getAttachments()).orElseThrow(); - telegramSending.deleteMessage(mail.getPersonId(), clickButton.getMessageId()); - }) - .build(); - } - } diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DeleteMessageUnit.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DeleteMessageUnit.java new file mode 100644 index 0000000..2e20aec --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DeleteMessageUnit.java @@ -0,0 +1,51 @@ +package dev.struchkov.bot.gitlab.telegram.unit.command; + +import dev.struchkov.bot.gitlab.context.domain.PersonInformation; +import dev.struchkov.bot.gitlab.context.service.AppSettingService; +import dev.struchkov.godfather.main.domain.annotation.Unit; +import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.simple.core.unit.AnswerText; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; +import dev.struchkov.godfather.telegram.main.core.util.Attachments; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DELETE_MESSAGE; + +@Component +@RequiredArgsConstructor +public class DeleteMessageUnit { + + private final TelegramSending telegramSending; + private final PersonInformation personInformation; + private final AppSettingService settingService; + + @Unit(value = DELETE_MESSAGE, global = true) + public AnswerText deleteMessage() { + return AnswerText.builder() + .triggerCheck(mail -> { + final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); + if (isAccess) { + final boolean isFirstStart = settingService.isFirstStart(); + if (!isFirstStart) { + final Optional optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments()); + if (optButtonClick.isPresent()) { + final ButtonClickAttachment buttonClick = optButtonClick.get(); + final String rawData = buttonClick.getRawCallBackData(); + return rawData.equals(DELETE_MESSAGE); + } + } + } + return false; + }) + .answer(mail -> { + final ButtonClickAttachment clickButton = Attachments.findFirstButtonClick(mail.getAttachments()).orElseThrow(); + telegramSending.deleteMessage(mail.getPersonId(), clickButton.getMessageId()); + }) + .build(); + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DisableNotifyMrUnit.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DisableNotifyMrUnit.java new file mode 100644 index 0000000..05ef5c2 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/unit/command/DisableNotifyMrUnit.java @@ -0,0 +1,100 @@ +package dev.struchkov.bot.gitlab.telegram.unit.command; + +import dev.struchkov.bot.gitlab.context.domain.PersonInformation; +import dev.struchkov.bot.gitlab.context.service.AppSettingService; +import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; +import dev.struchkov.bot.gitlab.context.utils.Icons; +import dev.struchkov.godfather.main.domain.annotation.Unit; +import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.simple.core.unit.AnswerText; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment.Arg; +import dev.struchkov.godfather.telegram.main.core.util.Attachments; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.Optional; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_CONFIRMATION; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_ARG_DISABLE_NOTIFY_MR_ID; +import static dev.struchkov.bot.gitlab.telegram.utils.Const.BUTTON_VALUE_TRUE; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DELETE_MESSAGE; +import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DISABLE_NOTIFY_MR; +import static dev.struchkov.godfather.main.domain.BoxAnswer.replaceBoxAnswer; +import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton; +import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine; +import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard; + +@Component +@RequiredArgsConstructor +public class DisableNotifyMrUnit { + + public static final String WARNING_ABOUT_DISABLE_NOTIFY = Icons.DISABLE_NOTIFY + """ + *Disabling notifications* + + Are you sure you want to stop receiving notifications? + -- -- -- -- -- + There will be no more notifications for this merge request about: status change, conflict, addition/removal from reviewers/responsible. + + Thread notifications will continue to come. + """; + public static final String SUCCESSFULLY_DISABLED = "Notifications successfully disabled for the given merge request"; + + private final MergeRequestsService mergeRequestsService; + private final PersonInformation personInformation; + private final AppSettingService settingService; + private final TelegramSending telegramSending; + + private final ScheduledExecutorService scheduledExecutorService; + + @Unit(value = DISABLE_NOTIFY_MR, global = true) + public AnswerText disableNotifyMr() { + return AnswerText.builder() + .triggerCheck(mail -> { + final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); + if (isAccess) { + final boolean isFirstStart = settingService.isFirstStart(); + if (!isFirstStart) { + final Optional optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments()); + if (optButtonClick.isPresent()) { + final ButtonClickAttachment buttonClick = optButtonClick.get(); + return buttonClick.getArgByType(BUTTON_ARG_DISABLE_NOTIFY_MR_ID).isPresent(); + } + } + } + return false; + }) + .answer(mail -> { + final ButtonClickAttachment clickButton = Attachments.findFirstButtonClick(mail.getAttachments()).orElseThrow(); + final boolean confirmation = clickButton.getArgByType(BUTTON_ARG_CONFIRMATION) + .map(Arg::getValue) + .map(BUTTON_VALUE_TRUE::equals) + .orElseThrow(); + final Long mrId = clickButton.getArgByType(BUTTON_ARG_DISABLE_NOTIFY_MR_ID) + .map(Arg::getValue) + .map(Long::parseLong) + .orElseThrow(); + + if (confirmation) { + mergeRequestsService.disableNotify(mrId); + scheduledExecutorService.schedule(() -> telegramSending.deleteMessage(mail.getPersonId(), clickButton.getMessageId()), 5, TimeUnit.SECONDS); + return replaceBoxAnswer(SUCCESSFULLY_DISABLED); + } else { + return replaceBoxAnswer( + WARNING_ABOUT_DISABLE_NOTIFY, + inlineKeyBoard( + simpleLine( + simpleButton(Icons.YES, "[" + BUTTON_ARG_DISABLE_NOTIFY_MR_ID + ":" + mrId + ";" + BUTTON_ARG_CONFIRMATION + ":" + BUTTON_VALUE_TRUE + "]"), + simpleButton(Icons.NO, DELETE_MESSAGE) + ) + ) + ); + } + }) + .build(); + } + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/Const.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/Const.java new file mode 100644 index 0000000..6e1e456 --- /dev/null +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/Const.java @@ -0,0 +1,15 @@ +package dev.struchkov.bot.gitlab.telegram.utils; + +import lombok.experimental.UtilityClass; + +@UtilityClass +public class Const { + + public static final String BUTTON_VALUE_FALSE = "f"; + public static final String BUTTON_VALUE_TRUE = "t"; + + public static final String BUTTON_ARG_DISABLE_NOTIFY_MR_ID = "dis_mr_id"; + public static final String BUTTON_ARG_CONFIRMATION = "conf"; + + +} diff --git a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java index 4b815c2..a10da81 100644 --- a/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java +++ b/telegram-bot/src/main/java/dev/struchkov/bot/gitlab/telegram/utils/UnitName.java @@ -20,7 +20,10 @@ public final class UnitName { public static final String ACCESS_ERROR = "ACCESS_ERROR"; public static final String CHECK_PARSER_PRIVATE_PROJECT_NO = "CHECK_PARSER_PRIVATE_PROJECT_NO"; public static final String CHECK_PARSE_OWNER_PROJECT_NO = "CHECK_PARSE_OWNER_PROJECT_NO"; - public static final String REPLACE_GENERAL_MENU = "REPLACE_GENERAL_MENU"; + + // команды + public static final String DELETE_MESSAGE = "DELETE_MESSAGE"; + public static final String DISABLE_NOTIFY_MR = "DISABLE_NOTIFY_MR"; private UnitName() { utilityClass();