aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-19 13:41:23 +0300
committerGravatar Vitaly Takmazov2016-12-19 13:41:23 +0300
commit274de5ede59ed4023826a9db09ec60788bc950e1 (patch)
tree107dbca37f559666eb065989aa92fd81e79fef24 /juick-server/src/main/java/com
parentacddd380e1868e28491eaf85f05e87d7a47a123f (diff)
all projects except juick-www are spring-managed now
Diffstat (limited to 'juick-server/src/main/java/com')
-rw-r--r--juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java136
1 files changed, 76 insertions, 60 deletions
diff --git a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java
index 68c7c37c..7b155747 100644
--- a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java
+++ b/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java
@@ -7,12 +7,12 @@ import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.formatters.PlainTextFormatter;
-import com.juick.server.*;
import com.juick.server.helpers.TagStats;
import com.juick.server.protocol.annotation.UserCommand;
+import com.juick.service.*;
import com.juick.util.TagUtils;
-import org.springframework.jdbc.core.JdbcTemplate;
+import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
@@ -25,13 +25,26 @@ import java.util.stream.Collectors;
*/
public class JuickProtocol {
- ObjectMapper json = new ObjectMapper();
- JdbcTemplate sql;
- String baseUri;
-
- public JuickProtocol(JdbcTemplate sql, String baseUri) {
- this.sql = sql;
+ private final ObjectMapper json;
+ private String baseUri;
+ @Inject
+ UserService userService;
+ @Inject
+ TagService tagService;
+ @Inject
+ MessagesService messagesService;
+ @Inject
+ SubscriptionService subscriptionService;
+ @Inject
+ PMQueriesService pmQueriesService;
+ @Inject
+ PrivacyQueriesService privacyQueriesService;
+ @Inject
+ ShowQueriesService showQueriesService;
+
+ public JuickProtocol(String baseUri) {
this.baseUri = baseUri;
+ json = new ObjectMapper();
json.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
json.setSerializationInclusion(JsonInclude.Include.NON_NULL);
json.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
@@ -71,21 +84,20 @@ public class JuickProtocol {
}
public ProtocolReply postMessage(User user, String input) throws JsonProcessingException {
- List<Tag> tags = TagQueries.fromString(sql, input, false);
+ List<Tag> tags = tagService.fromString(input, false);
String body = input.substring(TagUtils.toString(tags).length());
- int mid = MessagesQueries.createMessage(sql, user.getUid(), body, null, tags);
- SubscriptionsQueries.subscribeMessage(sql, mid, user.getUid());
- //app.events().publishEvent(new JuickMessageEvent(app.messages().getMessage(mid)));
+ int mid = messagesService.createMessage(user.getUid(), body, null, tags);
+ subscriptionService.subscribeMessage(mid, user.getUid());
return new ProtocolReply("New message posted.\n#" + mid + " " + baseUri + mid,
- Optional.of(json.writeValueAsString(Collections.singletonList(MessagesQueries.getMessage(sql, mid)))));
+ Optional.of(json.writeValueAsString(Collections.singletonList(messagesService.getMessage(mid)))));
}
@UserCommand(pattern = "^#(\\++)$", help = "#+ - Show last Juick messages (#++ - second page, ...)")
public ProtocolReply commandLast(User user, String... arguments) throws JsonProcessingException {
// number of + is the page count
int page = arguments[0].length() - 1;
- List<Integer> mids = MessagesQueries.getAll(sql, user.getUid(), page);
- List<Message> messages = MessagesQueries.getMessages(sql, mids);
+ List<Integer> mids = messagesService.getAll(user.getUid(), page);
+ List<Message> messages = messagesService.getMessages(mids);
return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost)
.collect(Collectors.toList())), Optional.of(json.writeValueAsString(messages)));
}
@@ -96,8 +108,8 @@ public class JuickProtocol {
List<User> blusers;
List<String> bltags;
- blusers = UserQueries.getUserBLUsers(sql, user_from.getUid());
- bltags = TagQueries.getUserBLTags(sql, user_from.getUid());
+ blusers = userService.getUserBLUsers(user_from.getUid());
+ bltags = tagService.getUserBLTags(user_from.getUid());
String txt = "";
@@ -124,10 +136,10 @@ public class JuickProtocol {
@UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE,
help = "BL @username - add @username to your blacklist")
public ProtocolReply blacklistUser(User from, String... arguments) {
- User blUser = UserQueries.getUserByName(sql, arguments[0]);
+ User blUser = userService.getUserByName(arguments[0]);
if (blUser != null) {
- PrivacyQueries.PrivacyResult result = PrivacyQueries.blacklistUser(sql, from, blUser);
- if (result == PrivacyQueries.PrivacyResult.Added) {
+ PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(from, blUser);
+ if (result == PrivacyQueriesService.PrivacyResult.Added) {
return new ProtocolReply("User added to your blacklist", Optional.empty());
} else {
return new ProtocolReply("User removed from your blacklist", Optional.empty());
@@ -139,12 +151,12 @@ public class JuickProtocol {
@UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "BL *tag - add *tag to your blacklist")
public ProtocolReply blacklistTag(User from, String... arguments) {
- User blUser = UserQueries.getUserByName(sql, arguments[0]);
+ User blUser = userService.getUserByName(arguments[0]);
if (blUser != null) {
- Tag tag = TagQueries.getTag(sql, arguments[0], false);
+ Tag tag = tagService.getTag(arguments[0], false);
if (tag != null) {
- PrivacyQueries.PrivacyResult result = PrivacyQueries.blacklistTag(sql, from, tag);
- if (result == PrivacyQueries.PrivacyResult.Added) {
+ PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistTag(from, tag);
+ if (result == PrivacyQueriesService.PrivacyResult.Added) {
return new ProtocolReply("Tag added to your blacklist", Optional.empty());
} else {
return new ProtocolReply("Tag removed from your blacklist", Optional.empty());
@@ -158,7 +170,7 @@ public class JuickProtocol {
public ProtocolReply commandUsers(User currentUser, String... args) {
StringBuilder msg = new StringBuilder();
msg.append("Recommended blogs");
- List<String> recommendedUsers = ShowQueries.getRecommendedUsers(sql, currentUser);
+ List<String> recommendedUsers = showQueriesService.getRecommendedUsers(currentUser);
if (recommendedUsers.size() > 0) {
for (String user : recommendedUsers) {
msg.append("\n@").append(user);
@@ -167,7 +179,7 @@ public class JuickProtocol {
msg.append("\nNo recommendations now. Subscribe to more blogs. ;)");
}
msg.append("\n\nTop 10 personal blogs:");
- List<String> topUsers = ShowQueries.getTopUsers(sql);
+ List<String> topUsers = showQueriesService.getTopUsers();
if (topUsers.size() > 0) {
for (String user : topUsers) {
msg.append("\n@").append(user);
@@ -180,7 +192,7 @@ public class JuickProtocol {
@UserCommand(pattern = "\\*", help = "* - Show your tags")
public ProtocolReply commandTags(User currentUser, String... args) {
- List<TagStats> tags = TagQueries.getTagsStats(sql, currentUser.getUid());
+ List<TagStats> tags = tagService.getUserTagStats(currentUser.getUid());
String msg = "Your tags: (tag - messages)\n" +
tags.stream()
.map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining());
@@ -189,9 +201,9 @@ public class JuickProtocol {
@UserCommand(pattern = "!", help = "! - Show your favorite messages")
public ProtocolReply commandFavorites(User currentUser, String... args) throws JsonProcessingException {
- List<Integer> mids = MessagesQueries.getUserRecommendations(sql, currentUser.getUid(), 0);
+ List<Integer> mids = messagesService.getUserRecommendations(currentUser.getUid(), 0);
if (mids.size() > 0) {
- List<Message> messages = MessagesQueries.getMessages(sql, mids);
+ List<Message> messages = messagesService.getMessages(mids);
return new ProtocolReply("Favorite messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost)
.collect(Collectors.toList())), Optional.of(json.writeValueAsString(messages)));
}
@@ -201,11 +213,11 @@ public class JuickProtocol {
@UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$",
help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)")
public ProtocolReply commandUser(User user, String... arguments) throws JsonProcessingException {
- User blogUser = UserQueries.getUserByName(sql, arguments[0]);
+ User blogUser = userService.getUserByName(arguments[0]);
int page = arguments[1].length();
if (blogUser != null) {
- List<Integer> mids = MessagesQueries.getUserBlog(sql, blogUser.getUid(), 0, page);
- List<Message> messages = MessagesQueries.getMessages(sql, mids);
+ List<Integer> mids = messagesService.getUserBlog(blogUser.getUid(), 0, page);
+ List<Message> messages = messagesService.getMessages(mids);
return new ProtocolReply(String.format("Last messages from @%s:\n%s", arguments[0],
String.join("\n", messages.stream()
.map(Object::toString).collect(Collectors.toList()))),
@@ -219,7 +231,7 @@ public class JuickProtocol {
public ProtocolReply commandDel(User user, String... args) {
try {
int mid = Integer.parseInt(args[0]);
- if (MessagesQueries.deleteMessage(sql, user.getUid(), mid)) {
+ if (messagesService.deleteMessage(user.getUid(), mid)) {
return new ProtocolReply(String.format("Message %s deleted", mid), Optional.empty());
}
} catch (NumberFormatException e) {
@@ -231,7 +243,7 @@ public class JuickProtocol {
@UserCommand(pattern = "^\\s*login\\s*$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "LOGIN - log in to Juick website")
public ProtocolReply commandLogin(User user, String... arguments) {
- return new ProtocolReply(baseUri + "?" + UserQueries.getHashByUID(sql, user.getUid()),
+ return new ProtocolReply(baseUri + "?" + userService.getHashByUID(user.getUid()),
Optional.empty());
}
@@ -239,8 +251,8 @@ public class JuickProtocol {
public ProtocolReply commandMyFeed(User user, String... arguments) throws JsonProcessingException {
// number of # is the page count
int page = arguments[0].length() - 1;
- List<Integer> mids = MessagesQueries.getMyFeed(sql, user.getUid(), page);
- List<Message> messages = MessagesQueries.getMessages(sql, mids);
+ List<Integer> mids = messagesService.getMyFeed(user.getUid(), page);
+ List<Message> messages = messagesService.getMessages(mids);
// TODO: add instructions for empty feed
return new ProtocolReply("Your feed: \n" + String.join("\n",
messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())),
@@ -250,17 +262,17 @@ public class JuickProtocol {
@UserCommand(pattern = "^\\s*(on|off)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "ON/OFF - Enable/disable subscriptions delivery")
public ProtocolReply commandOnOff(User user, String[] input) {
- UserQueries.ActiveStatus newStatus;
+ UserService.ActiveStatus newStatus;
String retValUpdated;
if (input[0].toLowerCase().equals("on")) {
- newStatus = UserQueries.ActiveStatus.Active;
+ newStatus = UserService.ActiveStatus.Active;
retValUpdated = "Notifications are activated for " + user.getJid();
} else {
- newStatus = UserQueries.ActiveStatus.Inactive;
+ newStatus = UserService.ActiveStatus.Inactive;
retValUpdated = "Notifications are disabled for " + user.getJid();
}
- if (UserQueries.setActiveStatusForJID(sql, user.getJid(), newStatus)) {
+ if (userService.setActiveStatusForJID(user.getJid(), newStatus)) {
return new ProtocolReply(retValUpdated, Optional.empty());
} else {
return new ProtocolReply(String.format("Subscriptions status for %s was not changed", user.getJid()),
@@ -285,17 +297,17 @@ public class JuickProtocol {
boolean haveInRoster = false;
if (user_to.indexOf('@') > 0) {
- uid_to = UserQueries.getUIDbyJID(sql, user_to);
+ uid_to = userService.getUIDbyJID(user_to);
} else {
- uid_to = UserQueries.getUIDbyName(sql, user_to);
+ uid_to = userService.getUIDbyName(user_to);
}
if (uid_to > 0) {
- if (!UserQueries.isInBLAny(sql, uid_to, user_from.getUid())) {
- if (PMQueries.createPM(sql, user_from.getUid(), uid_to, body)) {
+ if (!userService.isInBLAny(uid_to, user_from.getUid())) {
+ if (pmQueriesService.createPM(user_from.getUid(), uid_to, body)) {
//jid_to = UserQueries.getJIDsbyUID(sql, uid_to);
if (jid_to != null) {
- haveInRoster = PMQueries.havePMinRoster(sql, user_from.getUid(), jid_to);
+ haveInRoster = pmQueriesService.havePMinRoster(user_from.getUid(), jid_to);
}
ret = 200;
} else {
@@ -347,10 +359,10 @@ public class JuickProtocol {
} catch (NumberFormatException e) {
return new ProtocolReply("Error", Optional.empty());
}
- Message msg = MessagesQueries.getMessage(sql, mid);
+ Message msg = messagesService.getMessage(mid);
if (msg != null) {
if (showReplies) {
- List<Message> replies = MessagesQueries.getReplies(sql, mid);
+ List<Message> replies = messagesService.getReplies(mid);
replies.add(0, msg);
return new ProtocolReply(String.join("\n",
replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())),
@@ -377,18 +389,18 @@ public class JuickProtocol {
rid = 0;
}
String txt = args[5];
- List<Tag> messageTags = TagQueries.fromString(sql, txt, true);
+ List<Tag> messageTags = tagService.fromString(txt, true);
if (messageTags.size() > 0) {
- if (user.getUid() != MessagesQueries.getMessageAuthor(sql, mid).getUid()) {
+ if (user.getUid() != messagesService.getMessageAuthor(mid).getUid()) {
return new ProtocolReply("It is not your message", Optional.empty());
}
- TagQueries.updateTags(sql, mid, messageTags);
+ tagService.updateTags(mid, messageTags);
return new ProtocolReply("Tags are updated", Optional.empty());
} else {
- int newrid = MessagesQueries.createReply(sql, mid, rid, user.getUid(), txt, null);
+ int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, null);
return new ProtocolReply("Reply posted.\n#" + mid + "/" + newrid + " "
+ baseUri + mid + "/" + newrid,
- Optional.of(json.writeValueAsString(Collections.singletonList(MessagesQueries.getReply(sql, mid, newrid)))));
+ Optional.of(json.writeValueAsString(Collections.singletonList(messagesService.getReply(mid, newrid)))));
}
}
@@ -403,11 +415,11 @@ public class JuickProtocol {
return new ProtocolReply("Error", Optional.empty());
}
if (subscribe) {
- if (SubscriptionsQueries.subscribeMessage(sql, mid, user.getUid())) {
+ if (subscriptionService.subscribeMessage(mid, user.getUid())) {
return new ProtocolReply("Subscribed", Optional.empty());
}
} else {
- if (SubscriptionsQueries.unSubscribeMessage(sql, mid, user.getUid())) {
+ if (subscriptionService.unSubscribeMessage(mid, user.getUid())) {
return new ProtocolReply("Unsubscribed from #" + mid, Optional.empty());
}
return new ProtocolReply("You was not subscribed to #" + mid, Optional.empty());
@@ -418,16 +430,16 @@ public class JuickProtocol {
patternFlags = Pattern.CASE_INSENSITIVE)
public ProtocolReply commandSubscribeUser(User user, String... args) {
boolean subscribe = args[0].equalsIgnoreCase("s");
- User toUser = UserQueries.getUserByName(sql, args[1]);
+ User toUser = userService.getUserByName(args[1]);
if (toUser.getUid() > 0) {
if (subscribe) {
- if (SubscriptionsQueries.subscribeUser(sql, user, toUser)) {
+ if (subscriptionService.subscribeUser(user, toUser)) {
return new ProtocolReply("Subscribed", Optional.empty());
// TODO: notification
// TODO: already subscribed case
}
} else {
- if (SubscriptionsQueries.unSubscribeUser(sql, user, toUser)) {
+ if (subscriptionService.unSubscribeUser(user, toUser)) {
return new ProtocolReply("Unsubscribed from @" + toUser.getName(), Optional.empty());
}
return new ProtocolReply("You was not subscribed to @" + toUser.getName(), Optional.empty());
@@ -439,13 +451,13 @@ public class JuickProtocol {
"\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE)
public ProtocolReply commandSubscribeTag(User user, String... args) {
boolean subscribe = args[0].equalsIgnoreCase("s");
- Tag tag = TagQueries.getTag(sql, args[1], true);
+ Tag tag = tagService.getTag(args[1], true);
if (subscribe) {
- if (SubscriptionsQueries.subscribeTag(sql, user, tag)) {
+ if (subscriptionService.subscribeTag(user, tag)) {
return new ProtocolReply("Subscribed", Optional.empty());
}
} else {
- if (SubscriptionsQueries.unSubscribeTag(sql, user, tag)) {
+ if (subscriptionService.unSubscribeTag(user, tag)) {
return new ProtocolReply("Unsubscribed from " + tag.getName(), Optional.empty());
}
return new ProtocolReply("You was not subscribed to " + tag.getName(), Optional.empty());
@@ -462,4 +474,8 @@ public class JuickProtocol {
.collect(Collectors.toList());
return new ProtocolReply(String.join("\n", commandsHelp), Optional.empty());
}
+
+ public String getBaseUri() {
+ return baseUri;
+ }
}