aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/PM.java
blob: 7695226a6492839edc3a9c3de350d0b48fe10f8e (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Juick
 * Copyright (C) 2008-2011, Ugnich Anton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.juick.www.controllers;

import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.service.MessagesService;
import com.juick.service.PMQueriesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.server.util.UserUtils;
import com.juick.server.util.WebUtils;
import com.juick.www.WebApp;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Message;

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

/**
 *
 * @author Ugnich Anton
 */
@Controller
public class PM {
    private static final Logger logger = LoggerFactory.getLogger(PM.class);

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

    @GetMapping("/pm/inbox")
    protected String doGetInbox(ModelMap model) {
        com.juick.User visitor = UserUtils.getCurrentUser();
        if (visitor.getUid() == 0) {
            return "redirect:/login";
        }
        String title = "PM: Inbox";
        List<com.juick.Message> msgs = pmQueriesService.getLastPMInbox(visitor.getUid());
        model.addAttribute("title", title);
        model.addAttribute("visitor", visitor);
        model.addAttribute("msgs", msgs);
        model.addAttribute("tags", tagService.getPopularTags());
        return "views/pm_inbox";
    }

    @GetMapping("/pm/sent")
    protected String doGetSent(@RequestParam(required = false) String uname,
                               ModelMap model) {
        com.juick.User visitor = UserUtils.getCurrentUser();
        if (visitor.getUid() == 0) {
            return "redirect:/login";
        }
        String title = "PM: Sent";
        List<com.juick.Message> msgs = pmQueriesService.getLastPMSent(visitor.getUid());

        if (WebUtils.isNotUserName(uname)) {
            uname = StringUtils.EMPTY;
        }

        model.addAttribute("title", title);
        model.addAttribute("visitor", visitor);
        model.addAttribute("msgs", msgs);
        model.addAttribute("tags", tagService.getPopularTags());
        model.addAttribute("uname", uname);
        return "views/pm_sent";
    }

    @PostMapping("/pm/send")
    public String doPostPM(@RequestParam(name = "uname", required = false) String unameParam,
                         @RequestParam String body) throws IOException {
        com.juick.User visitor = UserUtils.getCurrentUser();
        if (visitor.getUid() == 0 || visitor.isBanned()) {
            throw new HttpForbiddenException();
        }
        String uname = unameParam;
        if (uname.startsWith("@")) {
            uname = uname.substring(1);
        }
        int uid = 0;
        if (WebUtils.isUserName(uname)) {
            uid = userService.getUIDbyName(uname);
        }

        if (uid == 0 || 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) {
                Message msg = new 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) {
                    Message mm = new Message();
                    mm.setTo(Jid.of(jid));
                    mm.setType(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";
        } else {
            throw new HttpBadRequestException();
        }
    }
}