diff options
author | Vitaly Takmazov | 2021-10-30 20:11:18 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2021-10-30 20:11:18 +0300 |
commit | e71e88549be3205107ddd69e43fcf6bbc15e82fd (patch) | |
tree | f652469576dee0622e84261fbd05ef040d07ee5f /src/main | |
parent | 582aae8c0cf4afa1583ad7351320f33c1a8de57f (diff) |
Move deleted messages to archive user
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/juick/config/WebConfig.java | 6 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 51 | ||||
-rw-r--r-- | src/main/resources/data-h2.sql | 1 |
3 files changed, 36 insertions, 22 deletions
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<Message> { @Override @@ -878,25 +880,26 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ Optional<Message> 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) diff --git a/src/main/resources/data-h2.sql b/src/main/resources/data-h2.sql index e279e524..bb1203a4 100644 --- a/src/main/resources/data-h2.sql +++ b/src/main/resources/data-h2.sql @@ -1,5 +1,6 @@ INSERT INTO users(id, nick, passw) VALUES(0, 'Anonymous', 'password'); INSERT INTO users(id, nick, passw) VALUES(2, 'juick', 'password'); +INSERT INTO users(id, nick, passw) VALUES(5, 'archive', 'password'); INSERT INTO tags(tag_id, name) VALUES(2, 'juick'); INSERT INTO reactions (like_id, description) VALUES (1, 'like'); INSERT INTO reactions (like_id, description) VALUES (2, 'love'); |