From e71e88549be3205107ddd69e43fcf6bbc15e82fd Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 30 Oct 2021 20:11:18 +0300 Subject: Move deleted messages to archive user --- src/main/java/com/juick/config/WebConfig.java | 6 +++ .../com/juick/service/MessagesServiceImpl.java | 51 ++++++++++++---------- 2 files changed, 35 insertions(+), 22 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/config/WebConfig.java b/src/main/java/com/juick/config/WebConfig.java index 94f30bd5..fcd9a46b 100644 --- a/src/main/java/com/juick/config/WebConfig.java +++ b/src/main/java/com/juick/config/WebConfig.java @@ -193,9 +193,15 @@ public class WebConfig implements WebMvcConfigurer { private UserService userService; @Value("${service_user:juick}") private String serviceUsername; + @Value("${archive_user:archive}") + private String archiveUsername; @Bean public User serviceUser() { return userService.getUserByName(serviceUsername); } + @Bean + public User archiveUser() { + return userService.getUserByName(archiveUsername); + } } diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 3e197bf1..7e366a01 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -68,6 +68,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ private WebApp webApp; @Value("${photos_url:https://i.juick.com/}") private String baseImagesUrl; + @Inject + private User archiveUser; private class MessageMapper implements RowMapper { @Override @@ -878,25 +880,26 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ Optional message = getMessage(mid); if (message.isPresent()) { Instant ts = message.get().getUpdatedAt(); + SqlParameterSource parameters = new MapSqlParameterSource().addValue("mid", mid).addValue("uid", uid) + .addValue("archive_uid", archiveUser.getUid()); if (ts.compareTo(messageDeletingWindow) >= 0) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("mid", mid).addValue("uid", - uid); - - if (getNamedParameterJdbcTemplate().update( - "DELETE FROM messages WHERE message_id = :mid AND user_id = :uid", - sqlParameterSource) > 0) { + if (getNamedParameterJdbcTemplate() + .update("DELETE FROM messages WHERE message_id = :mid AND user_id = :uid", parameters) > 0) { getNamedParameterJdbcTemplate().update("DELETE FROM messages_txt WHERE message_id = :mid", - sqlParameterSource); - getNamedParameterJdbcTemplate().update("DELETE FROM replies WHERE message_id = :mid", - sqlParameterSource); + parameters); + getNamedParameterJdbcTemplate().update("DELETE FROM replies WHERE message_id = :mid", parameters); getNamedParameterJdbcTemplate().update("DELETE FROM subscr_messages WHERE message_id = :mid", - sqlParameterSource); + parameters); getNamedParameterJdbcTemplate().update("DELETE FROM messages_tags WHERE message_id = :mid", - sqlParameterSource); + parameters); return true; } + } else { + return getNamedParameterJdbcTemplate().update( + "UPDATE messages SET user_id=:archive_uid WHERE message_id = :mid AND user_id = :uid", + parameters) > 0; } } return false; @@ -910,26 +913,31 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ Message reply = getReply(mid, rid); if (reply != null) { Instant ts = reply.getUpdatedAt(); + User author = getMessageAuthor(mid); + SqlParameterSource parameters = new MapSqlParameterSource().addValue("mid", mid).addValue("uid", uid) + .addValue("rid", rid).addValue("archive_uid", archiveUser.getUid()); if (ts.compareTo(messageDeletingWindow) >= 0) { - User author = getMessageAuthor(mid); - SqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("mid", mid) - .addValue("uid", uid).addValue("rid", rid); boolean result; + // allow to delete author replies and replies to author if (author.getUid() == uid) { - result = getNamedParameterJdbcTemplate().update( - "DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid", - sqlParameterSource) > 0; + result = getNamedParameterJdbcTemplate() + .update("DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid", parameters) > 0; } else { result = getNamedParameterJdbcTemplate().update( "DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid AND user_id=:uid", - sqlParameterSource) > 0; + parameters) > 0; } if (result) { getNamedParameterJdbcTemplate() - .update("UPDATE messages SET replies=replies-1 WHERE message_id=:mid", sqlParameterSource); + .update("UPDATE messages SET replies=replies-1 WHERE message_id=:mid", parameters); updateRepliesBy(mid); return true; } + } else { + // only archive author replies + return getNamedParameterJdbcTemplate().update( + "UPDATE replies SET user_id=:archive_uid WHERE message_id=:mid AND reply_id=:rid AND user_id=:uid", + parameters) > 0; } } return false; @@ -1060,9 +1068,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional @Override public boolean deleteReply(URI userUri, URI replyUri) { - return jdbcTemplate.update( - "DELETE FROM replies WHERE user_uri=? AND reply_uri=?", - userUri.toASCIIString(), replyUri.toASCIIString()) > 0; + return jdbcTemplate.update("DELETE FROM replies WHERE user_uri=? AND reply_uri=?", userUri.toASCIIString(), + replyUri.toASCIIString()) > 0; } @Transactional(readOnly = true) -- cgit v1.2.3