aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/XMPPPost.java
blob: c2ec42d7a0f07390e85e2fcecdd3cbe68cd9adb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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", StringUtils.EMPTY);
        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);
    }
}