aboutsummaryrefslogtreecommitdiff
path: root/juick-server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-19 15:02:56 +0300
committerGravatar Vitaly Takmazov2016-12-19 15:02:56 +0300
commit7293d22f1aa731610849a8657b8ae462eb67eee2 (patch)
tree49e36037b6cc09c8c37093a4d34a713803382d7a /juick-server
parent16d02ab9e31cd4a68334575499d039d48a5fb0a9 (diff)
juick-server: drop json part from JuickProtocol reply
Diffstat (limited to 'juick-server')
-rw-r--r--juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java121
-rw-r--r--juick-server/src/main/java/com/juick/server/protocol/ProtocolReply.java17
2 files changed, 55 insertions, 83 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 111a434a..6f61e4d6 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
@@ -11,6 +11,7 @@ import com.juick.server.helpers.TagStats;
import com.juick.server.protocol.annotation.UserCommand;
import com.juick.service.*;
import com.juick.util.TagUtils;
+import org.apache.commons.lang3.math.NumberUtils;
import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
@@ -88,8 +89,7 @@ public class JuickProtocol {
String body = input.substring(TagUtils.toString(tags).length());
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(messagesService.getMessage(mid)))));
+ return new ProtocolReply("New message posted.\n#" + mid + " " + baseUri + mid);
}
@UserCommand(pattern = "^#(\\++)$", help = "#+ - Show last Juick messages (#++ - second page, ...)")
@@ -99,7 +99,7 @@ public class JuickProtocol {
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)));
+ .collect(Collectors.toList())));
}
@UserCommand(pattern = "^\\s*bl\\s*$", patternFlags = Pattern.CASE_INSENSITIVE,
@@ -130,7 +130,7 @@ public class JuickProtocol {
if (txt.isEmpty()) {
txt = "You don't have any users or tags in your blacklist.";
}
- return new ProtocolReply(txt, Optional.empty());
+ return new ProtocolReply(txt);
}
@UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE,
@@ -140,12 +140,12 @@ public class JuickProtocol {
if (blUser != null) {
PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(from, blUser);
if (result == PrivacyQueriesService.PrivacyResult.Added) {
- return new ProtocolReply("User added to your blacklist", Optional.empty());
+ return new ProtocolReply("User added to your blacklist");
} else {
- return new ProtocolReply("User removed from your blacklist", Optional.empty());
+ return new ProtocolReply("User removed from your blacklist");
}
}
- return new ProtocolReply("User not found", Optional.empty());
+ return new ProtocolReply("User not found");
}
@UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE,
@@ -157,13 +157,13 @@ public class JuickProtocol {
if (tag != null) {
PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistTag(from, tag);
if (result == PrivacyQueriesService.PrivacyResult.Added) {
- return new ProtocolReply("Tag added to your blacklist", Optional.empty());
+ return new ProtocolReply("Tag added to your blacklist");
} else {
- return new ProtocolReply("Tag removed from your blacklist", Optional.empty());
+ return new ProtocolReply("Tag removed from your blacklist");
}
}
}
- return new ProtocolReply("Tag not found", Optional.empty());
+ return new ProtocolReply("Tag not found");
}
@UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs")
@@ -187,7 +187,7 @@ public class JuickProtocol {
} else {
msg.append("\nNo top users. Empty DB? ;)");
}
- return new ProtocolReply(msg.toString(), Optional.empty());
+ return new ProtocolReply(msg.toString());
}
@UserCommand(pattern = "\\*", help = "* - Show your tags")
@@ -196,7 +196,7 @@ public class JuickProtocol {
String msg = "Your tags: (tag - messages)\n" +
tags.stream()
.map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining());
- return new ProtocolReply(msg, Optional.empty());
+ return new ProtocolReply(msg);
}
@UserCommand(pattern = "!", help = "! - Show your favorite messages")
@@ -205,9 +205,9 @@ public class JuickProtocol {
if (mids.size() > 0) {
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)));
+ .collect(Collectors.toList())));
}
- return new ProtocolReply("No favorite messages, try to \"like\" something ;)", Optional.empty());
+ return new ProtocolReply("No favorite messages, try to \"like\" something ;)");
}
@UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$",
@@ -220,31 +220,25 @@ public class JuickProtocol {
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(PlainTextFormatter::formatPost).collect(Collectors.toList()))),
- Optional.of(json.writeValueAsString(messages)));
+ .map(PlainTextFormatter::formatPost).collect(Collectors.toList()))));
}
- return new ProtocolReply("User not found", Optional.empty());
+ return new ProtocolReply("User not found");
}
@UserCommand(pattern = "^\\s*d\\s*\\#([0-9]+)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "D #12345 - delete the message")
public ProtocolReply commandDel(User user, String... args) {
- try {
- int mid = Integer.parseInt(args[0]);
- if (messagesService.deleteMessage(user.getUid(), mid)) {
- return new ProtocolReply(String.format("Message %s deleted", mid), Optional.empty());
- }
- } catch (NumberFormatException e) {
- return new ProtocolReply("Error", Optional.empty());
+ int mid = NumberUtils.toInt(args[0], 0);
+ if (messagesService.deleteMessage(user.getUid(), mid)) {
+ return new ProtocolReply(String.format("Message %s deleted", mid));
}
- return new ProtocolReply("Error", Optional.empty());
+ return new ProtocolReply("Error");
}
@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 + "?" + userService.getHashByUID(user.getUid()),
- Optional.empty());
+ return new ProtocolReply(baseUri + "?" + userService.getHashByUID(user.getUid()));
}
@UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)")
@@ -255,8 +249,7 @@ public class JuickProtocol {
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())),
- Optional.of(json.writeValueAsString(messages)));
+ messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())));
}
@UserCommand(pattern = "^\\s*(on|off)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE,
@@ -273,17 +266,16 @@ public class JuickProtocol {
}
if (userService.setActiveStatusForJID(user.getJid(), newStatus)) {
- return new ProtocolReply(retValUpdated, Optional.empty());
+ return new ProtocolReply(retValUpdated);
} else {
- return new ProtocolReply(String.format("Subscriptions status for %s was not changed", user.getJid()),
- Optional.empty());
+ return new ProtocolReply(String.format("Subscriptions status for %s was not changed", user.getJid()));
}
}
@UserCommand(pattern = "^\\s*ping\\s*$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "PING - returns you a PONG")
public ProtocolReply commandPing(User user, String[] input) {
- return new ProtocolReply("PONG", Optional.empty());
+ return new ProtocolReply("PONG");
}
@UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username")
@@ -344,9 +336,9 @@ public class JuickProtocol {
*/
}
if (ret == 200) {
- return new ProtocolReply("Private message sent", Optional.empty());
+ return new ProtocolReply("Private message sent");
} else {
- return new ProtocolReply("Error " + ret, Optional.empty());
+ return new ProtocolReply("Error " + ret);
}
}
@@ -357,7 +349,7 @@ public class JuickProtocol {
try {
mid = Integer.parseInt(arguments[0]);
} catch (NumberFormatException e) {
- return new ProtocolReply("Error", Optional.empty());
+ return new ProtocolReply("Error");
}
Message msg = messagesService.getMessage(mid);
if (msg != null) {
@@ -365,13 +357,11 @@ public class JuickProtocol {
List<Message> replies = messagesService.getReplies(mid);
replies.add(0, msg);
return new ProtocolReply(String.join("\n",
- replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())),
- Optional.of(json.writeValueAsString(replies)));
+ replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())));
}
- return new ProtocolReply(PlainTextFormatter.formatPost(msg),
- Optional.of(json.writeValueAsString(Collections.singletonList(msg))));
+ return new ProtocolReply(PlainTextFormatter.formatPost(msg));
}
- return new ProtocolReply("Message not found", Optional.empty());
+ return new ProtocolReply("Message not found");
}
@UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)",
help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message")
@@ -380,27 +370,21 @@ public class JuickProtocol {
try {
mid = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
- return new ProtocolReply("Error", Optional.empty());
- }
- int rid;
- try {
- rid = Integer.parseInt(args[4]);
- } catch (NumberFormatException e) {
- rid = 0;
+ return new ProtocolReply("Error");
}
+ int rid = NumberUtils.toInt(args[4], 0);
String txt = args[5];
List<Tag> messageTags = tagService.fromString(txt, true);
if (messageTags.size() > 0) {
if (user.getUid() != messagesService.getMessageAuthor(mid).getUid()) {
- return new ProtocolReply("It is not your message", Optional.empty());
+ return new ProtocolReply("It is not your message");
}
tagService.updateTags(mid, messageTags);
- return new ProtocolReply("Tags are updated", Optional.empty());
+ return new ProtocolReply("Tags are updated");
} else {
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(messagesService.getReply(mid, newrid)))));
+ + baseUri + mid + "/" + newrid);
}
}
@@ -408,23 +392,18 @@ public class JuickProtocol {
patternFlags = Pattern.CASE_INSENSITIVE)
public ProtocolReply commandSubscribeMessage(User user, String... args) {
boolean subscribe = args[0].equalsIgnoreCase("s");
- int mid;
- try {
- mid = Integer.parseInt(args[1]);
- } catch (NumberFormatException e) {
- return new ProtocolReply("Error", Optional.empty());
- }
+ int mid = NumberUtils.toInt(args[1], 0);
if (subscribe) {
if (subscriptionService.subscribeMessage(mid, user.getUid())) {
- return new ProtocolReply("Subscribed", Optional.empty());
+ return new ProtocolReply("Subscribed");
}
} else {
if (subscriptionService.unSubscribeMessage(mid, user.getUid())) {
- return new ProtocolReply("Unsubscribed from #" + mid, Optional.empty());
+ return new ProtocolReply("Unsubscribed from #" + mid);
}
- return new ProtocolReply("You was not subscribed to #" + mid, Optional.empty());
+ return new ProtocolReply("You was not subscribed to #" + mid);
}
- return new ProtocolReply("Error", Optional.empty());
+ return new ProtocolReply("Error");
}
@UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @user - subscribe to user's posts",
patternFlags = Pattern.CASE_INSENSITIVE)
@@ -434,18 +413,18 @@ public class JuickProtocol {
if (toUser.getUid() > 0) {
if (subscribe) {
if (subscriptionService.subscribeUser(user, toUser)) {
- return new ProtocolReply("Subscribed", Optional.empty());
+ return new ProtocolReply("Subscribed");
// TODO: notification
// TODO: already subscribed case
}
} else {
if (subscriptionService.unSubscribeUser(user, toUser)) {
- return new ProtocolReply("Unsubscribed from @" + toUser.getName(), Optional.empty());
+ return new ProtocolReply("Unsubscribed from @" + toUser.getName());
}
- return new ProtocolReply("You was not subscribed to @" + toUser.getName(), Optional.empty());
+ return new ProtocolReply("You was not subscribed to @" + toUser.getName());
}
}
- return new ProtocolReply("Error", Optional.empty());
+ return new ProtocolReply("Error");
}
@UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" +
"\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE)
@@ -454,15 +433,15 @@ public class JuickProtocol {
Tag tag = tagService.getTag(args[1], true);
if (subscribe) {
if (subscriptionService.subscribeTag(user, tag)) {
- return new ProtocolReply("Subscribed", Optional.empty());
+ return new ProtocolReply("Subscribed");
}
} else {
if (subscriptionService.unSubscribeTag(user, tag)) {
- return new ProtocolReply("Unsubscribed from " + tag.getName(), Optional.empty());
+ return new ProtocolReply("Unsubscribed from " + tag.getName());
}
- return new ProtocolReply("You was not subscribed to " + tag.getName(), Optional.empty());
+ return new ProtocolReply("You was not subscribed to " + tag.getName());
}
- return new ProtocolReply("Error", Optional.empty());
+ return new ProtocolReply("Error");
}
@UserCommand(pattern = "^\\s*help\\s*$", patternFlags = Pattern.CASE_INSENSITIVE,
@@ -472,7 +451,7 @@ public class JuickProtocol {
.filter(m -> m.isAnnotationPresent(UserCommand.class))
.map(m -> m.getAnnotation(UserCommand.class).help())
.collect(Collectors.toList());
- return new ProtocolReply(String.join("\n", commandsHelp), Optional.empty());
+ return new ProtocolReply(String.join("\n", commandsHelp));
}
public String getBaseUri() {
diff --git a/juick-server/src/main/java/com/juick/server/protocol/ProtocolReply.java b/juick-server/src/main/java/com/juick/server/protocol/ProtocolReply.java
index d9d36a5d..f3483187 100644
--- a/juick-server/src/main/java/com/juick/server/protocol/ProtocolReply.java
+++ b/juick-server/src/main/java/com/juick/server/protocol/ProtocolReply.java
@@ -1,23 +1,16 @@
package com.juick.server.protocol;
-import java.util.Optional;
-
/**
* Created by vitalyster on 08.04.2016.
*/
public class ProtocolReply {
- private Optional<String> json;
- private String description;
+ private String result;
- public ProtocolReply(String text, Optional<String> json) {
- this.description = text;
- this.json = json;
- }
- public String getDescription() {
- return description;
+ public ProtocolReply(String text) {
+ this.result = text;
}
- public Optional<String> getJson() {
- return json;
+ public String getResult() {
+ return result;
}
}