Экранирование markdown

This commit is contained in:
upagge 2020-10-11 10:45:43 +03:00
parent 464e34c1fd
commit 232624ec8d
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
11 changed files with 49 additions and 24 deletions

View File

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

View File

@ -24,7 +24,7 @@ public class SimpleTextNotify extends Notify {
@Override
public String generateMessage() {
return message;
return escapeMarkdown(message);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -126,10 +126,12 @@ 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());
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(

View File

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