aboutsummaryrefslogtreecommitdiff
path: root/juick-core/src/main/java/com/juick
diff options
context:
space:
mode:
Diffstat (limited to 'juick-core/src/main/java/com/juick')
-rw-r--r--juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java38
-rw-r--r--juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java12
2 files changed, 44 insertions, 6 deletions
diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java
new file mode 100644
index 00000000..37b0c6d3
--- /dev/null
+++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java
@@ -0,0 +1,38 @@
+package com.juick.formatters;
+
+import com.juick.Message;
+
+/**
+ * Created by vitalyster on 12.10.2016.
+ */
+public class PlainTextFormatter {
+ public static String formatPost(com.juick.Message jmsg) {
+ StringBuilder sb = new StringBuilder();
+ boolean isReply = jmsg.getRID() > 0;
+ String title = isReply ? "Reply by @" : "@";
+ String subtitle = isReply ? getReplyQuote(jmsg) : jmsg.getTagsString();
+ sb.append(title).append(jmsg.getUser().getUName()).append(":\n")
+ .append(subtitle).append("\n").append(jmsg.getText()).append("\n");
+ if (jmsg.Photo != null) {
+ sb.append(jmsg.Photo);
+ }
+ return sb.toString();
+ }
+
+ public static String formatUrl(com.juick.Message jmsg) {
+ if (jmsg.getRID() > 0) {
+ return String.format("https://juick.com/%d#%d", jmsg.getMID(), jmsg.getRID());
+ }
+ return "https://juick.com/" + jmsg.getMID();
+ }
+
+ private static String getReplyQuote(Message msg) {
+ String quote = msg.getText();
+ if (quote.length() > 50) {
+ quote = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n";
+ } else if (quote.length() > 0) {
+ quote = ">" + quote.replace('\n', ' ') + "\n";
+ }
+ return quote;
+ }
+}
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 f7056cd4..024179a7 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,6 +1,7 @@
package com.juick.server.protocol;
import com.juick.*;
+import com.juick.formatters.PlainTextFormatter;
import com.juick.json.MessageSerializer;
import com.juick.server.*;
import com.juick.server.protocol.annotation.UserCommand;
@@ -39,7 +40,7 @@ public class JuickProtocol {
*/
public ProtocolReply getReply(User user, String userInput) throws InvocationTargetException,
IllegalAccessException, NoSuchMethodException {
- Optional<Method> cmd = Arrays.asList(getClass().getDeclaredMethods()).stream()
+ Optional<Method> cmd = Arrays.stream(getClass().getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(UserCommand.class))
.filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(),
m.getAnnotation(UserCommand.class).patternFlags()).matcher(userInput).matches())
@@ -74,11 +75,10 @@ public class JuickProtocol {
@UserCommand(pattern = "^#(\\++)$", help = "#+ - Show last Juick messages (#++ - second page, ...)")
public ProtocolReply commandLast(User user, String... arguments) {
// number of + is the page count
- int page = arguments[0].length();
+ int page = arguments[0].length() - 1;
List<Integer> mids = MessagesQueries.getAll(sql, user.getUID(), page);
List<Message> messages = MessagesQueries.getMessages(sql, mids);
- // TODO: message toString
- return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(Object::toString)
+ return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost)
.collect(Collectors.toList())), Optional.of(json.serializeList(messages)));
}
@@ -158,7 +158,7 @@ public class JuickProtocol {
List<Message> messages = MessagesQueries.getMessages(sql, mids);
// TODO: add instructions for empty feed
return new ProtocolReply("Your feed: \n" + String.join("\n",
- messages.stream().map(Object::toString).collect(Collectors.toList())),
+ messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())),
Optional.of(json.serializeList(messages)));
}
@@ -267,7 +267,7 @@ public class JuickProtocol {
List<Message> replies = MessagesQueries.getReplies(sql, mid);
replies.add(0, msg);
return new ProtocolReply(String.join("\n",
- replies.stream().map(Object::toString).collect(Collectors.toList())),
+ replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())),
Optional.of(json.serializeList(replies)));
}
return new ProtocolReply(msg.toString(), Optional.of(json.serializeList(Collections.singletonList(msg))));