Рефакторинг уведомлений о комментариях
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-02-08 00:46:46 +03:00
parent 99c9cd5ee2
commit 73643cc626
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
12 changed files with 191 additions and 126 deletions

View File

@ -1,7 +1,10 @@
package dev.struchkov.bot.gitlab.context.domain.entity; package dev.struchkov.bot.gitlab.context.domain.entity;
import dev.struchkov.bot.gitlab.context.domain.notify.level.DiscussionLevel;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.Getter; import lombok.Getter;
@ -34,4 +37,8 @@ public class AppSetting {
@Column(name = "project_private_scan") @Column(name = "project_private_scan")
private boolean projectPrivateScan; private boolean projectPrivateScan;
@Enumerated(EnumType.STRING)
@Column(name = "discussion_notify_level")
private DiscussionLevel discussionNotifyLevel;
} }

View File

@ -17,6 +17,8 @@ import lombok.Setter;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
/** /**
* @author upagge 11.02.2021 * @author upagge 11.02.2021
*/ */
@ -62,6 +64,20 @@ public class Discussion {
this.notes = notes; this.notes = notes;
} }
public int getNoteSize() {
if (checkNotEmpty(notes)) {
return notes.size();
}
return 0;
}
public Optional<Note> getNoteByNumber(int number) {
if (checkNotEmpty(notes) && number < notes.size()) {
return Optional.of(notes.get(number));
}
return Optional.empty();
}
public Note getFirstNote() { public Note getFirstNote() {
return this.notes.get(0); return this.notes.get(0);
} }

View File

