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
161
162
163
|
/*
* 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.service.PMQueriesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.util.MessageUtils;
import com.juick.util.WebUtils;
import com.juick.www.Utils;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Message;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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
WebApp webApp;
@RequestMapping(value = "/pm/inbox", method = RequestMethod.GET)
protected String doGetInbox(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
com.juick.User visitor = webApp.getVisitorUser(request, response);
if (visitor.getUid() == 0) {
Utils.sendTemporaryRedirect(response, "/login");
}
String title = "PM: Inbox";
List<com.juick.Message> msgs = pmQueriesService.getLastPMInbox(visitor.getUid());
msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText())));
model.addAttribute("title", title);
model.addAttribute("visitor", visitor);
model.addAttribute("msgs", msgs);
model.addAttribute("tags", tagService.getPopularTags());
return "views/pm_inbox";
}
@RequestMapping(value = "/pm/sent", method = RequestMethod.GET)
protected String doGetSent(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
com.juick.User visitor = webApp.getVisitorUser(request, response);
if (visitor.getUid() == 0) {
Utils.sendTemporaryRedirect(response, "/login");
}
String title = "PM: Sent";
List<com.juick.Message> msgs = pmQueriesService.getLastPMSent(visitor.getUid());
String uname = request.getParameter("uname");
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";
}
@RequestMapping(value = "/pm/send", method = RequestMethod.POST)
public void doPostPM(HttpServletRequest request, HttpServletResponse response) throws IOException {
com.juick.User visitor = webApp.getVisitorUser(request, response);
if (visitor.getUid() == 0 || visitor.isBanned()) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
String uname = request.getParameter("uname");
if (uname.startsWith("@")) {
uname = uname.substring(1);
}
int uid = 0;
if (WebUtils.isUserName(uname)) {
uid = userService.getUIDbyName(uname);
}
String body = request.getParameter("body");
if (uid == 0 || body == null || body.length() < 1 || body.length() > 10240) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (userService.isInBLAny(uid, visitor.getUid())) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
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");
}
Utils.sendTemporaryRedirect(response, "/pm/sent");
} else {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}
|