aboutsummaryrefslogtreecommitdiff
path: root/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java
blob: 3228b4c0a4f1a09295e0f485f376a9292dae9739 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.juick.www.controllers;

import com.juick.Message;
import com.juick.User;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.service.PMQueriesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.util.MessageUtils;
import com.juick.util.UserUtils;
import com.juick.util.WebUtils;
import com.juick.www.WebApp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import rocks.xmpp.addr.Jid;

import javax.inject.Inject;
import java.util.List;

/**
 * Created by vitalyster on 09.12.2016.
 */
@Controller
public class PMController {
    private static final Logger logger = LoggerFactory.getLogger(PMController.class);

    @Inject
    PMQueriesService pmQueriesService;
    @Inject
    UserService userService;
    @Inject
    TagService tagService;
    @Inject
    WebApp webApp;

    @RequestMapping("/pm/inbox")
    public String doGetInbox(ModelMap context) {
        User visitor = UserUtils.getCurrentUser();

        String title = "PM: Inbox";
        List<Message> msgs = pmQueriesService.getLastPMInbox(visitor.getUid());

        msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText())));

        context.put("title", title);
        context.put("msgs", msgs);
        context.put("tags", tagService.getPopularTags());

        return "views/pm_inbox";
    }

    @RequestMapping(value = "/pm/sent", method = RequestMethod.GET)
    public String doGetSent(
            @RequestParam String uname,
            ModelMap context) {
        String title = "PM: Sent";
        User visitor = UserUtils.getCurrentUser();
        List<com.juick.Message> msgs = pmQueriesService.getLastPMSent(visitor.getUid());

        if (WebUtils.isNotUserName(uname)) {
            uname = "";
        }
        context.put("title", title);
        context.put("msgs", msgs);
        context.put("tags", tagService.getPopularTags());
        context.put("uname", uname);
        return "views/pm_sent";
    }

    @RequestMapping(value = "/pm/sent", method = RequestMethod.POST)
    public String doPostPM(
            @RequestParam String uname,
            @RequestParam String body,
            ModelMap context) {
        User visitor = UserUtils.getCurrentUser();
        if (uname.startsWith("@")) {
            uname = uname.substring(1);
        }
        int uid = 0;
        if (WebUtils.isUserName(uname)) {
            uid = userService.getUIDbyName(uname);
        }

        if (uid == 0 || body == null || body.length() < 1 || body.length() > 10240) {
            throw new HttpBadRequestException();
        }

        if (userService.isInBLAny(uid, visitor.getUid())) {
            throw new HttpForbiddenException();
        }

        if (pmQueriesService.createPM(visitor.getUid(), uid, body)) {
            if (webApp.getXmpp() != null) {
                rocks.xmpp.core.stanza.model.Message msg = new rocks.xmpp.core.stanza.model.Message();
                msg.setFrom(Jid.of("juick@juick.com"));
                msg.setTo(Jid.of(String.format("%d@push.juick.com", uid)));
                com.juick.Message jmsg = new com.juick.Message();
                jmsg.setUser(visitor);
                jmsg.setText(body);
                msg.addExtension(jmsg);
                webApp.getXmpp().send(msg);

                msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid)));
                webApp.getXmpp().send(msg);

                List<String> jids = userService.getJIDsbyUID(uid);
                for (String jid : jids) {
                    rocks.xmpp.core.stanza.model.Message mm = new rocks.xmpp.core.stanza.model.Message();
                    mm.setTo(Jid.of(jid));
                    mm.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT);
                    if (pmQueriesService.havePMinRoster(visitor.getUid(), jid)) {
                        mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick"));
                        mm.setBody(body);
                    } else {
                        mm.setFrom(Jid.of("juick", "juick.com", "Juick"));
                        mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body);
                    }
                    webApp.getXmpp().send(mm);
                }
            } else {
                logger.warn("XMPP unavailable");
            }
            return "redirect:/pm/sent";
        }
        throw new HttpBadRequestException();
    }
}