aboutsummaryrefslogtreecommitdiff
path: root/juick-server-core/src/main/java/com/juick/server
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server-core/src/main/java/com/juick/server')
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java132
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java35
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/Auth.java22
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java24
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java34
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java29
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java22
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java72
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java29
-rw-r--r--juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java43
-rw-r--r--juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java426
-rw-r--r--juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java13
-rw-r--r--juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java33
-rw-r--r--juick-server-core/src/main/java/com/juick/server/util/HashUtils.java19
-rw-r--r--juick-server-core/src/main/java/com/juick/server/util/TagUtils.java25
15 files changed, 958 insertions, 0 deletions
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java b/juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java
new file mode 100644
index 00000000..1ce1c2c2
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java
@@ -0,0 +1,132 @@
+package com.juick.server.helpers;
+
+import com.juick.User;
+
+/**
+ * Created by aalexeev on 12/11/16.
+ */
+public final class AnonymousUser extends User {
+ public static final AnonymousUser INSTANCE = new AnonymousUser();
+
+ private AnonymousUser() {
+ super.setUid(getUid());
+ super.setName(getName());
+ super.setAvatar(getAvatar());
+ super.setFullName(getFullName());
+ super.setJid(getJid());
+ super.setMessagesCount(getMessagesCount());
+ super.setAuthHash(getAuthHash());
+ super.setBanned(isBanned());
+ super.setCredentials(getCredentials());
+ super.setLang(getLang());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj == this || obj instanceof AnonymousUser;
+ }
+
+ @Override
+ public int getUid() {
+ return 0;
+ }
+
+ @Override
+ public String getName() {
+ return "Anonymous";
+ }
+
+ @Override
+ public String getFullName() {
+ return getName();
+ }
+
+ @Override
+ public String getJid() {
+ return "anonym@localhost";
+ }
+
+ @Override
+ public String getAuthHash() {
+ return null;
+ }
+
+ @Override
+ public Integer getUnreadCount() {
+ return 0;
+ }
+
+ @Override
+ public boolean isBanned() {
+ return false;
+ }
+
+ @Override
+ public Object getAvatar() {
+ return null;
+ }
+
+ @Override
+ public String getCredentials() {
+ return null;
+ }
+
+ @Override
+ public String getLang() {
+ return "__";
+ }
+
+ @Override
+ public int getMessagesCount() {
+ return 0;
+ }
+
+ @Override
+ public boolean isAnonymous() {
+ return true;
+ }
+
+ @Override
+ public void setUid(int uid) {
+ }
+
+ @Override
+ public void setName(String name) {
+ }
+
+ @Override
+ public void setFullName(String fullName) {
+ }
+
+ @Override
+ public void setJid(String jid) {
+ }
+
+ @Override
+ public void setAuthHash(String authHash) {
+ }
+
+ @Override
+ public void setUnreadCount(Integer count) {
+ }
+
+ @Override
+ public void setBanned(boolean banned) {
+ }
+
+ @Override
+ public void setAvatar(Object avatar) {
+ }
+
+ @Override
+ public void setCredentials(String credentials) {
+ }
+
+ @Override
+ public void setLang(String lang) {
+ }
+
+ @Override
+ public void setMessagesCount(int messagesCount) {
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java b/juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java
new file mode 100644
index 00000000..61109c47
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java
@@ -0,0 +1,35 @@
+package com.juick.server.helpers;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+
+/**
+ * Created by vt on 03/09/16.
+ */
+public class ApplicationStatus {
+ private boolean connected;
+ private boolean crosspostEnabled;
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("connected", connected)
+ .append("crosspostEnabled", crosspostEnabled)
+ .toString();
+ }
+
+ public boolean isConnected() {
+ return connected;
+ }
+
+ public void setConnected(boolean connected) {
+ this.connected = connected;
+ }
+
+ public boolean isCrosspostEnabled() {
+ return crosspostEnabled;
+ }
+
+ public void setCrosspostEnabled(boolean crosspostEnabled) {
+ this.crosspostEnabled = crosspostEnabled;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/Auth.java b/juick-server-core/src/main/java/com/juick/server/helpers/Auth.java
new file mode 100644
index 00000000..3e1f0bd9
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/Auth.java
@@ -0,0 +1,22 @@
+package com.juick.server.helpers;
+
+/**
+ * Created by vt on 09/02/16.
+ */
+public class Auth {
+ private String account;
+ private String authCode;
+
+ public Auth(String account, String authCode) {
+ this.account = account;
+ this.authCode = authCode;
+ }
+
+ public String getAccount() {
+ return account;
+ }
+
+ public String getAuthCode() {
+ return authCode;
+ }
+} \ No newline at end of file
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java b/juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java
new file mode 100644
index 00000000..679d1a8d
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java
@@ -0,0 +1,24 @@
+package com.juick.server.helpers;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * Created by vitalyster on 09.02.2016.
+ */
+public class EmailOpts {
+ private String email;
+ private String subscriptionHour;
+
+ public EmailOpts(String email, int subscriptionHour) {
+ this.email = email;
+ this.subscriptionHour = StringUtils.leftPad(String.format("%d", subscriptionHour), 2, "0");
+ }
+
+ public String getSubscriptionHour() {
+ return subscriptionHour;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+} \ No newline at end of file
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java b/juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java
new file mode 100644
index 00000000..377b0a50
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java
@@ -0,0 +1,34 @@
+package com.juick.server.helpers;
+
+/**
+ * Created by vt on 03/09/16.
+ */
+public class NotifyOpts {
+ private boolean repliesEnabled;
+ private boolean subscriptionsEnabled;
+ private boolean recommendationsEnabled;
+
+ public boolean isRepliesEnabled() {
+ return repliesEnabled;
+ }
+
+ public void setRepliesEnabled(boolean repliesEnabled) {
+ this.repliesEnabled = repliesEnabled;
+ }
+
+ public boolean isSubscriptionsEnabled() {
+ return subscriptionsEnabled;
+ }
+
+ public void setSubscriptionsEnabled(boolean subscriptionsEnabled) {
+ this.subscriptionsEnabled = subscriptionsEnabled;
+ }
+
+ public boolean isRecommendationsEnabled() {
+ return recommendationsEnabled;
+ }
+
+ public void setRecommendationsEnabled(boolean recommendationsEnabled) {
+ this.recommendationsEnabled = recommendationsEnabled;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java b/juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java
new file mode 100644
index 00000000..66cf9410
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java
@@ -0,0 +1,29 @@
+package com.juick.server.helpers;
+
+/**
+ * Created by vt on 16/01/16.
+ */
+public class PrivacyOpts {
+ private int uid;
+ private int privacy;
+
+ public PrivacyOpts() {
+
+ }
+
+ public int getUid() {
+ return uid;
+ }
+
+ public void setUid(int uid) {
+ this.uid = uid;
+ }
+
+ public int getPrivacy() {
+ return privacy;
+ }
+
+ public void setPrivacy(int privacy) {
+ this.privacy = privacy;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java b/juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java
new file mode 100644
index 00000000..b1bfccf8
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java
@@ -0,0 +1,22 @@
+package com.juick.server.helpers;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.juick.User;
+
+import java.util.List;
+
+/**
+ * Created by vt on 24/11/2016.
+ */
+public class PrivateChats {
+ private List<User> users;
+
+ @JsonProperty("pms")
+ public List<User> getUsers() {
+ return users;
+ }
+
+ public void setUsers(List<User> users) {
+ this.users = users;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java b/juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java
new file mode 100644
index 00000000..f941c743
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java
@@ -0,0 +1,72 @@
+package com.juick.server.helpers;
+
+import java.util.Date;
+
+/**
+ * Created by vitalyster on 13.12.2016.
+ */
+public class ResponseReply {
+ private String muname;
+ private int mid;
+ private int rid;
+ private String uname;
+ private String description;
+ private Date pubDate;
+ private String attachmentType;
+
+ public String getMuname() {
+ return muname;
+ }
+
+ public void setMuname(String muname) {
+ this.muname = muname;
+ }
+
+ public int getMid() {
+ return mid;
+ }
+
+ public void setMid(int mid) {
+ this.mid = mid;
+ }
+
+ public int getRid() {
+ return rid;
+ }
+
+ public void setRid(int rid) {
+ this.rid = rid;
+ }
+
+ public String getUname() {
+ return uname;
+ }
+
+ public void setUname(String uname) {
+ this.uname = uname;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public Date getPubDate() {
+ return pubDate;
+ }
+
+ public void setPubDate(Date pubDate) {
+ this.pubDate = pubDate;
+ }
+
+ public String getAttachmentType() {
+ return attachmentType;
+ }
+
+ public void setAttachmentType(String attachmentType) {
+ this.attachmentType = attachmentType;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java b/juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java
new file mode 100644
index 00000000..e8720991
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java
@@ -0,0 +1,29 @@
+package com.juick.server.helpers;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.juick.Tag;
+
+/**
+ * Created by vitalyster on 01.12.2016.
+ */
+public class TagStats {
+ private Tag tag;
+ private int usageCount;
+
+ public Tag getTag() {
+ return tag;
+ }
+
+ public void setTag(Tag tag) {
+ this.tag = tag;
+ }
+
+ @JsonProperty("messages")
+ public int getUsageCount() {
+ return usageCount;
+ }
+
+ public void setUsageCount(int usageCount) {
+ this.usageCount = usageCount;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java b/juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java
new file mode 100644
index 00000000..5a4b6894
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java
@@ -0,0 +1,43 @@
+package com.juick.server.helpers;
+
+/**
+ * Created by vt on 03/09/16.
+ */
+public class UserInfo {
+ private String fullName;
+ private String country;
+ private String url;
+ private String description;
+
+ public String getFullName() {
+ return fullName;
+ }
+
+ public void setFullName(String fullName) {
+ this.fullName = fullName;
+ }
+
+ public String getCountry() {
+ return country;
+ }
+
+ public void setCountry(String country) {
+ this.country = country;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java
new file mode 100644
index 00000000..6ac05624
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java
@@ -0,0 +1,426 @@
+package com.juick.server.protocol;
+
+import com.juick.Message;
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.formatters.PlainTextFormatter;
+import com.juick.server.helpers.TagStats;
+import com.juick.server.protocol.annotation.UserCommand;
+import com.juick.server.util.TagUtils;
+import com.juick.service.*;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.apache.commons.lang3.reflect.MethodUtils;
+
+import javax.inject.Inject;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+/**
+ * Created by oxpa on 22.03.16.
+ */
+
+public class JuickProtocol {
+
+ private String baseUri;
+ private ProtocolListener listener;
+
+ @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;
+ }
+
+ /**
+ * find command by pattern and invoke
+ * @param user who send command
+ * @param userInput given by user
+ * @return command result
+ * @throws InvocationTargetException
+ * @throws IllegalAccessException
+ * @throws NoSuchMethodException
+ */
+ public String getReply(User user, String userInput) throws InvocationTargetException,
+ IllegalAccessException, NoSuchMethodException {
+ Optional<Method> cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream()
+ .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(),
+ m.getAnnotation(UserCommand.class).patternFlags()).matcher(userInput).matches())
+ .findFirst();
+ if (!cmd.isPresent()) {
+ // default command - post as new message
+ return postMessage(user, userInput.trim());
+ } else {
+ Matcher matcher = Pattern.compile(cmd.get().getAnnotation(UserCommand.class).pattern(),
+ cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(userInput.trim());
+ List<String> groups = new ArrayList<>();
+ while (matcher.find()) {
+ for (int i = 1; i <= matcher.groupCount(); i++) {
+ groups.add(matcher.group(i));
+ }
+ }
+ return (String) getClass().getMethod(cmd.get().getName(), User.class, String[].class)
+ .invoke(this, user, groups.toArray(new String[groups.size()]));
+ }
+ }
+
+ public String postMessage(User user, String input) {
+ List<Tag> tags = tagService.fromString(input, false);
+ String body = input.substring(TagUtils.toString(tags).length());
+ int mid = messagesService.createMessage(user.getUid(), body, null, tags);
+ subscriptionService.subscribeMessage(mid, user.getUid());
+ listener.messagePosted(messagesService.getMessage(mid));
+ return "New message posted.\n#" + mid + " " + baseUri + mid;
+ }
+
+ @UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages")
+ public String commandLast(User user, String... arguments) {
+ List<Integer> mids = messagesService.getAll(user.getUid(), 0);
+ List<Message> messages = messagesService.getMessages(mids);
+ return "Last messages: \n"
+ + messages.stream().sorted(Collections.reverseOrder()).map(PlainTextFormatter::formatPostSummary)
+ .collect(Collectors.joining("\n\n"));
+ }
+
+ @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "BL - Show your blacklist")
+ public String commandBL(User user_from, String... arguments) {
+ List<User> blusers;
+ List<String> bltags;
+
+ blusers = userService.getUserBLUsers(user_from.getUid());
+ bltags = tagService.getUserBLTags(user_from.getUid());
+
+
+ String txt = StringUtils.EMPTY;
+ if (bltags.size() > 0) {
+ for (String bltag : bltags) {
+ txt += "*" + bltag + "\n";
+ }
+
+ if (blusers.size() > 0) {
+ txt += "\n";
+ }
+ }
+ if (blusers.size() > 0) {
+ for (User bluser : blusers) {
+ txt += "@" + bluser.getName() + "\n";
+ }
+ }
+ if (txt.isEmpty()) {
+ txt = "You don't have any users or tags in your blacklist.";
+ }
+ return txt;
+ }
+
+ @UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "BL @username - add @username to your blacklist")
+ public String blacklistUser(User from, String... arguments) {
+ User blUser = userService.getUserByName(arguments[0]);
+ if (blUser != null) {
+ PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(from, blUser);
+ if (result == PrivacyQueriesService.PrivacyResult.Added) {
+ return "User added to your blacklist";
+ } else {
+ return "User removed from your blacklist";
+ }
+ }
+ return "User not found";
+ }
+
+ @UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "BL *tag - add *tag to your blacklist")
+ public String blacklistTag(User from, String... arguments) {
+ User blUser = userService.getUserByName(arguments[0]);
+ if (blUser != null) {
+ Tag tag = tagService.getTag(arguments[0], false);
+ if (tag != null) {
+ PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistTag(from, tag);
+ if (result == PrivacyQueriesService.PrivacyResult.Added) {
+ return "Tag added to your blacklist";
+ } else {
+ return "Tag removed from your blacklist";
+ }
+ }
+ }
+ return "Tag not found";
+ }
+
+ @UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs")
+ public String commandUsers(User currentUser, String... args) {
+ StringBuilder msg = new StringBuilder();
+ msg.append("Recommended blogs");
+ List<String> recommendedUsers = showQueriesService.getRecommendedUsers(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<String> topUsers = showQueriesService.getTopUsers();
+ if (topUsers.size() > 0) {
+ for (String user : topUsers) {
+ msg.append("\n@").append(user);
+ }
+ } else {
+ msg.append("\nNo top users. Empty DB? ;)");
+ }
+ return msg.toString();
+ }
+
+ @UserCommand(pattern = "\\*", help = "* - Show your tags")
+ public String commandTags(User currentUser, String... args) {
+ 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());
+ return msg;
+ }
+
+ @UserCommand(pattern = "S", help = "S - Show your subscriptions")
+ public String commandSubscriptions(User currentUser, String... args) {
+ List<User> friends = userService.getUserFriends(currentUser.getUid());
+ List<String> tags = subscriptionService.getSubscribedTags(currentUser);
+ String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName())
+ .collect(Collectors.joining())
+ : "You are not subscribed to any user.";
+ msg += tags.size() > 0 ? "\nYou are subscribed to tags:" + tags.stream().map(t -> "\n*" + t)
+ .collect(Collectors.joining())
+ : "\nYou are not subscribed to any tag.";
+ return msg;
+ }
+
+ @UserCommand(pattern = "!", help = "! - Show your favorite messages")
+ public String commandFavorites(User currentUser, String... args) {
+ List<Integer> mids = messagesService.getUserRecommendations(currentUser.getUid(), 0);
+ if (mids.size() > 0) {
+ List<Message> messages = messagesService.getMessages(mids);
+ return "Favorite messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost)
+ .collect(Collectors.toList()));
+ }
+ return "No favorite messages, try to \"like\" something ;)";
+ }
+
+ @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$",
+ help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)")
+ public String commandUser(User user, String... arguments) {
+ User blogUser = userService.getUserByName(arguments[0]);
+ int page = arguments[1].length();
+ if (blogUser.getUid() > 0) {
+ List<Integer> mids = messagesService.getUserBlog(blogUser.getUid(), 0, page);
+ List<Message> messages = messagesService.getMessages(mids);
+ return String.format("Last messages from @%s:\n%s", arguments[0],
+ String.join("\n", messages.stream()
+ .map(PlainTextFormatter::formatPost).collect(Collectors.toList())));
+ }
+ return "User not found";
+ }
+
+ @UserCommand(pattern = "^d\\s*\\#([0-9]+)$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "D #12345 - delete the message")
+ public String commandDel(User user, String... args) {
+ int mid = NumberUtils.toInt(args[0], 0);
+ if (messagesService.deleteMessage(user.getUid(), mid)) {
+ return String.format("Message %s deleted", mid);
+ }
+ return "Error";
+ }
+
+ @UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "LOGIN - log in to Juick website")
+ public String commandLogin(User user, String... arguments) {
+ return baseUri + "?" + userService.getHashByUID(user.getUid());
+ }
+
+ @UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)")
+ public String commandMyFeed(User user, String... arguments) {
+ // number of # is the page count
+ int page = arguments[0].length() - 1;
+ List<Integer> mids = messagesService.getMyFeed(user.getUid(), page);
+ List<Message> messages = messagesService.getMessages(mids);
+ // TODO: add instructions for empty feed
+ return "Your feed: \n" + String.join("\n",
+ messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList()));
+ }
+
+ @UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "ON/OFF - Enable/disable subscriptions delivery")
+ public String commandOnOff(User user, String[] input) {
+ UserService.ActiveStatus newStatus;
+ String retValUpdated;
+ if (input[0].toLowerCase().equals("on")) {
+ newStatus = UserService.ActiveStatus.Active;
+ retValUpdated = "Notifications are activated for " + user.getJid();
+ } else {
+ newStatus = UserService.ActiveStatus.Inactive;
+ retValUpdated = "Notifications are disabled for " + user.getJid();
+ }
+
+ if (userService.setActiveStatusForJID(user.getJid(), newStatus)) {
+ return retValUpdated;
+ } else {
+ return String.format("Subscriptions status for %s was not changed", user.getJid());
+ }
+ }
+
+ @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "PING - returns you a PONG")
+ public String commandPing(User user, String[] input) {
+ return "PONG";
+ }
+
+ @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username")
+ public String commandPM(User user_from, String... arguments) {
+ String user_to = arguments[0];
+ String body = arguments[1];
+
+ User toUser = userService.getUserByName(user_to);
+
+ if (toUser.getUid() > 0) {
+ if (!userService.isInBLAny(toUser.getUid(), user_from.getUid())) {
+ if (pmQueriesService.createPM(user_from.getUid(), toUser.getUid(), body)) {
+ listener.privateMessage(user_from, toUser, body);
+ return "Private message sent";
+ }
+ }
+ }
+ return "Error";
+ }
+
+ @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)")
+ public String commandShow(User user, String... arguments) {
+ boolean showReplies = arguments[1].length() > 0;
+ int mid = NumberUtils.toInt(arguments[0], 0);
+ if (mid == 0) {
+ return "Error";
+ }
+ Message msg = messagesService.getMessage(mid);
+ if (msg != null) {
+ if (showReplies) {
+ List<Message> replies = messagesService.getReplies(mid);
+ replies.add(0, msg);
+ return String.join("\n",
+ replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList()));
+ }
+ return PlainTextFormatter.formatPost(msg);
+ }
+ return "Message not found";
+ }
+ @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)",
+ help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message")
+ public String EditOrReply(User user, String... args) {
+ int mid = NumberUtils.toInt(args[1]);
+ 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 "It is not your message";
+ }
+ tagService.updateTags(mid, messageTags);
+ return "Tags are updated";
+ } else {
+ int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, null);
+ listener.messagePosted(messagesService.getReply(mid, newrid));
+ return "Reply posted.\n#" + mid + "/" + newrid + " "
+ + baseUri + mid + "#" + newrid;
+ }
+ }
+
+ @UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments",
+ patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandSubscribeMessage(User user, String... args) {
+ boolean subscribe = args[0].equalsIgnoreCase("s");
+ int mid = NumberUtils.toInt(args[1], 0);
+ if (messagesService.getMessage(mid) != null) {
+ if (subscribe) {
+ if (subscriptionService.subscribeMessage(mid, user.getUid())) {
+ return "Subscribed";
+ }
+ } else {
+ if (subscriptionService.unSubscribeMessage(mid, user.getUid())) {
+ return "Unsubscribed from #" + mid;
+ }
+ return "You was not subscribed to #" + mid;
+ }
+ }
+ return "Error";
+ }
+ @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @user - subscribe to user's posts",
+ patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandSubscribeUser(User user, String... args) {
+ boolean subscribe = args[0].equalsIgnoreCase("s");
+ User toUser = userService.getUserByName(args[1]);
+ if (toUser.getUid() > 0) {
+ if (subscribe) {
+ if (subscriptionService.subscribeUser(user, toUser)) {
+ listener.userSubscribed(user, toUser);
+ return "Subscribed";
+ // TODO: already subscribed case
+ }
+ } else {
+ if (subscriptionService.unSubscribeUser(user, toUser)) {
+ return "Unsubscribed from @" + toUser.getName();
+ }
+ return "You was not subscribed to @" + toUser.getName();
+ }
+ }
+ return "Error";
+ }
+ @UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" +
+ "\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE)
+ public String commandSubscribeTag(User user, String... args) {
+ boolean subscribe = args[0].equalsIgnoreCase("s");
+ Tag tag = tagService.getTag(args[1], true);
+ if (subscribe) {
+ if (subscriptionService.subscribeTag(user, tag)) {
+ return "Subscribed";
+ }
+ } else {
+ if (subscriptionService.unSubscribeTag(user, tag)) {
+ return "Unsubscribed from " + tag.getName();
+ }
+ return "You was not subscribed to " + tag.getName();
+ }
+ return "Error";
+ }
+
+ @UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE,
+ help = "HELP - returns this help message")
+ public String commandHelp(User user, String[] input) {
+ return Arrays.stream(getClass().getDeclaredMethods())
+ .filter(m -> m.isAnnotationPresent(UserCommand.class))
+ .map(m -> m.getAnnotation(UserCommand.class).help())
+ .collect(Collectors.joining("\n"));
+ }
+
+ public String getBaseUri() {
+ return baseUri;
+ }
+
+ public ProtocolListener getListener() {
+ return listener;
+ }
+
+ public void setListener(ProtocolListener listener) {
+ this.listener = listener;
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java b/juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java
new file mode 100644
index 00000000..11231e04
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java
@@ -0,0 +1,13 @@
+package com.juick.server.protocol;
+
+import com.juick.Message;
+import com.juick.User;
+
+/**
+ * Created by vitalyster on 19.12.2016.
+ */
+public interface ProtocolListener {
+ void privateMessage(User from, User to, String body);
+ void userSubscribed(User from, User to);
+ void messagePosted(Message msg);
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java b/juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java
new file mode 100644
index 00000000..42a9bb59
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java
@@ -0,0 +1,33 @@
+package com.juick.server.protocol.annotation;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Created by oxpa on 22.03.16.
+ */
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface UserCommand {
+ /**
+ *
+ * @return a command pattern
+ */
+ String pattern() default StringUtils.EMPTY;
+
+ /**
+ *
+ * @return pattern flags
+ */
+ int patternFlags() default 0;
+
+ /**
+ *
+ * @return a string used in HELP command output. Basically, only 1 string
+ */
+ String help() default StringUtils.EMPTY;
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/util/HashUtils.java b/juick-server-core/src/main/java/com/juick/server/util/HashUtils.java
new file mode 100644
index 00000000..7e166d43
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/util/HashUtils.java
@@ -0,0 +1,19 @@
+package com.juick.server.util;
+
+import java.util.Random;
+
+/**
+ * Created by vitalyster on 29.06.2017.
+ */
+public class HashUtils {
+ private static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ public static String generateHash(final int len) {
+ Random rnd = new Random();
+ StringBuilder sb = new StringBuilder(len);
+ for (int i = 0; i < len; i++) {
+ sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length())));
+ }
+ return sb.toString();
+ }
+}
diff --git a/juick-server-core/src/main/java/com/juick/server/util/TagUtils.java b/juick-server-core/src/main/java/com/juick/server/util/TagUtils.java
new file mode 100644
index 00000000..1a92d6d1
--- /dev/null
+++ b/juick-server-core/src/main/java/com/juick/server/util/TagUtils.java
@@ -0,0 +1,25 @@
+package com.juick.server.util;
+
+import com.juick.Tag;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public class TagUtils {
+ private TagUtils() {
+ throw new IllegalStateException();
+ }
+
+ public static String toString(final List<Tag> tags) {
+ if (CollectionUtils.isEmpty(tags))
+ return StringUtils.EMPTY;
+
+ return tags.stream().map(t -> " *" + t.getName())
+ .collect(Collectors.joining());
+ }
+}