Добавил возможность отписываться от уведомлений по конкретным MR
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-01-19 00:14:16 +03:00
parent 1040ce5ed2
commit 91bd1bbc4b
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
25 changed files with 290 additions and 48 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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,

View File

@ -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,

View File

@ -14,6 +14,7 @@ public abstract class NewMrNotify extends MrNotify {
protected final Set<String> labels;
protected NewMrNotify(
Long mrId,
String title,
String url,
String description,
@ -23,7 +24,7 @@ public abstract class NewMrNotify extends MrNotify {
String sourceBranch,
Set<String> labels
) {
super(projectName, title, url);
super(mrId, projectName, title, url);
this.description = description;
this.author = author;
this.targetBranch = targetBranch;

View File

@ -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;
}

View File

@ -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;

View File

@ -28,4 +28,6 @@ public interface MergeRequestRepository {
Set<Long> findAllIds();
void disableNotify(Long mrId);
}

View File

@ -38,4 +38,6 @@ public interface MergeRequestsService {
Set<Long> getAllIds();
void disableNotify(@NonNull Long mrId);
}

View File

@ -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();

View File

@ -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())

View File

@ -70,4 +70,9 @@ public class MergeRequestRepositoryImpl implements MergeRequestRepository {
return jpaRepository.findAllIds();
}
@Override
public void disableNotify(Long mrId) {
jpaRepository.disableNotify(mrId);
}
}

View File

@ -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<M
@Query("SELECT mr.id FROM MergeRequest mr")
Set<Long> findAllIds();
@Modifying
@Query("UPDATE MergeRequest mr SET mr.notification = false WHERE mr.id = :mrId")
void disableNotify(@Param("mrId") Long mrId);
}

View File

@ -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);
}
}

View File

@ -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<Object> config = List.of(menuConfig, unitConfig, commandUnit);
final List<Object> config = List.of(menuConfig, unitConfig, commandUnit, deleteMessageUnit, disableNotifyMrUnit);
return new StorylineMailService(
unitPointerService,
@ -89,11 +95,12 @@ public class TelegramBotConfig {
TelegramSending sending,
PersonSettingService personSettingService,
StorylineService<Mail> mailStorylineService
StorylineService<Mail> 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;
}

View File

@ -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 + "]")
)
)
);

View File

@ -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<N
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 + "]")
)
)
);

View File

@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
import java.util.Optional;
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;
@ -45,7 +46,7 @@ public class PipelineNotifyGenerator implements NotifyBoxAnswerGenerator<Pipelin
builder.toString(),
inlineKeyBoard(
simpleLine(
simpleButton(Icons.VIEW, "DELETE_MESSAGE"),
simpleButton(Icons.VIEW, DELETE_MESSAGE),
urlButton(Icons.LINK, notify.getWebUrl())
)
)

View File

@ -6,6 +6,10 @@ import dev.struchkov.bot.gitlab.context.utils.Smile;
import dev.struchkov.godfather.main.domain.BoxAnswer;
import org.springframework.stereotype.Component;
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;
@ -47,8 +51,9 @@ public class UpdatePrNotifyGenerator implements NotifyBoxAnswerGenerator<UpdateM
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 + "]")
)
)
);

View File

@ -0,0 +1,23 @@
package dev.struchkov.bot.gitlab.telegram.unit;
import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.simple.core.service.StorylineService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DISABLE_NOTIFY_MR;
@Component
@RequiredArgsConstructor
public class LinkService {
private final StorylineService<Mail> storylineService;
@EventListener
public void link(ContextStartedEvent event) {
storylineService.lazyLink(DISABLE_NOTIFY_MR, DISABLE_NOTIFY_MR);
}
}

View File

@ -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<Mail> answerNote() {
@ -82,29 +78,4 @@ public class CommandUnit {
.build();
}
@Unit(value = "DELETE_MESSAGE", main = true)
public AnswerText<Mail> deleteMessage() {
return AnswerText.<Mail>builder()
.triggerCheck(mail -> {
final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId());
if (isAccess) {
final boolean isFirstStart = settingService.isFirstStart();
if (!isFirstStart) {
final Optional<ButtonClickAttachment> 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();
}
}

View File

@ -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<Mail> deleteMessage() {
return AnswerText.<Mail>builder()
.triggerCheck(mail -> {
final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId());
if (isAccess) {
final boolean isFirstStart = settingService.isFirstStart();
if (!isFirstStart) {
final Optional<ButtonClickAttachment> 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();
}
}

View File

@ -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<Mail> disableNotifyMr() {
return AnswerText.<Mail>builder()
.triggerCheck(mail -> {
final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId());
if (isAccess) {
final boolean isFirstStart = settingService.isFirstStart();
if (!isFirstStart) {
final Optional<ButtonClickAttachment> 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();
}
}

View File

@ -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";
}

View File

@ -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();