Экранирование 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; package org.sadtech.bot.vcs.core.domain.notify;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
import org.sadtech.bot.vcs.core.domain.EntityType; import org.sadtech.bot.vcs.core.domain.EntityType;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Getter @Getter
@Setter @Setter
public abstract class Notify { public abstract class Notify {
public static final Set<Character> FORBIDDEN_SYMBOLS = Stream.of(
'\\', '+', '`', '[', ']', '\"', '~', '*', '#', '=', '-', '_', '>', '<', '!'
).collect(Collectors.toSet());
protected EntityType entityType; protected EntityType entityType;
protected Set<String> recipients; protected Set<String> recipients;
@ -20,4 +27,16 @@ public abstract class Notify {
public abstract String generateMessage(); 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 @Override
public String generateMessage() { public String generateMessage() {
return message; return escapeMarkdown(message);
} }
} }

View File

@ -41,9 +41,13 @@ public class AnswerCommentNotify extends Notify {
.collect(Collectors.joining("\n\n")); .collect(Collectors.joining("\n\n"));
return MessageFormat.format( return MessageFormat.format(
"{0} *Новые ответы на ваш комментарий* | [ПР]({1}){2}" + "{0} *Новые ответы на ваш комментарий* | [ПР]({1}){2}" +
"{3}{4}" + "{3}{2}" +
"{5}", "{4}",
Smile.BELL, url, Smile.HR, youMessage.substring(0, Math.min(youMessage.length(), 180)), Smile.HR, answerText 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( return MessageFormat.format(
"{0} *Новое упоминание* | [ПР]({1}){2}" + "{0} *Новое упоминание* | [ПР]({1}){2}" +
"{3}: {4}", "{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( return MessageFormat.format(
"{0} *Новый Pull Request*{1}" + "{0} *Новый Pull Request*{1}" +
"[{2}]({3})" + "[{2}]({3})" +
"{4}{5}{6}: {7}\n\n", "{1}{4}{5}: {6}\n\n",
Smile.FUN, Smile.HR, title, url, Smile.HR, Smile.FUN, Smile.HR, title, url,
(description != null && !"".equals(description)) ? description + Smile.HR : "", (description != null && !"".equals(description)) ? escapeMarkdown(description) + Smile.HR : "",
Smile.AUTHOR, author Smile.AUTHOR, author
); );
} }

View File

@ -32,9 +32,9 @@ public class StatusPrNotify extends PrNotify {
public String generateMessage() { public String generateMessage() {
return MessageFormat.format( return MessageFormat.format(
"{0} *Изменился статус вашего ПР*{1}" + "{0} *Изменился статус вашего ПР*{1}" +
"[{2}]({3}){4}" + "[{2}]({3}){1}" +
"{5} -> {6}\n\n", "{4} -> {5}\n\n",
Smile.PEN, Smile.HR, title, url, Smile.HR, oldStatus.name(), newStatus.name() Smile.PEN, Smile.HR, title, url, oldStatus.name(), newStatus.name()
); );
} }

View File

@ -28,7 +28,7 @@ public class TaskCloseNotify extends TaskNotify {
return MessageFormat.format( return MessageFormat.format(
"{0} *Задача выполнена* | [ПР]({1}){2}" + "{0} *Задача выполнена* | [ПР]({1}){2}" +
"{3}: {4}", "{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( return MessageFormat.format(
"{0} *Назначена новая задача* | [ПР]({1}){2}" + "{0} *Назначена новая задача* | [ПР]({1}){2}" +
"{3}: {4}", "{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(); final Set<Long> newAnswerIds = newComment.getAnswers();
if (!oldAnswerIds.equals(newAnswerIds)) { if (!oldAnswerIds.equals(newAnswerIds)) {
final Set<Long> existsNewAnswersIds = commentRepository.existsById(newAnswerIds); final Set<Long> existsNewAnswersIds = commentRepository.existsById(newAnswerIds);
if (!existsNewAnswersIds.isEmpty()) {
final List<Comment> newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream() final List<Comment> newAnswers = commentRepository.findAllById(existsNewAnswersIds).stream()
.filter(comment -> !oldAnswerIds.contains(comment.getId())) .filter(comment -> !oldAnswerIds.contains(comment.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
oldComment.getAnswers().clear(); oldComment.getAnswers().clear();
oldComment.setAnswers(existsNewAnswersIds); oldComment.setAnswers(existsNewAnswersIds);
if (!newAnswers.isEmpty()) {
notifyService.send( notifyService.send(
AnswerCommentNotify.builder() AnswerCommentNotify.builder()
.recipients(Collections.singleton(newComment.getAuthor())) .recipients(Collections.singleton(newComment.getAuthor()))

View File

@ -126,12 +126,14 @@ public class TaskServiceImpl extends AbstractSimpleManagerService<Task, Long> im
final Set<Long> newAnswerIds = task.getAnswers(); final Set<Long> newAnswerIds = task.getAnswers();
if (!oldAnswerIds.equals(newAnswerIds)) { if (!oldAnswerIds.equals(newAnswerIds)) {
final Set<Long> existsNewAnswersIds = commentService.existsById(newAnswerIds); final Set<Long> existsNewAnswersIds = commentService.existsById(newAnswerIds);
if (!existsNewAnswersIds.isEmpty()) {
final List<Comment> newAnswers = commentService.getAllById(existsNewAnswersIds).stream() final List<Comment> newAnswers = commentService.getAllById(existsNewAnswersIds).stream()
.filter(comment -> !oldAnswerIds.contains(comment.getId())) .filter(comment -> !oldAnswerIds.contains(comment.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
oldTask.getAnswers().clear(); oldTask.getAnswers().clear();
oldTask.setAnswers(existsNewAnswersIds); oldTask.setAnswers(existsNewAnswersIds);
if (!newAnswers.isEmpty()) {
oldTask.getAnswers().clear();
oldTask.setAnswers(existsNewAnswersIds);
notifyService.send( notifyService.send(
AnswerCommentNotify.builder() AnswerCommentNotify.builder()
.recipients(Collections.singleton(oldTask.getAuthor())) .recipients(Collections.singleton(oldTask.getAuthor()))

View File

@ -36,7 +36,7 @@ public class TeamcityBuildNotify extends Notify {
Smile.BUILD, Smile.BUILD,
buildShort.getId(), buildShort.getId(),
buildShort.getNumber(), buildShort.getNumber(),
buildShort.getProjectId(), escapeMarkdown(buildShort.getProjectId()),
Smile.HR, Smile.HR,
buildShort.getBuildTypeId(), buildShort.getBuildTypeId(),
buildShort.getUrl(), buildShort.getUrl(),