From 2d6e1dc76e659b78b3103af2bcf1b7f13db62f0a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 30 Jan 2018 14:23:50 +0300 Subject: xmpp: D #message --- .../java/com/juick/service/MessagesService.java | 2 ++ .../com/juick/service/MessagesServiceImpl.java | 34 ++++++++++++++++++++++ .../main/java/com/juick/components/JuickBot.java | 23 +++++++++++++++ .../com/juick/xmpp/server/XMPPServerTests.java | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/juick-server-core/src/main/java/com/juick/service/MessagesService.java b/juick-server-core/src/main/java/com/juick/service/MessagesService.java index c5ef13e0..ee679ca4 100644 --- a/juick-server-core/src/main/java/com/juick/service/MessagesService.java +++ b/juick-server-core/src/main/java/com/juick/service/MessagesService.java @@ -98,6 +98,8 @@ public interface MessagesService { boolean deleteMessage(int uid, int mid); + boolean deleteReply(int uid, int mid, int rid); + List getLastMessages(int hours); List getLastReplies(int hours); 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 2497e94a..b81ef578 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 @@ -48,6 +48,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; /** * Created by aalexeev on 11/13/16. @@ -227,6 +228,15 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } + @Transactional + void updateRepliesBy(int mid) { + List users = getJdbcTemplate().queryForList("SELECT users.nick FROM replies " + + "INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? " + + "GROUP BY replies.user_id ORDER BY COUNT(replies.reply_id) DESC LIMIT 5", String.class, mid); + String result = users.stream().map(u -> "@" + u).collect(Collectors.joining(",")); + getJdbcTemplate().update("UPDATE messages_txt SET repliesby=? WHERE message_id=?", result, mid); + } + @Transactional @Override public boolean recommendMessage(final int mid, final int vuid) { @@ -822,6 +832,30 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } return false; } + @Transactional + @Override + public boolean deleteReply(final int uid, final int mid, final int rid) { + User author = getMessageAuthor(mid); + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("mid", mid) + .addValue("uid", uid) + .addValue("rid", rid); + boolean result; + if (author.getUid() == uid) { + result = getNamedParameterJdbcTemplate() + .update("DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid", sqlParameterSource) > 0; + } else { + result = getNamedParameterJdbcTemplate() + .update("DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid AND user_id=:uid" + , sqlParameterSource) > 0; + } + if (result) { + getNamedParameterJdbcTemplate().update("UPDATE replies SET replies=replies-1 WHERE message_id=:mid", sqlParameterSource); + updateRepliesBy(mid); + return true; + } + return false; + } @Transactional(readOnly = true) @Override diff --git a/juick-xmpp/src/main/java/com/juick/components/JuickBot.java b/juick-xmpp/src/main/java/com/juick/components/JuickBot.java index 9855f958..42a5f8a6 100644 --- a/juick-xmpp/src/main/java/com/juick/components/JuickBot.java +++ b/juick-xmpp/src/main/java/com/juick/components/JuickBot.java @@ -632,7 +632,30 @@ public class JuickBot implements StanzaListener, AutoCloseable { } return "Message not found"; } + @UserCommand(pattern = "^D #(\\d+)$", help = "D #1234 - Delete post", patternFlags = Pattern.CASE_INSENSITIVE) + public String commandDeletePost(User user, Jid from, String... args) { + int mid = Integer.valueOf(args[0]); + if (messagesService.deleteMessage(user.getUid(), mid)) { + return "Message deleted"; + } + return "This is not your message"; + } + @UserCommand(pattern = "^D #(\\d+)(\\.|\\-|\\/)(\\d+)$", help = "D #1234/5 - Delete comment", patternFlags = Pattern.CASE_INSENSITIVE) + public String commandDeleteReply(User user, Jid from, String... args) { + int mid = Integer.valueOf(args[0]); + int rid = Integer.valueOf(args[2]); + if (messagesService.deleteReply(user.getUid(), mid, rid)) { + return "Reply deleted"; + } else { + return "This is not your reply"; + } + } + /* + @UserCommand(pattern = "(^(D L|DL|D LAST)$", help = "D #1234/5 - Delete comment", patternFlags = Pattern.CASE_INSENSITIVE) + public String commandDeleteLast(User user, Jid from, String... args) { + } + */ void sendReply(Jid jidTo, String txt) { Message reply = new Message(); reply.setFrom(jid); diff --git a/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java b/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java index 7f08602c..7c5a1a98 100644 --- a/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java +++ b/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java @@ -96,7 +96,7 @@ public class XMPPServerTests 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(21)); + assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(23)); } @Test -- cgit v1.2.3