Добавил возможность отключения конкретных уведомлений о тредах

This commit is contained in:
Struchkov Mark 2023-02-08 01:34:21 +03:00
parent 73643cc626
commit 215ac188ff
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
7 changed files with 117 additions and 51 deletions

View File

@ -40,6 +40,9 @@ public class Discussion {
@Column(name = "resolved") @Column(name = "resolved")
private Boolean resolved; private Boolean resolved;
@Column(name = "notification")
private boolean notification;
@ManyToOne(optional = false, cascade = CascadeType.REMOVE) @ManyToOne(optional = false, cascade = CascadeType.REMOVE)
@JoinTable( @JoinTable(
name = "discussion_merge_request", name = "discussion_merge_request",

View File

@ -45,4 +45,7 @@ public class MergeRequestForDiscussion {
@Column(name = "web_url") @Column(name = "web_url")
private String webUrl; private String webUrl;
@Column(name = "notification")
private boolean notification;
} }

View File

@ -1,5 +1,7 @@
package dev.struchkov.bot.gitlab.context.service; package dev.struchkov.bot.gitlab.context.service;
import dev.struchkov.bot.gitlab.context.domain.notify.level.DiscussionLevel;
/** /**
* Сервис отвечает за пользовательские настройки приложения. * Сервис отвечает за пользовательские настройки приложения.
* *
@ -33,4 +35,6 @@ public interface AppSettingService {
boolean isPrivateProjectScan(); boolean isPrivateProjectScan();
DiscussionLevel getLevelDiscussionNotify();
} }

View File

@ -1,6 +1,7 @@
package dev.struchkov.bot.gitlab.core.service.impl; package dev.struchkov.bot.gitlab.core.service.impl;
import dev.struchkov.bot.gitlab.context.domain.entity.AppSetting; import dev.struchkov.bot.gitlab.context.domain.entity.AppSetting;
import dev.struchkov.bot.gitlab.context.domain.notify.level.DiscussionLevel;
import dev.struchkov.bot.gitlab.context.repository.AppSettingRepository; import dev.struchkov.bot.gitlab.context.repository.AppSettingRepository;
import dev.struchkov.bot.gitlab.context.service.AppSettingService; import dev.struchkov.bot.gitlab.context.service.AppSettingService;
import dev.struchkov.haiti.context.exception.NotFoundException; import dev.struchkov.haiti.context.exception.NotFoundException;
@ -79,6 +80,11 @@ public class AppSettingServiceImpl implements AppSettingService {
return getAppSetting().isProjectPrivateScan(); return getAppSetting().isProjectPrivateScan();
} }
@Override
public DiscussionLevel getLevelDiscussionNotify() {
return getAppSetting().getDiscussionNotifyLevel();
}
private AppSetting getAppSetting() { private AppSetting getAppSetting() {
return appSettingRepository.findById(KEY) return appSettingRepository.findById(KEY)
.orElseThrow(NOT_FOUND_SETTINGS); .orElseThrow(NOT_FOUND_SETTINGS);

View File

@ -7,9 +7,11 @@ import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequestForDiscussion;
import dev.struchkov.bot.gitlab.context.domain.entity.Note; import dev.struchkov.bot.gitlab.context.domain.entity.Note;
import dev.struchkov.bot.gitlab.context.domain.entity.Person; import dev.struchkov.bot.gitlab.context.domain.entity.Person;
import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify; import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.level.DiscussionLevel;
import dev.struchkov.bot.gitlab.context.domain.notify.task.DiscussionNewNotify; import dev.struchkov.bot.gitlab.context.domain.notify.task.DiscussionNewNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.task.TaskCloseNotify; import dev.struchkov.bot.gitlab.context.domain.notify.task.TaskCloseNotify;
import dev.struchkov.bot.gitlab.context.repository.DiscussionRepository; import dev.struchkov.bot.gitlab.context.repository.DiscussionRepository;
import dev.struchkov.bot.gitlab.context.service.AppSettingService;
import dev.struchkov.bot.gitlab.context.service.DiscussionService; import dev.struchkov.bot.gitlab.context.service.DiscussionService;
import dev.struchkov.bot.gitlab.context.service.NotifyService; import dev.struchkov.bot.gitlab.context.service.NotifyService;
import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty; import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty;
@ -38,6 +40,8 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static dev.struchkov.bot.gitlab.context.domain.notify.level.DiscussionLevel.NOTIFY_WITH_CONTEXT;
import static dev.struchkov.bot.gitlab.context.domain.notify.level.DiscussionLevel.WITHOUT_NOTIFY;
import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException; import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException;
import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static java.lang.Boolean.FALSE; import static java.lang.Boolean.FALSE;
@ -53,24 +57,33 @@ import static java.lang.Boolean.FALSE;
public class DiscussionServiceImpl implements DiscussionService { public class DiscussionServiceImpl implements DiscussionService {
protected static final Pattern PATTERN = Pattern.compile("@[\\w]+"); protected static final Pattern PATTERN = Pattern.compile("@[\\w]+");
private final OkHttpClient client = new OkHttpClient();
private final DiscussionRepository repository; private final DiscussionRepository repository;
private final PersonInformation personInformation;
private final OkHttpClient client = new OkHttpClient(); private final NotifyService notifyService;
private final AppSettingService settingService;
private final PersonInformation personInformation;
private final GitlabProperty gitlabProperty; private final GitlabProperty gitlabProperty;
private final PersonProperty personProperty; private final PersonProperty personProperty;
private final NotifyService notifyService;
@Override @Override
@Transactional @Transactional
public Discussion create(@NonNull Discussion discussion) { public Discussion create(@NonNull Discussion discussion) {
final List<Note> notes = discussion.getNotes(); final List<Note> notes = discussion.getNotes();
if (isNeedNotifyNewNote(discussion)) { final DiscussionLevel levelDiscussionNotify = settingService.getLevelDiscussionNotify();
notifyNewDiscussion(discussion); if (!WITHOUT_NOTIFY.equals(levelDiscussionNotify)) {
discussion.setNotification(false);
if (isNeedNotifyNewNote(discussion)) {
notifyNewDiscussion(discussion);
} else {
notes.forEach(note -> notificationPersonal(discussion, note));
}
} else { } else {
notes.forEach(note -> notificationPersonal(discussion, note)); discussion.setNotification(true);
} }
final boolean resolved = discussion.getNotes().stream() final boolean resolved = discussion.getNotes().stream()
@ -89,6 +102,7 @@ public class DiscussionServiceImpl implements DiscussionService {
discussion.setResponsible(oldDiscussion.getResponsible()); discussion.setResponsible(oldDiscussion.getResponsible());
discussion.setMergeRequest(oldDiscussion.getMergeRequest()); discussion.setMergeRequest(oldDiscussion.getMergeRequest());
discussion.setNotification(oldDiscussion.isNotification());
final Person responsiblePerson = discussion.getResponsible(); final Person responsiblePerson = discussion.getResponsible();
if (checkNotNull(responsiblePerson)) { if (checkNotNull(responsiblePerson)) {
@ -104,7 +118,10 @@ public class DiscussionServiceImpl implements DiscussionService {
} }
} }
} }
notifyUpdateNote(oldDiscussion, discussion);
if (oldDiscussion.isNotification()) {
notifyUpdateNote(oldDiscussion, discussion);
}
final boolean resolved = discussion.getNotes().stream() final boolean resolved = discussion.getNotes().stream()
.allMatch(note -> note.isResolvable() && note.getResolved()); .allMatch(note -> note.isResolvable() && note.getResolved());
@ -292,28 +309,32 @@ public class DiscussionServiceImpl implements DiscussionService {
} }
private void notifyNewAnswer(Discussion discussion, Note note) { private void notifyNewAnswer(Discussion discussion, Note note) {
if (!personInformation.getId().equals(note.getAuthor().getId())) { final DiscussionLevel discussionLevel = settingService.getLevelDiscussionNotify();
if (!WITHOUT_NOTIFY.equals(discussionLevel)
&& !personInformation.getId().equals(note.getAuthor().getId())) {
final Note firstNote = discussion.getFirstNote(); final Note firstNote = discussion.getFirstNote();
final Optional<Note> prevLastNote = discussion.getPrevLastNote();
final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder() final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder()
.url(note.getWebUrl())
.mergeRequestName(discussion.getMergeRequest().getTitle()); .mergeRequestName(discussion.getMergeRequest().getTitle());
if (prevLastNote.isPresent()) { if (NOTIFY_WITH_CONTEXT.equals(discussionLevel)) {
final Note prevNote = prevLastNote.get(); final Optional<Note> prevLastNote = discussion.getPrevLastNote();
notifyBuilder.previousMessage(prevNote.getBody()); if (prevLastNote.isPresent()) {
notifyBuilder.previousAuthor(prevNote.getAuthor().getName()); final Note prevNote = prevLastNote.get();
notifyBuilder.previousMessage(prevNote.getBody());
notifyBuilder.previousAuthor(prevNote.getAuthor().getName());
}
notifyBuilder
.discussionMessage(firstNote.getBody())
.discussionAuthor(firstNote.getAuthor().getName())
.message(note.getBody())
.authorName(note.getAuthor().getName());
} }
notifyService.send( notifyService.send(notifyBuilder.build());
notifyBuilder
.url(note.getWebUrl())
.discussionMessage(firstNote.getBody())
.discussionAuthor(firstNote.getAuthor().getName())
.message(note.getBody())
.authorName(note.getAuthor().getName())
.build()
);
} }
} }
@ -321,32 +342,42 @@ public class DiscussionServiceImpl implements DiscussionService {
* Уведомляет пользователя, если его никнейм упоминается в комментарии. * Уведомляет пользователя, если его никнейм упоминается в комментарии.
*/ */
protected void notificationPersonal(Discussion discussion, Note note) { protected void notificationPersonal(Discussion discussion, Note note) {
final Matcher matcher = PATTERN.matcher(note.getBody()); final DiscussionLevel discussionLevel = settingService.getLevelDiscussionNotify();
final Set<String> recipientsLogins = new HashSet<>(); if (!WITHOUT_NOTIFY.equals(discussionLevel)) {
while (matcher.find()) { final Matcher matcher = PATTERN.matcher(note.getBody());
final String login = matcher.group(0).replace("@", ""); final Set<String> recipientsLogins = new HashSet<>();
recipientsLogins.add(login);
}
if (recipientsLogins.contains(personInformation.getUsername())) {
final Optional<Note> prevLastNote = discussion.getPrevLastNote();
final Note firstNote = discussion.getFirstNote();
final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder() while (matcher.find()) {
.mergeRequestName(discussion.getMergeRequest().getTitle()) final String login = matcher.group(0).replace("@", "");
.url(note.getWebUrl()) recipientsLogins.add(login);
.discussionMessage(firstNote.getBody())
.discussionAuthor(firstNote.getAuthor().getName());
if (!firstNote.equals(note)) {
notifyBuilder.message(note.getBody())
.authorName(note.getAuthor().getName());
}
if (prevLastNote.isPresent()) {
final Note prevNote = prevLastNote.get();
notifyBuilder.previousMessage(prevNote.getBody());
notifyBuilder.previousAuthor(prevNote.getAuthor().getName());
} }
notifyService.send(notifyBuilder.build()); if (recipientsLogins.contains(personInformation.getUsername())) {
final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder()
.mergeRequestName(discussion.getMergeRequest().getTitle())
.url(note.getWebUrl());
if (NOTIFY_WITH_CONTEXT.equals(discussionLevel)) {
final Optional<Note> prevLastNote = discussion.getPrevLastNote();
final Note firstNote = discussion.getFirstNote();
if (!firstNote.equals(note)) {
notifyBuilder.message(note.getBody())
.authorName(note.getAuthor().getName());
}
if (prevLastNote.isPresent()) {
final Note prevNote = prevLastNote.get();
notifyBuilder.previousMessage(prevNote.getBody());
notifyBuilder.previousAuthor(prevNote.getAuthor().getName());
}
notifyBuilder
.discussionMessage(firstNote.getBody())
.discussionAuthor(firstNote.getAuthor().getName());
}
notifyService.send(notifyBuilder.build());
}
} }
} }

View File

@ -163,6 +163,9 @@
references="person(id)"/> references="person(id)"/>
</column> </column>
<column name="resolved" type="boolean"/> <column name="resolved" type="boolean"/>
<column name="notification" type="boolean">
<constraints nullable="false"/>
</column>
</createTable> </createTable>
<createIndex tableName="discussion" indexName="i_discussion_responsible_id"> <createIndex tableName="discussion" indexName="i_discussion_responsible_id">

View File

@ -3,38 +3,54 @@ package dev.struchkov.bot.gitlab.telegram.service.notify;
import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify; import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify;
import dev.struchkov.bot.gitlab.context.utils.Icons; import dev.struchkov.bot.gitlab.context.utils.Icons;
import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.BoxAnswer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
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.BoxAnswer.boxAnswer;
import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton;
import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard;
import static dev.struchkov.godfather.telegram.domain.keyboard.button.UrlButton.urlButton;
import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; import static dev.struchkov.haiti.utils.Strings.escapeMarkdown;
@Component @Component
@RequiredArgsConstructor
public class NewCommentNotifyGenerator implements NotifyBoxAnswerGenerator<NewCommentNotify> { public class NewCommentNotifyGenerator implements NotifyBoxAnswerGenerator<NewCommentNotify> {
@Override @Override
public BoxAnswer generate(NewCommentNotify notify) { public BoxAnswer generate(NewCommentNotify notify) {
final StringBuilder builder = new StringBuilder(Icons.COMMENT).append(" *New answer in Thread*") final StringBuilder builder = new StringBuilder(Icons.COMMENT).append(" *New answer in Thread*")
.append(Icons.HR) .append(Icons.HR)
.append(escapeMarkdown(notify.getMergeRequestName())); .append(Icons.link(escapeMarkdown(notify.getMergeRequestName()), notify.getUrl()))
.append("\n");
if (checkNotNull(notify.getDiscussionMessage())) { if (checkNotNull(notify.getDiscussionMessage())) {
builder.append("\n-- -- thread first message -- --\n") builder.append("\n-- -- thread first message -- --\n")
.append("*").append(notify.getDiscussionAuthor()).append("*: ").append(escapeMarkdown(notify.getDiscussionMessage())); .append("*").append(notify.getDiscussionAuthor()).append("*: ").append(escapeMarkdown(notify.getDiscussionMessage()))
.append("\n");
} }
if (checkNotNull(notify.getPreviousMessage())) { if (checkNotNull(notify.getPreviousMessage())) {
builder.append("\n-- -- -- previous message -- -- --\n") builder.append("\n-- -- -- previous message -- -- --\n")
.append("*").append(notify.getPreviousAuthor()).append("*: ").append(escapeMarkdown(notify.getPreviousMessage())); .append("*").append(notify.getPreviousAuthor()).append("*: ").append(escapeMarkdown(notify.getPreviousMessage()))
.append("\n");
} }
if (checkNotNull(notify.getMessage())) { if (checkNotNull(notify.getMessage())) {
builder.append("\n-- -- -- --- new answer --- -- -- --\n") builder.append("\n-- -- -- --- new answer --- -- -- --\n")
.append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessage())); .append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessage()))
.append("\n");
} }
final String messageNotify = builder.toString(); final String messageNotify = builder.toString();
return boxAnswer(messageNotify); return boxAnswer(
messageNotify,
inlineKeyBoard(
simpleButton(Icons.VIEW, DELETE_MESSAGE),
urlButton(Icons.LINK, notify.getUrl())
)
);
} }
@Override @Override