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

This commit is contained in:
Struchkov Mark 2023-02-08 02:16:55 +03:00
parent 215ac188ff
commit def5eaa205
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
17 changed files with 152 additions and 21 deletions

View File

@ -9,6 +9,7 @@ public final class NewCommentNotify implements Notify {
public static final String TYPE = "NewCommentNotify";
private final String threadId;
private final String mergeRequestName;
private final String url;
private final String discussionMessage;
@ -21,6 +22,7 @@ public final class NewCommentNotify implements Notify {
@Builder
public NewCommentNotify(
String threadId,
String mergeRequestName,
String url,
String discussionMessage,
@ -31,6 +33,7 @@ public final class NewCommentNotify implements Notify {
String message,
int numberNotes
) {
this.threadId = threadId;
this.mergeRequestName = mergeRequestName;
this.url = url;
this.discussionMessage = discussionMessage;

View File

@ -15,11 +15,13 @@ public class DiscussionNewNotify extends TaskNotify {
public static final String TYPE = "DiscussionNewNotify";
private final String threadId;
private final String mrName;
private final List<Pair<String, String>> notes;
@Builder
public DiscussionNewNotify(
String threadId,
String mrName,
String authorName,
String url,
@ -27,6 +29,7 @@ public class DiscussionNewNotify extends TaskNotify {
@Singular List<Pair<String, String>> notes
) {
super(authorName, url, discussionMessage);
this.threadId = threadId;
this.mrName = mrName;
this.notes = notes;
}

View File

@ -30,4 +30,6 @@ public interface DiscussionRepository {
void cleanOld();
void notification(boolean enable, String discussionId);
}

View File

@ -43,4 +43,6 @@ public interface DiscussionService {
void cleanOld();
void notification(boolean enable, String discussionId);
}

View File

@ -13,7 +13,7 @@ import java.util.Set;
*/
public interface ProjectService {
Project create(@NonNull Project project);
Project create(@NonNull Project project, boolean sendNotify);
Project update(@NonNull Project project);

View File

@ -75,7 +75,7 @@ public class DiscussionServiceImpl implements DiscussionService {
final DiscussionLevel levelDiscussionNotify = settingService.getLevelDiscussionNotify();
if (!WITHOUT_NOTIFY.equals(levelDiscussionNotify)) {
discussion.setNotification(false);
discussion.setNotification(true);
if (isNeedNotifyNewNote(discussion)) {
notifyNewDiscussion(discussion);
@ -83,7 +83,7 @@ public class DiscussionServiceImpl implements DiscussionService {
notes.forEach(note -> notificationPersonal(discussion, note));
}
} else {
discussion.setNotification(true);
discussion.setNotification(false);
}
final boolean resolved = discussion.getNotes().stream()
@ -140,6 +140,7 @@ public class DiscussionServiceImpl implements DiscussionService {
final MergeRequestForDiscussion mergeRequest = discussion.getMergeRequest();
final DiscussionNewNotify.DiscussionNewNotifyBuilder notifyBuilder = DiscussionNewNotify.builder()
.threadId(discussion.getId())
.mrName(mergeRequest.getTitle())
.authorName(firstNote.getAuthor().getName())
.discussionMessage(firstNote.getBody())
@ -308,6 +309,12 @@ public class DiscussionServiceImpl implements DiscussionService {
log.debug("Конец очистки старых дискуссий");
}
@Override
@Transactional
public void notification(boolean enable, String discussionId) {
repository.notification(enable, discussionId);
}
private void notifyNewAnswer(Discussion discussion, Note note) {
final DiscussionLevel discussionLevel = settingService.getLevelDiscussionNotify();
@ -316,6 +323,7 @@ public class DiscussionServiceImpl implements DiscussionService {
final Note firstNote = discussion.getFirstNote();
final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder()
.threadId(discussion.getId())
.url(note.getWebUrl())
.mergeRequestName(discussion.getMergeRequest().getTitle());
@ -354,6 +362,7 @@ public class DiscussionServiceImpl implements DiscussionService {
if (recipientsLogins.contains(personInformation.getUsername())) {
final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder()
.threadId(discussion.getId())
.mergeRequestName(discussion.getMergeRequest().getTitle())
.url(note.getWebUrl());

View File

@ -33,11 +33,13 @@ public class ProjectServiceImpl implements ProjectService {
@Override
@Transactional
public Project create(@NonNull Project project) {
public Project create(@NonNull Project project, boolean sendNotify) {
final Project newProject = repository.save(project);
final String authorName = personService.getByIdOrThrown(newProject.getCreatorId()).getName();
notifyAboutNewProject(newProject, authorName);
if (sendNotify) {
final String authorName = personService.getByIdOrThrown(newProject.getCreatorId()).getName();
notifyAboutNewProject(newProject, authorName);
}
return newProject;
}
@ -57,7 +59,7 @@ public class ProjectServiceImpl implements ProjectService {
@Transactional
public List<Project> createAll(List<Project> newProjects) {
return newProjects.stream()
.map(this::create)
.map(newProject -> create(newProject, true))
.toList();
}

View File

@ -96,7 +96,7 @@ public class ProjectParser {
if (!projectService.existsById(projectJson.getId())) {
createNewPersons(List.of(projectJson));
final Project newProject = conversionService.convert(projectJson, Project.class);
return projectService.create(newProject);
return projectService.create(newProject, false);
} else {
return projectService.getByIdOrThrow(projectJson.getId());
}

View File

@ -62,4 +62,9 @@ public class DiscussionRepositoryImpl implements DiscussionRepository {
jpaRepository.removeAllByMergeRequestIsNull();
}
@Override
public void notification(boolean enable, String discussionId) {
jpaRepository.notification(enable, discussionId);
}
}

View File

@ -28,4 +28,8 @@ public interface DiscussionJpaRepository extends JpaRepository<Discussion, Strin
@Query("DELETE FROM Discussion d WHERE d.id = :id")
void deleteById(@Param("id") String id);
@Modifying
@Query("UPDATE Discussion d SET d.notification = :enable WHERE d.id = :discussionId")
void notification(@Param("enable") boolean enable, @Param("discussionId") String discussionId);
}

View File

@ -5,6 +5,7 @@ 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.command.DisableNotifyThreadUnit;
import dev.struchkov.bot.gitlab.telegram.unit.command.EnableProjectNotify;
import dev.struchkov.bot.gitlab.telegram.unit.flow.InitSettingFlow;
import dev.struchkov.godfather.main.core.unit.TypeUnit;
@ -80,9 +81,11 @@ public class TelegramBotConfig {
AnswerNoteUnit commandUnit,
DeleteMessageUnit deleteMessageUnit,
DisableNotifyMrUnit disableNotifyMrUnit,
DisableNotifyThreadUnit disableNotifyThreadUnit,
EnableProjectNotify enableProjectNotify
) {
final List<Object> config = List.of(menuConfig, unitConfig, commandUnit, deleteMessageUnit, disableNotifyMrUnit, enableProjectNotify);
final List<Object> config = List.of(menuConfig, unitConfig, commandUnit, deleteMessageUnit, disableNotifyMrUnit,
disableNotifyThreadUnit, enableProjectNotify);
return new StorylineMailService(
unitPointerService,

View File

@ -9,6 +9,9 @@ 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_THREAD_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;
@ -39,7 +42,8 @@ public class DiscussionNewNotifyGenerator implements NotifyBoxAnswerGenerator<Di
notifyMessage,
inlineKeyBoard(
simpleButton(Icons.VIEW, DELETE_MESSAGE),
urlButton(Icons.LINK, notify.getUrl())
urlButton(Icons.LINK, notify.getUrl()),
simpleButton(Icons.DISABLE_NOTIFY, "[" + BUTTON_ARG_DISABLE_NOTIFY_THREAD_ID + ":" + notify.getThreadId() + ";" + BUTTON_ARG_CONFIRMATION + ":" + BUTTON_VALUE_FALSE + "]")
)
);
}

View File

@ -6,6 +6,9 @@ import dev.struchkov.godfather.main.domain.BoxAnswer;
import lombok.RequiredArgsConstructor;
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_THREAD_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;
@ -48,7 +51,8 @@ public class NewCommentNotifyGenerator implements NotifyBoxAnswerGenerator<NewCo
messageNotify,
inlineKeyBoard(
simpleButton(Icons.VIEW, DELETE_MESSAGE),
urlButton(Icons.LINK, notify.getUrl())
urlButton(Icons.LINK, notify.getUrl()),
simpleButton(Icons.DISABLE_NOTIFY, "[" + BUTTON_ARG_DISABLE_NOTIFY_THREAD_ID + ":" + notify.getThreadId() + ";" + BUTTON_ARG_CONFIRMATION + ":" + BUTTON_VALUE_FALSE + "]")
)
);
}

View File

@ -14,7 +14,6 @@ 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;
@ -54,16 +53,13 @@ public class DisableNotifyMrUnit {
public AnswerText<Mail> disableNotifyMr() {
return AnswerText.<Mail>builder()
.triggerCheck(mail -> {
final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId());
if (isAccess) {
final boolean isDisableButtonClick = Attachments.findFirstButtonClick(mail.getAttachments())
.flatMap(buttonClick -> buttonClick.getArgByType(BUTTON_ARG_DISABLE_NOTIFY_MR_ID))
.isPresent();
if (isDisableButtonClick) {
final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId());
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 isAccess && !isFirstStart;
}
return false;
})

View File

@ -0,0 +1,92 @@
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.DiscussionService;
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.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_THREAD_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_THREAD;
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 DisableNotifyThreadUnit {
public static final String WARNING_ABOUT_DISABLE_NOTIFY = Icons.DISABLE_NOTIFY + """
*Disabling notifications*
Are you sure you want to stop receiving notifications of new replies to this thread?
""";
public static final String SUCCESSFULLY_DISABLED = "Notifications successfully disabled for this thread";
private final DiscussionService discussionService;
private final PersonInformation personInformation;
private final AppSettingService settingService;
private final TelegramSending telegramSending;
private final ScheduledExecutorService scheduledExecutorService;
@Unit(value = DISABLE_NOTIFY_THREAD, global = true)
public AnswerText<Mail> disableNotifyThread() {
return AnswerText.<Mail>builder()
.triggerCheck(mail -> {
final boolean isDisableButtonClick = Attachments.findFirstButtonClick(mail.getAttachments())
.flatMap(buttonClick -> buttonClick.getArgByType(BUTTON_ARG_DISABLE_NOTIFY_THREAD_ID))
.isPresent();
if (isDisableButtonClick) {
final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId());
final boolean isFirstStart = settingService.isFirstStart();
return isAccess && !isFirstStart;
}
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 String discussionId = clickButton.getArgByType(BUTTON_ARG_DISABLE_NOTIFY_THREAD_ID)
.map(Arg::getValue)
.map(String::valueOf)
.orElseThrow();
if (confirmation) {
discussionService.notification(false, discussionId);
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_THREAD_ID + ":" + discussionId + ";" + BUTTON_ARG_CONFIRMATION + ":" + BUTTON_VALUE_TRUE + "]"),
simpleButton(Icons.NO, DELETE_MESSAGE)
)
)
);
}
})
.build();
}
}

View File

@ -9,6 +9,7 @@ public class Const {
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_DISABLE_NOTIFY_THREAD_ID = "dis_th_id";
public static final String BUTTON_ARG_ENABLE_NOTIFY_PROJECT_ID = "ena_p_id";
public static final String BUTTON_ARG_CONFIRMATION = "conf";

View File

@ -30,6 +30,7 @@ public final class UnitName {
// команды
public static final String DELETE_MESSAGE = "DELETE_MESSAGE";
public static final String DISABLE_NOTIFY_MR = "DISABLE_NOTIFY_MR";
public static final String DISABLE_NOTIFY_THREAD = "DISABLE_NOTIFY_THREAD";
public static final String ENABLE_NOTIFY_PROJECT = "ENABLE_NOTIFY_PROJECT";
private UnitName() {