Map<String, Object> params = new HashMap<>();
final Topic message = messageDao.getById(msgid);
PreparedTopic preparedTopic = prepareService.prepareTopic(message, request.isSecure(), tmpl.getCurrentUser());
Group group = preparedTopic.getGroup();
User user = tmpl.getCurrentUser();
IPBlockDao.checkBlockIP(ipBlockInfo, errors, user);
boolean tagsEditable = permissionService.isTagsEditable(preparedTopic, user);
boolean editable = permissionService.isEditable(preparedTopic, user);
if (!editable && !tagsEditable) {
throw new AccessViolationException("это сообщение нельзя править");
}
params.put("message", message);
params.put("preparedMessage", preparedTopic);
params.put("group", group);
params.put("topicMenu", prepareService.getTopicMenu(
preparedTopic,
tmpl.getCurrentUser(),
request.isSecure(),
tmpl.getProf(),
true
));
params.put("groups", groupDao.getGroups(preparedTopic.getSection()));
if (editable) {
String title = request.getParameter("title");
if (title == null || title.trim().isEmpty()) {
throw new BadInputException("заголовок сообщения не может быть пустым");
}
}
boolean preview = request.getParameter("preview") != null;
if (preview) {
params.put("info", "Предпросмотр");
}
boolean publish = request.getParameter("publish") != null;
List<EditHistoryDto> editInfoList = editHistoryService.getEditInfo(message.getId(), EditHistoryObjectTypeEnum.TOPIC);
if (!editInfoList.isEmpty()) {
EditHistoryDto editHistoryDto = editInfoList.get(0);
params.put("editInfo", editHistoryDto);
if (lastEdit == null || editHistoryDto.getEditdate().getTime()!=lastEdit) {
errors.reject(null, "Сообщение было отредактировано независимо");
}
}
boolean commit = request.getParameter("commit") != null;
if (commit) {
user.checkCommit();
if (message.isCommited()) {
throw new BadInputException("сообщение уже подтверждено");
}
}
params.put("commit", !message.isCommited() && preparedTopic.getSection().isPremoderated() && user.isModerator());
Topic newMsg = new Topic(group, message, form, publish);
boolean modified = false;
if (!message.getTitle().equals(newMsg.getTitle())) {
modified = true;
}
if (form.getMsg()!=null) {
String oldText = msgbaseDao.getMessageText(message.getId()).getText();
if (!oldText.equals(form.getMsg())) {
modified = true;
}
}
if (message.getLinktext() == null) {
if (newMsg.getLinktext() != null) {
modified = true;
}
} else if (!message.getLinktext().equals(newMsg.getLinktext())) {
modified = true;
}
if (group.isLinksAllowed()) {
if (message.getUrl() == null) {
if (newMsg.getUrl() != null) {
modified = true;
}
} else if (!message.getUrl().equals(newMsg.getUrl())) {
modified = true;
}
}
if (!editable && modified) {
throw new AccessViolationException("нельзя править это сообщение, только теги");
}
if (form.getMinor()!=null && !tmpl.isModeratorSession()) {
throw new AccessViolationException("вы не можете менять статус новости");
}
List<String> newTags = null;
if (form.getTags()!=null) {
newTags = TagName.parseAndSanitizeTags(form.getTags());
}
if (changeGroupId != null) {
if (message.getGroupId() != changeGroupId) {
Group changeGroup = groupDao.getGroup(changeGroupId);
int section = message.getSectionId();
if (changeGroup.getSectionId() != section) {
throw new AccessViolationException("Can't move topics between sections");
}
}
}