diff options
6 files changed, 46 insertions, 13 deletions
diff --git a/juick-common/src/main/java/com/juick/service/MessagesService.java b/juick-common/src/main/java/com/juick/service/MessagesService.java index 341175dd..2dce2806 100644 --- a/juick-common/src/main/java/com/juick/service/MessagesService.java +++ b/juick-common/src/main/java/com/juick/service/MessagesService.java @@ -33,7 +33,13 @@ public interface MessagesService { int getReplyIDIncrement(int mid); - boolean recommendMessage(int mid, int vuid); + enum RecommendStatus { + Error, + Added, + Deleted + } + + RecommendStatus recommendMessage(int mid, int vuid); boolean canViewThread(int mid, int uid); diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java index 87ab35f3..4398259c 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -230,12 +230,19 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional @Override - public boolean recommendMessage(final int mid, final int vuid) { + public RecommendStatus recommendMessage(final int mid, final int vuid) { int wasDeleted = getJdbcTemplate() .update("DELETE FROM favorites WHERE user_id=? AND message_id=?", vuid, mid); - return wasDeleted == 0 - && getJdbcTemplate() - .update("INSERT INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", vuid, mid) == 1; + if (wasDeleted > 0) { + return RecommendStatus.Deleted; + } else { + boolean wasAdded = getJdbcTemplate() + .update("INSERT INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", vuid, mid) == 1; + if (wasAdded) { + return RecommendStatus.Added; + } + } + return RecommendStatus.Error; } @Transactional(readOnly = true) diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java index 57e0970a..7b321496 100644 --- a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java +++ b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java @@ -102,9 +102,9 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { assertThat(messagesService.getMyFeed(freefdId, 0, true).size(), equalTo(3)); assertThat(messagesService.getMyFeed(freefdId, mid2, true).size(), equalTo(1)); assertThat(messagesService.getMyFeed(freefdId, mid, false).size(), equalTo(0)); - assertThat(messagesService.recommendMessage(mid, ugnichId), equalTo(true)); + assertThat(messagesService.recommendMessage(mid, ugnichId), equalTo(MessagesService.RecommendStatus.Added)); assertThat(messagesService.getMessage(mid).getLikes(), equalTo(1)); - assertThat(messagesService.recommendMessage(mid, ugnichId), equalTo(false)); + assertThat(messagesService.recommendMessage(mid, ugnichId), equalTo(MessagesService.RecommendStatus.Deleted)); assertThat(messagesService.getMessage(mid).getLikes(), equalTo(0)); assertThat(messagesService.getAll(ugnichId, 0).size(), equalTo(3)); diff --git a/juick-server/src/main/java/com/juick/server/XMPPBot.java b/juick-server/src/main/java/com/juick/server/XMPPBot.java index a07dbe86..6215576a 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPBot.java +++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java @@ -517,6 +517,27 @@ public class XMPPBot implements StanzaListener, AutoCloseable { } return "No favorite messages, try to \"like\" something ;)"; } + @UserCommand(pattern = "^\\!\\s+#(\\d+)", help = "! #12345 - recommend message") + public String commandRecommend(User user, Jid from, String... arguments) { + int mid = NumberUtils.toInt(arguments[0], 0); + if (mid > 0) { + com.juick.Message msg = messagesService.getMessage(mid); + if (msg != null) { + if (msg.getUser() == user) { + return "You can't recommend your own messages."; + } + MessagesService.RecommendStatus status = messagesService.recommendMessage(mid, user.getUid()); + switch (status) { + case Added: + return "Message is added to your recommendations"; + case Deleted: + return "Message deleted from your recommendations."; + } + } + return "Message not found"; + } + return "Message not found"; + } // TODO: target notification @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @username - subscribe to user" + "\nU @username - unsubscribe from user", patternFlags = Pattern.CASE_INSENSITIVE) diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index fcc1a777..4bf8ac9e 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -385,7 +385,7 @@ public class ServerTests extends AbstractJUnit4SpringContextTests { public void botCommandsTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "PING").get(), is("PONG")); // subscription commands have two lines, others have 1 - assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(24)); + assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(25)); } @Test diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index a252bb69..531babd2 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -310,9 +310,9 @@ public class NewMessage { throw new HttpForbiddenException(); } - boolean res = messagesService.recommendMessage(mid, visitor.getUid()); + MessagesService.RecommendStatus res = messagesService.recommendMessage(mid, visitor.getUid()); - if (res) { + if (res == MessagesService.RecommendStatus.Added) { if (xmpp.isConnected()) { Message xmsg = new Message(); xmsg.setFrom(Jid.of("juick@juick.com")); @@ -325,11 +325,10 @@ public class NewMessage { } else { logger.warn("XMPP unavailable"); } - - return Status.OK; - } else { + } else if (res == MessagesService.RecommendStatus.Error) { throw new HttpBadRequestException(); } + return Status.OK; } @PostMapping("/pm/send") public String doPostPM(@RequestParam(name = "uname", required = false) String unameParam, |