@ -9,6 +9,7 @@ public final class NewCommentNotify implements Notify {
public static final String TYPE = "NewCommentNotify"; public static final String TYPE = "NewCommentNotify";
private final String mergeRequestName;
private final String url; private final String url;
private final String discussionMessage; private final String discussionMessage;
private final String discussionAuthor; private final String discussionAuthor;
@ -16,17 +17,21 @@ public final class NewCommentNotify implements Notify {
private final String previousAuthor; private final String previousAuthor;
private final String authorName; private final String authorName;
private final String message; private final String message;
private final int numberNotes;
@Builder @Builder
public NewCommentNotify( public NewCommentNotify(
String mergeRequestName,
String url, String url,
String discussionMessage, String discussionMessage,
String discussionAuthor, String discussionAuthor,
String previousMessage, String previousMessage,
String previousAuthor, String previousAuthor,
String authorName, String authorName,
String message String message,
int numberNotes
) { ) {
this.mergeRequestName = mergeRequestName;
this.url = url; this.url = url;
this.discussionMessage = discussionMessage; this.discussionMessage = discussionMessage;
this.discussionAuthor = discussionAuthor; this.discussionAuthor = discussionAuthor;
@ -34,9 +39,9 @@ public final class NewCommentNotify implements Notify {
this.previousAuthor = previousAuthor; this.previousAuthor = previousAuthor;
this.authorName = authorName; this.authorName = authorName;
this.message = message; this.message = message;
this.numberNotes = numberNotes;
} }
@Override @Override
public String getType() { public String getType() {
return TYPE; return TYPE;

View File

@ -0,0 +1,7 @@
package dev.struchkov.bot.gitlab.context.domain.notify.level;
public enum DiscussionLevel {
WITHOUT_NOTIFY, NOTIFY_WITHOUT_CONTEXT, NOTIFY_WITH_CONTEXT
}

View File

@ -70,7 +70,7 @@ public class DiscussionServiceImpl implements DiscussionService {
if (isNeedNotifyNewNote(discussion)) { if (isNeedNotifyNewNote(discussion)) {
notifyNewDiscussion(discussion); notifyNewDiscussion(discussion);
} else { } else {
notes.forEach(this::notificationPersonal); notes.forEach(note -> notificationPersonal(discussion, note));
} }
final boolean resolved = discussion.getNotes().stream() final boolean resolved = discussion.getNotes().stream()
@ -81,6 +81,39 @@ public class DiscussionServiceImpl implements DiscussionService {
return repository.save(discussion); return repository.save(discussion);
} }
@Override
@Transactional
public Discussion update(@NonNull Discussion discussion) {
final Discussion oldDiscussion = repository.findById(discussion.getId())
.orElseThrow(notFoundException("Дискуссия не найдена"));
discussion.setResponsible(oldDiscussion.getResponsible());
discussion.setMergeRequest(oldDiscussion.getMergeRequest());
final Person responsiblePerson = discussion.getResponsible();
if (checkNotNull(responsiblePerson)) {
for (Note note : discussion.getNotes()) {
if (responsiblePerson.getId().equals(note.getAuthor().getId())) {
note.setAuthor(responsiblePerson);
}
final Person resolvedBy = note.getResolvedBy();
if (checkNotNull(resolvedBy)) {
if (responsiblePerson.getId().equals(resolvedBy.getId())) {
note.setResolvedBy(responsiblePerson);
}
}
}
}
notifyUpdateNote(oldDiscussion, discussion);
final boolean resolved = discussion.getNotes().stream()
.allMatch(note -> note.isResolvable() && note.getResolved());
discussion.setResolved(resolved);
return repository.save(discussion);
}
/** /**
* <p>Уведомляет пользователя, если появился новый комментарий</p> * <p>Уведомляет пользователя, если появился новый комментарий</p>
*/ */
@ -116,39 +149,6 @@ public class DiscussionServiceImpl implements DiscussionService {
&& FALSE.equals(firstNote.getResolved()); // Комментарий не отмечен как решенный && FALSE.equals(firstNote.getResolved()); // Комментарий не отмечен как решенный
} }
@Override
@Transactional
public Discussion update(@NonNull Discussion discussion) {
final Discussion oldDiscussion = repository.findById(discussion.getId())
.orElseThrow(notFoundException("Дискуссия не найдена"));
discussion.setResponsible(oldDiscussion.getResponsible());
discussion.setMergeRequest(oldDiscussion.getMergeRequest());
final Person responsiblePerson = discussion.getResponsible();
if (checkNotNull(responsiblePerson)) {
for (Note note : discussion.getNotes()) {
if (responsiblePerson.getId().equals(note.getAuthor().getId())) {
note.setAuthor(responsiblePerson);
}
final Person resolvedBy = note.getResolvedBy();
if (checkNotNull(resolvedBy)) {
if (responsiblePerson.getId().equals(resolvedBy.getId())) {
note.setResolvedBy(responsiblePerson);
}
}
}
}
notifyUpdateNote(oldDiscussion, discussion);
final boolean resolved = discussion.getNotes().stream()
.allMatch(note -> note.isResolvable() && note.getResolved());
discussion.setResolved(resolved);
return repository.save(discussion);
}
@Override @Override
public List<Discussion> updateAll(@NonNull List<Discussion> discussions) { public List<Discussion> updateAll(@NonNull List<Discussion> discussions) {
return discussions.stream() return discussions.stream()
@ -178,39 +178,13 @@ public class DiscussionServiceImpl implements DiscussionService {
if (userParticipatedInDiscussion) { if (userParticipatedInDiscussion) {
notifyNewAnswer(discussion, newNote); notifyNewAnswer(discussion, newNote);
} else { } else {
notificationPersonal(newNote); notificationPersonal(discussion, newNote);
} }
} }
} }
} }
private void notifyNewAnswer(Discussion discussion, Note note) {
if (!personInformation.getId().equals(note.getAuthor().getId())) {
final Note firstNote = discussion.getFirstNote();
final Optional<Note> prevLastNote = discussion.getPrevLastNote();
final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder();
if (prevLastNote.isPresent()) {
final Note prevNote = prevLastNote.get();
notifyBuilder.previousMessage(prevNote.getBody());
notifyBuilder.previousAuthor(prevNote.getAuthor().getName());
}
notifyService.send(
notifyBuilder
.url(note.getWebUrl())
.discussionMessage(firstNote.getBody())
.discussionAuthor(firstNote.getAuthor().getName())
.message(note.getBody())
.authorName(note.getAuthor().getName())
.build()
);
}
}
private void updateTask(Note note, Note oldNote) { private void updateTask(Note note, Note oldNote) {
if (isResolved(note, oldNote)) { if (isResolved(note, oldNote)) {
final MergeRequestForDiscussion mergeRequest = oldNote.getDiscussion().getMergeRequest(); final MergeRequestForDiscussion mergeRequest = oldNote.getDiscussion().getMergeRequest();
@ -253,20 +227,18 @@ public class DiscussionServiceImpl implements DiscussionService {
final String requestUrl = MessageFormat.format(gitlabProperty.getNewNoteUrl(), projectId, mergeRequest.getTwoId(), discussion.getId(), text); final String requestUrl = MessageFormat.format(gitlabProperty.getNewNoteUrl(), projectId, mergeRequest.getTwoId(), discussion.getId(), text);
RequestBody formBody = new FormBody.Builder().build(); final RequestBody formBody = new FormBody.Builder().build();
Request request = new Request.Builder() final Request request = new Request.Builder()
.post(formBody) .post(formBody)
.header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken()) .header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken())
.url(requestUrl) .url(requestUrl)
.build(); .build();
try { try {
client.newCall(request).execute(); client.newCall(request).execute();
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
} }
@Override @Override
@ -319,10 +291,36 @@ public class DiscussionServiceImpl implements DiscussionService {
log.debug("Конец очистки старых дискуссий"); log.debug("Конец очистки старых дискуссий");
} }
private void notifyNewAnswer(Discussion discussion, Note note) {
if (!personInformation.getId().equals(note.getAuthor().getId())) {
final Note firstNote = discussion.getFirstNote();
final Optional<Note> prevLastNote = discussion.getPrevLastNote();
final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder()
.mergeRequestName(discussion.getMergeRequest().getTitle());
if (prevLastNote.isPresent()) {
final Note prevNote = prevLastNote.get();
notifyBuilder.previousMessage(prevNote.getBody());
notifyBuilder.previousAuthor(prevNote.getAuthor().getName());
}
notifyService.send(
notifyBuilder
.url(note.getWebUrl())
.discussionMessage(firstNote.getBody())
.discussionAuthor(firstNote.getAuthor().getName())
.message(note.getBody())
.authorName(note.getAuthor().getName())
.build()
);
}
}
/** /**
* Уведомляет пользователя, если его никнейм упоминается в комментарии. * Уведомляет пользователя, если его никнейм упоминается в комментарии.
*/ */
protected void notificationPersonal(@NonNull Note note) { protected void notificationPersonal(Discussion discussion, Note note) {
final Matcher matcher = PATTERN.matcher(note.getBody()); final Matcher matcher = PATTERN.matcher(note.getBody());
final Set<String> recipientsLogins = new HashSet<>(); final Set<String> recipientsLogins = new HashSet<>();
while (matcher.find()) { while (matcher.find()) {
@ -330,13 +328,25 @@ public class DiscussionServiceImpl implements DiscussionService {
recipientsLogins.add(login); recipientsLogins.add(login);
} }
if (recipientsLogins.contains(personInformation.getUsername())) { if (recipientsLogins.contains(personInformation.getUsername())) {
notifyService.send( final Optional<Note> prevLastNote = discussion.getPrevLastNote();
NewCommentNotify.builder() final Note firstNote = discussion.getFirstNote();
.authorName(note.getAuthor().getName())
.message(note.getBody()) final NewCommentNotify.NewCommentNotifyBuilder notifyBuilder = NewCommentNotify.builder()
.url(note.getWebUrl()) .mergeRequestName(discussion.getMergeRequest().getTitle())
.build() .url(note.getWebUrl())
); .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());
} }
} }

View File

@ -26,7 +26,6 @@ import java.util.stream.Collectors;
import static dev.struchkov.bot.gitlab.core.utils.HttpParse.ACCEPT; import static dev.struchkov.bot.gitlab.core.utils.HttpParse.ACCEPT;
import static dev.struchkov.haiti.context.exception.ConvertException.convertException; import static dev.struchkov.haiti.context.exception.ConvertException.convertException;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
import static java.util.Collections.singleton;
/** /**
* Парсер проектов. * Парсер проектов.
@ -88,7 +87,7 @@ public class ProjectParser {
} }
} }
public void parseByUrl(String projectUrl) { public Project parseByUrl(String projectUrl) {
final ProjectJson projectJson = HttpParse.request(projectUrl) final ProjectJson projectJson = HttpParse.request(projectUrl)
.header(ACCEPT) .header(ACCEPT)
.header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken()) .header(StringUtils.H_PRIVATE_TOKEN, personProperty.getToken())
@ -96,14 +95,10 @@ public class ProjectParser {
.orElseThrow(convertException("Ошибка получения репозитория.")); .orElseThrow(convertException("Ошибка получения репозитория."));
if (!projectService.existsById(projectJson.getId())) { if (!projectService.existsById(projectJson.getId())) {
createNewPersons(List.of(projectJson)); createNewPersons(List.of(projectJson));
final Project newProject = conversionService.convert(projectJson, Project.class); final Project newProject = conversionService.convert(projectJson, Project.class);
projectService.create(newProject); return projectService.create(newProject);
} else { } else {
final Set<Long> projectId = singleton(projectJson.getId()); return projectService.getByIdOrThrow(projectJson.getId());
projectService.notification(true, projectId);
projectService.processing(true, projectId);
mergeRequestsService.notificationByProjectId(true, projectId);
} }
} }

View File

@ -32,7 +32,7 @@ public class SchedulerService {
private final MergeRequestsService mergeRequestsService; private final MergeRequestsService mergeRequestsService;
private final DiscussionService discussionService; private final DiscussionService discussionService;
@Scheduled(cron = "0 */2 * * * *") @Scheduled(cron = "0 */1 * * * *")
public void newMergeRequest() { public void newMergeRequest() {
log.info("Запуск процесса обновления данных c GitLab"); log.info("Запуск процесса обновления данных c GitLab");
if (!settingService.isFirstStart()) { if (!settingService.isFirstStart()) {

View File

@ -20,6 +20,9 @@
<column name="project_private_scan" type="boolean" defaultValue="false"> <column name="project_private_scan" type="boolean" defaultValue="false">
<constraints nullable="false"/> <constraints nullable="false"/>
</column> </column>
<column name="discussion_notify_level" type="varchar(64)" defaultValue="NOTIFY_WITH_CONTEXT">
<constraints nullable="false"/>
</column>
</createTable> </createTable>
</changeSet> </changeSet>

View File

@ -9,8 +9,12 @@ import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
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.haiti.utils.Checker.checkNotNull; 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.checkNotEmpty;
import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; import static dev.struchkov.haiti.utils.Strings.escapeMarkdown;
@Component @Component
@ -18,20 +22,26 @@ public class DiscussionNewNotifyGenerator implements NotifyBoxAnswerGenerator<Di
@Override @Override
public BoxAnswer generate(DiscussionNewNotify notify) { public BoxAnswer generate(DiscussionNewNotify notify) {
final StringBuilder builder = new StringBuilder(Icons.TASK).append(" [New discussion](").append(notify.getUrl()).append(")") final StringBuilder builder = new StringBuilder(Icons.TASK).append(" *New Thread in your MR*")
.append(Icons.HR) .append(Icons.HR)
.append(escapeMarkdown(notify.getMrName())) .append(Icons.link(escapeMarkdown(notify.getMrName()), notify.getUrl()))
.append(Icons.HR) .append(Icons.HR)
.append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessageTask())); .append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessageTask()));
final List<Pair<String, String>> notes = notify.getNotes(); final List<Pair<String, String>> notes = notify.getNotes();
if (checkNotNull(notes)) { if (checkNotEmpty(notes)) {
builder.append("\n-- -- -- -- comments -- -- -- --\n") builder.append("\n-- -- -- -- comments -- -- -- --\n")
.append(convertNotes(notes)); .append(convertNotes(notes));
} }
final String notifyMessage = builder.toString(); final String notifyMessage = builder.toString();
return boxAnswer(notifyMessage); return boxAnswer(
notifyMessage,
inlineKeyBoard(
simpleButton(Icons.VIEW, DELETE_MESSAGE),
urlButton(Icons.LINK, notify.getUrl())
)
);
} }
private String convertNotes(List<Pair<String, String>> notes) { private String convertNotes(List<Pair<String, String>> notes) {

View File

@ -14,10 +14,12 @@ public class NewCommentNotifyGenerator implements NotifyBoxAnswerGenerator<NewCo
@Override @Override
public BoxAnswer generate(NewCommentNotify notify) { public BoxAnswer generate(NewCommentNotify notify) {
final StringBuilder builder = new StringBuilder(Icons.COMMENT).append(" [New answer in discussion](").append(notify.getUrl()).append(")"); final StringBuilder builder = new StringBuilder(Icons.COMMENT).append(" *New answer in Thread*")
.append(Icons.HR)
.append(escapeMarkdown(notify.getMergeRequestName()));
if (checkNotNull(notify.getDiscussionMessage())) { if (checkNotNull(notify.getDiscussionMessage())) {
builder.append("\n-- -- discussion 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()));
} }
@ -26,8 +28,10 @@ public class NewCommentNotifyGenerator implements NotifyBoxAnswerGenerator<NewCo
.append("*").append(notify.getPreviousAuthor()).append("*: ").append(escapeMarkdown(notify.getPreviousMessage())); .append("*").append(notify.getPreviousAuthor()).append("*: ").append(escapeMarkdown(notify.getPreviousMessage()));
} }
builder.append("\n-- -- -- --- new answer --- -- -- --\n") if (checkNotNull(notify.getMessage())) {
.append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessage())); builder.append("\n-- -- -- --- new answer --- -- -- --\n")
.append("*").append(notify.getAuthorName()).append("*: ").append(escapeMarkdown(notify.getMessage()));
}
final String messageNotify = builder.toString(); final String messageNotify = builder.toString();
return boxAnswer(messageNotify); return boxAnswer(messageNotify);

View File

@ -2,9 +2,11 @@ package dev.struchkov.bot.gitlab.telegram.unit;
import dev.struchkov.bot.gitlab.context.domain.PersonInformation; import dev.struchkov.bot.gitlab.context.domain.PersonInformation;
import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest; import dev.struchkov.bot.gitlab.context.domain.entity.MergeRequest;
import dev.struchkov.bot.gitlab.context.domain.entity.Project;
import dev.struchkov.bot.gitlab.context.service.AppSettingService; import dev.struchkov.bot.gitlab.context.service.AppSettingService;
import dev.struchkov.bot.gitlab.context.service.MergeRequestsService; import dev.struchkov.bot.gitlab.context.service.MergeRequestsService;
import dev.struchkov.bot.gitlab.context.service.NoteService; import dev.struchkov.bot.gitlab.context.service.NoteService;
import dev.struchkov.bot.gitlab.context.service.ProjectService;
import dev.struchkov.bot.gitlab.context.utils.Icons; import dev.struchkov.bot.gitlab.context.utils.Icons;
import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty; import dev.struchkov.bot.gitlab.core.config.properties.GitlabProperty;
import dev.struchkov.bot.gitlab.core.service.parser.ProjectParser; import dev.struchkov.bot.gitlab.core.service.parser.ProjectParser;
@ -17,14 +19,13 @@ import dev.struchkov.godfather.simple.core.unit.MainUnit;
import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.main.core.util.Attachments; 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 dev.struchkov.haiti.utils.Checker;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.List; import java.util.List;
import java.util.concurrent.ScheduledExecutorService; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.ACCESS_ERROR; import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.ACCESS_ERROR;
@ -41,6 +42,7 @@ import static dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoard
import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard; import static dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard.inlineKeyBoard;
import static dev.struchkov.godfather.telegram.simple.core.util.TriggerChecks.clickButtonRaw; import static dev.struchkov.godfather.telegram.simple.core.util.TriggerChecks.clickButtonRaw;
import static dev.struchkov.godfather.telegram.simple.core.util.TriggerChecks.isLinks; import static dev.struchkov.godfather.telegram.simple.core.util.TriggerChecks.isLinks;
import static java.util.Collections.singleton;
/** /**
* // TODO: 16.01.2021 Добавить описание. * // TODO: 16.01.2021 Добавить описание.
@ -51,15 +53,16 @@ import static dev.struchkov.godfather.telegram.simple.core.util.TriggerChecks.is
@RequiredArgsConstructor @RequiredArgsConstructor
public class MenuConfig { public class MenuConfig {
private final TelegramSending sending;
private final ProjectParser projectParser;
private final GitlabProperty gitlabProperty; private final GitlabProperty gitlabProperty;
private final PersonInformation personInformation; private final PersonInformation personInformation;
private final ProjectParser projectParser;
private final ProjectService projectService;
private final NoteService noteService; private final NoteService noteService;
private final MergeRequestsService mergeRequestsService; private final MergeRequestsService mergeRequestsService;
private final AppSettingService settingService; private final AppSettingService settingService;
private final ScheduledExecutorService scheduledExecutorService;
@Unit(value = ACCESS_ERROR, main = true) @Unit(value = ACCESS_ERROR, main = true)
public AnswerText<Mail> accessError() { public AnswerText<Mail> accessError() {
@ -134,7 +137,11 @@ public class MenuConfig {
final String projectUrl = gitlabProperty.getProjectAddUrl() + link.getUrl().replace(gitlabProperty.getBaseUrl(), "") final String projectUrl = gitlabProperty.getProjectAddUrl() + link.getUrl().replace(gitlabProperty.getBaseUrl(), "")
.substring(1) .substring(1)
.replace("/", "%2F"); .replace("/", "%2F");
projectParser.parseByUrl(projectUrl); final Project project = projectParser.parseByUrl(projectUrl);
final Set<Long> projectId = singleton(project.getId());
projectService.notification(true, projectId);
projectService.processing(true, projectId);
mergeRequestsService.notificationByProjectId(true, projectId);
} }
return boxAnswer("\uD83D\uDC4D Projects added successfully!"); return boxAnswer("\uD83D\uDC4D Projects added successfully!");
}) })

View File

@ -5,20 +5,19 @@ import dev.struchkov.bot.gitlab.context.domain.entity.Note;
import dev.struchkov.bot.gitlab.context.service.AppSettingService; 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.NoteService; import dev.struchkov.bot.gitlab.context.service.NoteService;
import dev.struchkov.godfather.main.domain.BoxAnswer;
import dev.struchkov.godfather.main.domain.annotation.Unit; 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.main.domain.content.Mail;
import dev.struchkov.godfather.simple.core.unit.AnswerText; import dev.struchkov.godfather.simple.core.unit.AnswerText;
import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; 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.main.core.util.Attachments;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending;
import dev.struchkov.haiti.utils.Checker; import dev.struchkov.haiti.utils.Checker;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.List; import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -35,21 +34,24 @@ public class AnswerNoteUnit {
private final AppSettingService settingService; private final AppSettingService settingService;
private final NoteService noteService; private final NoteService noteService;
private final DiscussionService discussionService; private final DiscussionService discussionService;
private final ScheduledExecutorService scheduledExecutorService;
private final TelegramSending telegramSending;
@Unit(value = ANSWER_NOTE, main = true) //TODO [07.02.2023|uPagge]: Можно возвращать ссылку на ответ
@Unit(value = ANSWER_NOTE, global = true)
public AnswerText<Mail> answerNote() { public AnswerText<Mail> answerNote() {
return AnswerText.<Mail>builder() return AnswerText.<Mail>builder()
.triggerCheck( .triggerCheck(
mail -> { mail -> {
final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId()); final List<Mail> forwardMails = mail.getForwardMail();
if (isAccess) { if (Checker.checkNotNull(forwardMails) && forwardMails.size() == 1) {
final boolean isFirstStart = settingService.isFirstStart(); final Mail forwardMail = forwardMails.get(0);
if (!isFirstStart) { final boolean isLink = Attachments.findFirstLink(forwardMail.getAttachments())
final List<Mail> forwardMails = mail.getForwardMail(); .isPresent();
if (Checker.checkNotNull(forwardMails) && forwardMails.size() == 1) { if (isLink) {
final Mail forwardMail = forwardMails.get(0); final boolean isAccess = personInformation.getTelegramId().equals(mail.getPersonId());
return Attachments.findFirstLink(forwardMail.getAttachments()).isPresent(); final boolean firstStart = settingService.isFirstStart();
} return isAccess && !firstStart;
} }
} }
return false; return false;
@ -57,20 +59,19 @@ public class AnswerNoteUnit {
) )
.answer( .answer(
mail -> { mail -> {
final List<Attachment> attachments = mail.getForwardMail().get(0).getAttachments(); final String noteUrl = Attachments.findFirstLink(mail.getForwardMail().get(0).getAttachments())
for (Attachment attachment : attachments) { .map(LinkAttachment::getUrl)
if (TelegramAttachmentType.LINK.name().equals(attachment.getType())) { .orElseThrow();
final String url = ((LinkAttachment) attachment).getUrl(); final Matcher matcher = NOTE_LINK.matcher(noteUrl);
final Matcher matcher = NOTE_LINK.matcher(url); if (matcher.find()) {
if (matcher.find()) { final String noteText = noteUrl.substring(matcher.start(), matcher.end());
final String noteText = url.substring(matcher.start(), matcher.end()); final Long noteId = Long.valueOf(noteText.replace("#note_", ""));
final Long noteId = Long.valueOf(noteText.replaceAll("#note_", "")); final Note note = noteService.getByIdOrThrow(noteId);
final Note note = noteService.getByIdOrThrow(noteId); final String discussionId = note.getDiscussion().getId();
final String discussionId = note.getDiscussion().getId(); discussionService.answer(discussionId, MessageFormat.format("@{0}, {1}", note.getAuthor().getUserName(), mail.getText()));
discussionService.answer(discussionId, MessageFormat.format("@{0}, {1}", note.getAuthor().getUserName(), mail.getText())); return boxAnswer(
return BoxAnswer.builder().build(); "\uD83D\uDC4D Response sent successfully"
} );
}
} }
return boxAnswer("Error"); return boxAnswer("Error");
} }