From 384c61ceae3301c6bc92ee6f591ed9d186b15204 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 6 Apr 2019 21:53:13 +0300 Subject: Message properties --- .../java/com/juick/server/TelegramBotManager.java | 6 ++-- .../server/www/controllers/AnythingFilter.java | 2 +- .../java/com/juick/service/MessagesService.java | 3 ++ .../com/juick/service/MessagesServiceImpl.java | 39 +++++++++++++++++++++- .../db/migration/V1.19__messages_properties.sql | 6 ++++ .../java/com/juick/server/tests/ServerTests.java | 16 ++++++--- 6 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 src/main/resources/db/migration/V1.19__messages_properties.sql diff --git a/src/main/java/com/juick/server/TelegramBotManager.java b/src/main/java/com/juick/server/TelegramBotManager.java index 56b5b4f9..22864af8 100644 --- a/src/main/java/com/juick/server/TelegramBotManager.java +++ b/src/main/java/com/juick/server/TelegramBotManager.java @@ -268,7 +268,7 @@ public class TelegramBotManager implements NotificationListener { telegramPhoto.caption(msg); isSendTxt = false; } - bot.execute(telegramPhoto, new Callback() { + bot.execute(telegramPhoto, new Callback<>() { @Override public void onResponse(SendPhoto request, SendResponse response) { processTelegramResponse(chatId, response, source); @@ -286,7 +286,7 @@ public class TelegramBotManager implements NotificationListener { telegramMessage.replyToMessageId(replyTo); } telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true); - bot.execute(telegramMessage, new Callback() { + bot.execute(telegramMessage, new Callback<>() { @Override public void onResponse(SendMessage request, SendResponse response) { processTelegramResponse(chatId, response, source); @@ -338,7 +338,7 @@ public class TelegramBotManager implements NotificationListener { String.format("You are subscribed to all Juick messages. " + "[Create or link](http://juick.com/signup?type=durov&hash=%s) " + "an existing Juick account to get your subscriptions and ability to post messages", hash)) - .parseMode(ParseMode.Markdown), new Callback() { + .parseMode(ParseMode.Markdown), new Callback<>() { @Override public void onResponse(SendMessage request, SendResponse response) { logger.info("got response: {}", response.message()); diff --git a/src/main/java/com/juick/server/www/controllers/AnythingFilter.java b/src/main/java/com/juick/server/www/controllers/AnythingFilter.java index 57b298eb..1f4cf75a 100644 --- a/src/main/java/com/juick/server/www/controllers/AnythingFilter.java +++ b/src/main/java/com/juick/server/www/controllers/AnythingFilter.java @@ -53,7 +53,7 @@ public class AnythingFilter extends OncePerRequestFilter { } com.juick.User user = userService.getUserByName(anything); if (user.getUid() > 0) { - ((HttpServletResponse)servletResponse).sendRedirect("/" + user.getName() + "/"); + servletResponse.sendRedirect("/" + user.getName() + "/"); } else { filterChain.doFilter(servletRequest, servletResponse); } diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index 080319cc..b29337b2 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -140,4 +140,7 @@ public interface MessagesService { boolean replyExists(URI replyUri); boolean deleteReply(URI userUri, URI replyUri); + + String getMessageProperty(Integer mid, String key); + void setMessageProperty(Integer mid, String key, String value); } diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index def6324a..3e09d204 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -33,6 +33,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.ConnectionCallback; @@ -1162,21 +1163,57 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } } + @Transactional @Override public boolean updateReplyUri(Message reply, URI replyUri) { return jdbcTemplate.update("UPDATE replies SET reply_uri=?, html=0 WHERE message_id=? AND reply_id=?", replyUri.toASCIIString(), reply.getMid(), reply.getRid()) > 0; } + @Transactional(readOnly = true) @Override public boolean replyExists(URI replyUri) { return jdbcTemplate.queryForList("SELECT reply_id FROM replies WHERE reply_uri=?", Integer.class, replyUri.toASCIIString()).size() > 0; } + @Transactional @Override public boolean deleteReply(URI userUri, URI replyUri) { - return jdbcTemplate.update("DELETE FROM replies WHERE user_uri=? AND reply_uri=?", + return jdbcTemplate.update("DELETE FROM replies WHERE user_uri=? AND reply_uri=?", userUri.toASCIIString(), replyUri.toASCIIString()) > 0; } + + @Transactional(readOnly = true) + @Override + public String getMessageProperty(Integer mid, String key) { + List results = jdbcTemplate.queryForList( + "SELECT property_value FROM messages_properties WHERE message_id=? AND property_key=?", + String.class, mid, key); + if (results.size() > 0) { + return results.get(0); + } + return StringUtils.EMPTY; + } + + @Transactional + @Override + public void setMessageProperty(Integer mid, String key, String value) { + SqlParameterSource parameterSource = new MapSqlParameterSource() + .addValue("mid", mid) + .addValue("key", key) + .addValue("value", value); + if (StringUtils.isNotEmpty(value)) { + try { + getNamedParameterJdbcTemplate().update("INSERT INTO messages_properties(message_id, property_key, property_value) " + + "VALUES(:mid, :key, :value)", parameterSource); + } catch (DataIntegrityViolationException ex) { + getNamedParameterJdbcTemplate().update("UPDATE messages_properties SET property_value=:value " + + "WHERE message_id=:mid AND property_key=:key", parameterSource); + } + } else { + getNamedParameterJdbcTemplate().update("DELETE FROM messages_properties " + + "WHERE message_id=:mid AND property_key=:key", parameterSource); + } + } } diff --git a/src/main/resources/db/migration/V1.19__messages_properties.sql b/src/main/resources/db/migration/V1.19__messages_properties.sql new file mode 100644 index 00000000..2bb3baf2 --- /dev/null +++ b/src/main/resources/db/migration/V1.19__messages_properties.sql @@ -0,0 +1,6 @@ +CREATE TABLE messages_properties ( + message_id int(10) unsigned NOT NULL, + property_key varchar(255) NOT NULL, + property_value mediumtext NOT NULL, + UNIQUE KEY message_key(message_id, property_key) +) \ No newline at end of file diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index d4f79f3e..afe7f659 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -106,7 +106,6 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.*; import java.net.URI; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -141,8 +140,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @TestPropertySource(properties = { "broken_ssl_hosts=localhost,serverstorageisfull.tld", - "ios_app_id=12345678.com.juick.ExampleApp", - "spring.jackson.default-property-inclusion=non_default" + "ios_app_id=12345678.com.juick.ExampleApp" }) @AutoConfigureMockMvc public class ServerTests { @@ -178,8 +176,6 @@ public class ServerTests { @Inject private CrosspostService crosspostService; @Inject - private ImagesService imagesService; - @Inject private ServerManager serverManager; @Inject private KeystoreManager keystoreManager; @@ -2020,4 +2016,14 @@ public class ServerTests { User user = userService.getUserByUID(0).orElse(AnonymousUser.INSTANCE); assertThat(user.isAnonymous(), is(true)); } + @Test + public void messagePropertiesTest() { + int mid = messagesService.createMessage(ugnich.getUid(), "YO", null, null); + messagesService.setMessageProperty(mid, "tg_id", "YO"); + assertThat(messagesService.getMessageProperty(mid, "tg_id"), is("YO")); + messagesService.setMessageProperty(mid, "tg_id", "YO2"); + assertThat(messagesService.getMessageProperty(mid, "tg_id"), is("YO2")); + messagesService.setMessageProperty(mid, "tg_id", ""); + assertThat(messagesService.getMessageProperty(mid, "tg_id"), is(StringUtils.EMPTY)); + } } -- cgit v1.2.3