Экранирование markdown
This commit is contained in:
parent
464e34c1fd
commit
232624ec8d
@ -1,15 +1,22 @@
|
||||
package org.sadtech.bot.vcs.core.domain.notify;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import org.sadtech.bot.vcs.core.domain.EntityType;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public abstract class Notify {
|
||||
|
||||
public static final Set<Character> FORBIDDEN_SYMBOLS = Stream.of(
|
||||
'\\', '+', '`', '[', ']', '\"', '~', '*', '#', '=', '-', '_', '>', '<', '!'
|
||||
).collect(Collectors.toSet());
|
||||
|
||||
protected EntityType entityType;
|
||||
protected Set<String> recipients;
|
||||
|
||||
@ -20,4 +27,16 @@ public abstract class Notify {
|
||||
|
||||
public abstract String generateMessage();
|
||||
|
||||
public static String escapeMarkdown(@NonNull String s) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
if (FORBIDDEN_SYMBOLS.contains(c)) {
|
||||
sb.append('\\');
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class SimpleTextNotify extends Notify {
|
||||
|
||||
@Override
|
||||
public String generateMessage() {
|
||||
return message;
|
||||
return escapeMarkdown(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -41,9 +41,13 @@ public class AnswerCommentNotify extends Notify {
|
||||
.collect(Collectors.joining("\n\n"));
|
||||
return MessageFormat.format(
|
||||
"{0} *Новые ответы на ваш комментарий* | [ПР]({1}){2}" +
|
||||
"{3}{4}" +
|
||||
"{5}",
|
||||
Smile.BELL, url, Smile.HR, youMessage.substring(0, Math.min(youMessage.length(), 180)), Smile.HR, answerText
|
||||
"{3}{2}" +
|
||||
"{4}",
|
||||
Smile.BELL,
|
||||
url,
|
||||
Smile.HR,
|
||||
escapeMarkdown(youMessage.substring(0, Math.min(youMessage.length(), 180))),
|
||||
escapeMarkdown(answerText)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ public class CommentNotify extends Notify {
|
||||
return MessageFormat.format(
|
||||
"{0} *Новое упоминание* | [ПР]({1}){2}" +
|
||||
"{3}: {4}",
|
||||
Smile.BELL, url, Smile.HR, authorName, message.replaceAll("@[\\w]+", "")
|
||||
Smile.BELL, url, Smile.HR, authorName, escapeMarkdown(message.replaceAll("@[\\w]+", ""))
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -32,9 +32,9 @@ public class NewPrNotify extends PrNotify {
|
||||
return MessageFormat.format(
|
||||
"{0} *Новый Pull Request*{1}" +
|
||||
"[{2}]({3})" +
|
||||
"{4}{5}{6}: {7}\n\n",
|
||||
Smile.FUN, Smile.HR, title, url, Smile.HR,
|
||||
(description != null && !"".equals(description)) ? description + Smile.HR : "",
|
||||
"{1}{4}{5}: {6}\n\n",
|
||||
Smile.FUN, Smile.HR, title, url,
|
||||
(description != null && !"".equals(description)) ? escapeMarkdown(description) + Smile.HR : "",
|
||||
Smile.AUTHOR, author
|
||||
);
|
||||
}
|
||||
|
@ -32,9 +32,9 @@ public class StatusPrNotify extends PrNotify {
|
||||
public String generateMessage() {
|
||||
return MessageFormat.format(
|
||||
"{0} *Изменился статус вашего ПР*{1}" +
|
||||
"[{2}]({3}){4}" +
|
||||
"{5} -> {6}\n\n",
|
||||
Smile.PEN, Smile.HR, title, url, Smile.HR, oldStatus.name(), newStatus.name()
|
||||
"[{2}]({3}){1}" +
|
||||
"{4} -> {5}\n\n",
|
||||
Smile.PEN, Smile.HR, title, url, oldStatus.name(), newStatus.name()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class TaskCloseNotify extends TaskNotify {
|
||||
return MessageFormat.format(
|
||||
"{0} *Задача выполнена* | [ПР]({1}){2}" +
|
||||
"{3}: {4}",
|
||||
Smile.TASK, url, Smile.HR, authorName, messageTask
|
||||
Smile.TASK, url, Smile.HR, authorName, escapeMarkdown(messageTask)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ public class TaskNewNotify extends TaskNotify {
|
||||
return MessageFormat.format(
|
||||
"{0} *Назначена новая задача* | [ПР]({1}){2}" +
|
||||
"{3}: {4}",
|
||||
Smile.TASK, url, Smile.HR, authorName, messageTask
|
||||
Smile.TASK, url, Smile.HR, authorName, escapeMarkdown(messageTask)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -134,12 +134,12 @@ public class CommentServiceImpl extends AbstractSimpleManagerService<Comment, Lo
|
||||
final Set<Long> newAnswerIds = newComment.getAnswers();
|
||||
if (!oldAnswerIds.equals(newAnswerIds)) {
|
||||
final Set<Long> existsNewAnswersIds = commentRepository.existsById(newAnswerIds);
|
||||
if (!existsNewAnswersIds.isEmpty()) {
|
||||
final List<Comment> newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream()
|
||||
.filter(comment -> !oldAnswerIds.contains(comment.getId()))
|
||||
.collect(Collectors.toList());
|
||||
oldComment.getAnswers().clear();
|
||||
oldComment.setAnswers(existsNewAnswersIds);
|
||||
if (!newAnswers.isEmpty()) {
|
||||
notifyService.send(
|
||||
AnswerCommentNotify.builder()
|
||||
.recipients(Collections.singleton(newComment.getAuthor()))
|
||||
|
@ -126,12 +126,14 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
|
||||
final Set<Long> newAnswerIds = task.getAnswers();
|
||||
if (!oldAnswerIds.equals(newAnswerIds)) {
|
||||
final Set<Long> existsNewAnswersIds = commentService.existsById(newAnswerIds);
|
||||
if (!existsNewAnswersIds.isEmpty()) {
|
||||
final List<Comment> newAnswers = commentService.getAllById(existsNewAnswersIds).stream()
|
||||
.filter(comment -> !oldAnswerIds.contains(comment.getId()))
|
||||
.collect(Collectors.toList());
|
||||
oldTask.getAnswers().clear();
|
||||
oldTask.setAnswers(existsNewAnswersIds);
|
||||
if (!newAnswers.isEmpty()) {
|
||||
oldTask.getAnswers().clear();
|
||||
oldTask.setAnswers(existsNewAnswersIds);
|
||||
notifyService.send(
|
||||
AnswerCommentNotify.builder()
|
||||
.recipients(Collections.singleton(oldTask.getAuthor()))
|
||||
|
@ -36,7 +36,7 @@ public class TeamcityBuildNotify extends Notify {
|
||||
Smile.BUILD,
|
||||
buildShort.getId(),
|
||||
buildShort.getNumber(),
|
||||
buildShort.getProjectId(),
|
||||
escapeMarkdown(buildShort.getProjectId()),
|
||||
Smile.HR,
|
||||
buildShort.getBuildTypeId(),
|
||||
buildShort.getUrl(),
|
||||
|
Loading…
Reference in New Issue
Block a user