aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/XMPPPost.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-08 10:36:41 +0300
committerGravatar Vitaly Takmazov2016-12-08 15:24:32 +0300
commit70b1e26036175b78213b4350b0f9a504c6d7e9df (patch)
tree8fc9448977a8eb1e8fc5fab9647b1bc81fc204a3 /juick-www/src/main/java/com/juick/www/XMPPPost.java
parente4f1bdaf728e2189d1002a175fe744b80caf8dee (diff)
juick-www: /post2 from perl
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/XMPPPost.java')
-rw-r--r--juick-www/src/main/java/com/juick/www/XMPPPost.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/juick-www/src/main/java/com/juick/www/XMPPPost.java b/juick-www/src/main/java/com/juick/www/XMPPPost.java
new file mode 100644
index 00000000..b2fda6e2
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/XMPPPost.java
@@ -0,0 +1,79 @@
+package com.juick.www;
+
+import com.juick.Tag;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.jdbc.core.JdbcTemplate;
+import rocks.xmpp.addr.Jid;
+import rocks.xmpp.core.session.XmppSession;
+import rocks.xmpp.core.stanza.model.Message;
+import rocks.xmpp.extensions.oob.model.x.OobX;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Created by vitalyster on 08.12.2016.
+ */
+public class XMPPPost {
+ private final static Logger logger = LoggerFactory.getLogger(XMPPPost.class);
+
+ public void doPostMessage(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response, XmppSession xmpp, com.juick.User visitor)
+ throws ServletException, IOException {
+ if (visitor.getUid() == 0) {
+ response.sendError(HttpServletResponse.SC_FORBIDDEN);
+ return;
+ }
+ String body = request.getParameter("body").replace("\r", "");
+ int mid = NumberUtils.toInt(request.getParameter("mid"), 0);
+ int rid = NumberUtils.toInt(request.getParameter("rid"), 0);
+ if (mid > 0 && rid > 0) {
+ body = String.format("#%d/%d %s", mid, rid, body);
+ } else if (mid > 0) {
+ body = String.format("#%d %s", mid, body);
+ } else {
+ // is a post
+ List<Tag> tags = Utils.parseTags(sql, request.getParameter("tags"));
+ body = String.format("%s %s", tags.stream()
+ .map(t -> "*" + t.getName()).collect(Collectors.joining(" ")), body);
+ }
+ String attachmentFName;
+ try {
+ attachmentFName = Utils.receiveAttachment(request.getPart("attach"), request.getParameter("img"));
+ } catch (Exception e) {
+ logger.error("MULTIPART ERROR", e);
+ response.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ return;
+ }
+ Message msg = new Message();
+ msg.setType(Message.Type.CHAT);
+ msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl"));
+ msg.setTo(Jid.of("juick@juick.com/Juick"));
+ msg.setBody(body);
+ try {
+ if (attachmentFName != null) {
+ String attachmentUrl = String.format("juick://%s", attachmentFName);
+ msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!"));
+ }
+ xmpp.sendMessage(msg);
+ } catch (URISyntaxException e1) {
+ logger.warn("attachment error", e1);
+ }
+ String referer = request.getHeader("referer");
+ if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post")
+ || referer.substring(0, 22).equals("https://juick.com/post")) {
+ response.sendRedirect("/?show=my");
+ return;
+ }
+ response.sendRedirect(referer);
+ }
+}