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 | |
parent | 582aae8c0cf4afa1583ad7351320f33c1a8de57f (diff) |
Move deleted messages to archive user
Diffstat (limited to 'src')
-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 | ||||
-rw-r--r-- | src/test/java/com/juick/server/tests/ServerTests.java | 17 |
4 files changed, 48 insertions, 27 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'); diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index 5bb10ae9..9ed4b727 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -360,6 +360,8 @@ public class ServerTests { private Users usersController; @Inject private User serviceUser; + @Inject + private User archiveUser; private static User ugnich, freefd; static String ugnichName, ugnichPassword, freefdName, freefdPassword; @@ -1401,16 +1403,21 @@ public class ServerTests { assertThat(editedComment.getText(), is("HEY, JOE")); assertThat(editedComment.getUpdatedAt(), greaterThan(editedComment.getCreated())); jdbcTemplate.update("UPDATE replies SET updated_at='1990-05-05 00:00:00' WHERE message_id=? AND reply_id=?", editedComment.getMid(), editedComment.getRid()); + Message updatedComment = comment.getNewMessage().get(); result = mockMvc .perform(post("/api/update").with(httpBasic(freefdName, freefdPassword)) - .param("mid", String.valueOf(comment.getNewMessage().get().getMid())) - .param("rid", String.valueOf(comment.getNewMessage().get().getRid())).param("body", "HEY, JOE AGAIN")) + .param("mid", String.valueOf(updatedComment.getMid())) + .param("rid", String.valueOf(updatedComment.getRid())).param("body", "HEY, JOE AGAIN")) .andExpect(status().isBadRequest()).andReturn(); + assertThat(messagesService.deleteReply(ugnich.getUid(), updatedComment.getMid(), updatedComment.getRid()), is(false)); + assertThat(messagesService.deleteReply(freefd.getUid(), updatedComment.getMid(), updatedComment.getRid()), is(true)); + assertThat(messagesService.getReply(updatedComment.getMid(), updatedComment.getRid()).getUser(), is(archiveUser)); jdbcTemplate.update("UPDATE messages_txt SET updated_at='1990-05-05 00:00:00' WHERE message_id=?", original.getMid()); - assertThat(messagesService.deleteMessage(ugnich.getUid(), original.getMid()), is(false)); - jdbcTemplate.update("UPDATE messages_txt SET updated_at=? WHERE message_id=?", Instant.now(), original.getMid()); assertThat(messagesService.deleteMessage(ugnich.getUid(), original.getMid()), is(true)); - + assertThat(messagesService.getMessageAuthor(original.getMid()), is(archiveUser)); + jdbcTemplate.update("UPDATE messages_txt SET updated_at=? WHERE message_id=?", Instant.now(), original.getMid()); + assertThat(messagesService.deleteMessage(ugnich.getUid(), original.getMid()), is(false)); + assertThat(messagesService.deleteMessage(archiveUser.getUid(), original.getMid()), is(true)); } @Test |