Skip to content

Commit

Permalink
линки на пропущенные комментарии при удалении
Browse files Browse the repository at this point in the history
+ фикс на опцию "не блокировать"
  • Loading branch information
maxcom committed Aug 17, 2024
1 parent 7425299 commit 3b7c6ce
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 103 deletions.
92 changes: 32 additions & 60 deletions src/main/java/ru/org/linux/comment/CommentDeleteService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2022 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -168,7 +168,7 @@ private List<Integer> deleteReplys(Comment root, String rootReason, List<Comment
List<DeleteInfoDao.InsertDeleteInfo> deleteInfos = new ArrayList<>(replys.size());

for (CommentAndDepth cur : replys) {
Comment child = cur.getComment();
Comment child = cur.comment();

DeleteInfoDao.InsertDeleteInfo info = cur.deleteInfo(score, user);

Expand Down Expand Up @@ -214,11 +214,7 @@ public DeleteCommentResult deleteCommentsByIPAddress(
{
List<Integer> deletedTopics = topicService.deleteByIPAddress(ip, timeDelta, moderator, reason);

Map<Integer, String> deleteInfo = new HashMap<>();

for (int msgid : deletedTopics) {
deleteInfo.put(msgid, "Топик " + msgid + " удален");
}
List<Integer> skippedComments = new ArrayList<>();

// Удаляем комментарии если на них нет ответа
List<Integer> commentIds = commentDao.getCommentsByIPAddressForUpdate(ip, timeDelta);
Expand All @@ -229,12 +225,9 @@ public DeleteCommentResult deleteCommentsByIPAddress(
if (commentDao.getRepliesCount(msgid) == 0) {
if (doDeleteComment(msgid, reason, moderator)) {
deletedCommentIds.add(msgid);
deleteInfo.put(msgid, "Комментарий " + msgid + " удален");
} else {
deleteInfo.put(msgid, "Комментарий " + msgid + " уже был удален");
}
} else {
deleteInfo.put(msgid, "Комментарий " + msgid + " пропущен");
skippedComments.add(msgid);
}
}

Expand All @@ -244,7 +237,7 @@ public DeleteCommentResult deleteCommentsByIPAddress(

userEventService.processCommentsDeleted(deletedCommentIds);

return new DeleteCommentResult(deletedTopics, deletedCommentIds, deleteInfo);
return new DeleteCommentResult(deletedTopics, deletedCommentIds, skippedComments);
}

/**
Expand All @@ -261,35 +254,25 @@ public DeleteCommentResult deleteAllCommentsAndBlock(User user, final User moder

List<Integer> deletedTopicIds = topicService.deleteAllByUser(user, moderator);

List<Integer> deletedCommentIds = deleteAllCommentsByUser(user, moderator);

return new DeleteCommentResult(deletedTopicIds, deletedCommentIds, null);
}

/**
* Массовое удаление комментариев пользователя со всеми ответами на комментарии.
*
* @param user пользователь для экзекуции
* @param moderator экзекутор-модератор
* @return список удаленных комментариев
*/
private List<Integer> deleteAllCommentsByUser(User user, final User moderator) {
final List<Integer> deletedCommentIds = new ArrayList<>();

// Удаляем все комментарии
List<Integer> commentIds = commentDao.getAllByUserForUpdate(user);
List<Integer> skippedComments = new ArrayList<>();

for (int msgid : commentIds) {
if (commentDao.getRepliesCount(msgid) == 0) {
doDeleteComment(msgid, "Блокировка пользователя с удалением сообщений", moderator);
commentDao.updateStatsAfterDelete(msgid, 1);
deletedCommentIds.add(msgid);
} else {
skippedComments.add(msgid);
}
}

userEventService.processCommentsDeleted(deletedCommentIds);

return deletedCommentIds;
return new DeleteCommentResult(deletedTopicIds, deletedCommentIds, skippedComments);
}

private static List<CommentAndDepth> getAllReplys(CommentNode node, int depth) {
Expand All @@ -303,46 +286,35 @@ private static List<CommentAndDepth> getAllReplys(CommentNode node, int depth) {
return replys;
}

private static class CommentAndDepth {
private final Comment comment;
private final int depth;

private CommentAndDepth(Comment comment, int depth) {
this.comment = comment;
this.depth = depth;
}

public Comment getComment() {
return comment;
}
private record CommentAndDepth(Comment comment, int depth) {

private DeleteInfoDao.InsertDeleteInfo deleteInfo(boolean score, User user) {
int bonus;
String reason;

if (score) {
switch (depth) {
case 0 -> {
reason = "7.1 Ответ на некорректное сообщение (авто, уровень 0)";
bonus = -2;
}
case 1 -> {
reason = "7.1 Ответ на некорректное сообщение (авто, уровень 1)";
bonus = -1;
}
default -> {
reason = "7.1 Ответ на некорректное сообщение (авто, уровень >1)";
bonus = 0;
int bonus;
String reason;

if (score) {
switch (depth) {
case 0 -> {
reason = "7.1 Ответ на некорректное сообщение (авто, уровень 0)";
bonus = -2;
}
case 1 -> {
reason = "7.1 Ответ на некорректное сообщение (авто, уровень 1)";
bonus = -1;
}
default -> {
reason = "7.1 Ответ на некорректное сообщение (авто, уровень >1)";
bonus = 0;
}
}
} else {
reason = "7.1 Ответ на некорректное сообщение (авто)";
bonus = 0;
}
} else {
reason = "7.1 Ответ на некорректное сообщение (авто)";
bonus = 0;
}

return new DeleteInfoDao.InsertDeleteInfo(comment.getId(), reason, bonus, user.getId());
return new DeleteInfoDao.InsertDeleteInfo(comment.getId(), reason, bonus, user.getId());
}
}
}

@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public void undeleteComment(Comment comment) {
Expand Down
25 changes: 6 additions & 19 deletions src/main/java/ru/org/linux/comment/DeleteCommentResult.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2015 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -16,29 +16,16 @@
package ru.org.linux.comment;

import java.util.List;
import java.util.Map;

/**
* Результат работы deleteCommentsByIPAddress
*/
public class DeleteCommentResult {
/**
* список id удаленных топиков
*/
private final List<Integer> deletedTopicIds;
/**
* список id удаленных комментариев
*/
private final List<Integer> deletedCommentIds;
/**
* хэш id удаляемого топика -> строка с результатом удален или пропущен
*/
private final Map<Integer, String> deleteInfo;
private final List<Integer> skippedComments;

DeleteCommentResult(List<Integer> deletedTopicIds, List<Integer> deletedCommentIds, Map<Integer, String> deleteInfo) {
DeleteCommentResult(List<Integer> deletedTopicIds, List<Integer> deletedCommentIds, List<Integer> skippedComments) {
this.deletedCommentIds = deletedCommentIds;
this.deletedTopicIds = deletedTopicIds;
this.deleteInfo = deleteInfo;
this.skippedComments = skippedComments;
}

public List<Integer> getDeletedTopicIds() {
Expand All @@ -49,7 +36,7 @@ public List<Integer> getDeletedCommentIds() {
return deletedCommentIds;
}

public Map<Integer, String> getDeleteInfo() {
return deleteInfo;
public List<Integer> getSkippedComments() {
return skippedComments;
}
}
12 changes: 7 additions & 5 deletions src/main/java/ru/org/linux/spring/DelIPController.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ public ModelAndView delIp(@RequestParam("reason") String reason,

User moderator = AuthUtil.getCurrentUser();

DeleteCommentResult deleteResult = commentDeleteService.deleteCommentsByIPAddress(ip, ts, moderator, reason);

params.put("topics", deleteResult.getDeletedTopicIds().size()); // кол-во удаленных топиков
params.put("deleted", deleteResult.getDeleteInfo());

if (banTime != null) {
Optional<OffsetDateTime> banTo = switch (banTime) {
case "hour" -> Optional.of(OffsetDateTime.now().plusHours(1));
Expand All @@ -96,6 +91,7 @@ public ModelAndView delIp(@RequestParam("reason") String reason,
case "3month" -> Optional.of(OffsetDateTime.now().plusMonths(3));
case "6month" -> Optional.of(OffsetDateTime.now().plusMonths(6));
case "unlim" -> Optional.empty();
case "remove" -> Optional.of(OffsetDateTime.now());
default -> throw new UserErrorException("Invalid count");
};

Expand All @@ -119,6 +115,12 @@ public ModelAndView delIp(@RequestParam("reason") String reason,
ipBlockDao.blockIP(ip, moderator.getId(), reason, banTo.orElse(null), allowPosting, captchaRequired);
}

DeleteCommentResult deleteResult = commentDeleteService.deleteCommentsByIPAddress(ip, ts, moderator, reason);

params.put("topics", deleteResult.getDeletedTopicIds().size());
params.put("comments", deleteResult.getDeletedCommentIds().size());
params.put("skipped", deleteResult.getSkippedComments());

for (int topicId : deleteResult.getDeletedTopicIds()) {
searchQueueSender.updateMessage(topicId, true);
}
Expand Down
20 changes: 11 additions & 9 deletions src/main/scala/ru/org/linux/user/UserModificationController.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2023 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -134,21 +134,23 @@ class UserModificationController(searchQueueSender: SearchQueueSender, userDao:
throw new UserErrorException("Пользователь уже блокирован")
}

val params = new mutable.HashMap[String, AnyRef]
params.put("message", "Удалено")
val deleteCommentResult = commentService.deleteAllCommentsAndBlock(user, moderator.user, reason)
val deleteResult = commentService.deleteAllCommentsAndBlock(user, moderator.user, reason)

logger.info(s"User ${user.getNick} blocked by ${moderator.user.getNick}")

params.put("bigMessage", s"Удалено комментариев: ${deleteCommentResult.getDeletedCommentIds.size}<br>Удалено тем: ${deleteCommentResult.getDeletedTopicIds.size}")

deleteCommentResult.getDeletedTopicIds.asScala.foreach { topicId =>
deleteResult.getDeletedTopicIds.asScala.foreach { topicId =>
searchQueueSender.updateMessage(topicId, true)
}

searchQueueSender.updateComment(deleteCommentResult.getDeletedCommentIds)
searchQueueSender.updateComment(deleteResult.getDeletedCommentIds)

val params = new mutable.HashMap[String, AnyRef]

params.put("topics", Integer.valueOf(deleteResult.getDeletedTopicIds.size))
params.put("comments", Integer.valueOf(deleteResult.getDeletedCommentIds.size))
params.put("skipped", deleteResult.getSkippedComments)

new ModelAndView("action-done", params.asJava)
return new ModelAndView("delip", params.asJava)
}

/**
Expand Down
14 changes: 4 additions & 10 deletions src/main/webapp/WEB-INF/jsp/delip.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%@ page import="java.util.Date" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%--
~ Copyright 1998-2015 Linux.org.ru
~ Copyright 1998-2024 Linux.org.ru
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
Expand All @@ -17,12 +17,6 @@
--%>

<jsp:include page="/WEB-INF/jsp/head.jsp"/>
<%
response.setDateHeader("Expires", new Date(new Date().getTime() - 20 * 3600 * 1000).getTime());
response.setDateHeader("Last-Modified", new Date(new Date().getTime() - 2 * 1000).getTime());
%>
<title>delip</title>
<link rel="parent" title="Linux.org.ru" href="/">
<jsp:include page="/WEB-INF/jsp/header.jsp"/>
Expand All @@ -33,15 +27,15 @@ ${message}

<br/>

Удалено тем: ${topics}
Удалено тем: ${topics}; удалено комментариев: ${comments}

<ul>

<c:forEach var="del" items="${deleted}">
<c:forEach var="del" items="${skipped}">

<li>

Удаляется #${del.key}: ${del.value}
Пропущен <a href="delete_comment.jsp?msgid=${del}">#${del}</a>

</li>

Expand Down

0 comments on commit 3b7c6ce

Please sign in to comment.