From 5e0c202f6d29d1146b3b6b643fc1c7fd477bf6ac Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 19 Oct 2016 17:43:19 +0300 Subject: protocol: some new commands and fixes, part 2 --- .../java/com/juick/server/MessagesQueries.java | 1 - .../com/juick/server/protocol/JuickProtocol.java | 48 +++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) (limited to 'juick-core/src/main') diff --git a/juick-core/src/main/java/com/juick/server/MessagesQueries.java b/juick-core/src/main/java/com/juick/server/MessagesQueries.java index 40449e75..b824feba 100644 --- a/juick-core/src/main/java/com/juick/server/MessagesQueries.java +++ b/juick-core/src/main/java/com/juick/server/MessagesQueries.java @@ -547,7 +547,6 @@ public class MessagesQueries { return sql.queryForList("SELECT message_id FROM favorites " + "WHERE user_id=? ORDER BY message_id DESC LIMIT 20", Integer.class, UID); - } } diff --git a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java index 01f7d691..aff8010a 100644 --- a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -1,10 +1,12 @@ package com.juick.server.protocol; import com.juick.*; +import com.juick.Message; import com.juick.formatters.PlainTextFormatter; import com.juick.json.MessageSerializer; import com.juick.server.*; import com.juick.server.protocol.annotation.UserCommand; +import com.juick.xmpp.*; import com.juick.xmpp.extensions.JuickMessage; import org.springframework.jdbc.core.JdbcTemplate; @@ -146,12 +148,56 @@ public class JuickProtocol { return new ProtocolReply("Tag not found", Optional.empty()); } + @UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs") + public ProtocolReply commandUsers(User currentUser, String... args) { + StringBuilder msg = new StringBuilder(); + msg.append("Recommended blogs"); + List recommendedUsers = ShowQueries.getRecommendedUsers(sql, currentUser); + if (recommendedUsers.size() > 0) { + for (String user : recommendedUsers) { + msg.append("\n@").append(user); + } + } else { + msg.append("\nNo recommendations now. Subscribe to more blogs. ;)"); + } + msg.append("\n\nTop 10 personal blogs:"); + List topUsers = ShowQueries.getTopUsers(sql); + if (topUsers.size() > 0) { + for (String user : topUsers) { + msg.append("\n@").append(user); + } + } else { + msg.append("\nNo top users. Empty DB? ;)"); + } + return new ProtocolReply(msg.toString(), Optional.empty()); + } + + @UserCommand(pattern = "\\*", help = "* - Show your tags") + public ProtocolReply commandTags(User currentUser, String... args) { + List tags = TagQueries.getUserTagsAll(sql, currentUser.getUID()); + String msg = "Your tags: (tag - messages)\n" + + tags.stream() + .map(t -> String.format("\n*%s - %d", t.getName(), t.UsageCnt)).collect(Collectors.joining()); + return new ProtocolReply(msg, Optional.empty()); + } + + @UserCommand(pattern = "!", help = "! - Show your favorite messages") + public ProtocolReply commandFavorites(User currentUser, String... args) { + List mids = MessagesQueries.getUserRecommendations(sql, currentUser.getUID(), 0); + if (mids.size() > 0) { + List messages = MessagesQueries.getMessages(sql, mids); + return new ProtocolReply("Favorite messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost) + .collect(Collectors.toList())), Optional.of(json.serializeList(messages))); + } + return new ProtocolReply("No favorite messages, try to \"like\" something ;)", Optional.empty()); + } + @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$", help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)") public ProtocolReply commandUser(User user, String... arguments) { User blogUser = UserQueries.getUserByName(sql, arguments[0]); int page = arguments[1].length(); - if (blogUser.getUID() > 0) { + if (blogUser != null) { List mids = MessagesQueries.getUserBlog(sql, blogUser.getUID(), 0, page); List messages = MessagesQueries.getMessages(sql, mids); return new ProtocolReply(String.format("Last messages from @%s:\n%s", arguments[0], -- cgit v1.2.3